From 6dd24a638c1242b41e2bc6310211955417c848c5 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Thu, 5 Nov 2026 16:21:21 +0800 Subject: [PATCH 1/4] YETUS-1280. Support updating the GitHub PR comment in place --github-update-comment[=] implies --github-write-comment, and updates the comment the token's user previously wrote with the same key instead of adding a new one on every run. The report is skipped when the PR head moved past the tested commit. Assisted-by: Claude Opus 5.5 --- .../precommit/plugins/github.html.md | 17 ++ precommit/src/main/shell/plugins.d/github.sh | 191 +++++++++++++ .../src/test/shell/github_update_comment.bats | 270 ++++++++++++++++++ 3 files changed, 478 insertions(+) create mode 100644 precommit/src/test/shell/github_update_comment.bats diff --git a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md index 90d4ee29e..12d33de5e 100644 --- a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md +++ b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md @@ -43,6 +43,23 @@ None | `--github-repo=` | `username/repository` identifier | | `--github-status-use-htmlreport=` | Use htmlout for Github Status 'Details' link | | `--github-token=` | Token used to perform read and write operations | +| `--github-update-comment[=]` | Update the PR comment instead of adding a new one | +| `--github-write-comment` | Write the final report as a PR comment | + +## Updating The PR Comment + +`--github-write-comment` adds a new comment with every run. `--github-update-comment` implies it, and instead +updates the comment the same GitHub user previously wrote with the same key (default: `default`). The key may only +contain `A-Z`, `a-z`, `0-9`, `.`, `_` and `-`. Use a different key per job when one build writes several reports, +e.g., one per OS. + +1. The report carries a hidden `` marker and a `Tested commit` row. +1. The token's user comes from `GET /user`. Under GitHub Actions, where that call is denied, `github-actions[bot]` + is assumed. +1. Hidden (minimized) comments are not updated. +1. If the PR head moved past the tested commit, the comment is skipped so a slow build cannot replace the report of + a newer one. Statuses are still written. +1. If looking up or updating the comment fails, a new comment is written. ## HTML Details LInk diff --git a/precommit/src/main/shell/plugins.d/github.sh b/precommit/src/main/shell/plugins.d/github.sh index cef6bab18..6430fd858 100755 --- a/precommit/src/main/shell/plugins.d/github.sh +++ b/precommit/src/main/shell/plugins.d/github.sh @@ -39,6 +39,7 @@ GITHUB_USE_EMOJI_VOTE=false GITHUB_STATUS_RECOVERY_COUNTER=1 GITHUB_STATUS_RECOVER_TOOL=false GITHUB_WRITE_COMMENT=false +GITHUB_UPDATE_COMMENT="" GITHUB_ANNOTATION_LIMIT=50 GITHUB_STATUS_USE_HTMLREPORT=${GITHUB_STATUS_USE_HTMLREPORT:-true} declare -a GITHUB_AUTH @@ -54,6 +55,7 @@ function github_usage yetus_add_option "--github-repo=" "github repo to use (default:'${GITHUB_REPO}')" yetus_add_option "--github-token=" "The token to use to read/write to github" yetus_add_option "--github-write-comment" "Write final report as github comment (default: '${GITHUB_WRITE_COMMENT}')" + yetus_add_option "--github-update-comment[=]" "Write final report as github comment, updating the one previously written with the same key" yetus_add_option "--github-use-emoji-vote" "Whether to use emoji to represent the vote result on github [default: ${GITHUB_USE_EMOJI_VOTE}]" yetus_add_option "--github-status-use-htmlreport=" "Use htmlout for Github Status 'Details' link [default: ${GITHUB_STATUS_USE_HTMLREPORT}]" } @@ -88,6 +90,19 @@ function github_parse_args delete_parameter "${i}" GITHUB_WRITE_COMMENT=true ;; + --github-update-comment|--github-update-comment=*) + delete_parameter "${i}" + GITHUB_WRITE_COMMENT=true + GITHUB_UPDATE_COMMENT=default + if [[ "${i}" == *=* ]]; then + GITHUB_UPDATE_COMMENT=${i#*=} + fi + # the key goes into the JSON body unescaped + if [[ ! "${GITHUB_UPDATE_COMMENT}" =~ ^[A-Za-z0-9._-]+$ ]]; then + yetus_error "WARNING: --github-update-comment key must match [A-Za-z0-9._-]+. Writing a new comment instead." + GITHUB_UPDATE_COMMENT="" + fi + ;; --github-use-emoji-vote) delete_parameter "${i}" GITHUB_USE_EMOJI_VOTE=true @@ -874,7 +889,142 @@ EOF rm "${tempfile}" } +## @description Print the PR head sha from a pull request JSON file +## @audience private +## @stability evolving +## @replaceable no +## @param filename +function github_pull_head_sha +{ + "${GREP}" \"sha\" "${1}" 2>/dev/null \ + | head -1 \ + | cut -f4 -d\" +} + +## @description Check if the PR head moved past the tested commit, so that +## @description a slow build of an older commit does not replace the report +## @description of a newer one +## @audience private +## @stability evolving +## @replaceable no +## @return 0 if the PR has a newer head +function github_report_superseded +{ + declare latest="${PATCH_DIR}/github-pull-latest.json" + declare tested + declare current + + tested=$(github_pull_head_sha "${PATCH_DIR}/github-pull.json") + if [[ -z "${tested}" ]]; then + return 1 + fi + + if ! "${CURL}" --silent --fail \ + -H "Accept: application/vnd.github.v3+json" \ + "${GITHUB_AUTH[@]}" \ + --output "${latest}" \ + --location \ + "${GITHUB_API_URL}/repos/${GITHUB_REPO}/pulls/${GITHUB_ISSUE}" \ + 2>/dev/null; then + return 1 + fi + + current=$(github_pull_head_sha "${latest}") + [[ -n "${current}" && "${current}" != "${tested}" ]] +} + +## @description Print the login of the user that owns the token +## @audience private +## @stability evolving +## @replaceable no +## @return 1 if unknown +function github_comment_login +{ + declare login + + # shellcheck disable=SC2016 + login=$("${CURL}" --silent --fail \ + -H "Accept: application/vnd.github.v3+json" \ + "${GITHUB_AUTH[@]}" \ + --location \ + "${GITHUB_API_URL}/user" 2>/dev/null \ + | "${AWK}" -F\" '/^ "login": "/ {print $4; exit}') + + # the GitHub Actions token cannot read /user + if [[ -z "${login}" && "${ROBOTTYPE}" == 'githubactions' ]]; then + login='github-actions[bot]' + fi + + if [[ -z "${login}" ]]; then + return 1 + fi + echo "${login}" +} + +## @description Print the id of the oldest visible PR comment written by +## @description the token's user that contains the marker +## @audience private +## @stability evolving +## @replaceable no +## @param marker +## @return 1 if not found +function github_find_comment +{ + declare marker=$1 + declare pagefile="${PATCH_DIR}/github-comments.json" + declare login + declare id + declare -i page=1 + + if ! login=$(github_comment_login); then + yetus_error "WARNING: Cannot determine the GitHub user of the token." + return 1 + fi + + while [[ ${page} -le 100 ]]; do + if ! "${CURL}" --silent --fail \ + -H "Accept: application/vnd.github.v3.raw+json" \ + "${GITHUB_AUTH[@]}" \ + --output "${pagefile}" \ + --location \ + "${GITHUB_API_URL}/repos/${GITHUB_REPO}/issues/${GITHUB_ISSUE}/comments?per_page=100&page=${page}" \ + 2>/dev/null; then + return 1 + fi + + # GitHub prints one field per line and keeps each body on a single + # line, so text in a body cannot fake these anchored lines. + # shellcheck disable=SC2016 + id=$(MARKER="${marker}" LOGIN="${login}" "${AWK}" -F\" ' + /^ "id": / { id = $3; gsub(/[^0-9]/, "", id) } + /^ "login": "/ { who = $4 } + /^ "body": "/ { hit = index($0, ENVIRON["MARKER"]) > 0 } + /^ "minimized": / { hidden = ($0 !~ /: null,?$/) } + /^ }/ { + if (hit && !hidden && who == ENVIRON["LOGIN"]) { + print id + exit + } + id = ""; who = ""; hit = 0; hidden = 0 + }' "${pagefile}") + + if [[ -n "${id}" ]]; then + echo "${id}" + return 0 + fi + + # a short page is the last one + if [[ $("${GREP}" -c '^ }' "${pagefile}") -lt 100 ]]; then + return 1 + fi + ((page=page+1)) + done + return 1 +} + ## @description Write the contents of a file to github +## @description With --github-update-comment, update the comment +## @description previously written with the same key instead. ## @param filename ## @stability stable ## @audience public @@ -883,6 +1033,8 @@ function github_write_comment declare -r commentfile=${1} declare retval=0 declare restfile="${PATCH_DIR}/ghcomment.$$" + declare marker="" + declare commentid if [[ "${OFFLINE}" == true ]]; then yetus_error "WARNING: Running offline, GitHub comment skipped." @@ -894,12 +1046,19 @@ function github_write_comment return 0 fi + if [[ -n "${GITHUB_UPDATE_COMMENT}" ]]; then + marker="yetus-report:${GITHUB_UPDATE_COMMENT} --" + fi + { printf "{\"body\":\"" "${SED}" -e 's,\\,\\\\,g' \ -e 's,\",\\\",g' \ -e 's,$,\\r\\n,g' "${commentfile}" \ | tr -d '\n' + if [[ -n "${marker}" ]]; then + printf '' + +# shellcheck disable=SC2034 +setup_github() { + big_console_header() { :; } + clock_display() { echo "$1s"; } + get_artifact_url() { echo ""; } + delete_parameter() { :; } + + AWK="awk" + GREP="grep" + SED="sed" + OFFLINE=false + ROBOT=true + ROBOTTYPE=jenkins + PATCH_DIR="${TMP}" + GITHUB_API_URL=https://api.example.com + GITHUB_REPO=apache/yetus + GITHUB_ISSUE=42 + GITHUB_AUTH=(-H "Authorization: token secret") + GITHUB_UPDATE_COMMENT=ubuntu + TP_VOTE_TABLE=() + TP_TEST_TABLE=() + TP_HEADER=() + TP_FOOTER_TABLE=() + + # serves ${TMP} fixtures, logs every call to ${TMP}/calls + CURL="${TMP}/curl" + cat > "${CURL}" <<'EOF' +#!/usr/bin/env bash +out="" +method=GET +data="" +url="" +while [[ $# -gt 0 ]]; do + case "$1" in + --output) out=$2; shift 2 ;; + -X) method=$2; shift 2 ;; + -d) data=${2#@}; shift 2 ;; + -H) shift 2 ;; + --*) shift ;; + *) url=$1; shift ;; + esac +done +echo "${method} ${url}" >> "${TMP}/calls" +serve() { + [[ -f "$1" ]] || exit 22 + if [[ -n "${out}" ]]; then cp "$1" "${out}"; else cat "$1"; fi +} +case "${method} ${url}" in + "GET "*/user) serve "${TMP}/user.json" ;; + "GET "*/pulls/*) serve "${TMP}/pull-latest.json" ;; + "GET "*/comments*) serve "${TMP}/page${url##*page=}.json" ;; + PATCH*) [[ -f "${TMP}/patchfail" ]] && exit 22; cp "${data}" "${TMP}/patched" ;; + POST*) cp "${data}" "${TMP}/posted" ;; +esac +EOF + chmod +x "${CURL}" + + printf '{\n "login": "yetus-bot",\n "id": 1\n}\n' > "${TMP}/user.json" + pull_json aaa > "${TMP}/github-pull.json" + pull_json aaa > "${TMP}/pull-latest.json" + echo 'report "body"' > "${TMP}/report.txt" +} + +# pull request JSON in GitHub's layout +pull_json() { + printf '{\n "number": 42,\n "merge_commit_sha": "mmm",\n "head": {\n "sha": "%s"\n },\n "base": {\n "sha": "bbb"\n }\n}\n' "$1" +} + +# one comment in GitHub's layout: id login body [minimized] +comment_json() { + cat <, POST, or nothing +outcome() { + if [[ -f "${TMP}/patched" ]]; then + echo "PATCH:$(grep -o 'comments/[0-9]*$' "${TMP}/calls" | cut -f2 -d/)" + elif [[ -f "${TMP}/posted" ]]; then + echo POST + fi +} + +@test "github_parse_args (--github-update-comment)" { + setup_github + GITHUB_WRITE_COMMENT=false + github_parse_args --github-update-comment + [ "${GITHUB_WRITE_COMMENT}" = true ] + [ "${GITHUB_UPDATE_COMMENT}" = default ] + github_parse_args --github-update-comment=Dockerfile_ubuntu_24 + [ "${GITHUB_UPDATE_COMMENT}" = Dockerfile_ubuntu_24 ] + github_parse_args '--github-update-comment=a"b' 2>/dev/null + [ "${GITHUB_UPDATE_COMMENT}" = "" ] + [ "${GITHUB_WRITE_COMMENT}" = true ] +} + +@test "github_write_comment (update off posts without lookups)" { + setup_github + GITHUB_UPDATE_COMMENT="" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + [ "$(wc -l < "${TMP}/calls")" -eq 1 ] + run grep -q yetus-report "${TMP}/posted" + [ "${status}" -ne 0 ] +} + +@test "github_write_comment (first report posts with marker)" { + setup_github + comments_page "1|someone|hi" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + grep -qF 'report \"body\"' "${TMP}/posted" +} + +@test "github_write_comment (updates own comment)" { + setup_github + comments_page "1|someone|hi" "2|yetus-bot|old\\r\\n${MARKER}" \ + "3|yetus-bot|newer\\r\\n${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = PATCH:2 ] + grep -qF "${MARKER}" "${TMP}/patched" +} + +@test "github_write_comment (ignores marker from other users)" { + setup_github + comments_page "1|someone|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (ignores other keys)" { + setup_github + comments_page "1|yetus-bot|" \ + "2|yetus-bot|" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (body cannot fake fields)" { + setup_github + comments_page "1|someone|\\n \\\"id\\\": 9,\\n \\\"login\\\": \\\"yetus-bot\\\",\\n${MARKER}" \ + "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:2 ] +} + +@test "github_write_comment (skips hidden comments)" { + setup_github + comments_page "1|yetus-bot|${MARKER}|{\"reason\": \"outdated\"}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (finds comment on a later page)" { + setup_github + declare -a cs=() + declare -i i + for ((i=1; i<=100; i++)); do + cs+=("${i}|someone|hi") + done + comments_page "${cs[@]}" > "${TMP}/page1.json" + comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:777 ] +} + +@test "github_write_comment (falls back to POST)" { + setup_github + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + touch "${TMP}/patchfail" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + + setup_github + rm "${TMP}/patchfail" "${TMP}/posted" "${TMP}/user.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + + setup_github + rm "${TMP}/posted" "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (GitHub Actions token)" { + setup_github + # shellcheck disable=SC2034 + ROBOTTYPE=githubactions + rm "${TMP}/user.json" + comments_page "5|github-actions[bot]|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:5 ] +} + +@test "github_finalreport_as_comment (adds tested commit)" { + setup_github + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "$(outcome)" = PATCH:2 ] + grep -qF '| Tested commit | aaa |' "${TMP}/patched" +} + +@test "github_finalreport_as_comment (skips superseded build)" { + setup_github + pull_json ccc > "${TMP}/pull-latest.json" + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "${status}" -eq 0 ] + [ -z "$(outcome)" ] + + setup_github + GITHUB_UPDATE_COMMENT="" + pull_json ccc > "${TMP}/pull-latest.json" + run github_finalreport_as_comment 0 + [ "$(outcome)" = POST ] + run grep -q 'Tested commit' "${TMP}/posted" + [ "${status}" -ne 0 ] +} From 006c3b294dc3fa4a683e2211eaa74518a608c95b Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 30 Nov 2026 16:33:58 +0800 Subject: [PATCH 2/4] YETUS-1280. Support update and hide modes via --github-comment-mode Replace --github-update-comment with --github-comment-mode= and --github-comment-key=. In hide mode, post a new comment and minimize prior visible comments written by the same GitHub user with the same key via GraphQL minimizeComment (classifier: OUTDATED). Assisted-by: Gemini 4 Neon --- .../precommit/plugins/github.html.md | 50 +- precommit/src/main/shell/plugins.d/github.sh | 175 +++++-- .../src/test/shell/github_comment_mode.bats | 446 ++++++++++++++++++ .../src/test/shell/github_update_comment.bats | 270 ----------- 4 files changed, 611 insertions(+), 330 deletions(-) create mode 100644 precommit/src/test/shell/github_comment_mode.bats delete mode 100644 precommit/src/test/shell/github_update_comment.bats diff --git a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md index 12d33de5e..11c404079 100644 --- a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md +++ b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md @@ -35,31 +35,39 @@ None ## Options -| Option | Notes | -| :-------------------------------------- | :------------------------------------------------- | -| `--github-annotations-limit=` | Max number of GitHub Checks Annotations to attempt | -| `--github-api-url=` | REST API URL (for GitHub Enterprise) | -| `--github-base-url=` | Non-REST API URL (for GitHub Enterprise) | -| `--github-repo=` | `username/repository` identifier | -| `--github-status-use-htmlreport=` | Use htmlout for Github Status 'Details' link | -| `--github-token=` | Token used to perform read and write operations | -| `--github-update-comment[=]` | Update the PR comment instead of adding a new one | -| `--github-write-comment` | Write the final report as a PR comment | - -## Updating The PR Comment - -`--github-write-comment` adds a new comment with every run. `--github-update-comment` implies it, and instead -updates the comment the same GitHub user previously wrote with the same key (default: `default`). The key may only -contain `A-Z`, `a-z`, `0-9`, `.`, `_` and `-`. Use a different key per job when one build writes several reports, -e.g., one per OS. +| Option | Notes | +| :--------------------------------------------- | :-------------------------------------------------------------- | +| `--github-annotations-limit=` | Max number of GitHub Checks Annotations to attempt | +| `--github-api-url=` | REST API URL (for GitHub Enterprise) | +| `--github-base-url=` | Non-REST API URL (for GitHub Enterprise) | +| `--github-comment-key=` | Key for matching prior comments in `update` or `hide` mode | +| `--github-comment-mode=` | How to handle prior PR comments (`append`, `update`, or `hide`) | +| `--github-repo=` | `username/repository` identifier | +| `--github-status-use-htmlreport=` | Use htmlout for Github Status 'Details' link | +| `--github-token=` | Token used to perform read and write operations | +| `--github-write-comment` | Write the final report as a PR comment | + +## Managing PR Comments + +`--github-write-comment` appends a new PR comment on every run (`--github-comment-mode=append`). To avoid comment +spam across rebuilds, `--github-comment-mode` supports two additional modes (both imply `--github-write-comment`): + +1. `update`: edits the oldest visible comment previously written by the same GitHub user with the same key. +1. `hide`: posts a new comment and hides (minimizes as outdated via GraphQL) prior visible comments written by the + same GitHub user with the same key. + +Use `--github-comment-key=` (default: `default`; allowed characters: `A-Z`, `a-z`, `0-9`, `.`, `_`, `-`) to +assign a separate key per job when one build posts multiple reports, e.g., one per OS. + +In `update` and `hide` modes: 1. The report carries a hidden `` marker and a `Tested commit` row. 1. The token's user comes from `GET /user`. Under GitHub Actions, where that call is denied, `github-actions[bot]` is assumed. -1. Hidden (minimized) comments are not updated. -1. If the PR head moved past the tested commit, the comment is skipped so a slow build cannot replace the report of - a newer one. Statuses are still written. -1. If looking up or updating the comment fails, a new comment is written. +1. Already-hidden (minimized) comments are ignored. +1. If the PR head moved past the tested commit, the comment is skipped so a slow build cannot replace or hide the + report of a newer one. Statuses are still written. +1. If looking up, updating, or hiding prior comments fails, a new comment is still posted. ## HTML Details LInk diff --git a/precommit/src/main/shell/plugins.d/github.sh b/precommit/src/main/shell/plugins.d/github.sh index 6430fd858..1787656b7 100755 --- a/precommit/src/main/shell/plugins.d/github.sh +++ b/precommit/src/main/shell/plugins.d/github.sh @@ -39,7 +39,8 @@ GITHUB_USE_EMOJI_VOTE=false GITHUB_STATUS_RECOVERY_COUNTER=1 GITHUB_STATUS_RECOVER_TOOL=false GITHUB_WRITE_COMMENT=false -GITHUB_UPDATE_COMMENT="" +GITHUB_COMMENT_MODE="append" +GITHUB_COMMENT_KEY="default" GITHUB_ANNOTATION_LIMIT=50 GITHUB_STATUS_USE_HTMLREPORT=${GITHUB_STATUS_USE_HTMLREPORT:-true} declare -a GITHUB_AUTH @@ -55,7 +56,8 @@ function github_usage yetus_add_option "--github-repo=" "github repo to use (default:'${GITHUB_REPO}')" yetus_add_option "--github-token=" "The token to use to read/write to github" yetus_add_option "--github-write-comment" "Write final report as github comment (default: '${GITHUB_WRITE_COMMENT}')" - yetus_add_option "--github-update-comment[=]" "Write final report as github comment, updating the one previously written with the same key" + yetus_add_option "--github-comment-mode=" "How to handle prior PR comments: append, update in place, or hide prior ones (default: '${GITHUB_COMMENT_MODE}')" + yetus_add_option "--github-comment-key=" "Key used to match prior PR comments in update or hide mode (default: '${GITHUB_COMMENT_KEY}')" yetus_add_option "--github-use-emoji-vote" "Whether to use emoji to represent the vote result on github [default: ${GITHUB_USE_EMOJI_VOTE}]" yetus_add_option "--github-status-use-htmlreport=" "Use htmlout for Github Status 'Details' link [default: ${GITHUB_STATUS_USE_HTMLREPORT}]" } @@ -90,17 +92,30 @@ function github_parse_args delete_parameter "${i}" GITHUB_WRITE_COMMENT=true ;; - --github-update-comment|--github-update-comment=*) + --github-comment-mode=*) + delete_parameter "${i}" + case "${i#*=}" in + append) + GITHUB_COMMENT_MODE=append + ;; + update|hide) + GITHUB_COMMENT_MODE=${i#*=} + GITHUB_WRITE_COMMENT=true + ;; + *) + yetus_error "WARNING: Unknown --github-comment-mode '${i#*=}'. Using 'append'." + GITHUB_COMMENT_MODE=append + ;; + esac + ;; + --github-comment-key=*) delete_parameter "${i}" - GITHUB_WRITE_COMMENT=true - GITHUB_UPDATE_COMMENT=default - if [[ "${i}" == *=* ]]; then - GITHUB_UPDATE_COMMENT=${i#*=} - fi # the key goes into the JSON body unescaped - if [[ ! "${GITHUB_UPDATE_COMMENT}" =~ ^[A-Za-z0-9._-]+$ ]]; then - yetus_error "WARNING: --github-update-comment key must match [A-Za-z0-9._-]+. Writing a new comment instead." - GITHUB_UPDATE_COMMENT="" + if [[ "${i#*=}" =~ ^[A-Za-z0-9._-]+$ ]]; then + GITHUB_COMMENT_KEY=${i#*=} + else + yetus_error "WARNING: --github-comment-key must match [A-Za-z0-9._-]+. Writing a new comment instead." + GITHUB_COMMENT_KEY="" fi ;; --github-use-emoji-vote) @@ -889,6 +904,16 @@ EOF rm "${tempfile}" } +## @description Return 0 when comment update or hide mode is active with a valid key +## @audience private +## @stability evolving +## @replaceable no +function github_comment_mode_active +{ + [[ "${GITHUB_COMMENT_MODE}" == "update" || "${GITHUB_COMMENT_MODE}" == "hide" ]] \ + && [[ "${GITHUB_COMMENT_KEY}" =~ ^[A-Za-z0-9._-]+$ ]] +} + ## @description Print the PR head sha from a pull request JSON file ## @audience private ## @stability evolving @@ -896,14 +921,17 @@ EOF ## @param filename function github_pull_head_sha { - "${GREP}" \"sha\" "${1}" 2>/dev/null \ - | head -1 \ - | cut -f4 -d\" + # shellcheck disable=SC2016 + "${AWK}" -F\" ' + /^ "head": \{/ { in_head = 1 } + in_head && /^ "sha": "/ { print $4; exit } + /^ \}/ { in_head = 0 } + ' "${1}" 2>/dev/null } ## @description Check if the PR head moved past the tested commit, so that -## @description a slow build of an older commit does not replace the report -## @description of a newer one +## @description a slow build of an older commit does not replace or hide +## @description the report of a newer one ## @audience private ## @stability evolving ## @replaceable no @@ -914,6 +942,10 @@ function github_report_superseded declare tested declare current + if [[ "${OFFLINE}" == true || "${#GITHUB_AUTH[@]}" -lt 1 ]]; then + return 1 + fi + tested=$(github_pull_head_sha "${PATCH_DIR}/github-pull.json") if [[ -z "${tested}" ]]; then return 1 @@ -961,19 +993,22 @@ function github_comment_login echo "${login}" } -## @description Print the id of the oldest visible PR comment written by -## @description the token's user that contains the marker +## @description Print ids or node_ids of visible PR comments written by +## @description the token's user that contain the marker. ## @audience private ## @stability evolving ## @replaceable no ## @param marker -## @return 1 if not found -function github_find_comment +## @param field ("id" prints the oldest match; "node_id" prints all) +## @return 0 if at least one comment was found +function github_find_comments { declare marker=$1 + declare field=$2 declare pagefile="${PATCH_DIR}/github-comments.json" declare login - declare id + declare matches + declare found=false declare -i page=1 if ! login=$(github_comment_login); then @@ -989,42 +1024,88 @@ function github_find_comment --location \ "${GITHUB_API_URL}/repos/${GITHUB_REPO}/issues/${GITHUB_ISSUE}/comments?per_page=100&page=${page}" \ 2>/dev/null; then + [[ "${found}" == true ]] && return 0 return 1 fi # GitHub prints one field per line and keeps each body on a single # line, so text in a body cannot fake these anchored lines. # shellcheck disable=SC2016 - id=$(MARKER="${marker}" LOGIN="${login}" "${AWK}" -F\" ' + matches=$(MARKER="${marker}" LOGIN="${login}" FIELD="${field}" "${AWK}" -F\" ' /^ "id": / { id = $3; gsub(/[^0-9]/, "", id) } + /^ "node_id": "/ { nid = $4 } /^ "login": "/ { who = $4 } /^ "body": "/ { hit = index($0, ENVIRON["MARKER"]) > 0 } /^ "minimized": / { hidden = ($0 !~ /: null,?$/) } /^ }/ { if (hit && !hidden && who == ENVIRON["LOGIN"]) { - print id - exit + if (ENVIRON["FIELD"] == "node_id") { + if (nid != "") { + print nid + } + } else if (id != "") { + print id + exit + } } - id = ""; who = ""; hit = 0; hidden = 0 + id = ""; nid = ""; who = ""; hit = 0; hidden = 0 }' "${pagefile}") - if [[ -n "${id}" ]]; then - echo "${id}" - return 0 + if [[ -n "${matches}" ]]; then + printf '%s\n' "${matches}" + found=true + if [[ "${field}" != "node_id" ]]; then + return 0 + fi fi # a short page is the last one if [[ $("${GREP}" -c '^ }' "${pagefile}") -lt 100 ]]; then - return 1 + break fi ((page=page+1)) done - return 1 + + [[ "${found}" == true ]] +} + +## @description Hide a GitHub PR comment as outdated via GraphQL +## @audience private +## @stability evolving +## @replaceable no +## @param node_id +## @return 0 on success +function github_minimize_comment +{ + declare nodeid=$1 + declare graphql_url="${GITHUB_API_URL%/}" + declare respfile="${PATCH_DIR}/github-comment-minimize-response.json" + declare query + + if [[ ! "${nodeid}" =~ ^[A-Za-z0-9_+=/:-]+$ ]]; then + return 1 + fi + + graphql_url="${graphql_url%/v3}/graphql" + query="{\"query\":\"mutation { minimizeComment(input: {subjectId: \\\"${nodeid}\\\", classifier: OUTDATED}) { minimizedComment { isMinimized } } }\"}" + + if ! "${CURL}" --silent --fail -X POST \ + -H "Accept: application/vnd.github+json" \ + -H "Content-Type: application/json" \ + "${GITHUB_AUTH[@]}" \ + -d "${query}" \ + --output "${respfile}" \ + --location \ + "${graphql_url}" \ + >/dev/null 2>&1; then + return 1 + fi + + "${GREP}" -q '"isMinimized":[[:space:]]*true' "${respfile}" } ## @description Write the contents of a file to github -## @description With --github-update-comment, update the comment -## @description previously written with the same key instead. +## @description Honors --github-comment-mode (append, update, or hide). ## @param filename ## @stability stable ## @audience public @@ -1035,6 +1116,8 @@ function github_write_comment declare restfile="${PATCH_DIR}/ghcomment.$$" declare marker="" declare commentid + declare nodeid + declare -a old_nodeids=() if [[ "${OFFLINE}" == true ]]; then yetus_error "WARNING: Running offline, GitHub comment skipped." @@ -1046,8 +1129,8 @@ function github_write_comment return 0 fi - if [[ -n "${GITHUB_UPDATE_COMMENT}" ]]; then - marker="yetus-report:${GITHUB_UPDATE_COMMENT} --" + if github_comment_mode_active; then + marker="yetus-report:${GITHUB_COMMENT_KEY} --" fi { @@ -1067,8 +1150,8 @@ function github_write_comment return 0 fi - if [[ -n "${marker}" ]] \ - && commentid=$(github_find_comment "${marker}"); then + if [[ -n "${marker}" && "${GITHUB_COMMENT_MODE}" == "update" ]] \ + && commentid=$(github_find_comments "${marker}" id); then if "${CURL}" --silent --fail -X PATCH \ -H "Accept: application/vnd.github.v3.full+json" \ -H "Content-Type: application/json" \ @@ -1082,6 +1165,12 @@ function github_write_comment return 0 fi yetus_error "WARNING: Cannot update GitHub comment ${commentid}. Writing a new one." + elif [[ -n "${marker}" && "${GITHUB_COMMENT_MODE}" == "hide" ]]; then + while IFS= read -r nodeid; do + if [[ -n "${nodeid}" ]]; then + old_nodeids+=("${nodeid}") + fi + done < <(github_find_comments "${marker}" node_id) fi "${CURL}" --silent --fail -X POST \ @@ -1096,6 +1185,15 @@ function github_write_comment retval=$? rm "${restfile}" + + if [[ ${retval} -eq 0 && ${#old_nodeids[@]} -gt 0 ]]; then + for nodeid in "${old_nodeids[@]}"; do + if ! github_minimize_comment "${nodeid}"; then + yetus_error "WARNING: Cannot hide prior GitHub comment ${nodeid}." + fi + done + fi + return ${retval} } @@ -1130,7 +1228,7 @@ function github_finalreport_as_comment big_console_header "Adding comment to Github" - if [[ -n "${GITHUB_UPDATE_COMMENT}" ]] && github_report_superseded; then + if github_comment_mode_active && github_report_superseded; then yetus_error "WARNING: The PR has a newer commit than the one tested. Skipping GitHub comment." return 0 fi @@ -1243,8 +1341,7 @@ function github_finalreport_as_comment ((i=i+1)) done - # an updated comment does not show which push it belongs to - if [[ -n "${GITHUB_UPDATE_COMMENT}" ]]; then + if github_comment_mode_active; then sha=$(github_pull_head_sha "${PATCH_DIR}/github-pull.json") if [[ -n "${sha}" ]]; then printf '| Tested commit | %s |\n' "${sha}" >> "${commentfile}" diff --git a/precommit/src/test/shell/github_comment_mode.bats b/precommit/src/test/shell/github_comment_mode.bats new file mode 100644 index 000000000..67d9ab631 --- /dev/null +++ b/precommit/src/test/shell/github_comment_mode.bats @@ -0,0 +1,446 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load functions_test_helper + +# sourced at file level so the plugin's declares stay global +add_bugsystem() { :; } +# shellcheck disable=SC1091 +. "${BATS_TEST_DIRNAME}/../../main/shell/plugins.d/github.sh" + +MARKER='' + +# shellcheck disable=SC2034 +setup_github() { + big_console_header() { :; } + clock_display() { echo "$1s"; } + get_artifact_url() { echo ""; } + delete_parameter() { :; } + + AWK="awk" + GREP="grep" + SED="sed" + OFFLINE=false + ROBOT=true + ROBOTTYPE=jenkins + PATCH_DIR="${TMP}" + GITHUB_API_URL=https://api.example.com + GITHUB_REPO=apache/yetus + GITHUB_ISSUE=42 + GITHUB_AUTH=(-H "Authorization: token secret") + GITHUB_WRITE_COMMENT=true + GITHUB_COMMENT_MODE=update + GITHUB_COMMENT_KEY=ubuntu + TP_VOTE_TABLE=() + TP_TEST_TABLE=() + TP_HEADER=() + TP_FOOTER_TABLE=() + + rm -f "${TMP}/calls" "${TMP}/posted" "${TMP}/patched" "${TMP}/minimized" \ + "${TMP}/patchfail" "${TMP}/postfail" "${TMP}/graphqlfail" \ + "${TMP}/graphqlerror" + + # serves ${TMP} fixtures, logs every call to ${TMP}/calls + CURL="${TMP}/curl" + cat > "${CURL}" <<'EOF' +#!/usr/bin/env bash +out="" +method=GET +data="" +datafile="" +url="" +while [[ $# -gt 0 ]]; do + case "$1" in + --output) out=$2; shift 2 ;; + -X) method=$2; shift 2 ;; + -d) + if [[ "$2" == @* ]]; then + datafile=${2#@} + else + data=$2 + fi + shift 2 + ;; + -H) shift 2 ;; + --*) shift ;; + *) url=$1; shift ;; + esac +done +echo "${method} ${url}" >> "${TMP}/calls" +serve() { + [[ -f "$1" ]] || exit 22 + if [[ -n "${out}" ]]; then cp "$1" "${out}"; else cat "$1"; fi +} +case "${method} ${url}" in + "GET "*/user) serve "${TMP}/user.json" ;; + "GET "*/pulls/*) serve "${TMP}/pull-latest.json" ;; + "GET "*/comments*) serve "${TMP}/page${url##*page=}.json" ;; + "POST "*/graphql) + [[ -f "${TMP}/graphqlfail" ]] && exit 22 + echo "${data}" >> "${TMP}/minimized" + if [[ -f "${TMP}/graphqlerror" ]]; then + resp='{"data":{"minimizeComment":null},"errors":[{"type":"FORBIDDEN"}]}' + else + resp='{"data":{"minimizeComment":{"minimizedComment":{"isMinimized":true}}}}' + fi + if [[ -n "${out}" ]]; then echo "${resp}" > "${out}"; else echo "${resp}"; fi + ;; + PATCH*) [[ -f "${TMP}/patchfail" ]] && exit 22; cp "${datafile}" "${TMP}/patched" ;; + POST*) [[ -f "${TMP}/postfail" ]] && exit 22; cp "${datafile}" "${TMP}/posted" ;; +esac +EOF + chmod +x "${CURL}" + + printf '{\n "login": "yetus-bot",\n "id": 1\n}\n' > "${TMP}/user.json" + pull_json aaa > "${TMP}/github-pull.json" + pull_json aaa > "${TMP}/pull-latest.json" + echo 'report "body"' > "${TMP}/report.txt" +} + +# pull request JSON in GitHub's layout +pull_json() { + printf '{\n "number": 42,\n "merge_commit_sha": "mmm",\n "head": {\n "sha": "%s"\n },\n "base": {\n "sha": "bbb"\n }\n}\n' "$1" +} + +# one comment in GitHub's layout: id login body [minimized] +comment_json() { + cat <, POST, or nothing +outcome() { + if [[ -f "${TMP}/patched" ]]; then + echo "PATCH:$(grep -o 'comments/[0-9]*$' "${TMP}/calls" | cut -f2 -d/)" + elif [[ -f "${TMP}/posted" ]]; then + echo POST + fi +} + +@test "github_parse_args (--github-comment-mode and --github-comment-key)" { + setup_github + GITHUB_WRITE_COMMENT=false + GITHUB_COMMENT_MODE=append + GITHUB_COMMENT_KEY=default + + github_parse_args --github-comment-mode=update + [ "${GITHUB_WRITE_COMMENT}" = true ] + [ "${GITHUB_COMMENT_MODE}" = update ] + [ "${GITHUB_COMMENT_KEY}" = default ] + + GITHUB_WRITE_COMMENT=false + github_parse_args --github-comment-mode=hide --github-comment-key=Dockerfile_ubuntu_24 + [ "${GITHUB_WRITE_COMMENT}" = true ] + [ "${GITHUB_COMMENT_MODE}" = hide ] + [ "${GITHUB_COMMENT_KEY}" = Dockerfile_ubuntu_24 ] + + GITHUB_WRITE_COMMENT=false + github_parse_args --github-comment-mode=append + [ "${GITHUB_WRITE_COMMENT}" = false ] + [ "${GITHUB_COMMENT_MODE}" = append ] + + github_parse_args --github-comment-mode=invalid 2>/dev/null + [ "${GITHUB_COMMENT_MODE}" = append ] + + github_parse_args '--github-comment-key=a"b' --github-comment-mode=update 2>/dev/null + [ "${GITHUB_COMMENT_KEY}" = "" ] + [ "${GITHUB_WRITE_COMMENT}" = true ] + run github_comment_mode_active + [ "${status}" -ne 0 ] +} + +@test "github_write_comment (append mode posts without lookups)" { + setup_github + GITHUB_COMMENT_MODE=append + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + [ "$(wc -l < "${TMP}/calls")" -eq 1 ] + run grep -q yetus-report "${TMP}/posted" + [ "${status}" -ne 0 ] +} + +@test "github_write_comment (update: first report posts with marker)" { + setup_github + comments_page "1|someone|hi" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + grep -qF 'report \"body\"' "${TMP}/posted" +} + +@test "github_write_comment (update: edits oldest own visible comment)" { + setup_github + comments_page "1|someone|hi" "2|yetus-bot|old\\r\\n${MARKER}" \ + "3|yetus-bot|newer\\r\\n${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = PATCH:2 ] + grep -qF "${MARKER}" "${TMP}/patched" +} + +@test "github_write_comment (update: ignores marker from other users)" { + setup_github + comments_page "1|someone|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (update: ignores other keys)" { + setup_github + comments_page "1|yetus-bot|" \ + "2|yetus-bot|" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (update: body cannot fake fields)" { + setup_github + comments_page "1|someone|\\n \\\"id\\\": 9,\\n \\\"node_id\\\": \\\"IC_fake\\\",\\n \\\"login\\\": \\\"yetus-bot\\\",\\n${MARKER}" \ + "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:2 ] +} + +@test "github_write_comment (update: skips hidden comments)" { + setup_github + comments_page "1|yetus-bot|${MARKER}|{\"reason\": \"outdated\"}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (update: finds comment on a later page)" { + setup_github + declare -a cs=() + declare -i i + for ((i=1; i<=100; i++)); do + cs+=("${i}|someone|hi") + done + comments_page "${cs[@]}" > "${TMP}/page1.json" + comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:777 ] +} + +@test "github_write_comment (update: falls back to POST)" { + setup_github + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + touch "${TMP}/patchfail" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + + setup_github + rm "${TMP}/user.json" + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + + setup_github + rm -f "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_comment (update: GitHub Actions token)" { + setup_github + # shellcheck disable=SC2034 + ROBOTTYPE=githubactions + rm "${TMP}/user.json" + comments_page "5|github-actions[bot]|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:5 ] +} + +@test "github_write_comment (hide: first report posts without GraphQL)" { + setup_github + GITHUB_COMMENT_MODE=hide + comments_page "1|someone|hi" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + [ ! -f "${TMP}/minimized" ] +} + +@test "github_write_comment (hide: posts new and minimizes own matching comments)" { + setup_github + GITHUB_COMMENT_MODE=hide + comments_page \ + "1|someone|${MARKER}" \ + "2|yetus-bot|old1\\r\\n${MARKER}" \ + "3|yetus-bot|hidden\\r\\n${MARKER}|{\"reason\": \"outdated\"}" \ + "4|yetus-bot|" \ + "5|yetus-bot|old2\\r\\n${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + [ "$(wc -l < "${TMP}/minimized")" -eq 2 ] + grep -qF 'IC_node_2' "${TMP}/minimized" + grep -qF 'IC_node_5' "${TMP}/minimized" + run grep -qE 'IC_node_(1|3|4)|MDQ_user' "${TMP}/minimized" + [ "${status}" -ne 0 ] +} + +@test "github_write_comment (hide: scans across pages)" { + setup_github + GITHUB_COMMENT_MODE=hide + declare -a cs=("1|yetus-bot|${MARKER}") + declare -i i + for ((i=2; i<=100; i++)); do + cs+=("${i}|someone|hi") + done + comments_page "${cs[@]}" > "${TMP}/page1.json" + comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + [ "$(wc -l < "${TMP}/minimized")" -eq 2 ] + grep -qF 'IC_node_1' "${TMP}/minimized" + grep -qF 'IC_node_777' "${TMP}/minimized" +} + +@test "github_write_comment (hide: derives GHE GraphQL URL from /api/v3)" { + setup_github + GITHUB_COMMENT_MODE=hide + # shellcheck disable=SC2034 + GITHUB_API_URL=https://ghe.example.com/api/v3 + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + grep -qF 'POST https://ghe.example.com/api/graphql' "${TMP}/calls" +} + +@test "github_write_comment (hide: skips minimize when POST fails)" { + setup_github + GITHUB_COMMENT_MODE=hide + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + touch "${TMP}/postfail" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -ne 0 ] + [ ! -f "${TMP}/minimized" ] +} + +@test "github_write_comment (hide: warns on GraphQL failure without failing)" { + setup_github + GITHUB_COMMENT_MODE=hide + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + touch "${TMP}/graphqlerror" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot hide prior GitHub comment IC_node_2"* ]] + + setup_github + GITHUB_COMMENT_MODE=hide + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + touch "${TMP}/graphqlfail" + run github_write_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot hide prior GitHub comment IC_node_2"* ]] +} + +@test "github_finalreport_as_comment (adds tested commit in update and hide modes)" { + setup_github + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "$(outcome)" = PATCH:2 ] + grep -qF '| Tested commit | aaa |' "${TMP}/patched" + + setup_github + GITHUB_COMMENT_MODE=hide + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "$(outcome)" = POST ] + grep -qF '| Tested commit | aaa |' "${TMP}/posted" + grep -qF 'IC_node_2' "${TMP}/minimized" +} + +@test "github_pull_head_sha (ignores sha in PR body and base)" { + setup_github + cat > "${TMP}/pr.json" <<'EOF' +{ + "number": 42, + "body": "mentions \"sha\": \"fake-in-body\" before head", + "head": { + "ref": "feature", + "repo": { + "name": "yetus" + }, + "sha": "deadbeef1234" + }, + "base": { + "sha": "base0000" + } +} +EOF + run github_pull_head_sha "${TMP}/pr.json" + [ "${status}" -eq 0 ] + [ "${output}" = "deadbeef1234" ] +} + +@test "github_finalreport_as_comment (skips superseded build in update and hide modes)" { + setup_github + pull_json ccc > "${TMP}/pull-latest.json" + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "${status}" -eq 0 ] + [ -z "$(outcome)" ] + + setup_github + GITHUB_COMMENT_MODE=hide + pull_json ccc > "${TMP}/pull-latest.json" + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "${status}" -eq 0 ] + [ -z "$(outcome)" ] + [ ! -f "${TMP}/minimized" ] + + setup_github + GITHUB_COMMENT_MODE=append + pull_json ccc > "${TMP}/pull-latest.json" + run github_finalreport_as_comment 0 + [ "$(outcome)" = POST ] + run grep -q 'Tested commit' "${TMP}/posted" + [ "${status}" -ne 0 ] +} diff --git a/precommit/src/test/shell/github_update_comment.bats b/precommit/src/test/shell/github_update_comment.bats deleted file mode 100644 index 0f8d5d1be..000000000 --- a/precommit/src/test/shell/github_update_comment.bats +++ /dev/null @@ -1,270 +0,0 @@ -#!/usr/bin/env bash -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -load functions_test_helper - -# sourced at file level so the plugin's declares stay global -add_bugsystem() { :; } -# shellcheck disable=SC1091 -. "${BATS_TEST_DIRNAME}/../../main/shell/plugins.d/github.sh" - -MARKER='' - -# shellcheck disable=SC2034 -setup_github() { - big_console_header() { :; } - clock_display() { echo "$1s"; } - get_artifact_url() { echo ""; } - delete_parameter() { :; } - - AWK="awk" - GREP="grep" - SED="sed" - OFFLINE=false - ROBOT=true - ROBOTTYPE=jenkins - PATCH_DIR="${TMP}" - GITHUB_API_URL=https://api.example.com - GITHUB_REPO=apache/yetus - GITHUB_ISSUE=42 - GITHUB_AUTH=(-H "Authorization: token secret") - GITHUB_UPDATE_COMMENT=ubuntu - TP_VOTE_TABLE=() - TP_TEST_TABLE=() - TP_HEADER=() - TP_FOOTER_TABLE=() - - # serves ${TMP} fixtures, logs every call to ${TMP}/calls - CURL="${TMP}/curl" - cat > "${CURL}" <<'EOF' -#!/usr/bin/env bash -out="" -method=GET -data="" -url="" -while [[ $# -gt 0 ]]; do - case "$1" in - --output) out=$2; shift 2 ;; - -X) method=$2; shift 2 ;; - -d) data=${2#@}; shift 2 ;; - -H) shift 2 ;; - --*) shift ;; - *) url=$1; shift ;; - esac -done -echo "${method} ${url}" >> "${TMP}/calls" -serve() { - [[ -f "$1" ]] || exit 22 - if [[ -n "${out}" ]]; then cp "$1" "${out}"; else cat "$1"; fi -} -case "${method} ${url}" in - "GET "*/user) serve "${TMP}/user.json" ;; - "GET "*/pulls/*) serve "${TMP}/pull-latest.json" ;; - "GET "*/comments*) serve "${TMP}/page${url##*page=}.json" ;; - PATCH*) [[ -f "${TMP}/patchfail" ]] && exit 22; cp "${data}" "${TMP}/patched" ;; - POST*) cp "${data}" "${TMP}/posted" ;; -esac -EOF - chmod +x "${CURL}" - - printf '{\n "login": "yetus-bot",\n "id": 1\n}\n' > "${TMP}/user.json" - pull_json aaa > "${TMP}/github-pull.json" - pull_json aaa > "${TMP}/pull-latest.json" - echo 'report "body"' > "${TMP}/report.txt" -} - -# pull request JSON in GitHub's layout -pull_json() { - printf '{\n "number": 42,\n "merge_commit_sha": "mmm",\n "head": {\n "sha": "%s"\n },\n "base": {\n "sha": "bbb"\n }\n}\n' "$1" -} - -# one comment in GitHub's layout: id login body [minimized] -comment_json() { - cat <, POST, or nothing -outcome() { - if [[ -f "${TMP}/patched" ]]; then - echo "PATCH:$(grep -o 'comments/[0-9]*$' "${TMP}/calls" | cut -f2 -d/)" - elif [[ -f "${TMP}/posted" ]]; then - echo POST - fi -} - -@test "github_parse_args (--github-update-comment)" { - setup_github - GITHUB_WRITE_COMMENT=false - github_parse_args --github-update-comment - [ "${GITHUB_WRITE_COMMENT}" = true ] - [ "${GITHUB_UPDATE_COMMENT}" = default ] - github_parse_args --github-update-comment=Dockerfile_ubuntu_24 - [ "${GITHUB_UPDATE_COMMENT}" = Dockerfile_ubuntu_24 ] - github_parse_args '--github-update-comment=a"b' 2>/dev/null - [ "${GITHUB_UPDATE_COMMENT}" = "" ] - [ "${GITHUB_WRITE_COMMENT}" = true ] -} - -@test "github_write_comment (update off posts without lookups)" { - setup_github - GITHUB_UPDATE_COMMENT="" - run github_write_comment "${TMP}/report.txt" - [ "${status}" -eq 0 ] - [ "$(outcome)" = POST ] - [ "$(wc -l < "${TMP}/calls")" -eq 1 ] - run grep -q yetus-report "${TMP}/posted" - [ "${status}" -ne 0 ] -} - -@test "github_write_comment (first report posts with marker)" { - setup_github - comments_page "1|someone|hi" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] - grep -qF "${MARKER}" "${TMP}/posted" - grep -qF 'report \"body\"' "${TMP}/posted" -} - -@test "github_write_comment (updates own comment)" { - setup_github - comments_page "1|someone|hi" "2|yetus-bot|old\\r\\n${MARKER}" \ - "3|yetus-bot|newer\\r\\n${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "${status}" -eq 0 ] - [ "$(outcome)" = PATCH:2 ] - grep -qF "${MARKER}" "${TMP}/patched" -} - -@test "github_write_comment (ignores marker from other users)" { - setup_github - comments_page "1|someone|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] -} - -@test "github_write_comment (ignores other keys)" { - setup_github - comments_page "1|yetus-bot|" \ - "2|yetus-bot|" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] -} - -@test "github_write_comment (body cannot fake fields)" { - setup_github - comments_page "1|someone|\\n \\\"id\\\": 9,\\n \\\"login\\\": \\\"yetus-bot\\\",\\n${MARKER}" \ - "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = PATCH:2 ] -} - -@test "github_write_comment (skips hidden comments)" { - setup_github - comments_page "1|yetus-bot|${MARKER}|{\"reason\": \"outdated\"}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] -} - -@test "github_write_comment (finds comment on a later page)" { - setup_github - declare -a cs=() - declare -i i - for ((i=1; i<=100; i++)); do - cs+=("${i}|someone|hi") - done - comments_page "${cs[@]}" > "${TMP}/page1.json" - comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = PATCH:777 ] -} - -@test "github_write_comment (falls back to POST)" { - setup_github - comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - touch "${TMP}/patchfail" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] - - setup_github - rm "${TMP}/patchfail" "${TMP}/posted" "${TMP}/user.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] - - setup_github - rm "${TMP}/posted" "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = POST ] -} - -@test "github_write_comment (GitHub Actions token)" { - setup_github - # shellcheck disable=SC2034 - ROBOTTYPE=githubactions - rm "${TMP}/user.json" - comments_page "5|github-actions[bot]|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" - [ "$(outcome)" = PATCH:5 ] -} - -@test "github_finalreport_as_comment (adds tested commit)" { - setup_github - comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_finalreport_as_comment 0 - [ "$(outcome)" = PATCH:2 ] - grep -qF '| Tested commit | aaa |' "${TMP}/patched" -} - -@test "github_finalreport_as_comment (skips superseded build)" { - setup_github - pull_json ccc > "${TMP}/pull-latest.json" - comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_finalreport_as_comment 0 - [ "${status}" -eq 0 ] - [ -z "$(outcome)" ] - - setup_github - GITHUB_UPDATE_COMMENT="" - pull_json ccc > "${TMP}/pull-latest.json" - run github_finalreport_as_comment 0 - [ "$(outcome)" = POST ] - run grep -q 'Tested commit' "${TMP}/posted" - [ "${status}" -ne 0 ] -} From 8e124795e57a9a6411f77de9b0754997e05d38cf Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 30 Nov 2026 17:13:23 +0800 Subject: [PATCH 3/4] YETUS-1280. Avoid detect-secrets false positive in bats test Assisted-by: Gemini 4 Neon --- precommit/src/test/shell/github_comment_mode.bats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/precommit/src/test/shell/github_comment_mode.bats b/precommit/src/test/shell/github_comment_mode.bats index 67d9ab631..974a0c6a3 100644 --- a/precommit/src/test/shell/github_comment_mode.bats +++ b/precommit/src/test/shell/github_comment_mode.bats @@ -407,7 +407,7 @@ outcome() { "repo": { "name": "yetus" }, - "sha": "deadbeef1234" + "sha": "abc1111" }, "base": { "sha": "base0000" @@ -416,7 +416,7 @@ outcome() { EOF run github_pull_head_sha "${TMP}/pr.json" [ "${status}" -eq 0 ] - [ "${output}" = "deadbeef1234" ] + [ "${output}" = "abc1111" ] } @test "github_finalreport_as_comment (skips superseded build in update and hide modes)" { From 45b08fd1eba46397ca5d5dd7f9117c7518e9cf98 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 30 Nov 2026 19:48:13 +0800 Subject: [PATCH 4/4] YETUS-1280. Address review feedback on GitHub comment modes 1. Keep public github_write_comment append-only; apply update/hide modes only to final reports via github_write_report_comment. 2. Minimize duplicate visible comments after a successful PATCH in update mode so transient write failures converge back to one visible report. 3. Have all --github-comment-mode values (including append and fallback) enable GITHUB_WRITE_COMMENT, and clarify invalid key warning text. 4. Log warnings on comment-list GET failures and when assuming github-actions[bot] after /user lookup fails under GitHub Actions. 5. Tighten bats assertions and add tests for duplicate cleanup, non-report writes, missing github-pull.json, and non-GHA bot fallback. Assisted-by: Gemini 4 Neon --- .../precommit/plugins/github.html.md | 20 ++- precommit/src/main/shell/plugins.d/github.sh | 168 ++++++++++++------ .../src/test/shell/github_comment_mode.bats | 148 +++++++++++---- 3 files changed, 236 insertions(+), 100 deletions(-) diff --git a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md index 11c404079..0f9d217ed 100644 --- a/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md +++ b/asf-site-src/source/documentation/in-progress/precommit/plugins/github.html.md @@ -49,25 +49,29 @@ None ## Managing PR Comments -`--github-write-comment` appends a new PR comment on every run (`--github-comment-mode=append`). To avoid comment -spam across rebuilds, `--github-comment-mode` supports two additional modes (both imply `--github-write-comment`): +`--github-write-comment` appends a new PR comment on every run. `--github-comment-mode=` implies +`--github-write-comment` and controls how prior final report comments are handled: -1. `update`: edits the oldest visible comment previously written by the same GitHub user with the same key. -1. `hide`: posts a new comment and hides (minimizes as outdated via GraphQL) prior visible comments written by the - same GitHub user with the same key. +1. `append` (default): appends a new PR comment on every run. +1. `update`: edits the oldest visible report comment previously written by the same GitHub user with the same key, + and minimizes any extra visible duplicates as outdated. +1. `hide`: posts a new report comment and hides (minimizes as outdated via GraphQL) prior visible report comments + written by the same GitHub user with the same key. Use `--github-comment-key=` (default: `default`; allowed characters: `A-Z`, `a-z`, `0-9`, `.`, `_`, `-`) to assign a separate key per job when one build posts multiple reports, e.g., one per OS. -In `update` and `hide` modes: +In `update` and `hide` modes (applied to final report comments only; other comments such as re-exec notices are +always appended): 1. The report carries a hidden `` marker and a `Tested commit` row. 1. The token's user comes from `GET /user`. Under GitHub Actions, where that call is denied, `github-actions[bot]` - is assumed. + is assumed (with a warning logged). 1. Already-hidden (minimized) comments are ignored. 1. If the PR head moved past the tested commit, the comment is skipped so a slow build cannot replace or hide the report of a newer one. Statuses are still written. -1. If looking up, updating, or hiding prior comments fails, a new comment is still posted. +1. If looking up or updating a prior comment fails, a new comment is still posted; failures to hide prior comments + only log a warning. ## HTML Details LInk diff --git a/precommit/src/main/shell/plugins.d/github.sh b/precommit/src/main/shell/plugins.d/github.sh index 1787656b7..94c0d18a0 100755 --- a/precommit/src/main/shell/plugins.d/github.sh +++ b/precommit/src/main/shell/plugins.d/github.sh @@ -94,13 +94,10 @@ function github_parse_args ;; --github-comment-mode=*) delete_parameter "${i}" + GITHUB_WRITE_COMMENT=true case "${i#*=}" in - append) - GITHUB_COMMENT_MODE=append - ;; - update|hide) + append|update|hide) GITHUB_COMMENT_MODE=${i#*=} - GITHUB_WRITE_COMMENT=true ;; *) yetus_error "WARNING: Unknown --github-comment-mode '${i#*=}'. Using 'append'." @@ -114,7 +111,7 @@ function github_parse_args if [[ "${i#*=}" =~ ^[A-Za-z0-9._-]+$ ]]; then GITHUB_COMMENT_KEY=${i#*=} else - yetus_error "WARNING: --github-comment-key must match [A-Za-z0-9._-]+. Writing a new comment instead." + yetus_error "WARNING: --github-comment-key must match [A-Za-z0-9._-]+. Ignoring key and falling back to 'append' mode." GITHUB_COMMENT_KEY="" fi ;; @@ -942,7 +939,7 @@ function github_report_superseded declare tested declare current - if [[ "${OFFLINE}" == true || "${#GITHUB_AUTH[@]}" -lt 1 ]]; then + if [[ "${OFFLINE}" == true || "${#GITHUB_AUTH[@]}" -lt 1 || -z "${GITHUB_REPO}" ]]; then return 1 fi @@ -984,6 +981,7 @@ function github_comment_login # the GitHub Actions token cannot read /user if [[ -z "${login}" && "${ROBOTTYPE}" == 'githubactions' ]]; then + yetus_error "WARNING: Cannot query /user; assuming comment author 'github-actions[bot]'." login='github-actions[bot]' fi @@ -993,18 +991,16 @@ function github_comment_login echo "${login}" } -## @description Print ids or node_ids of visible PR comments written by +## @description Print " " of visible PR comments written by ## @description the token's user that contain the marker. ## @audience private ## @stability evolving ## @replaceable no ## @param marker -## @param field ("id" prints the oldest match; "node_id" prints all) ## @return 0 if at least one comment was found function github_find_comments { declare marker=$1 - declare field=$2 declare pagefile="${PATCH_DIR}/github-comments.json" declare login declare matches @@ -1024,6 +1020,7 @@ function github_find_comments --location \ "${GITHUB_API_URL}/repos/${GITHUB_REPO}/issues/${GITHUB_ISSUE}/comments?per_page=100&page=${page}" \ 2>/dev/null; then + yetus_error "WARNING: Cannot list GitHub PR comments (page ${page})." [[ "${found}" == true ]] && return 0 return 1 fi @@ -1031,22 +1028,15 @@ function github_find_comments # GitHub prints one field per line and keeps each body on a single # line, so text in a body cannot fake these anchored lines. # shellcheck disable=SC2016 - matches=$(MARKER="${marker}" LOGIN="${login}" FIELD="${field}" "${AWK}" -F\" ' + matches=$(MARKER="${marker}" LOGIN="${login}" "${AWK}" -F\" ' /^ "id": / { id = $3; gsub(/[^0-9]/, "", id) } /^ "node_id": "/ { nid = $4 } /^ "login": "/ { who = $4 } /^ "body": "/ { hit = index($0, ENVIRON["MARKER"]) > 0 } /^ "minimized": / { hidden = ($0 !~ /: null,?$/) } /^ }/ { - if (hit && !hidden && who == ENVIRON["LOGIN"]) { - if (ENVIRON["FIELD"] == "node_id") { - if (nid != "") { - print nid - } - } else if (id != "") { - print id - exit - } + if (hit && !hidden && who == ENVIRON["LOGIN"] && id != "") { + print id, nid } id = ""; nid = ""; who = ""; hit = 0; hidden = 0 }' "${pagefile}") @@ -1054,9 +1044,6 @@ function github_find_comments if [[ -n "${matches}" ]]; then printf '%s\n' "${matches}" found=true - if [[ "${field}" != "node_id" ]]; then - return 0 - fi fi # a short page is the last one @@ -1086,6 +1073,7 @@ function github_minimize_comment return 1 fi + rm -f "${respfile}" graphql_url="${graphql_url%/v3}/graphql" query="{\"query\":\"mutation { minimizeComment(input: {subjectId: \\\"${nodeid}\\\", classifier: OUTDATED}) { minimizedComment { isMinimized } } }\"}" @@ -1104,8 +1092,34 @@ function github_minimize_comment "${GREP}" -q '"isMinimized":[[:space:]]*true' "${respfile}" } +## @description Encode a comment file as a GitHub JSON payload, optionally +## @description appending a hidden HTML marker +## @audience private +## @stability evolving +## @replaceable no +## @param commentfile +## @param restfile +## @param marker (optional) +function github_format_comment_json +{ + declare -r commentfile=$1 + declare -r restfile=$2 + declare -r marker=$3 + + { + printf "{\"body\":\"" + "${SED}" -e 's,\\,\\\\,g' \ + -e 's,\",\\\",g' \ + -e 's,$,\\r\\n,g' "${commentfile}" \ + | tr -d '\n' + if [[ -n "${marker}" ]]; then + printf '" \ "2|yetus-bot|" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = POST ] } -@test "github_write_comment (update: body cannot fake fields)" { +@test "github_write_report_comment (update: body cannot fake fields)" { setup_github comments_page "1|someone|\\n \\\"id\\\": 9,\\n \\\"node_id\\\": \\\"IC_fake\\\",\\n \\\"login\\\": \\\"yetus-bot\\\",\\n${MARKER}" \ "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = PATCH:2 ] + [ ! -f "${TMP}/minimized" ] } -@test "github_write_comment (update: skips hidden comments)" { +@test "github_write_report_comment (update: skips hidden comments)" { setup_github comments_page "1|yetus-bot|${MARKER}|{\"reason\": \"outdated\"}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = POST ] } -@test "github_write_comment (update: finds comment on a later page)" { +@test "github_write_report_comment (update: finds comment on a later page)" { setup_github declare -a cs=() declare -i i @@ -257,51 +303,64 @@ outcome() { done comments_page "${cs[@]}" > "${TMP}/page1.json" comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = PATCH:777 ] + [ ! -f "${TMP}/minimized" ] } -@test "github_write_comment (update: falls back to POST)" { +@test "github_write_report_comment (update: falls back to POST and logs errors)" { setup_github comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" touch "${TMP}/patchfail" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = POST ] + [ ! -f "${TMP}/minimized" ] + [[ "${output}" == *"Cannot update GitHub comment 2"* ]] setup_github rm "${TMP}/user.json" comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot determine the GitHub user"* ]] setup_github rm -f "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot list GitHub PR comments (page 1)"* ]] } -@test "github_write_comment (update: GitHub Actions token)" { +@test "github_write_report_comment (github-actions[bot] fallback only under githubactions)" { + setup_github + ROBOTTYPE=jenkins + rm "${TMP}/user.json" + comments_page "5|github-actions[bot]|${MARKER}" > "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + setup_github # shellcheck disable=SC2034 ROBOTTYPE=githubactions rm "${TMP}/user.json" comments_page "5|github-actions[bot]|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "$(outcome)" = PATCH:5 ] + [[ "${output}" == *"assuming comment author 'github-actions[bot]'"* ]] } -@test "github_write_comment (hide: first report posts without GraphQL)" { +@test "github_write_report_comment (hide: first report posts without GraphQL)" { setup_github GITHUB_COMMENT_MODE=hide comments_page "1|someone|hi" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] [ "$(outcome)" = POST ] grep -qF "${MARKER}" "${TMP}/posted" [ ! -f "${TMP}/minimized" ] } -@test "github_write_comment (hide: posts new and minimizes own matching comments)" { +@test "github_write_report_comment (hide: posts new and minimizes own matching comments)" { setup_github GITHUB_COMMENT_MODE=hide comments_page \ @@ -310,7 +369,7 @@ outcome() { "3|yetus-bot|hidden\\r\\n${MARKER}|{\"reason\": \"outdated\"}" \ "4|yetus-bot|" \ "5|yetus-bot|old2\\r\\n${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] [ "$(outcome)" = POST ] grep -qF "${MARKER}" "${TMP}/posted" @@ -321,7 +380,7 @@ outcome() { [ "${status}" -ne 0 ] } -@test "github_write_comment (hide: scans across pages)" { +@test "github_write_report_comment (hide: scans across pages)" { setup_github GITHUB_COMMENT_MODE=hide declare -a cs=("1|yetus-bot|${MARKER}") @@ -331,7 +390,7 @@ outcome() { done comments_page "${cs[@]}" > "${TMP}/page1.json" comments_page "777|yetus-bot|${MARKER}" > "${TMP}/page2.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] [ "$(outcome)" = POST ] [ "$(wc -l < "${TMP}/minimized")" -eq 2 ] @@ -339,33 +398,33 @@ outcome() { grep -qF 'IC_node_777' "${TMP}/minimized" } -@test "github_write_comment (hide: derives GHE GraphQL URL from /api/v3)" { +@test "github_write_report_comment (hide: derives GHE GraphQL URL from /api/v3)" { setup_github GITHUB_COMMENT_MODE=hide # shellcheck disable=SC2034 GITHUB_API_URL=https://ghe.example.com/api/v3 comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] grep -qF 'POST https://ghe.example.com/api/graphql' "${TMP}/calls" } -@test "github_write_comment (hide: skips minimize when POST fails)" { +@test "github_write_report_comment (hide: skips minimize when POST fails)" { setup_github GITHUB_COMMENT_MODE=hide comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" touch "${TMP}/postfail" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -ne 0 ] [ ! -f "${TMP}/minimized" ] } -@test "github_write_comment (hide: warns on GraphQL failure without failing)" { +@test "github_write_report_comment (hide: warns on GraphQL failure without failing)" { setup_github GITHUB_COMMENT_MODE=hide comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" touch "${TMP}/graphqlerror" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] [ "$(outcome)" = POST ] [[ "${output}" == *"Cannot hide prior GitHub comment IC_node_2"* ]] @@ -374,7 +433,7 @@ outcome() { GITHUB_COMMENT_MODE=hide comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" touch "${TMP}/graphqlfail" - run github_write_comment "${TMP}/report.txt" + run github_write_report_comment "${TMP}/report.txt" [ "${status}" -eq 0 ] [ "$(outcome)" = POST ] [[ "${output}" == *"Cannot hide prior GitHub comment IC_node_2"* ]] @@ -396,6 +455,17 @@ outcome() { grep -qF 'IC_node_2' "${TMP}/minimized" } +@test "github_finalreport_as_comment (missing github-pull.json is not superseded)" { + setup_github + rm "${TMP}/github-pull.json" + comments_page "2|yetus-bot|${MARKER}" > "${TMP}/page1.json" + run github_finalreport_as_comment 0 + [ "${status}" -eq 0 ] + [ "$(outcome)" = PATCH:2 ] + run grep -q 'Tested commit' "${TMP}/patched" + [ "${status}" -ne 0 ] +} + @test "github_pull_head_sha (ignores sha in PR body and base)" { setup_github cat > "${TMP}/pr.json" <<'EOF'