| 1 | #!/usr/bin/env sh |
| 2 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | # |
| 4 | # DESCR: Check that CB:<change-id> does not refer to a merged change |
| 5 | |
| 6 | |
| 7 | LINTDIR="$( |
| 8 | cd -- "$(dirname "$0")" > /dev/null 2>&1 || return |
| 9 | pwd -P |
| 10 | )" |
| 11 | |
| 12 | # shellcheck source=helper_functions.sh |
| 13 | . "${LINTDIR}/helper_functions.sh" |
| 14 | |
| 15 | if [ "${IN_GIT_TREE}" -eq 0 ]; then |
| 16 | exit 0 |
| 17 | fi |
| 18 | |
| 19 | if ! command -v jq > /dev/null; then |
| 20 | printf 'This script requires "jq" to be installed\n' >&2 |
| 21 | exit 1 |
| 22 | fi |
| 23 | |
| 24 | GERRIT_REST_API_URL="https://review.coreboot.org" |
| 25 | # Excerpt from documentation[1]: |
| 26 | # |
| 27 | # > To prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON |
| 28 | # > response body starts with a magic prefix line that must be stripped before |
| 29 | # > feeding the rest of the response body to a JSON parser. |
| 30 | # |
| 31 | # [1] https://gerrit-review.googlesource.com/Documentation/rest-api.html#output |
| 32 | GERRIT_REST_API_MAGIC_PREFIX=")]}'" |
| 33 | |
| 34 | api_request() { |
| 35 | api="$1" |
| 36 | curl --silent "${GERRIT_REST_API_URL}/${api}" \ |
| 37 | | sed "s/^${GERRIT_REST_API_MAGIC_PREFIX}//" |
| 38 | } |
| 39 | |
| 40 | # Fetch git log in the format specified by the Linux kernel guidelines[1], which |
| 41 | # coreboot follows, for referencing merged commits. |
| 42 | # |
| 43 | # [1] https://www.kernel.org/doc/html/v4.10/process/submitting-patches.html |
| 44 | search_commit() { |
| 45 | ${GIT} log -n 1 --abbrev=12 --pretty='commit %h ("%s")' "$@" 2> /dev/null |
| 46 | } |
| 47 | |
| 48 | get_commit_reference() { |
| 49 | change_id="$1" |
| 50 | response="$(api_request "changes/coreboot~${change_id}/revisions/current/commit")" |
| 51 | |
| 52 | hash="$(echo "${response}" | jq --raw-output .commit)" |
| 53 | reference="$(search_commit "${hash}")" |
| 54 | |
| 55 | # Earlier changes merged through Gerrit (around < CB:1000) did not |
| 56 | # update the patchset once being cherry-picked into the codebase. |
| 57 | # Therefore, the pre-merged commit remains in Gerrit's database (what we |
| 58 | # query with the REST API) and causes problems, as the commit hash would |
| 59 | # be different from the one found in the main branch. |
| 60 | # |
| 61 | # To workaround this issue, we attempt to find the corresponding commit |
| 62 | # by grepping for a commit message that contains a specific "Change-Id". |
| 63 | if [ -z "${reference}" ]; then |
| 64 | hash="$(echo "${response}" \ |
| 65 | | jq .message \ |
| 66 | | sed -E 's/.*Change-Id: ([[:alnum:]]+).*/\1/')" |
| 67 | reference="$(search_commit --grep "${hash}")" |
| 68 | fi |
| 69 | |
| 70 | if [ -z "${reference}" ]; then |
| 71 | reference="(cannot find commit; probably requires a rebase)" |
| 72 | fi |
| 73 | |
| 74 | echo "${reference}" |
| 75 | } |
| 76 | |
| 77 | # This test is mainly for the jenkins server |
| 78 | for change_id in $(${GIT} log -n 1 | grep -Eo 'CB:[0-9]+'); do |
| 79 | change_id="${change_id#CB:}" # strip "CB:" prefix from change ID |
| 80 | |
| 81 | response="$(api_request "changes/coreboot~${change_id}")" |
| 82 | |
| 83 | if echo "${response}" | grep -qF 'Not found'; then |
| 84 | printf 'CB:%d does not exist\n' "${change_id}" |
| 85 | continue |
| 86 | fi |
| 87 | |
| 88 | status="$(echo "${response}" | jq --raw-output .status)" |
| 89 | |
| 90 | if [ "${status}" = "MERGED" ]; then |
| 91 | reference="$(get_commit_reference "${change_id}")" |
| 92 | |
| 93 | printf 'Using a change ID (CB:%d) for an already merged change; please replace it with:\n' \ |
| 94 | "${change_id}" |
| 95 | printf '%s\n' "${reference}" |
| 96 | fi |
| 97 | done |