Skip to content

Commit 34b575c

Browse files
committed
HBASE-28642 Hide old PR comments when posting new (apache#5967)
Signed-off-by: Nihal Jain <[email protected]> Signed-off-by: Peter Somogyi <[email protected]>
1 parent bfd5a41 commit 34b575c

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

dev-support/Jenkinsfile_GitHub

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pipeline {
9292
DOCKERFILE = "${WORKDIR}/${DOCKERFILE_REL}"
9393
YETUS_DRIVER = "${WORKDIR}/${YETUS_DRIVER_REL}"
9494
ASF_NIGHTLIES_GENERAL_CHECK_BASE="${ASF_NIGHTLIES_BASE}/${WORKDIR_REL}/${PATCH_REL}"
95+
HIDE_OLD_COMMENTS = "${SOURCEDIR}/dev-support/gh_hide_old_comments.sh"
9596
}
9697
when {
9798
// this will return true if the pipeline is building a change request, such as a GitHub pull request.
@@ -174,6 +175,19 @@ pipeline {
174175
reportFiles: 'report.html',
175176
reportName: 'PR General Check Report'
176177
]
178+
withCredentials([
179+
usernamePassword(
180+
credentialsId: 'apache-hbase-at-github.zerozr99.workers.dev',
181+
passwordVariable: 'GITHUB_TOKEN',
182+
usernameVariable: 'GITHUB_USER'
183+
)
184+
]) {
185+
script {
186+
sh label: 'Hide Stale PR Comments', script: '''#!/bin/bash -e
187+
"${HIDE_OLD_COMMENTS}" "${CHANGE_ID}" || :
188+
'''
189+
}
190+
}
177191
}
178192
// Jenkins pipeline jobs fill slaves on PRs without this :(
179193
cleanup() {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
set -eEuo pipefail
21+
trap 'exit 1' ERR
22+
23+
declare DEBUG
24+
if [ "${DEBUG}" = 'true' ] ; then
25+
set -x
26+
fi
27+
28+
declare GITHUB_TOKEN
29+
declare -a GITHUB_AUTH
30+
GITHUB_AUTH=('--header' "Authorization: token ${GITHUB_TOKEN}")
31+
declare GITHUB_API_URL="https://api.github.com"
32+
33+
# the user_id of the buildbot that leaves the comments we want to hide.
34+
declare BUILD_BOT_USER_ID="${BUILD_BOT_USER_ID:-49303554}"
35+
declare REPO="${REPO:-apache/hbase}"
36+
declare JOB_NAME="${JOB_NAME:-HBase-PreCommit-GitHub-PR}"
37+
JOB_NAME="$(echo "${JOB_NAME}" | cut -d/ -f1)"
38+
declare CURL="${CURL:-curl}"
39+
40+
function fetch_comments {
41+
local pr="$1"
42+
local comments_file
43+
local -a curl_args
44+
curl_args=(
45+
--fail
46+
"${GITHUB_AUTH[@]}"
47+
--header 'Accept: application/vnd.github+json'
48+
--header 'X-GitHub-Api-Version: 2022-11-28'
49+
--request GET
50+
--url "${GITHUB_API_URL}/repos/${REPO}/issues/${pr}/comments?per_page=500"
51+
)
52+
if [ "${DEBUG}" = true ] ; then
53+
curl_args+=(--verbose)
54+
else
55+
curl_args+=(--silent)
56+
fi
57+
58+
comments_file="$(mktemp "comments_${pr}" 2>/dev/null || mktemp -t "comments_${pr}.XXXXXXXXXX")" || \
59+
{ >&2 echo 'cannot create temp file'; exit 1 ;}
60+
"${CURL}" "${curl_args[@]}" > "${comments_file}"
61+
if [ "${DEBUG}" = 'true' ] ; then
62+
>&2 cat "${comments_file}"
63+
fi
64+
echo "${comments_file}"
65+
}
66+
67+
function hide_old_comment {
68+
local pr="$1"
69+
local comment_id="$2"
70+
local -a curl_args
71+
local query
72+
read -r -d '' query << EOF || :
73+
mutation {
74+
minimizeComment(input: { subjectId: \"$comment_id\", classifier: OUTDATED }) {
75+
minimizedComment {
76+
isMinimized,
77+
minimizedReason
78+
}
79+
}
80+
}
81+
EOF
82+
# remove newlines and whitespace rather than escaping them
83+
query="${query//[$'\r\n\t ']/}"
84+
85+
curl_args=(
86+
--fail
87+
"${GITHUB_AUTH[@]}"
88+
--header 'Accept: application/vnd.github+json'
89+
--header 'Content-Type: application/json'
90+
--header 'X-GitHub-Api-Version: 2022-11-28'
91+
--request POST
92+
--url "${GITHUB_API_URL}/graphql"
93+
)
94+
if [ "${DEBUG}" = true ] ; then
95+
curl_args+=(--verbose)
96+
else
97+
curl_args+=(--silent)
98+
fi
99+
100+
"${CURL}" "${curl_args[@]}" --data "{ \"query\": \"${query}\" }"
101+
}
102+
103+
function identify_most_recent_build_number {
104+
local pr="$1"
105+
local comments_file="$2"
106+
local jq_filter
107+
read -r -d '' jq_filter << EOF || :
108+
.[] \
109+
| select(.user.id == ${BUILD_BOT_USER_ID}) \
110+
| .body \
111+
| capture("${JOB_NAME}/job/PR-${pr}/(?<buildnum>[0-9]+)/") \
112+
| .buildnum
113+
EOF
114+
115+
jq -r "${jq_filter}" "${comments_file}" \
116+
| sort -nu \
117+
| tail -n1
118+
}
119+
120+
function identify_old_comment_ids {
121+
local pr="$1"
122+
local comments_file="$2"
123+
local most_recent_build_number="$3"
124+
local jq_filter
125+
read -r -d '' jq_filter << EOF || :
126+
.[] \
127+
| select(.user.id == ${BUILD_BOT_USER_ID}) \
128+
| { node_id, buildnum: (.body | capture("${JOB_NAME}/job/PR-${pr}/(?<buildnum>[0-9]+)/") | .buildnum | tonumber) } \
129+
| select(.buildnum < (${most_recent_build_number} | tonumber)) \
130+
| .node_id
131+
EOF
132+
133+
jq -r "${jq_filter}" "${comments_file}"
134+
}
135+
136+
function main {
137+
local pr="$1"
138+
local comments_file
139+
local most_recent_build_number
140+
141+
comments_file="$(fetch_comments "$pr")"
142+
most_recent_build_number="$(identify_most_recent_build_number "${pr}" "${comments_file}")"
143+
for comment_id in $(identify_old_comment_ids "${pr}" "${comments_file}" "${most_recent_build_number}") ; do
144+
hide_old_comment "${pr}" "${comment_id}"
145+
sleep 1
146+
done
147+
}
148+
149+
main "$@"

0 commit comments

Comments
 (0)