|
| 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