-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
106 lines (97 loc) · 4.61 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: 'Comment Pull Request for workflow'
description: 'A GitHub Action for commenting on pull requests from workflows.'
author: 'YUMEMI Inc.'
branding:
icon: 'message-circle'
color: 'blue'
inputs:
comment:
description: 'Comment text.'
required: true
comment-if-failure:
description: 'Comment text to be used with priority when job status is failure. If not specified, normal comment will be used.'
required: false
grouping-key:
description: 'Key to group comments. Comments from the same group are deleted, hidden, or edited.'
required: false
default: '${{ github.workflow }}-${{ github.job }}'
previous-comment:
description: 'One of delete, hide, edit, or keep. See README for details.'
required: false
default: 'delete'
pull-request-number:
description: 'Target pull request number.'
required: false
default: ${{ github.event.pull_request.number }}
sha:
description: 'Commit SHA used to search for pull request number.'
required: false
default: ${{ github.sha }}
runs:
using: 'composite'
steps:
- name: Comment
shell: bash
env:
COMMENT: ${{ job.status == 'failure' && inputs.comment-if-failure || inputs.comment }}
TAG: '<comment-pull-request:${{ inputs.grouping-key }}>'
PREVIOUS_COMMENT: ${{ inputs.previous-comment }}
PR_NUMBER: ${{ inputs.pull-request-number }}
SHA: ${{ inputs.sha }}
GH_REPO: ${{ github.repository }} # for GitHub CLI
GH_TOKEN: ${{ github.token }} # for GitHub CLI
IS_VALID_PREVIOUS_COMMENT: ${{ inputs.previous-comment != null && contains(fromJSON('["delete","hide","edit","keep"]'), inputs.previous-comment) }}
run: |
echo "::debug::gh version: $(gh --version | tr '\n' ' ')"
echo "::debug::jq version: $(jq --version)"
source "$GITHUB_ACTION_PATH/raise_error.sh"
if [ -z "$COMMENT" ]; then raise_error "Specify 'comment' or 'failure-comment' input."; fi
if [ "$IS_VALID_PREVIOUS_COMMENT" != 'true' ]; then raise_error "'previous-comment' input must be one of delete, hide, edit, or keep."; fi
# have pemisson?
gh api repos/{owner}/{repo}/pulls --silent > /dev/null 2>&1 || raise_permission_error
# get pull request number
if [ -n "$PR_NUMBER" ]; then
# valid pull request number?
gh api "repos/{owner}/{repo}/pulls/$PR_NUMBER" --silent > /dev/null 2>&1 || raise_error "'pull-request-number' input is not valid."
pr_number="$PR_NUMBER"
else
if [ -n "$SHA" ]; then
number="$(gh api "repos/{owner}/{repo}/commits/$SHA/pulls" | jq '.[0].number')" > /dev/null 2>&1 || raise_error "'sha' input is not valid."
if [[ -n "$number" && "$number" != 'null' ]]; then
pr_number="$number"
else
raise_error "Specify 'pull-request-number' input or specify 'sha' input related to pull request."
fi
else
raise_error "Specify 'pull-request-number' input or 'sha' input."
fi
fi
# on windows, jq's return contains CR
comments="$(gh api "repos/{owner}/{repo}/issues/$pr_number/comments" --paginate | jq --arg tag "$TAG" 'reverse|.[]|select(.body|contains($tag))' | tr -d '\r')"
ids="$(echo "$comments" | jq '.id' | tr -d '\r')"
node_ids="$(echo "$comments" | jq -r '.node_id' | tr -d '\r')"
if [[ "$PREVIOUS_COMMENT" == 'delete' && -n "$ids" ]]; then
IFS=$'\n'
for id in $ids ; do
gh api --method DELETE "repos/{owner}/{repo}/issues/comments/$id" --silent > /dev/null 2>&1 || raise_permission_error
done
fi
if [[ "$PREVIOUS_COMMENT" == 'hide' && -n "$node_ids" ]]; then
IFS=$'\n'
for node_id in $node_ids ; do
gh api graphql -f subjectId="$node_id" -f query='
mutation($subjectId:ID!) {
minimizeComment(input: { subjectId: $subjectId, classifier: OUTDATED }) {
minimizedComment { isMinimized }
}
}
' --silent > /dev/null 2>&1 || raise_permission_error
done
fi
comment="$(echo -e "<!-- $TAG -->\n\n$COMMENT")"
if [[ "$PREVIOUS_COMMENT" == 'edit' && -n "$ids" ]]; then
last_id="$(echo "$ids" | head -1)"
gh api --method PATCH "repos/{owner}/{repo}/issues/comments/$last_id" -f "body=$comment" --silent > /dev/null 2>&1 || raise_permission_error
else
gh api --method POST "repos/{owner}/{repo}/issues/$pr_number/comments" -f "body=$comment" --silent > /dev/null 2>&1 || raise_permission_error
fi