From 7ed3b8558e1ea3b8add57458963b66e707cc791d Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 1 Dec 2022 20:33:50 +0100 Subject: [PATCH] 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 --- .../get-collaborator-permissions.js | 28 +++++++++++++++++++ .../get-installation-id-for-repo.js | 10 +++++++ 2 files changed, 38 insertions(+) create mode 100644 GitForWindowsHelper/get-collaborator-permissions.js create mode 100644 GitForWindowsHelper/get-installation-id-for-repo.js diff --git a/GitForWindowsHelper/get-collaborator-permissions.js b/GitForWindowsHelper/get-collaborator-permissions.js new file mode 100644 index 00000000..5b654b4d --- /dev/null +++ b/GitForWindowsHelper/get-collaborator-permissions.js @@ -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()) +} \ No newline at end of file diff --git a/GitForWindowsHelper/get-installation-id-for-repo.js b/GitForWindowsHelper/get-installation-id-for-repo.js new file mode 100644 index 00000000..66caff2a --- /dev/null +++ b/GitForWindowsHelper/get-installation-id-for-repo.js @@ -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 +} \ No newline at end of file