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 15GERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc-project.xyz" 16GERRIT_SSH_CMD=( \ 17 ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \ 18 -p 29418 jenkins-openbmc-ci@gerrit.openbmc-project.xyz gerrit \ 19) 20 21echo "Checking ${GERRIT_PROJECT}:${GERRIT_BRANCH}:${GERRIT_CHANGE_ID}:${GERRIT_PATCHSET_REVISION}" 22 23# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit. 24COMMITTER_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'])") 25if [ "x${COMMITTER_EMAIL}" == "x" ]; then 26 echo "Unable to find committer." 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 31fi 32 33#echo "Commit by '${COMMITTER_EMAIL}'" 34# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit. 35COMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python2 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['username'])") 36#COMMITTER_USERNAME=`${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL}` 37echo "USERNAME: $COMMITTER_USERNAME" 38if [ "x${COMMITTER_USERNAME}" == "x" ]; then 39 echo "Unable to determine github user for ${COMMITTER_EMAIL}." 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 44fi 45 46# Reset the vote to 0 so jenkins will detect a new +1 on retriggers 47"${GERRIT_SSH_CMD[@]}" review \ 48 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ 49 --label=Ok-To-Test=0 -t autogenerated:jenkins 50 51# Write full list of users to a file 52GERRIT_CI_GROUPS=( \ 53 alibaba/ci-authorized \ 54 amd/ci-authorized \ 55 ami/ci-authorized \ 56 ampere/ci-authorized \ 57 aspeed/ci-authorized \ 58 bytedance/ci-authorized \ 59 code-construct/ci-authorized \ 60 facebook/ci-authorized \ 61 fii/ci-authorized \ 62 gager-in/ci-authorized \ 63 google/ci-authorized \ 64 hcl/ci-authorized \ 65 hpe/ci-authorized \ 66 ibm/ci-authorized \ 67 individual/ci-authorized \ 68 inspur/ci-authorized \ 69 intel/ci-authorized \ 70 inventec/ci-authorized \ 71 lenovo/ci-authorized \ 72 nineelements/ci-authorized \ 73 nuvoton/ci-authorized \ 74 nvidia/ci-authorized \ 75 openbmc/ci-authorized \ 76 pcpartner/ci-authorized \ 77 quanta/ci-authorized \ 78 rcs/ci-authorized \ 79 supermicro/ci-authorized \ 80 wistron/ci-authorized \ 81 wiwynn/ci-authorized \ 82 yadro/ci-authorized \ 83) 84 85rm -f "$WORKSPACE/users.txt" 86for g in "${GERRIT_CI_GROUPS[@]}"; do 87 "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \ 88 >> "$WORKSPACE/users.txt" 89done 90 91# grep for the specific username word in the file 92if grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then 93 "${GERRIT_SSH_CMD[@]}" review \ 94 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ 95 --label=Ok-To-Test=1 -t autogenerated:jenkins \ 96 "--message='User approved, CI ok to start'" 97 exit 0 98fi 99 100echo "${COMMITTER_USERNAME} is not on the approved list." 101"${GERRIT_SSH_CMD[@]}" review \ 102 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \ 103 --label=Ok-To-Test=0 -t autogenerated:jenkins \ 104 "--message='User not approved, see admin, no CI'" 105 106exit 0 107