Skip to content

Commit

Permalink
Add utility function, change API syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb176 committed Jun 21, 2024
1 parent daa2e4b commit 6ba49a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/pr-verification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
});
}
}
}
33 changes: 33 additions & 0 deletions github-actions/utils/check-team-membership.js
Original file line number Diff line number Diff line change
@@ -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({

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable result.
org : 'hackforla',
team_slug : team,
username : githubUsername
});
return true;
} catch (verificationError) {
if (verificationError.status == 404) {
return false;
}
else {
throw verificationError;
}
}
}

module.exports = isMemberOfTeam;

0 comments on commit 6ba49a9

Please sign in to comment.