Skip to content

Commit e86dd74

Browse files
authored
Try paginating comments (#14)
* Try paginating comments * Fix range * Update action to run from branch (undo before merge) * Update beta release to track v2; add flag to run beta commenter * Fix pagination url * Better logging * Really fix query parameter * Detect rate limiting(ish) * Fix deletes during pagination
1 parent 5fe12c2 commit e86dd74

File tree

6 files changed

+172
-7
lines changed

6 files changed

+172
-7
lines changed

.github/workflows/release-beta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set Beta Git tag
1414
uses: weareyipyip/walking-tag-action@v2
1515
with:
16-
tag-name: v1-beta
16+
tag-name: v2-beta
1717
tag-message: The current beta is based on this commit
1818
env:
1919
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# IDEs
22
.idea
33
.vscode
4+
.DS_Store

action.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@ inputs:
2121
description: 'The version of terraform with which a plan was generated'
2222
required: false
2323
default: "1.0.6"
24+
use_beta_version:
25+
description: 'Whether to use the beta version of the commenter'
26+
required: false
27+
default: 'false'
2428
runs:
2529
using: "composite"
2630
steps:
27-
- name: Build commenter docker image
31+
- name: Build commenter docker image (master)
32+
if: inputs.use_beta_version != 'true'
33+
run: docker build --build-arg TERRAFORM_VERSION=${{ inputs.terraform_version }} -t commenter https://github.com/GetTerminus/terraform-pr-commenter.git#master
34+
shell: bash
35+
- name: Build commenter docker image (beta)
36+
if: inputs.use_beta_version == 'true'
2837
# append branch with a pound (#) if developing. e.g., `commenter.git#my-branch`
29-
run: docker build --build-arg TERRAFORM_VERSION=${{ inputs.terraform_version }} -t commenter https://github.com/GetTerminus/terraform-pr-commenter.git
38+
run: docker build --build-arg TERRAFORM_VERSION=${{ inputs.terraform_version }} -t commenter https://github.com/GetTerminus/terraform-pr-commenter.git#v2-beta
3039
shell: bash
3140
- name: Run commenter image (plan)
3241
env:

entrypoint.sh

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ parse_args () {
107107
CONTENT_HEADER="Content-Type: application/json"
108108

109109
PR_COMMENTS_URL=$(echo "$GITHUB_EVENT" | jq -r ".pull_request.comments_url")
110+
PR_COMMENTS_URL+="?per_page=100"
111+
110112
PR_COMMENT_URI=$(echo "$GITHUB_EVENT" | jq -r ".repository.issue_comment_url" | sed "s|{/number}||g")
111113
}
112114

@@ -159,24 +161,72 @@ substitute_and_colorize () {
159161
echo "$current_plan"
160162
}
161163

164+
get_page_count () {
165+
local link_header
166+
local last_page=1
167+
168+
link_header=$(curl -sSI -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENTS_URL" | grep -i ^link)
169+
170+
# if I find a matching link header...
171+
if grep -Fi 'rel="next"' <<< "$link_header"; then
172+
# we found a next page -> find the last page
173+
IFS=',' read -ra links <<< "$link_header"
174+
for link in "${links[@]}"; do
175+
# process "$i"
176+
local regex
177+
page_regex='^.*page=([0-9]+).*$'
178+
179+
# if this is the 'last' ref...
180+
if grep -Fi 'rel="last"' <<< "$link" ; then
181+
if [[ $link =~ $page_regex ]]; then
182+
last_page="${BASH_REMATCH[1]}"
183+
break
184+
fi
185+
fi
186+
done
187+
fi
188+
189+
eval "$1"="$last_page"
190+
}
191+
162192
delete_existing_comments () {
163193
# Look for an existing PR comment and delete
164194
# debug "Existing comments: $(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L $PR_COMMENTS_URL)"
165195

166196
local type=$1
167197
local regex=$2
198+
local last_page
168199

169200
local jq='.[] | select(.body|test ("'
170201
jq+=$regex
171202
jq+='")) | .id'
203+
204+
# gross, but... bash.
205+
get_page_count PAGE_COUNT
206+
last_page=$PAGE_COUNT
207+
info "Found $last_page page(s) of comments at $PR_COMMENTS_URL."
208+
172209
info "Looking for an existing $type PR comment."
173-
for PR_COMMENT_ID in $(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L $PR_COMMENTS_URL | jq "$jq")
210+
local comment_ids=()
211+
for page in $(seq $last_page)
212+
do
213+
# first, we read *all* of the comment IDs across all pages. saves us from the problem where we read a page, then
214+
# delete some, then read the next page, *after* our page boundary has moved due to the delete.
215+
# CAUTION. this line assumes the PR_COMMENTS_URL already has at least one query parameter. (note the '&')
216+
readarray -t -O "${#comment_ids[@]}" comment_ids < <(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENTS_URL&page=$page" | jq "$jq")
217+
done
218+
219+
for PR_COMMENT_ID in "${comment_ids[@]}"
174220
do
175221
FOUND=true
176222
info "Found existing $type PR comment: $PR_COMMENT_ID. Deleting."
177223
PR_COMMENT_URL="$PR_COMMENT_URI/$PR_COMMENT_ID"
178-
curl -sS -X DELETE -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENT_URL" > /dev/null
224+
STATUS=$(curl -sS -X DELETE -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -o /dev/null -w "%{http_code}" -L "$PR_COMMENT_URL")
225+
if [ "$STATUS" != "204" ]; then
226+
info "Failed to delete: status $STATUS (most likely rate limited)"
227+
fi
179228
done
229+
180230
if [ -z $FOUND ]; then
181231
info "No existing $type PR comment found."
182232
fi

testing/paginate.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env bash
2+
set -x
3+
4+
ACCEPT_HEADER="Accept: application/vnd.github.v3+json"
5+
AUTH_HEADER="Authorization: token $GH_TOKEN"
6+
CONTENT_HEADER="Content-Type: application/json"
7+
8+
PR_COMMENTS_URL="https://api.github.com/repositories/520589422/issues/7/comments?per_page=100"
9+
#PR_COMMENTS_URL="https://api.github.com/repos/GetTerminus/eks-autoscaling-infra/issues/7/comments"
10+
PR_COMMENT_URI="https://api.github.com/repositories/520589422/issues/comments"
11+
12+
PAGE_COUNT=1
13+
14+
###########
15+
# Logging #
16+
###########
17+
debug () {
18+
if [ -n "${COMMENTER_DEBUG+x}" ]; then
19+
echo -e "\033[33;1mDEBUG:\033[0m $1"
20+
fi
21+
}
22+
23+
info () {
24+
echo -e "\033[34;1mINFO:\033[0m $1"
25+
}
26+
27+
error () {
28+
echo -e "\033[31;1mERROR:\033[0m $1"
29+
}
30+
31+
get_page_count () {
32+
local link_header
33+
local last_page=1
34+
35+
link_header=$(curl -sSI -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENTS_URL" | grep -i ^link)
36+
37+
# if I find a matching link header...
38+
if grep -Fi 'rel="next"' <<< "$link_header"; then
39+
# we found a next page -> find the last page
40+
IFS=',' read -ra links <<< "$link_header"
41+
for link in "${links[@]}"; do
42+
# process "$i"
43+
local regex
44+
page_regex='^.*page=([0-9]+).*$'
45+
46+
# if this is the 'last' ref...
47+
if grep -Fi 'rel="last"' <<< "$link" ; then
48+
if [[ $link =~ $page_regex ]]; then
49+
last_page="${BASH_REMATCH[1]}"
50+
break
51+
fi
52+
fi
53+
done
54+
fi
55+
56+
eval "$1"="$last_page"
57+
}
58+
59+
delete_existing_comments () {
60+
# Look for an existing PR comment and delete
61+
# debug "Existing comments: $(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L $PR_COMMENTS_URL)"
62+
63+
local type=$1
64+
local regex=$2
65+
local last_page
66+
67+
local jq='.[] | select(.body|test ("'
68+
jq+=$regex
69+
jq+='")) | .id'
70+
71+
# gross, but... bash.
72+
get_page_count PAGE_COUNT
73+
last_page=$PAGE_COUNT
74+
info "Found $last_page page(s) of comments at $PR_COMMENTS_URL."
75+
76+
info "Looking for an existing $type PR comment."
77+
local comment_ids=()
78+
for page in $(seq $last_page)
79+
do
80+
# first, we read *all* of the comment IDs across all pages. saves us from the problem where we read a page, then
81+
# delete some, then read the next page, *after* our page boundary has moved due to the delete.
82+
# CAUTION. this line assumes the PR_COMMENTS_URL already has at least one query parameter. (note the '&')
83+
readarray -t -O "${#comment_ids[@]}" comment_ids < <(curl -sS -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L "$PR_COMMENTS_URL&page=$page" | jq "$jq")
84+
done
85+
86+
for PR_COMMENT_ID in "${comment_ids[@]}"
87+
do
88+
FOUND=true
89+
info "Found existing $type PR comment: $PR_COMMENT_ID. Deleting."
90+
PR_COMMENT_URL="$PR_COMMENT_URI/$PR_COMMENT_ID"
91+
# STATUS=$(curl -sS -X DELETE -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -o /dev/null -w "%{http_code}" -L "$PR_COMMENT_URL")
92+
# if [ "$STATUS" != "204" ]; then
93+
# info "Failed to delete: status $STATUS (most likely rate limited)"
94+
# fi
95+
done
96+
97+
if [ -z $FOUND ]; then
98+
info "No existing $type PR comment found."
99+
fi
100+
}
101+
102+
WORKSPACE=prod-east
103+
#delete_existing_comments 'plan' '### Terraform `plan` .* for Workspace: `'$WORKSPACE'`.*'
104+
delete_existing_comments 'outputs' '### Changes to outputs for Workspace: `'$WORKSPACE'`.*'

testing/testing.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,8 +2403,9 @@ ACCEPT_HEADER="Accept: application/vnd.github.v3+json"
24032403
AUTH_HEADER="Authorization: token $GH_TOKEN"
24042404
CONTENT_HEADER="Content-Type: application/json"
24052405

2406-
PR_COMMENTS_URL="https://api.github.com/repos/GetTerminus/web-event-capture-infra/issues/122/comments"
2407-
PR_COMMENT_URI="https://api.github.com/repos/GetTerminus/web-event-capture-infra/issues/comments/122"
2406+
PR_COMMENTS_URL="https://api.github.com/repos/GetTerminus/eks-observability-infra/issues/4/comments"
2407+
PR_COMMENT_URI="https://api.github.com/repos/GetTerminus/eks-observability-infra/issues/comments/4"
2408+
# curl -sSi -H "$AUTH_HEADER" -H "$ACCEPT_HEADER" -L $PR_COMMENTS_URL
24082409

24092410
WORKSPACE=ninja
24102411
POST_PLAN_OUTPUTS=true

0 commit comments

Comments
 (0)