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..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 @@ -35,14 +35,43 @@ 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 | +| 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=` implies +`--github-write-comment` and controls how prior final report comments are handled: + +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 (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 (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 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 cef6bab18..94c0d18a0 100755 --- a/precommit/src/main/shell/plugins.d/github.sh +++ b/precommit/src/main/shell/plugins.d/github.sh @@ -39,6 +39,8 @@ GITHUB_USE_EMOJI_VOTE=false GITHUB_STATUS_RECOVERY_COUNTER=1 GITHUB_STATUS_RECOVER_TOOL=false GITHUB_WRITE_COMMENT=false +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 @@ -54,6 +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-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}]" } @@ -88,6 +92,29 @@ function github_parse_args delete_parameter "${i}" GITHUB_WRITE_COMMENT=true ;; + --github-comment-mode=*) + delete_parameter "${i}" + GITHUB_WRITE_COMMENT=true + case "${i#*=}" in + append|update|hide) + GITHUB_COMMENT_MODE=${i#*=} + ;; + *) + yetus_error "WARNING: Unknown --github-comment-mode '${i#*=}'. Using 'append'." + GITHUB_COMMENT_MODE=append + ;; + esac + ;; + --github-comment-key=*) + delete_parameter "${i}" + # the key goes into the JSON body unescaped + if [[ "${i#*=}" =~ ^[A-Za-z0-9._-]+$ ]]; then + GITHUB_COMMENT_KEY=${i#*=} + else + yetus_error "WARNING: --github-comment-key must match [A-Za-z0-9._-]+. Ignoring key and falling back to 'append' mode." + GITHUB_COMMENT_KEY="" + fi + ;; --github-use-emoji-vote) delete_parameter "${i}" GITHUB_USE_EMOJI_VOTE=true @@ -874,6 +901,224 @@ 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 +## @replaceable no +## @param filename +function github_pull_head_sha +{ + # 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 or hide +## @description the report 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 + + if [[ "${OFFLINE}" == true || "${#GITHUB_AUTH[@]}" -lt 1 || -z "${GITHUB_REPO}" ]]; then + return 1 + fi + + 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 + yetus_error "WARNING: Cannot query /user; assuming comment author 'github-actions[bot]'." + login='github-actions[bot]' + fi + + if [[ -z "${login}" ]]; then + return 1 + fi + echo "${login}" +} + +## @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 +## @return 0 if at least one comment was found +function github_find_comments +{ + declare marker=$1 + declare pagefile="${PATCH_DIR}/github-comments.json" + declare login + declare matches + declare found=false + 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 + yetus_error "WARNING: Cannot list GitHub PR comments (page ${page})." + [[ "${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 + 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"] && id != "") { + print id, nid + } + id = ""; nid = ""; who = ""; hit = 0; hidden = 0 + }' "${pagefile}") + + if [[ -n "${matches}" ]]; then + printf '%s\n' "${matches}" + found=true + fi + + # a short page is the last one + if [[ $("${GREP}" -c '^ }' "${pagefile}") -lt 100 ]]; then + break + fi + ((page=page+1)) + done + + [[ "${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 + + rm -f "${respfile}" + 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 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 '' + +# 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 +# reject malformed URLs containing control chars or whitespace, like real curl +if [[ "${url}" == *$'\n'* || "${url}" == *$'\r'* || "${url}" == *" "* ]]; then + exit 3 +fi +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() { + declare patch_count + declare patch_line + if [[ -f "${TMP}/patched" ]]; then + patch_count=$(grep -c '^PATCH ' "${TMP}/calls") + patch_line=$(grep '^PATCH ' "${TMP}/calls") + if [[ "${patch_count}" -eq 1 && "${patch_line}" =~ ^PATCH\ ${GITHUB_API_URL}/repos/${GITHUB_REPO}/issues/comments/([0-9]+)$ ]]; then + echo "PATCH:${BASH_REMATCH[1]}" + else + echo "INVALID_PATCH:${patch_count}:${patch_line}" + fi + 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}" = true ] + [ "${GITHUB_COMMENT_MODE}" = append ] + + GITHUB_WRITE_COMMENT=false + github_parse_args --github-comment-mode=Hide 2>/dev/null + [ "${GITHUB_WRITE_COMMENT}" = true ] + [ "${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 (public hook stays append-only in all modes)" { + setup_github + comments_page "2|yetus-bot|old\\r\\n${MARKER}" > "${TMP}/page1.json" + + GITHUB_COMMENT_MODE=update + 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 ] + [ ! -f "${TMP}/minimized" ] + + setup_github + comments_page "2|yetus-bot|old\\r\\n${MARKER}" > "${TMP}/page1.json" + GITHUB_COMMENT_MODE=hide + 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 ] + [ ! -f "${TMP}/minimized" ] +} + +@test "github_write_report_comment (append mode posts without lookups)" { + setup_github + GITHUB_COMMENT_MODE=append + run github_write_report_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_report_comment (update: first report posts with marker)" { + setup_github + comments_page "1|someone|hi" > "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + grep -qF 'report \"body\"' "${TMP}/posted" +} + +@test "github_write_report_comment (update: edits oldest and hides duplicates)" { + setup_github + comments_page "1|someone|hi" "2|yetus-bot|old\\r\\n${MARKER}" \ + "3|yetus-bot|dup1\\r\\n${MARKER}" \ + "4|yetus-bot|dup2\\r\\n${MARKER}" > "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = PATCH:2 ] + grep -qF "${MARKER}" "${TMP}/patched" + [ "$(wc -l < "${TMP}/minimized")" -eq 2 ] + grep -qF 'IC_node_3' "${TMP}/minimized" + grep -qF 'IC_node_4' "${TMP}/minimized" + run grep -qF 'IC_node_2' "${TMP}/minimized" + [ "${status}" -ne 0 ] +} + +@test "github_write_report_comment (update: ignores marker from other users)" { + setup_github + comments_page "1|someone|${MARKER}" > "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_report_comment (update: ignores other keys)" { + setup_github + comments_page "1|yetus-bot|" \ + "2|yetus-bot|" > "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@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_report_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:2 ] + [ ! -f "${TMP}/minimized" ] +} + +@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_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] +} + +@test "github_write_report_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_report_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:777 ] + [ ! -f "${TMP}/minimized" ] +} + +@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_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_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot determine the GitHub user"* ]] + + setup_github + rm -f "${TMP}/page1.json" + run github_write_report_comment "${TMP}/report.txt" + [ "$(outcome)" = POST ] + [[ "${output}" == *"Cannot list GitHub PR comments (page 1)"* ]] +} + +@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_report_comment "${TMP}/report.txt" + [ "$(outcome)" = PATCH:5 ] + [[ "${output}" == *"assuming comment author 'github-actions[bot]'"* ]] +} + +@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_report_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + [ "$(outcome)" = POST ] + grep -qF "${MARKER}" "${TMP}/posted" + [ ! -f "${TMP}/minimized" ] +} + +@test "github_write_report_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_report_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_report_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_report_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_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_report_comment "${TMP}/report.txt" + [ "${status}" -eq 0 ] + grep -qF 'POST https://ghe.example.com/api/graphql' "${TMP}/calls" +} + +@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_report_comment "${TMP}/report.txt" + [ "${status}" -ne 0 ] + [ ! -f "${TMP}/minimized" ] +} + +@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_report_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_report_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_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' +{ + "number": 42, + "body": "mentions \"sha\": \"fake-in-body\" before head", + "head": { + "ref": "feature", + "repo": { + "name": "yetus" + }, + "sha": "abc1111" + }, + "base": { + "sha": "base0000" + } +} +EOF + run github_pull_head_sha "${TMP}/pr.json" + [ "${status}" -eq 0 ] + [ "${output}" = "abc1111" ] +} + +@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 ] +}