How to fetch a user's permission level using GitHub App and GraphQL?

I’m trying to fetch a user’s permission level using GitHub App and GraphQL. However, there seems to be only viewerPermission in the attributes of the repository. This value is always null in GitHub App.

Can I write queries that add permissions for the specific user in a repository list?

1 Like

Hey there @wata727, help’s arrived!

If you want, GitHub provides an API endpoint for adding different users as collaborators on a repository: https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator. Is this what you’re looking for? 🙂

Hello @timrossback, Thanks for your reply!

Unfortunately, that is not the API I want…

I would like to list repositories including a user’s permission like the following:

{
  "data": {
    "organization": {
      "repositories": {
        "edges": [
          {
            "node": {
              "name": "transfer",
              "viewerPermission": "ADMIN"
            }
          },
          {
            "node": {
              "name": "sandbox_fuelphp",
              "viewerPermission": "ADMIN"
            }
          },
          {
            "node": {
              "name": "php_sandbox",
              "viewerPermission": "ADMIN"
            }
          }
        ]
      }
    }
  }
}

The above response can be obtained by the following query:

query {
  organization(login: "wata727-sandbox") {
    repositories(first: 3) {
      edges {
        node {
          name
          viewerPermission
        }
      }
    }
  }
}

However, viewPermission is always null in GitHub App like the following:

{
  "data": {
    "organization": {
      "repositories": {
        "edges": [
          {
            "node": {
              "name": "transfer",
              "viewerPermission": null
            }
          },
          {
            "node": {
              "name": "sandbox_fuelphp",
              "viewerPermission": null
            }
          },
          {
            "node": {
              "name": "php_sandbox",
              "viewerPermission": null
            }
          }
        ]
      }
    }
  }
}

I want to avoid this and know how to fetch permission for a specific user. GitHub API v3 provides permission level API but this API requires to call it for each repository. This is very expensive…

1 Like

Hey @wata727,

I get what you mean! I’m probably not the best person to talk to about this since I don’t actively develop anything that integrates with GitHub. You might find that Stack Overflow might be able to help, though. Otherwise, you can always try asking the wonderful folks over at GitHub support :slightly_smiling_face:

The field you want is on the *edge* of collaborators. See this object’s permission field.

4 Likes

It will remain null for anyone other than the owner

1 Like

Would be great to have a way to query the permission type given some user and the repository.

This API does exist:

https://api.github.com/repos/OWNER/REPO/collaborators/USERNAME/permission

The response is the following:

{
    "permission": "admin",
    "user": {
        "login": "USERNAME",
        "id": 1,
        "node_id": "NODEID",
        "avatar_url": "https://avatars1.githubusercontent.com/u/",
        "gravatar_id": "",
        "url": "https://api.github.com/users/USERNAME",
        "html_url": "https://github.com/USERNAME",
        "followers_url": "https://api.github.com/users/USERNAME/followers",
        "following_url": "https://api.github.com/users/USERNAME/following{/other_user}",
        "gists_url": "https://api.github.com/users/USERNAME/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/USERNAME/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/USERNAME/subscriptions",
        "organizations_url": "https://api.github.com/users/USERNAME/orgs",
        "repos_url": "https://api.github.com/users/USERNAME/repos",
        "events_url": "https://api.github.com/users/USERNAME/events{/privacy}",
        "received_events_url": "https://api.github.com/users/USERNAME/received_events",
        "type": "User",
        "site_admin": false
    }
}