Skip to content

Commit d11a995

Browse files
authored
Add context param to check-team-membership.js and update calls (#8277)
* Refactor check-team-membership.js to use context.repo.org and update calls * Refactor check-team-membership.js to use context.repo.org and fix parameters * Delete github-actions/utils/check-team-membership.j * Update verify-pr.js to fix PR #8277 logic * Update issue-trigger workflow for PR #8277 * Fix check-team-membership.js for PR #8277 * Fix check-team-membership.js for PR #8277 * Fix check-team-membership.js for PR #8277
1 parent ea853a4 commit d11a995

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

.github/workflows/issue-trigger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
const username = '${{ github.actor }}'
2121
const team = 'website-write'
2222
const script = require('./github-actions/utils/check-team-membership.js')
23-
return script(github, username, team)
23+
return script(github, context, username, team)
2424
2525
# If user is team member: checks if the issue has required labels
2626
- if: ${{ steps.check-team-membership.outputs.result == 'true' }}

github-actions/trigger-pr-target/verify-pr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ async function main({github,context}) {
99
const prNumber = context.payload.number;
1010
const repo = context.payload.pull_request.base.repo.name;
1111
const owner = context.payload.pull_request.base.repo.owner.login;
12-
const isMember = await isMemberOfTeam(github, prAuthor, 'website-write');
12+
const isMember = await isMemberOfTeam(github, context, prAuthor, 'website-write');
1313
if (isMember || prAuthor =='dependabot[bot]') {
1414
console.log('Successfully verified!');
1515
}
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
/**
2-
* @param {octokit} github - Octokit object used to access GitHub API
3-
* @param {String} githubUsername - The GitHub username of the user whose membership is to be checked.
4-
* @param {String} team - The HFLA team the username's membership is checked against. Example: 'website-write'
5-
6-
- Returns true or false depending on whether the username is found on the passed team, 404 means the user passed wasn't
7-
found on the team passed. Any other type of error will be thrown.
8-
- Need read:org permission to use this function, the least permissive token which contains this is the TEAMS token.
9-
Lack of permission will result in a 403 error.
10-
- The method of obtaining the GitHub username will vary depending on the contents of the context object. See GitHub action
11-
docs on printing context information into the log.
12-
*/
13-
14-
async function isMemberOfTeam(github, githubUsername, team) {
15-
try {
16-
await github.rest.teams.getMembershipForUserInOrg({
17-
org: 'hackforla',
18-
team_slug: team,
19-
username: githubUsername
20-
});
21-
console.log(`User '${githubUsername}' is member of team '${team}'`);
22-
return true;
23-
} catch (verificationError) {
24-
if (verificationError.status == 404) {
25-
console.log(`User '${githubUsername}' is not a team member`);
26-
return false;
27-
}
28-
else {
29-
throw verificationError;
30-
}
1+
/**
2+
* @param {octokit} github - Octokit object used to access GitHub API
3+
* @param {Object} context - context object from actions/github-script
4+
* @param {String} githubUsername - The GitHub username of the user whose membership is to be checked.
5+
* @param {String} team - The HFLA team the username's membership is checked against. Example: 'website-write'
6+
*
7+
* Returns true or false depending on whether the username is found on the passed team, 404 means the user passed
8+
* wasn't found on the team passed. Any other type of error will be thrown.
9+
*
10+
* Need read:org permission to use this function. Lack of permission will result in a 403 error.
11+
*
12+
* The method of obtaining the GitHub username will vary depending on the contents of the context object. See GitHub
13+
* action docs on printing context information into the log.
14+
*/
15+
async function isMemberOfTeam(github, context, githubUsername, team) {
16+
try {
17+
await github.rest.teams.getMembershipForUserInOrg({
18+
org: context.repo.owner,
19+
team_slug: team,
20+
username: githubUsername
21+
});
22+
console.log(`User '${githubUsername}' is member of team '${team}'`);
23+
return true;
24+
} catch (verificationError) {
25+
if (verificationError.status === 404) {
26+
console.log(`User '${githubUsername}' is not a team member`);
27+
return false;
28+
} else {
29+
throw verificationError;
3130
}
31+
}
3232
}
3333

3434
module.exports = isMemberOfTeam;
35+

0 commit comments

Comments
 (0)