blob: 6f7dafc27385a5431a3ffed58c36dc5541430aff [file] [log] [blame]
Andrew Geissler3c88e2d2021-01-28 15:18:11 -06001#!/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"
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050016GERRIT_SSH_CMD=( \
17 ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \
18 -p 29418 jenkins-openbmc-ci@gerrit.openbmc-project.xyz gerrit \
19)
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060020
21echo "Checking ${GERRIT_PROJECT}:${GERRIT_BRANCH}:${GERRIT_CHANGE_ID}:${GERRIT_PATCHSET_REVISION}"
22
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050023# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
Andrew Geissler6b69ff02021-03-17 15:04:21 -050024COMMITTER_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'])")
Ed Tanous5d3031d2022-02-16 15:57:15 -080025if [ "${COMMITTER_EMAIL}" = "" ]; then
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060026 echo "Unable to find committer."
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050027 "${GERRIT_SSH_CMD[@]}" review \
28 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williamsf8ee6f02022-01-04 12:03:19 -060029 --ok-to-test=0 "--message='Unable to determine committer'"
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050030 exit 1
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060031fi
32
33#echo "Commit by '${COMMITTER_EMAIL}'"
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050034# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
Andrew Geissler6b69ff02021-03-17 15:04:21 -050035COMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python2 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['username'])")
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060036#COMMITTER_USERNAME=`${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL}`
37echo "USERNAME: $COMMITTER_USERNAME"
Ed Tanous5d3031d2022-02-16 15:57:15 -080038if [ "${COMMITTER_USERNAME}" = "" ]; then
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060039 echo "Unable to determine github user for ${COMMITTER_EMAIL}."
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050040 "${GERRIT_SSH_CMD[@]}" review \
41 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williamsf8ee6f02022-01-04 12:03:19 -060042 --ok-to-test=0 "--message='Unable to determine github user'"
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050043 exit 1
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060044fi
45
46# Reset the vote to 0 so jenkins will detect a new +1 on retriggers
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050047"${GERRIT_SSH_CMD[@]}" review \
48 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williams8ac8e092022-01-04 12:05:50 -060049 --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060050
51# Write full list of users to a file
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050052GERRIT_CI_GROUPS=( \
53 alibaba/ci-authorized \
54 amd/ci-authorized \
Andrew Geisslere63bb542021-06-04 10:15:26 -050055 ami/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050056 ampere/ci-authorized \
Gilbert Chen76f25de2022-02-10 21:05:06 +000057 arm/ci-authorized \
Andrew Geissler2fde84b2021-04-01 10:17:36 -050058 aspeed/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050059 bytedance/ci-authorized \
Andrew Geissler0cc575a2021-09-07 07:53:58 -050060 code-construct/ci-authorized \
Patrick Williams123ffbe2022-03-01 09:18:41 -060061 depo/ci-authorized \
Andrew Geisslera9a79192021-12-09 14:04:18 -060062 erg/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050063 facebook/ci-authorized \
Patrick Williams870b41a2021-03-27 09:05:56 -050064 fii/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050065 gager-in/ci-authorized \
66 google/ci-authorized \
67 hcl/ci-authorized \
68 hpe/ci-authorized \
69 ibm/ci-authorized \
70 individual/ci-authorized \
71 inspur/ci-authorized \
72 intel/ci-authorized \
73 inventec/ci-authorized \
Andrew Geisslere13a7112021-03-31 08:14:25 -050074 lenovo/ci-authorized \
Andrew Geisslera56c7012021-08-20 10:03:30 -050075 nineelements/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050076 nuvoton/ci-authorized \
77 nvidia/ci-authorized \
78 openbmc/ci-authorized \
Andrew Geissler46182102021-06-08 08:13:13 -050079 pcpartner/ci-authorized \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050080 quanta/ci-authorized \
81 rcs/ci-authorized \
82 supermicro/ci-authorized \
83 wistron/ci-authorized \
84 wiwynn/ci-authorized \
85 yadro/ci-authorized \
86)
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060087
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050088rm -f "$WORKSPACE/users.txt"
89for g in "${GERRIT_CI_GROUPS[@]}"; do
90 "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
91 >> "$WORKSPACE/users.txt"
92done
Andrew Geissler3c88e2d2021-01-28 15:18:11 -060093
94# grep for the specific username word in the file
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050095if grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
96 "${GERRIT_SSH_CMD[@]}" review \
97 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williams8ac8e092022-01-04 12:05:50 -060098 --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050099 "--message='User approved, CI ok to start'"
Patrick Williams2e93e4e2021-12-30 10:29:38 -0600100
101 # Immediately erase the score to prevent infinite triggers.
102 "${GERRIT_SSH_CMD[@]}" review \
103 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williams8ac8e092022-01-04 12:05:50 -0600104 --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
Patrick Williams2e93e4e2021-12-30 10:29:38 -0600105
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600106 exit 0
107fi
108
109echo "${COMMITTER_USERNAME} is not on the approved list."
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500110"${GERRIT_SSH_CMD[@]}" review \
111 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williamsf8ee6f02022-01-04 12:03:19 -0600112 --ok-to-test=0 -t autogenerated:jenkins \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500113 "--message='User not approved, see admin, no CI'"
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600114
115exit 0