xref: /openbmc/openbmc-build-scripts/jenkins/userid-validation (revision a4d19dcbe28fe598c1a2a3c735c006972eefb36c)
13c88e2d1SAndrew Geissler#!/bin/bash -e
23c88e2d1SAndrew Geissler#
33c88e2d1SAndrew Geissler# Purpose:
43c88e2d1SAndrew Geissler#  This script is responsible for determining the owner of a gerrit
53c88e2d1SAndrew Geissler#  commit, verifying they are within an approved gerrit group, and
63c88e2d1SAndrew Geissler#  then updating gerrit with that verification info.
73c88e2d1SAndrew Geissler#
83c88e2d1SAndrew Geissler# Note: It is assumed this script is run as a part of a jenkins job triggered
93c88e2d1SAndrew Geissler#       by the gerrit plugin. Therefore it assumes certain env variables
103c88e2d1SAndrew Geissler#       provided by that plugin are avialable (i.e. GERRIT_PROJECT, ...)
113c88e2d1SAndrew Geissler#
123c88e2d1SAndrew Geissler# Required Inputs:
133c88e2d1SAndrew Geissler#  SSH_KEY:  Path to private ssh key used to post messages to gerrit
143c88e2d1SAndrew Geissler
153c88e2d1SAndrew GeisslerGERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc-project.xyz"
16*a4d19dcbSPatrick WilliamsGERRIT_SSH_CMD=( \
17*a4d19dcbSPatrick Williams    ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \
18*a4d19dcbSPatrick Williams    -p 29418 jenkins-openbmc-ci@gerrit.openbmc-project.xyz gerrit \
19*a4d19dcbSPatrick Williams)
203c88e2d1SAndrew Geissler
213c88e2d1SAndrew Geisslerecho "Checking ${GERRIT_PROJECT}:${GERRIT_BRANCH}:${GERRIT_CHANGE_ID}:${GERRIT_PATCHSET_REVISION}"
223c88e2d1SAndrew Geissler
23*a4d19dcbSPatrick Williams# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
24*a4d19dcbSPatrick WilliamsCOMMITTER_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'])")
253c88e2d1SAndrew Geisslerif [ "x${COMMITTER_EMAIL}" == "x" ]; then
263c88e2d1SAndrew Geissler    echo "Unable to find committer."
27*a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
28*a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
29*a4d19dcbSPatrick Williams        --label=Ok-To-Test=0 "--message='Unable to determine committer'"
30*a4d19dcbSPatrick Williams    exit 1
313c88e2d1SAndrew Geisslerfi
323c88e2d1SAndrew Geissler
333c88e2d1SAndrew Geissler#echo "Commit by '${COMMITTER_EMAIL}'"
34*a4d19dcbSPatrick Williams# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
35*a4d19dcbSPatrick WilliamsCOMMITTER_USERNAME=$(${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL} | python3 -c "import sys, json; sys.stdin.read(4); print(json.load(sys.stdin)['username'])")
363c88e2d1SAndrew Geissler#COMMITTER_USERNAME=`${GERRIT_COMMAND}/a/accounts/${COMMITTER_EMAIL}`
373c88e2d1SAndrew Geisslerecho "USERNAME: $COMMITTER_USERNAME"
383c88e2d1SAndrew Geisslerif [ "x${COMMITTER_USERNAME}" == "x" ]; then
393c88e2d1SAndrew Geissler    echo "Unable to determine github user for ${COMMITTER_EMAIL}."
40*a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
41*a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
42*a4d19dcbSPatrick Williams        --label=Ok-To-Test=0 "--message='Unable to determine github user'"
43*a4d19dcbSPatrick Williams    exit 1
443c88e2d1SAndrew Geisslerfi
453c88e2d1SAndrew Geissler
463c88e2d1SAndrew Geissler# Reset the vote to 0 so jenkins will detect a new +1 on retriggers
47*a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
48*a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
49*a4d19dcbSPatrick Williams    --label=Ok-To-Test=0 -t autogenerated:jenkins
503c88e2d1SAndrew Geissler
513c88e2d1SAndrew Geissler# Write full list of users to a file
52*a4d19dcbSPatrick WilliamsGERRIT_CI_GROUPS=( \
53*a4d19dcbSPatrick Williams    alibaba/ci-authorized \
54*a4d19dcbSPatrick Williams    amd/ci-authorized \
55*a4d19dcbSPatrick Williams    ampere/ci-authorized \
56*a4d19dcbSPatrick Williams    bytedance/ci-authorized \
57*a4d19dcbSPatrick Williams    facebook/ci-authorized \
58*a4d19dcbSPatrick Williams    gager-in/ci-authorized \
59*a4d19dcbSPatrick Williams    google/ci-authorized \
60*a4d19dcbSPatrick Williams    hcl/ci-authorized \
61*a4d19dcbSPatrick Williams    hpe/ci-authorized \
62*a4d19dcbSPatrick Williams    ibm/ci-authorized \
63*a4d19dcbSPatrick Williams    individual/ci-authorized \
64*a4d19dcbSPatrick Williams    inspur/ci-authorized \
65*a4d19dcbSPatrick Williams    intel/ci-authorized \
66*a4d19dcbSPatrick Williams    inventec/ci-authorized \
67*a4d19dcbSPatrick Williams    nuvoton/ci-authorized \
68*a4d19dcbSPatrick Williams    nvidia/ci-authorized \
69*a4d19dcbSPatrick Williams    openbmc/ci-authorized \
70*a4d19dcbSPatrick Williams    quanta/ci-authorized \
71*a4d19dcbSPatrick Williams    rcs/ci-authorized \
72*a4d19dcbSPatrick Williams    supermicro/ci-authorized \
73*a4d19dcbSPatrick Williams    wistron/ci-authorized \
74*a4d19dcbSPatrick Williams    wiwynn/ci-authorized \
75*a4d19dcbSPatrick Williams    yadro/ci-authorized \
76*a4d19dcbSPatrick Williams)
773c88e2d1SAndrew Geissler
78*a4d19dcbSPatrick Williamsrm -f "$WORKSPACE/users.txt"
79*a4d19dcbSPatrick Williamsfor g in "${GERRIT_CI_GROUPS[@]}"; do
80*a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
81*a4d19dcbSPatrick Williams        >> "$WORKSPACE/users.txt"
82*a4d19dcbSPatrick Williamsdone
833c88e2d1SAndrew Geissler
843c88e2d1SAndrew Geissler# grep for the specific username word in the file
85*a4d19dcbSPatrick Williamsif grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
86*a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
87*a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
88*a4d19dcbSPatrick Williams        --label=Ok-To-Test=1 -t autogenerated:jenkins \
89*a4d19dcbSPatrick Williams        "--message='User approved, CI ok to start'"
903c88e2d1SAndrew Geissler    exit 0
913c88e2d1SAndrew Geisslerfi
923c88e2d1SAndrew Geissler
933c88e2d1SAndrew Geisslerecho "${COMMITTER_USERNAME} is not on the approved list."
94*a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
95*a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
96*a4d19dcbSPatrick Williams    --label=Ok-To-Test=0 -t autogenerated:jenkins \
97*a4d19dcbSPatrick Williams    "--message='User not approved, see admin, no CI'"
983c88e2d1SAndrew Geissler
993c88e2d1SAndrew Geisslerexit 0
100