|
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; |
31 | 30 | } |
| 31 | + } |
32 | 32 | } |
33 | 33 |
|
34 | 34 | module.exports = isMemberOfTeam; |
| 35 | + |
0 commit comments