| #!/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='<!-- yetus-report:ubuntu -->' | |
| # 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 <<EOF | |
| { | |
| "id": $1, | |
| "node_id": "IC_node_$1", | |
| "user": { | |
| "login": "$2", | |
| "id": 7, | |
| "node_id": "MDQ_user_$1" | |
| }, | |
| "body": "$3", | |
| "performed_via_github_app": null, | |
| "minimized": ${4:-null} | |
| } | |
| EOF | |
| } | |
| # build a page from "id|login|body[|minimized]" args | |
| comments_page() { | |
| declare sep="" | |
| declare id login body minimized | |
| echo "[" | |
| for c in "$@"; do | |
| IFS='|' read -r id login body minimized <<< "${c}" | |
| printf '%s' "${sep}" | |
| comment_json "${id}" "${login}" "${body}" "${minimized}" | |
| sep="," | |
| done | |
| echo "]" | |
| } | |
| # what happened: PATCH:<id>, 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|<!-- yetus-report:debian -->" \ | |
| "2|yetus-bot|<!-- yetus-report:ubuntu-24 -->" > "${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|<!-- yetus-report:debian -->" \ | |
| "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 ] | |
| } |