blob: ef02d53a64900eb880d6fb595080660d21c7d47c [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
Andrew Geissler37052412022-05-20 15:28:26 -040015GERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc.org"
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050016GERRIT_SSH_CMD=( \
Patrick Williams476a7e92022-12-06 09:52:53 -060017 ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \
18 -p 29418 jenkins-openbmc-ci@gerrit.openbmc.org 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 Geisslerc4917092022-05-20 16:07:58 -040024COMMITTER_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 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 Geisslerc4917092022-05-20 16:07:58 -040035COMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python3 -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
Patrick Williams47b59dc2022-07-18 16:58:42 -050051# 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 Geissler3c88e2d2021-01-28 15:18:11 -060064# Write full list of users to a file
Patrick Williamsa4d19dc2021-03-15 14:51:06 -050065GERRIT_CI_GROUPS=( \
Patrick Williamsb836d242023-10-13 21:10:41 -050066 akamai/ci-authorized \
Patrick Williams476a7e92022-12-06 09:52:53 -060067 alibaba/ci-authorized \
68 amd/ci-authorized \
69 ami/ci-authorized \
70 ampere/ci-authorized \
71 arm/ci-authorized \
72 aspeed/ci-authorized \
Andrew Geissler971d68b2023-11-10 08:54:49 -060073 asus/ci-authorized \
Patrick Williams476a7e92022-12-06 09:52:53 -060074 bytedance/ci-authorized \
75 code-construct/ci-authorized \
76 depo/ci-authorized \
77 erg/ci-authorized \
Patrick Williams93d22a42023-04-21 14:55:26 -050078 equinix/ci-authorized \
Patrick Williams476a7e92022-12-06 09:52:53 -060079 facebook/ci-authorized \
80 fii/ci-authorized \
81 gager-in/ci-authorized \
82 google/ci-authorized \
83 hcl/ci-authorized \
84 hpe/ci-authorized \
85 ibm/ci-authorized \
86 individual/ci-authorized \
87 inspur/ci-authorized \
88 intel/ci-authorized \
89 inventec/ci-authorized \
90 lenovo/ci-authorized \
91 microsoft/ci-authorized \
92 nineelements/ci-authorized \
93 nuvoton/ci-authorized \
94 nvidia/ci-authorized \
95 openbmc/ci-authorized \
96 pcpartner/ci-authorized \
97 phytium/ci-authorized \
98 quanta/ci-authorized \
99 quic/ci-authorized \
100 rcs/ci-authorized \
101 supermicro/ci-authorized \
Jordan Changfd0e80d2023-03-06 09:49:30 +0800102 ufispace/ci-authorized \
Patrick Williamseb4961b2023-05-04 11:28:26 -0500103 vaisala/ci-authorized \
Patrick Williams476a7e92022-12-06 09:52:53 -0600104 wistron/ci-authorized \
105 wiwynn/ci-authorized \
106 yadro/ci-authorized \
107 )
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600108
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500109rm -f "$WORKSPACE/users.txt"
110for g in "${GERRIT_CI_GROUPS[@]}"; do
111 "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
112 >> "$WORKSPACE/users.txt"
113done
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600114
115# grep for the specific username word in the file
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500116if grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
117 "${GERRIT_SSH_CMD[@]}" review \
118 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williams8ac8e092022-01-04 12:05:50 -0600119 --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500120 "--message='User approved, CI ok to start'"
Patrick Williams2e93e4e2021-12-30 10:29:38 -0600121
122 # Immediately erase the score to prevent infinite triggers.
123 "${GERRIT_SSH_CMD[@]}" review \
124 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williams8ac8e092022-01-04 12:05:50 -0600125 --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
Patrick Williams2e93e4e2021-12-30 10:29:38 -0600126
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600127 exit 0
128fi
129
130echo "${COMMITTER_USERNAME} is not on the approved list."
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500131"${GERRIT_SSH_CMD[@]}" review \
132 "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
Patrick Williamsf8ee6f02022-01-04 12:03:19 -0600133 --ok-to-test=0 -t autogenerated:jenkins \
Patrick Williamsa4d19dc2021-03-15 14:51:06 -0500134 "--message='User not approved, see admin, no CI'"
Andrew Geissler3c88e2d2021-01-28 15:18:11 -0600135
136exit 0