Skip to content

Commit 849b5f6

Browse files
Improve logging, fix error logs and comments, and fixing wildcard search of approval (#11)
* posting separate message if not failing #4 * changing failure default and fixing link #4 * updating message #4 * using github-script for error * adding if statement and using notice #4 * fixing if statement * updating logs and using verbose #7 * trying to fix wildcard finds * remove debug * using xargs -0 * trying quotes * increasing to 100 comments * ending the loop if we find approver * improving log * removing new lines * fixing trim * trying to fix trim
1 parent 31094fe commit 849b5f6

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

action.yml

+32-15
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ inputs:
1818
fail-if-approval-not-found:
1919
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"
2020
required: true
21-
default: 'false'
21+
default: 'true'
2222
post-successful-approval-comment:
2323
description: "Boolean whether to post successful approval comment"
2424
required: true
@@ -51,30 +51,34 @@ runs:
5151
--header "Accept: application/vnd.github.v3+json" \
5252
--header "Authorization: Bearer ${{ steps.get_installation_token.outputs.token }}" | jq -c '.[].login')
5353
approveCommand="/approve"
54-
comments=$(curl -sLX GET '${{ github.event.comment.issue_url }}/comments' \
54+
comments=$(curl -sLX GET '${{ github.event.comment.issue_url }}/comments?&per_page=100' \
5555
--header "Accept: application/vnd.github.v3+json" \
5656
--header "Authorization: Bearer ${{ steps.get_installation_token.outputs.token }}")
5757
authorized=false
5858
for comment in $(echo $comments | jq -r '.[] | @base64'); do
59-
body=$(echo $comment | base64 --decode | jq -r '.body')
59+
body=$(echo $comment | base64 --decode | jq -r '.body' | tr -d ' ' | tr -d '\r\n')
6060
actor=$(echo $comment | base64 --decode | jq -r '.user.login')
61-
if [[ $body == *"$approveCommand"* ]]; then
62-
echo "Approve command found..."
63-
echo $users | grep -q $actor && echo "Found $actor in users" && authorized=true || echo "Not found $actor in users"
61+
id=$(echo $comment | base64 --decode | jq -r '.id')
62+
if [[ $body == "$approveCommand" ]]; then
63+
echo "Approval command found in comment id $id ..."
64+
echo $users | grep -q $actor && echo "Found $actor in team: ${{ inputs.team-name }}" && authorized=true || echo "Not found $actor in team: ${{ inputs.team-name }}"
65+
break
6466
else
65-
echo "Approve command not found..."
67+
echo "Approval command not found in comment id $id ..."
6668
fi
6769
done
6870
if $authorized; then
69-
echo "Approval authorized"
71+
echo "Approval authorized by $actor"
7072
echo "::set-output name=approved::true"
7173
else
7274
echo "Approval not found or not authorized"
7375
echo "::set-output name=approved::false"
74-
echo "::error title=Not Approved::There is no /approve command in the comments from someone in the ${{ github.repository_owner }}/${{ inputs.team-name }} team"
76+
if !(${{ inputs.fail-if-approval-not-found }}); then
77+
echo "::notice title=Not Approved::There is no /approve command in the comments from someone in the ${{ github.repository_owner }}/${{ inputs.team-name }} team"
78+
fi
7579
fi
7680
77-
- if: ${{ steps.check-approval.outputs.approved == 'false' }}
81+
- if: ${{ steps.check-approval.outputs.approved == 'false' && inputs.fail-if-approval-not-found == 'true' }}
7882
name: Create completed comment
7983
uses: peter-evans/create-or-update-comment@v1
8084
with:
@@ -83,8 +87,19 @@ runs:
8387
body: |
8488
Hey, @${{ github.event.comment.user.login }}!
8589
:cry: No one approved your run yet! Have someone from the @${{ github.repository_owner }}/${{ inputs.team-name }} team run `/approve` and then try your command again
86-
:no_entry_sign: :no_entry: Marking the workflow run as failed
90+
:no_entry_sign: :no_entry: Marking the [workflow run](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}) as failed
8791
92+
- if: ${{ steps.check-approval.outputs.approved == 'false' && inputs.fail-if-approval-not-found == 'false' }}
93+
name: Create completed comment
94+
uses: peter-evans/create-or-update-comment@v1
95+
with:
96+
token: ${{ steps.get_installation_token.outputs.token }}
97+
issue-number: ${{ github.event.issue.number }}
98+
body: |
99+
Hey, @${{ github.event.comment.user.login }}!
100+
:cry: No one approved your run yet! Have someone from the @${{ github.repository_owner }}/${{ inputs.team-name }} team run `/approve` and then try your command again
101+
:warning: :pause_button: The [workflow run](${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}) wasn't marked as failed
102+
88103
- if: ${{ steps.check-approval.outputs.approved == 'true' && inputs.post-successful-approval-comment == 'true' }}
89104
name: Create completed comment
90105
uses: peter-evans/create-or-update-comment@v1
@@ -96,7 +111,9 @@ runs:
96111
${{ inputs.successful-approval-comment }}
97112
98113
# if specified, exit with an error if approval is not found
99-
- if: ${{ inputs.fail-if-approval-not-found == 'true' && steps.check-approval.outputs.approved == 'false' }}
100-
shell: bash
101-
name: exit and error if not approved
102-
run: exit 1
114+
- name: exit and fail workflow if not approved
115+
if: ${{ inputs.fail-if-approval-not-found == 'true' && steps.check-approval.outputs.approved == 'false' }}
116+
uses: actions/github-script@v6
117+
with:
118+
script: |
119+
core.setFailed("There is no /approve command in the comments from someone in the ${{ github.repository_owner }}/${{ inputs.team-name }} team");

0 commit comments

Comments
 (0)