Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update checking functionality to support checking if user exists in one of multiple teams #17

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 28 additions & 11 deletions .github/workflows/integration tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ on:
branches: 'master'
push:
branches: 'master'

jobs:
test:
runs-on: ubuntu-latest
env:
user: tspascoal
check-team: 'Team-1'
check-more-than-one-team: 'Team-No-Users, Team-1'
check-not-team: 'dummy2314332'
failed: "false"
organization: get-user-teams-membership-tests
steps:
- uses: actions/checkout@v3

- name: build
run: |
run: |
npm install
npm run build

- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v2
Expand All @@ -36,9 +37,9 @@ jobs:
id: get-teams
with:
organization: ${{ env.organization }}
username: tspascoal
username: tspascoal
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}

- name: validate teams
run: |
echo "found teams ${{ steps.get-teams.outputs.teams }}"
Expand All @@ -51,7 +52,7 @@ jobs:
if diff \
< $(echo '["Team-1","Team-Secret","Parent","Child"]' | jq --sort-keys .) \
< $(echo '${{ steps.get-teams.outputs.teams }}' | jq --sort-keys .) ; then

echo 'Teams mismatch ${{ steps.get-teams.outputs.teams }}' >> $GITHUB_SUMMARY
failed="true"
fi
Expand All @@ -66,11 +67,28 @@ jobs:
username: tspascoal
team: ${{ env.check-team }}
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}

- name: Not a team member? Fail
if: ${{ steps.check-teams-success.outputs.isTeamMember == 'false' }}
run: |
echo "failed=true" >> $$GITHUB_ENV
echo "failed=true" >> $$GITHUB_ENV

#################### Check if it belongs to at least one of two teams
- name: check if member of ${{ env.check-more-than-one-team }}
uses: ./
id: check-more-than-one-team-success
with:
organization: ${{ env.organization }}
username: tspascoal
team: ${{ env.check-more-than-one-team }}
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}

- name: Not a team member in at least one of the teams? Fail
if: ${{ steps.check-more-than-one-team-success.outputs.isTeamMember == 'false' }}
run: |
echo "Expected to belong to at least one team. Failing"
echo "failed=true" >> $$GITHUB_ENV
####################

- name: check if member of ${{ env.check-not-team }} should not be
uses: ./
Expand All @@ -80,7 +98,6 @@ jobs:
username: tspascoal
team: ${{ env.check-not-team }}
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}


- name: Dummy Team membership? Fail
if: ${{ steps.check-not-team.outputs.isTeamMember == 'true' }}
Expand All @@ -91,4 +108,4 @@ jobs:
if: ${{ env.failed == 'true' }}
run:
exit 1

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ and if not adds a label to the pull request to signal it's an external contribut
...
```

### Checks if user belongs to a given team
### Checks if user belongs to a given team/s

Checks if the user who triggered the worfklow (actor) doesn't belong to the `octocats` team
Checks if the user who triggered the workflow (actor) doesn't belong to the `octocats` or `testing` team

```yaml
- uses: tspascoal/get-user-teams-membership@v2
id: checkUserMember
with:
username: ${{ github.actor }}
team: 'octocats'
team: 'octocats,testing'
GITHUB_TOKEN: ${{ secrets.PAT }}
- if: ${{ steps.checkUserMember.outputs.isTeamMember == 'false' }}
...
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inputs:
description: 'Username to get teams or check team membership'
required: true
team:
description: 'If you specify a team name it will output if the user is a member of that team (case insensitive)'
description: 'If you specify a team name it will output if the user is a member of that team (case insensitive). If you specificy multiple teams (CSV) true will be returned if at least member of one of the teams.'
required: false
default: ''
GITHUB_TOKEN:
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ async function run() {

const organization = getInput("organization") || context.repo.owner
const username = getInput("username")
const team = getInput("team")
const inputTeams = getInput("team").trim().toLowerCase().split(",").map(item => item.trim())

console.log(`Getting teams for ${username} in org ${organization}. Will check if belongs to ${team}`)
console.log(`Getting teams for ${username} in org ${organization}.${inputTeams.length ? ` Will check if belongs to one of [${inputTeams.join(",")}]` : ''}`)

const query = `query($cursor: String, $org: String!, $userLogins: [String!], $username: String!) {
user(login: $username) {
Expand Down Expand Up @@ -54,9 +54,7 @@ async function run() {
cursor = data.organization.teams.pageInfo.endCursor
} while (data.organization.teams.pageInfo.hasNextPage)

let isTeamMember = teams.some((teamName) => {
return team.toLowerCase() === teamName.toLowerCase()
})
const isTeamMember = teams.some((teamName) => inputTeams.includes(teamName.toLowerCase()))

setOutput("teams", teams)
setOutput("isTeamMember", isTeamMember)
Expand All @@ -65,4 +63,4 @@ async function run() {
console.log(error)
setFailed(error.message)
}
}
}