diff --git a/.github/workflows/pr-verification.yml b/.github/workflows/pr-verification.yml index 27ef13276f..9a9adfbe7b 100644 --- a/.github/workflows/pr-verification.yml +++ b/.github/workflows/pr-verification.yml @@ -16,26 +16,25 @@ jobs: const prAuthor = context.payload.sender.login; const prNumber = context.payload.number; try { - const getMembershipStatus = await github.rest.teams.getMembershipForUserInOrg({ - org: 'hackforla', - team_slug: 'website-write', - username: prAuthor + await github.rest.teams.getMembershipForUserInOrg({ + org: 'hackforla', + team_slug: 'website-write', + username: prAuthor }); console.log('Successfully verified!') } catch (verificationError) { if (verificationError.status==404) { - await github.request('PATCH /repos/{owner}/{repo}/pulls/{pull_number}', { + await github.rest.issues.update({ owner : 'hackforla', repo : 'website', - pull_number : prNumber, + issue_number : prNumber, state : 'closed' }); - await github.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', { + await github.rest.issues.createComment({ owner : 'hackforla', repo : 'website', issue_number : prNumber, body : 'You must be a member of the HFLA website team in order to create pull requests. Please see our page on how to join us as a member at HFLA: https://www.hackforla.org/getting-started. If you have been though onboarding, and feel this message in error, please message us in the #hfla-site team slack channel with the link to this PR.' }); } - } - + } \ No newline at end of file diff --git a/github-actions/utils/check-team-membership.js b/github-actions/utils/check-team-membership.js new file mode 100644 index 0000000000..b34c7f14dd --- /dev/null +++ b/github-actions/utils/check-team-membership.js @@ -0,0 +1,33 @@ +/** +* @param {octokit} github - Octokit object used to access GitHub API +* @param {String} githubUsername - The github username of the user whose membership is to be checked. +* @param {String} team - The HFLA team the username's membership is checked against. Example: 'website-write' + +- Returns true or false depending on whether the username is found on the passed team, 404 means the user passed wasn't +found on the team passed. Any other type of error will be thrown. +- Need read:org permission to use this function, the least permissive token which contains this is the secrets.TEAMS token. +Lack of permission will result in a 403 error. +- The method of obtaining the github username will vary depending on the contents of the context object. See github action +docs on printing context information into the log. +*/ + +async function isMemberOfTeam(github, githubUsername, team) +{ + try { + const result = await github.rest.teams.getMembershipForUserInOrg({ + org : 'hackforla', + team_slug : team, + username : githubUsername + }); + return true; + } catch (verificationError) { + if (verificationError.status == 404) { + return false; + } + else { + throw verificationError; + } + } +} + +module.exports = isMemberOfTeam; \ No newline at end of file