| 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 |  | 
|  | 15 | GERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc-project.xyz" | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 16 | GERRIT_SSH_CMD=( \ | 
|  | 17 | ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \ | 
|  | 18 | -p 29418 jenkins-openbmc-ci@gerrit.openbmc-project.xyz gerrit \ | 
|  | 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 | 6b69ff0 | 2021-03-17 15:04:21 -0500 | [diff] [blame] | 24 | COMMITTER_EMAIL=$(${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/revisions/${GERRIT_PATCHSET_REVISION}/commit | python2 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['committer']['email'])") | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 25 | if [ "x${COMMITTER_EMAIL}" == "x" ]; then | 
|  | 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}" \ | 
|  | 29 | --label=Ok-To-Test=0 "--message='Unable to determine committer'" | 
|  | 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 | 6b69ff0 | 2021-03-17 15:04:21 -0500 | [diff] [blame] | 35 | COMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python2 -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" | 
|  | 38 | if [ "x${COMMITTER_USERNAME}" == "x" ]; then | 
|  | 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}" \ | 
|  | 42 | --label=Ok-To-Test=0 "--message='Unable to determine github user'" | 
|  | 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}" \ | 
|  | 49 | --label=Ok-To-Test=0 -t autogenerated:jenkins | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 50 |  | 
|  | 51 | # Write full list of users to a file | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 52 | GERRIT_CI_GROUPS=( \ | 
|  | 53 | alibaba/ci-authorized \ | 
|  | 54 | amd/ci-authorized \ | 
|  | 55 | ampere/ci-authorized \ | 
| Andrew Geissler | 2fde84b | 2021-04-01 10:17:36 -0500 | [diff] [blame^] | 56 | aspeed/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 57 | bytedance/ci-authorized \ | 
|  | 58 | facebook/ci-authorized \ | 
| Patrick Williams | 870b41a | 2021-03-27 09:05:56 -0500 | [diff] [blame] | 59 | fii/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 60 | gager-in/ci-authorized \ | 
|  | 61 | google/ci-authorized \ | 
|  | 62 | hcl/ci-authorized \ | 
|  | 63 | hpe/ci-authorized \ | 
|  | 64 | ibm/ci-authorized \ | 
|  | 65 | individual/ci-authorized \ | 
|  | 66 | inspur/ci-authorized \ | 
|  | 67 | intel/ci-authorized \ | 
|  | 68 | inventec/ci-authorized \ | 
| Andrew Geissler | e13a711 | 2021-03-31 08:14:25 -0500 | [diff] [blame] | 69 | lenovo/ci-authorized \ | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 70 | nuvoton/ci-authorized \ | 
|  | 71 | nvidia/ci-authorized \ | 
|  | 72 | openbmc/ci-authorized \ | 
|  | 73 | quanta/ci-authorized \ | 
|  | 74 | rcs/ci-authorized \ | 
|  | 75 | supermicro/ci-authorized \ | 
|  | 76 | wistron/ci-authorized \ | 
|  | 77 | wiwynn/ci-authorized \ | 
|  | 78 | yadro/ci-authorized \ | 
|  | 79 | ) | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 80 |  | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 81 | rm -f "$WORKSPACE/users.txt" | 
|  | 82 | for g in "${GERRIT_CI_GROUPS[@]}"; do | 
|  | 83 | "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \ | 
|  | 84 | >> "$WORKSPACE/users.txt" | 
|  | 85 | done | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 86 |  | 
|  | 87 | # grep for the specific username word in the file | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 88 | if grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then | 
|  | 89 | "${GERRIT_SSH_CMD[@]}" review \ | 
|  | 90 | "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
|  | 91 | --label=Ok-To-Test=1 -t autogenerated:jenkins \ | 
|  | 92 | "--message='User approved, CI ok to start'" | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 93 | exit 0 | 
|  | 94 | fi | 
|  | 95 |  | 
|  | 96 | echo "${COMMITTER_USERNAME} is not on the approved list." | 
| Patrick Williams | a4d19dc | 2021-03-15 14:51:06 -0500 | [diff] [blame] | 97 | "${GERRIT_SSH_CMD[@]}" review \ | 
|  | 98 | "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ | 
|  | 99 | --label=Ok-To-Test=0 -t autogenerated:jenkins \ | 
|  | 100 | "--message='User not approved, see admin, no CI'" | 
| Andrew Geissler | 3c88e2d | 2021-01-28 15:18:11 -0600 | [diff] [blame] | 101 |  | 
|  | 102 | exit 0 |