Skip to content

Commit caad905

Browse files
Merge pull request #26 from joshjohanning/check-team-exists
Check if team exists
2 parents 238104c + edea4fd commit caad905

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

README.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ jobs:
1818
if: contains(github.event.comment.body, '/do-stuff')
1919

2020
steps:
21-
# get the app's installation token
2221
- uses: actions/create-github-app-token@v1
2322
id: app-token
2423
with:
@@ -29,16 +28,28 @@ jobs:
2928
uses: joshjohanning/approveops@v2
3029
id: check-approval
3130
with:
32-
token: ${{ steps.app-token.outputs.token }} # use a github app token or a PAT
33-
approve-command: '/approve' # defaults to '/approve', the command to look for in the comments
34-
team-name: 'approver-team' # the name of the team in GitHub to check for the approval command; e.g.: approver-team
35-
fail-if-approval-not-found: true # defaults to true, fail the action (show the action run as red) if the command is not found in the comments from someone in the approver team"
36-
post-successful-approval-comment: false # defaults to true, whether to post successful approval comment
37-
successful-approval-comment: ':tada: You were able to run the workflow because someone left an approval in the comments!! :tada:' # Optional, only if post-successful-approval-comment is true, comment to post if an approval is found
31+
token: ${{ steps.app-token.outputs.token }}
32+
approve-command: '/approve'
33+
team-name: 'approver-team'
34+
fail-if-approval-not-found: true
35+
post-successful-approval-comment: false
3836
```
3937
38+
### Inputs
39+
40+
| Name | Description | Required | Default |
41+
| --- | --- | --- | --- |
42+
| `token` | GitHub App installation token or PAT that has access to read+write comments and list the team's membership | `true` | `''` |
43+
| `approve-command` | The approval command to look for in the comments | `true` | `/approve` |
44+
| `team-name` | The name of the team in GitHub to check for the approval command, e.g. `approver-team` | `true` | `''` |
45+
| `fail-if-approval-not-found` | Fail the action (show the action run as red) if the command is not found in the comments from someone in the approver team | `true` | `true` |
46+
| `post-successful-approval-comment` | Whether to post successful approval comment | `true` | `true` |
47+
| `successful-approval-comment` | Comment to post if an approval is found | `true` | `':tada: You were able to run the workflow because someone left an approval in the comments!!'` |
48+
4049
## Prerequisites
4150

51+
### Team and Authentication
52+
4253
1. Create a GitHub team and add at least one member
4354
2. Authentication options:
4455
- GitHub App
@@ -62,6 +73,13 @@ Notes:
6273
- A Personal Access Token (PAT) is not used since we want the comment to show as from a bot
6374
- The `github.token` is not used since the token can't provide hyperlinks for @ mentions since it doesn't have the scope for org teams, only repository data
6475

76+
### Runner Software Requirements
77+
78+
Required software installed on runner:
79+
80+
- [`gh` (GitHub CLI)](https://cli.github.com/)
81+
- [`jq`](https://jqlang.github.io/jq/download/)
82+
6583
## Breaking Changes
6684

6785
### v1 to v2

action.yml

+16-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ inputs:
1515
required: true
1616
token:
1717
description: "GitHub App installation token or PAT that has access to read the comments and check the org team's membership"
18-
default: ${{ github.token }} # this doesn't allow tagging of the approval team; better to use GitHub App
1918
required: true
19+
default: ${{ github.token }} # this doesn't allow tagging of the approval team; better to use GitHub App
2020
fail-if-approval-not-found:
2121
description: "Fail the action (i.e. show the action run as red) if the command is not found in the comments from someone in the approver team"
2222
required: true
@@ -28,7 +28,7 @@ inputs:
2828
successful-approval-comment:
2929
description: "Comment to post if an approval is found"
3030
required: true
31-
default: ":tada: You were able to run the workflow because someone left an approval in the comments!! :tada:"
31+
default: ":tada: You were able to run the workflow because someone left an approval in the comments!!"
3232

3333
outputs:
3434
approved:
@@ -45,10 +45,22 @@ runs:
4545
shell: bash
4646
run: |
4747
# "checking for a ${{ inputs.approve-command }} command in the comments from someone in the ${{ inputs.team-name}} team"
48-
users=$(gh api --paginate '/orgs/${{ github.repository_owner }}/teams/${{ inputs.team-name }}/members' | jq -c '.[].login')
48+
49+
# prerequisite check
50+
for cmd in gh jq; do
51+
if ! command -v $cmd &> /dev/null; then
52+
echo "::error title=${cmd} not installed::Could not find \`${cmd}\` on the runner"
53+
exit 1
54+
fi
55+
done
56+
57+
# checking team and getting team membership"
58+
echo "getting team membership for the team: @${{ github.repository_owner }}/${{ inputs.team-name }} ..."
59+
users=$(gh api --paginate '${{ github.event.organization.url }}/teams/${{ inputs.team-name }}/members' --jq '.[].login' 2> /dev/null) || { echo "::error title=Team doesn't exist or token doesn't have access::The ${{ inputs.team-name }} team doesn't exist or the token doesn't have access to it"; exit 1; }
60+
4961
approveCommand="${{ inputs.approve-command }}"
5062
authorized=false
51-
comments=$(gh api --paginate '${{ github.event.comment.issue_url }}/comments')
63+
comments=$(gh api --paginate ${{ github.event.issue.comments_url }})
5264
for comment in $(echo $comments | jq -r '.[] | @base64'); do
5365
body=$(echo $comment | base64 --decode | jq -r '.body' | tr -d ' ' | tr -d '\r\n')
5466
actor=$(echo $comment | base64 --decode | jq -r '.user.login')

0 commit comments

Comments
 (0)