Skip to content

Commit

Permalink
Add functions to determine collaborators' permission
Browse files Browse the repository at this point in the history
We will want to ensure that only users with write permissions can
trigger certain slash commands. To that end, let's add a function to
determine the permissions.

This is a bit more tricky than previously-added functions because there
is no REST API call to determine that. Only GraphQL, which makes things
infinitely more complex.

Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed Dec 1, 2022
1 parent 5cfc56d commit 7ed3b85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
28 changes: 28 additions & 0 deletions GitForWindowsHelper/get-collaborator-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Gets the permission level of a collaborator on the specified repository
// Returns `ADMIN`, `MAINTAIN`, `READ`, `TRIAGE` or `WRITE`.
module.exports = async (context, token, owner, repo, collaborator) => {
const gitHubAPIRequest = require('./github-api-request')
const answer = await gitHubAPIRequest(
context,
token,
'POST',
'/graphql', {
query: `query CollaboratorPermission($owner: String!, $repo: String!, $collaborator: String) {
repository(owner:$owner, name:$repo) {
collaborators(query: $collaborator) {
edges {
permission
}
}
}
}`,
variables: {
owner,
repo,
collaborator
}
}
)
if (answer.error) throw answer.error
return answer.data.repository.collaborators.edges.map(e => e.permission.toString())
}
10 changes: 10 additions & 0 deletions GitForWindowsHelper/get-installation-id-for-repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = async (context, owner, repo) => {
const gitHubAPIRequestAsApp = require('./github-api-request-as-app')
const answer = await gitHubAPIRequestAsApp(
context,
'GET',
`/repos/${owner}/${repo}/installation`
)
if (answer.error) throw answer.error
return answer.id
}

0 comments on commit 7ed3b85

Please sign in to comment.