-
Notifications
You must be signed in to change notification settings - Fork 121
142 lines (126 loc) · 5.3 KB
/
autocomment.yaml
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
name: Auto Comment on PR
on:
pull_request:
paths:
- 'content/**'
types:
- opened
- synchronize
permissions:
pull-requests: write
contents: read
jobs:
auto-comment:
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: 'actions/checkout@v3'
- name: 'Install jq'
run: sudo apt-get install jq
- name: Create Comment
run: |
set -e
PR_NUMBER=${{ github.event.pull_request.number }}
BRANCH_NAME=${{ github.head_ref }}
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
CREATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
UPDATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/comments"
FILES_URL="https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}/files"
# Check if comment already exists
EXISTING_COMMENT_JSON=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
| jq -r '.[] | select(.user.login == "github-actions[bot]")'
)
EXISTING_COMMENT_ID=$(jq -r '.id' <<<"$EXISTING_COMMENT_JSON")
EXISTING_COMMENT=$(jq -r '.body' <<<"$EXISTING_COMMENT_JSON")
# Get all changed md files
CHANGED_MD_FILES=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$FILES_URL \
| jq -r --arg prefix $BRANCH_NAME/ '.[] | select(((.filename | test("content\/.+\\.md")) and .status != "removed")) | ($prefix + .filename)' \
| sed -E -e 's|(^[^/]+/)([^/]+/)|\1|' -e 's|^|https://redis.io/docs/staging/|' -e 's|(_?index)?\.md||' \
| uniq \
| xargs \
| sed 's/ /<br>/g')
# Get all changed image files
CHANGED_IMAGE_FILES=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$FILES_URL \
| jq -r '.[] | select(.filename | test("^static/images\/.+(.png|.svg|.gif|.)")) | .filename | sub("^static/";"")')
if [[ -n "$CHANGED_IMAGE_FILES" ]]
then
# For each image, work out in which README it is present
MD_FILES_WITH_IMAGE=()
for CHANGED_IMAGE_FILE in $CHANGED_IMAGE_FILES; do
MD_FILE_WITH_IMAGE=$(grep -ro "$CHANGED_IMAGE_FILE" content \
| sed -E -e 's|:.+||' -e "s|^content/|https://redis.io/docs/staging/$BRANCH_NAME/|" -e 's|(_?index)?\.md||' \
| uniq)
MD_FILES_WITH_IMAGE+=($MD_FILE_WITH_IMAGE)
done
CHANGED_MD_FILES_WITH_IMAGE=$(printf "%s\n" "${MD_FILES_WITH_IMAGE[@]}" \
| uniq \
| xargs \
| sed 's/ /<br>/g')
CHANGED_MD_FILES="${CHANGED_MD_FILES}<br>${CHANGED_MD_FILES_WITH_IMAGE}"
fi
if [[ -z "$CHANGED_MD_FILES" ]]
then
if [[ -z "$EXISTING_COMMENT_ID" ]]
then
printf '%s\n' 'Nothing to do!'
exit 0
else
# If a comment already exists but there are no changed files in the PR anymore, delete the comment
curl -Ls \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${UPDATE_COMMENT_URL}/${EXISTING_COMMENT_ID}"
printf '%s\n' 'Comment deleted!'
exit 0
fi
fi
COMMENT="Staging links:<br> $CHANGED_MD_FILES"
printf '%s %s\n' 'Writing comment: ' "$COMMENT"
generate_post_data()
{
cat <<EOF
{
"body": "$COMMENT"
EOF
}
if [[ -n "$EXISTING_COMMENT_ID" ]]
then
# Update comment on pull request if comment is actually different
if [[ "$COMMENT" != "$EXISTING_COMMENT" ]]
then
curl -Ls \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${UPDATE_COMMENT_URL}/${EXISTING_COMMENT_ID}" \
--data "$(generate_post_data)" 1>/dev/null
printf '%s\n' 'Comment updated!'
else
printf '%s\n' 'Nothing changed!'
fi
else
# Write comment on pull request
curl -Ls \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
--data "$(generate_post_data)" 1>/dev/null
printf '%s\n' 'Comment written!'
fi