Skip to content

Commit 8d4b317

Browse files
committed
initial version
0 parents  commit 8d4b317

File tree

365 files changed

+87691
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

365 files changed

+87691
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Tiago Pascoal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# get-user-teams-membership
2+
3+
[GitHub Action](https://github.com/features/actions) to get the list of teams a user belongs in a given organization.
4+
It can also be optionally used to check if the user belongs to a given team
5+
6+
It emits two outputs which are available via the `steps` [output context](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#steps-context)
7+
8+
* Array with the least of teams the user belongs (since it's array you can check if a user belongs to a team using [contains](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#contains) function)
9+
* A boolean indicating if a user belongs to a given team (always false if `team` parameter is not used)
10+
11+
# Usage
12+
13+
See [action.yml](action.yml)
14+
15+
```yaml
16+
- uses: tspascoal/get-user-teams-membership@v1
17+
with:
18+
username: # The github username for which we want to fetch teams membership in a given organization.
19+
organization: # optional. Default value ${{ github.repository_owner }}
20+
# Organization to get membership from.
21+
team: # optional. Check if user belong to this team.
22+
# If you just want to check membership of a particular team. (only team name, don't include orgname)
23+
GITHUB_TOKEN: # Personal access token used to query github (Requires scope: `read:org`)
24+
```
25+
26+
## Requirements
27+
28+
In order to use this action you need to use a [personal access token]
29+
with `read:org` [scope](https://docs.github.com/en/developers/apps/scopes-for-oauth-apps#available-scopes)
30+
(so the builtin in [GITHUB_TOKEN](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) is not enough)
31+
32+
## Scenarios
33+
34+
- [Checks if user belongs to one team or another](#Checks-if-user-belongs-to-one-of-two-teams)
35+
- [Checks if a user belongs to a given team](#Checks-if-user-belongs-to-a-given-team)
36+
37+
### Checks if user belongs to one of two teams
38+
39+
Checks if the user who triggered the worfklow (actor) belongs to one of two teams that define `A team`
40+
and if not adds a label to the pull request to signal it's an external contribution.
41+
42+
```yaml
43+
- uses: tspascoal/get-user-teams-membership@v1
44+
id: actorTeams
45+
with:
46+
username: ${{ github.actor }}
47+
GITHUB_TOKEN: ${{ secrets.PAT }}
48+
- if: ${{ !(contains(steps.actorTeams.outputs.teams, 'A team members') || contains(steps.actorTeams.outputs.teams.teams, 'A team admins')) }}
49+
name: Label PR as external contribution
50+
...
51+
```
52+
53+
### Checks if user belongs to a given team
54+
55+
Checks if the user who triggered the worfklow (actor) doesn't belong to the `octocats` team
56+
57+
```yaml
58+
- uses: tspascoal/get-user-teams-membership@v1
59+
id: checkUserMember
60+
with:
61+
username: ${{ github.actor }}
62+
team: 'octocats'
63+
GITHUB_TOKEN: ${{ secrets.PAT }}
64+
- if: ${{ !steps.checkUserMember.outputs.isTeamMember }}
65+
...
66+
```
67+
68+
## License
69+
70+
The scripts and documentation in this project are released under the [MIT License](LICENSE)

action.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: 'login-teams-membership'
2+
description: 'Get a user teams membership in an organization'
3+
inputs:
4+
organization:
5+
description: 'Organization name (default is repo owner)'
6+
required: false
7+
default: ${{ github.repository_owner }}
8+
username:
9+
description: 'Username to get teams or check team membership'
10+
required: true
11+
team:
12+
description: 'If you specify a team name it will output if the user is a member of that team (case insensitive)'
13+
required: false
14+
default: ''
15+
GITHUB_TOKEN:
16+
description: 'GITHUB_TOKEN'
17+
required: true
18+
19+
outputs:
20+
teams:
21+
description: 'The list of teams the user belongs (array)'
22+
isTeamMember:
23+
description: 'Predicate to indicate if user belongs to team'
24+
runs:
25+
using: 'node12'
26+
main: 'index.js'

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const core = require('@actions/core')
2+
const github = require('@actions/github')
3+
4+
run()
5+
6+
async function run() {
7+
8+
try {
9+
10+
const api = github.getOctokit(core.getInput("GITHUB_TOKEN", { required: true }), {})
11+
12+
const organization = core.getInput("organization") || context.repo.owner
13+
const username = core.getInput("username")
14+
const team = core.getInput("team")
15+
16+
console.log(`Getting teams for ${username} in org ${organization}. Will check if belongs to ${team}`)
17+
18+
const query = `query($cursor: String, $org: String!, $userLogins: [String!]) {
19+
organization(login: $org) {
20+
teams (first:1, userLogins: $userLogins, after: $cursor) {
21+
nodes {
22+
name
23+
}
24+
pageInfo {
25+
hasNextPage
26+
endCursor
27+
}
28+
}
29+
}
30+
}`
31+
32+
let org
33+
let teams = []
34+
let cursor = null
35+
36+
// Paginate
37+
do {
38+
org = await api.graphql(query, {
39+
"cursor": cursor,
40+
"org": organization,
41+
"userLogins": [username]
42+
})
43+
44+
teams = teams.concat(org.organization.teams.nodes.map((val) => {
45+
return val.name
46+
}))
47+
48+
cursor = org.organization.teams.pageInfo.endCursor
49+
50+
} while (org.organization.teams.pageInfo.hasNextPage)
51+
52+
let isTeamMember = teams.some((teamName) => {
53+
return team.toLowerCase() === teamName.toLowerCase()
54+
})
55+
56+
core.setOutput("teams", teams)
57+
core.setOutput("isTeamMember", isTeamMember)
58+
59+
} catch (error) {
60+
core.setFailed(error.message)
61+
}
62+
}

node_modules/@actions/core/README.md

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/lib/command.d.ts

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)