xref: /openbmc/openbmc-build-scripts/jenkins/userid-validation (revision b836d2480ab494db63869449718572d6ca708636)
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
1537052418SAndrew GeisslerGERRIT_COMMAND="curl -s --anyauth -n https://gerrit.openbmc.org"
16a4d19dcbSPatrick WilliamsGERRIT_SSH_CMD=( \
17a4d19dcbSPatrick Williams        ssh -o 'StrictHostKeyChecking no' -i "$SSH_KEY" \
1837052418SAndrew Geissler        -p 29418 jenkins-openbmc-ci@gerrit.openbmc.org gerrit \
19a4d19dcbSPatrick Williams    )
203c88e2d1SAndrew Geissler
213c88e2d1SAndrew Geisslerecho "Checking ${GERRIT_PROJECT}:${GERRIT_BRANCH}:${GERRIT_CHANGE_ID}:${GERRIT_PATCHSET_REVISION}"
223c88e2d1SAndrew Geissler
23a4d19dcbSPatrick Williams# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
24c491709cSAndrew GeisslerCOMMITTER_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'])")
255d3031d6SEd Tanousif [ "${COMMITTER_EMAIL}" = "" ]; then
263c88e2d1SAndrew Geissler    echo "Unable to find committer."
27a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
28a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
29f8ee6f07SPatrick Williams        --ok-to-test=0 "--message='Unable to determine committer'"
30a4d19dcbSPatrick Williams    exit 1
313c88e2d1SAndrew Geisslerfi
323c88e2d1SAndrew Geissler
333c88e2d1SAndrew Geissler#echo "Commit by '${COMMITTER_EMAIL}'"
34a4d19dcbSPatrick Williams# shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
35c491709cSAndrew GeisslerCOMMITTER_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"
385d3031d6SEd Tanousif [ "${COMMITTER_USERNAME}" = "" ]; then
393c88e2d1SAndrew Geissler    echo "Unable to determine github user for ${COMMITTER_EMAIL}."
40a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
41a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
42f8ee6f07SPatrick Williams        --ok-to-test=0 "--message='Unable to determine github user'"
43a4d19dcbSPatrick Williams    exit 1
443c88e2d1SAndrew Geisslerfi
453c88e2d1SAndrew Geissler
463c88e2d1SAndrew Geissler# Reset the vote to 0 so jenkins will detect a new +1 on retriggers
47a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
48a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
498ac8e093SPatrick Williams    --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
503c88e2d1SAndrew Geissler
5147b59dc8SPatrick Williams# Add reviewers based on OWNERS files.
5247b59dc8SPatrick Williams"${WORKSPACE}/openbmc-build-scripts/tools/owners" -p "${WORKSPACE}" reviewers |
5347b59dc8SPatrick Williams{
5447b59dc8SPatrick Williams    while read -r reviewer ;
5547b59dc8SPatrick Williams    do
5647b59dc8SPatrick Williams        # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
5747b59dc8SPatrick Williams        ${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/reviewers \
5847b59dc8SPatrick Williams            -X POST \
5947b59dc8SPatrick Williams            -H "Content-Type: application/json" \
6047b59dc8SPatrick Williams            -d "$reviewer" || true
6147b59dc8SPatrick Williams    done
6247b59dc8SPatrick Williams} || true
6347b59dc8SPatrick Williams
643c88e2d1SAndrew Geissler# Write full list of users to a file
65a4d19dcbSPatrick WilliamsGERRIT_CI_GROUPS=( \
66*b836d248SPatrick Williams        akamai/ci-authorized \
67a4d19dcbSPatrick Williams        alibaba/ci-authorized \
68a4d19dcbSPatrick Williams        amd/ci-authorized \
69e63bb548SAndrew Geissler        ami/ci-authorized \
70a4d19dcbSPatrick Williams        ampere/ci-authorized \
7176f25debSGilbert Chen        arm/ci-authorized \
722fde84b0SAndrew Geissler        aspeed/ci-authorized \
73a4d19dcbSPatrick Williams        bytedance/ci-authorized \
740cc575a4SAndrew Geissler        code-construct/ci-authorized \
75123ffbe9SPatrick Williams        depo/ci-authorized \
76a9a79198SAndrew Geissler        erg/ci-authorized \
7793d22a49SPatrick Williams        equinix/ci-authorized \
78a4d19dcbSPatrick Williams        facebook/ci-authorized \
79870b41abSPatrick Williams        fii/ci-authorized \
80a4d19dcbSPatrick Williams        gager-in/ci-authorized \
81a4d19dcbSPatrick Williams        google/ci-authorized \
82a4d19dcbSPatrick Williams        hcl/ci-authorized \
83a4d19dcbSPatrick Williams        hpe/ci-authorized \
84a4d19dcbSPatrick Williams        ibm/ci-authorized \
85a4d19dcbSPatrick Williams        individual/ci-authorized \
86a4d19dcbSPatrick Williams        inspur/ci-authorized \
87a4d19dcbSPatrick Williams        intel/ci-authorized \
88a4d19dcbSPatrick Williams        inventec/ci-authorized \
89e13a7111SAndrew Geissler        lenovo/ci-authorized \
90ec22c46fSAndrew Geissler        microsoft/ci-authorized \
91a56c7012SAndrew Geissler        nineelements/ci-authorized \
92a4d19dcbSPatrick Williams        nuvoton/ci-authorized \
93a4d19dcbSPatrick Williams        nvidia/ci-authorized \
94a4d19dcbSPatrick Williams        openbmc/ci-authorized \
9546182106SAndrew Geissler        pcpartner/ci-authorized \
96bc73b757SPatrick Williams        phytium/ci-authorized \
97a4d19dcbSPatrick Williams        quanta/ci-authorized \
987031b334SJae Hyun Yoo        quic/ci-authorized \
99a4d19dcbSPatrick Williams        rcs/ci-authorized \
100a4d19dcbSPatrick Williams        supermicro/ci-authorized \
101fd0e80d0SJordan Chang        ufispace/ci-authorized \
102eb4961b1SPatrick Williams        vaisala/ci-authorized \
103a4d19dcbSPatrick Williams        wistron/ci-authorized \
104a4d19dcbSPatrick Williams        wiwynn/ci-authorized \
105a4d19dcbSPatrick Williams        yadro/ci-authorized \
106a4d19dcbSPatrick Williams    )
1073c88e2d1SAndrew Geissler
108a4d19dcbSPatrick Williamsrm -f "$WORKSPACE/users.txt"
109a4d19dcbSPatrick Williamsfor g in "${GERRIT_CI_GROUPS[@]}"; do
110a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
111a4d19dcbSPatrick Williams        >> "$WORKSPACE/users.txt"
112a4d19dcbSPatrick Williamsdone
1133c88e2d1SAndrew Geissler
1143c88e2d1SAndrew Geissler# grep for the specific username word in the file
115a4d19dcbSPatrick Williamsif grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
116a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
117a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1188ac8e093SPatrick Williams        --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \
119a4d19dcbSPatrick Williams        "--message='User approved, CI ok to start'"
1202e93e4e6SPatrick Williams
1212e93e4e6SPatrick Williams    # Immediately erase the score to prevent infinite triggers.
1222e93e4e6SPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
1232e93e4e6SPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1248ac8e093SPatrick Williams        --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
1252e93e4e6SPatrick Williams
1263c88e2d1SAndrew Geissler    exit 0
1273c88e2d1SAndrew Geisslerfi
1283c88e2d1SAndrew Geissler
1293c88e2d1SAndrew Geisslerecho "${COMMITTER_USERNAME} is not on the approved list."
130a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
131a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
132f8ee6f07SPatrick Williams    --ok-to-test=0 -t autogenerated:jenkins \
133a4d19dcbSPatrick Williams    "--message='User not approved, see admin, no CI'"
1343c88e2d1SAndrew Geissler
1353c88e2d1SAndrew Geisslerexit 0
136