| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 1 | #!/bin/bash -e | 
 | 2 | # | 
 | 3 | # Purpose: | 
 | 4 | #  This script is responsible for determining the owner of a gerrit | 
 | 5 | #  commit, verifying they are within an approved gerrit group, and | 
 | 6 | #  then updating gerrit with that verification info. | 
 | 7 | # | 
 | 8 | # Note: It is assumed this script is run as a part of a jenkins job triggered | 
 | 9 | #       by the gerrit plugin. Therefore it assumes certain env variables | 
 | 10 | #       provided by that plugin are avialable (i.e. GERRIT_PROJECT, ...) | 
 | 11 | # | 
 | 12 | # Required Inputs: | 
 | 13 | #  SSH_KEY:  Path to private ssh key used to post messages to gerrit | 
 | 14 |  | 
| Andrew Geissler | 3705241 | 2022-05-20 15:28:26 -0400 | [diff] [blame] | 15 | GERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc.org" | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 16 | GERRIT_SSH_CMD=( \ | 
 | 17 |     ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \ | 
| Andrew Geissler | 3705241 | 2022-05-20 15:28:26 -0400 | [diff] [blame] | 18 |     -p 29418 jenkins-openbmc-ci@gerrit.openbmc.org gerrit \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 19 | ) | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 20 |  | 
 | 21 | echo "Checking ${GERRIT_PROJECT}:${GERRIT_BRANCH}:${GERRIT_CHANGE_ID}:${GERRIT_PATCHSET_REVISION}" | 
 | 22 |  | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 23 | # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit. | 
| Andrew Geissler | c491709 | 2022-05-20 16:07:58 -0400 | [diff] [blame] | 24 | COMMITTER_EMAIL=$(${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/revisions/${GERRIT_PATCHSET_REVISION}/commit | python3 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['committer']['email'])") | 
| Ed Tanous | 5d3031d | 2022-02-16 15:57:15 -0800 | [diff] [blame] | 25 | if [ "${COMMITTER_EMAIL}" = "" ]; then | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 26 |     echo "Unable to find committer." | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 27 |     "${GERRIT_SSH_CMD[@]}" review \ | 
 | 28 |         "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | f8ee6f0 | 2022-01-04 12:03:19 -0600 | [diff] [blame] | 29 |         --ok-to-test=0 "--message='Unable to determine committer'" | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 30 |     exit 1 | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 31 | fi | 
 | 32 |  | 
 | 33 | #echo "Commit by '${COMMITTER_EMAIL}'" | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 34 | # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit. | 
| Andrew Geissler | c491709 | 2022-05-20 16:07:58 -0400 | [diff] [blame] | 35 | COMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python3 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['username'])") | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 36 | #COMMITTER_USERNAME=`${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL}` | 
 | 37 | echo "USERNAME: $COMMITTER_USERNAME" | 
| Ed Tanous | 5d3031d | 2022-02-16 15:57:15 -0800 | [diff] [blame] | 38 | if [ "${COMMITTER_USERNAME}" = "" ]; then | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 39 |     echo "Unable to determine github user for ${COMMITTER_EMAIL}." | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 40 |     "${GERRIT_SSH_CMD[@]}" review \ | 
 | 41 |         "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | f8ee6f0 | 2022-01-04 12:03:19 -0600 | [diff] [blame] | 42 |         --ok-to-test=0 "--message='Unable to determine github user'" | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 43 |     exit 1 | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 44 | fi | 
 | 45 |  | 
 | 46 | # Reset the vote to 0 so jenkins will detect a new +1 on retriggers | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 47 | "${GERRIT_SSH_CMD[@]}" review \ | 
 | 48 |     "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | 8ac8e09 | 2022-01-04 12:05:50 -0600 | [diff] [blame] | 49 |     --ok-to-test=0 -t autogenerated:jenkins --notify=NONE | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 50 |  | 
| Patrick Williams | 47b59dc | 2022-07-18 16:58:42 -0500 | [diff] [blame] | 51 | # Add reviewers based on OWNERS files. | 
 | 52 | "${WORKSPACE}/openbmc-build-scripts/tools/owners" -p "${WORKSPACE}" reviewers | | 
 | 53 | { | 
 | 54 |     while read -r reviewer ; | 
 | 55 |     do | 
 | 56 |         # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit. | 
 | 57 |         ${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/reviewers \ | 
 | 58 |             -X POST \ | 
 | 59 |             -H "Content-Type: application/json" \ | 
 | 60 |             -d "$reviewer" || true | 
 | 61 |     done | 
 | 62 | } || true | 
 | 63 |  | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 64 | # Write full list of users to a file | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 65 | GERRIT_CI_GROUPS=( \ | 
 | 66 |     alibaba/ci-authorized \ | 
 | 67 |     amd/ci-authorized \ | 
| Andrew Geissler | e63bb54 | 2021-06-04 10:15:26 -0500 | [diff] [blame] | 68 |     ami/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 69 |     ampere/ci-authorized \ | 
| Gilbert Chen | 76f25de | 2022-02-10 21:05:06 +0000 | [diff] [blame] | 70 |     arm/ci-authorized \ | 
| Andrew Geissler | 2fde84b | 2021-04-01 10:17:36 -0500 | [diff] [blame] | 71 |     aspeed/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 72 |     bytedance/ci-authorized \ | 
| Andrew Geissler | 0cc575a | 2021-09-07 07:53:58 -0500 | [diff] [blame] | 73 |     code-construct/ci-authorized \ | 
| Patrick Williams | 123ffbe | 2022-03-01 09:18:41 -0600 | [diff] [blame] | 74 |     depo/ci-authorized \ | 
| Andrew Geissler | a9a7919 | 2021-12-09 14:04:18 -0600 | [diff] [blame] | 75 |     erg/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 76 |     facebook/ci-authorized \ | 
| Patrick Williams | 870b41a | 2021-03-27 09:05:56 -0500 | [diff] [blame] | 77 |     fii/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 78 |     gager-in/ci-authorized \ | 
 | 79 |     google/ci-authorized \ | 
 | 80 |     hcl/ci-authorized \ | 
 | 81 |     hpe/ci-authorized \ | 
 | 82 |     ibm/ci-authorized \ | 
 | 83 |     individual/ci-authorized \ | 
 | 84 |     inspur/ci-authorized \ | 
 | 85 |     intel/ci-authorized \ | 
 | 86 |     inventec/ci-authorized \ | 
| Andrew Geissler | e13a711 | 2021-03-31 08:14:25 -0500 | [diff] [blame] | 87 |     lenovo/ci-authorized \ | 
| Andrew Geissler | ec22c46 | 2022-08-15 09:41:34 -0400 | [diff] [blame] | 88 |     microsoft/ci-authorized \ | 
| Andrew Geissler | a56c701 | 2021-08-20 10:03:30 -0500 | [diff] [blame] | 89 |     nineelements/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 90 |     nuvoton/ci-authorized \ | 
 | 91 |     nvidia/ci-authorized \ | 
 | 92 |     openbmc/ci-authorized \ | 
| Andrew Geissler | 4618210 | 2021-06-08 08:13:13 -0500 | [diff] [blame] | 93 |     pcpartner/ci-authorized \ | 
| Patrick Williams | bc73b75 | 2022-07-08 06:59:05 -0500 | [diff] [blame] | 94 |     phytium/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 95 |     quanta/ci-authorized \ | 
| Jae Hyun Yoo | 7031b33 | 2022-04-13 15:21:31 -0700 | [diff] [blame] | 96 |     quic/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 97 |     rcs/ci-authorized \ | 
 | 98 |     supermicro/ci-authorized \ | 
 | 99 |     wistron/ci-authorized \ | 
 | 100 |     wiwynn/ci-authorized \ | 
 | 101 |     yadro/ci-authorized \ | 
 | 102 | ) | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 103 |  | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 104 | rm -f "$WORKSPACE/users.txt" | 
 | 105 | for g in "${GERRIT_CI_GROUPS[@]}"; do | 
 | 106 |     "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \ | 
 | 107 |         >> "$WORKSPACE/users.txt" | 
 | 108 | done | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 109 |  | 
 | 110 | # grep for the specific username word in the file | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 111 | if grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then | 
 | 112 |     "${GERRIT_SSH_CMD[@]}" review \ | 
 | 113 |         "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | 8ac8e09 | 2022-01-04 12:05:50 -0600 | [diff] [blame] | 114 |         --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 115 |         "--message='User approved, CI ok to start'" | 
| Patrick Williams | 2e93e4e | 2021-12-30 10:29:38 -0600 | [diff] [blame] | 116 |  | 
 | 117 |     # Immediately erase the score to prevent infinite triggers. | 
 | 118 |     "${GERRIT_SSH_CMD[@]}" review \ | 
 | 119 |         "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | 8ac8e09 | 2022-01-04 12:05:50 -0600 | [diff] [blame] | 120 |         --ok-to-test=0 -t autogenerated:jenkins --notify=NONE | 
| Patrick Williams | 2e93e4e | 2021-12-30 10:29:38 -0600 | [diff] [blame] | 121 |  | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 122 |     exit 0 | 
 | 123 | fi | 
 | 124 |  | 
 | 125 | echo "${COMMITTER_USERNAME} is not on the approved list." | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 126 | "${GERRIT_SSH_CMD[@]}" review \ | 
 | 127 |     "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
| Patrick Williams | f8ee6f0 | 2022-01-04 12:03:19 -0600 | [diff] [blame] | 128 |     --ok-to-test=0 -t autogenerated:jenkins \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 129 |     "--message='User not approved, see admin, no CI'" | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 130 |  | 
 | 131 | exit 0 |