-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functions to determine collaborators' permission
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
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |