xref: /openbmc/openbmc-build-scripts/jenkins/userid-validation (revision 605d4fdd35a406639b410cd0349409dd40fb07fa)
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
10a6ebc6e2SManojkiran Eda#       provided by that plugin are available (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
23ac912df8SPatrick WilliamsCOMMITTER_USERNAME=$("${GERRIT_SSH_CMD[@]}" query "${GERRIT_CHANGE_NUMBER}" --current-patch-set --format json | jq -r '.currentPatchSet.uploader.username | select (. != null )')
243c88e2d1SAndrew Geisslerecho "USERNAME: $COMMITTER_USERNAME"
255d3031d6SEd Tanousif [ "${COMMITTER_USERNAME}" = "" ]; then
263c88e2d1SAndrew Geissler    echo "Unable to determine github user for ${COMMITTER_EMAIL}."
27a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
28a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
29f8ee6f07SPatrick Williams        --ok-to-test=0 "--message='Unable to determine github user'"
30a4d19dcbSPatrick Williams    exit 1
313c88e2d1SAndrew Geisslerfi
323c88e2d1SAndrew Geissler
333c88e2d1SAndrew Geissler# Reset the vote to 0 so jenkins will detect a new +1 on retriggers
34a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
35a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
368ac8e093SPatrick Williams    --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
373c88e2d1SAndrew Geissler
3847b59dc8SPatrick Williams# Add reviewers based on OWNERS files.
3947b59dc8SPatrick Williams"${WORKSPACE}/openbmc-build-scripts/tools/owners" -p "${WORKSPACE}" reviewers |
4047b59dc8SPatrick Williams{
4147b59dc8SPatrick Williams    while read -r reviewer ;
4247b59dc8SPatrick Williams    do
4347b59dc8SPatrick Williams        # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
4447b59dc8SPatrick Williams        ${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/reviewers \
4547b59dc8SPatrick Williams            -X POST \
4647b59dc8SPatrick Williams            -H "Content-Type: application/json" \
4747b59dc8SPatrick Williams            -d "$reviewer" || true
4847b59dc8SPatrick Williams    done
4947b59dc8SPatrick Williams} || true
5047b59dc8SPatrick Williams
513c88e2d1SAndrew Geissler# Write full list of users to a file
52a4d19dcbSPatrick WilliamsGERRIT_CI_GROUPS=( \
537e153073SAndrew Geissler        aimvalley/ci-authorized \
54b836d248SPatrick Williams        akamai/ci-authorized \
55a4d19dcbSPatrick Williams        alibaba/ci-authorized \
56a4d19dcbSPatrick Williams        amd/ci-authorized \
57e63bb548SAndrew Geissler        ami/ci-authorized \
58a4d19dcbSPatrick Williams        ampere/ci-authorized \
5976f25debSGilbert Chen        arm/ci-authorized \
602fde84b0SAndrew Geissler        aspeed/ci-authorized \
61971d68b1SAndrew Geissler        asus/ci-authorized \
62a4d19dcbSPatrick Williams        bytedance/ci-authorized \
63ad42f700SAndrew Geissler        celestica/ci-authorized \
640cc575a4SAndrew Geissler        code-construct/ci-authorized \
658c012379SAndrew Geissler        cornelis/ci-authorized \
66f38b5c91SAndrew Geissler        dell/ci-authorized \
67123ffbe9SPatrick Williams        depo/ci-authorized \
68a9a79198SAndrew Geissler        erg/ci-authorized \
6993d22a49SPatrick Williams        equinix/ci-authorized \
70a4d19dcbSPatrick Williams        facebook/ci-authorized \
71870b41abSPatrick Williams        fii/ci-authorized \
7283d69a2aSPaul Fertser        gagar-in/ci-authorized \
73a4d19dcbSPatrick Williams        google/ci-authorized \
74a4d19dcbSPatrick Williams        hcl/ci-authorized \
75f6b3db1dSAndrew Geissler        hetzner/ci-authorized \
76f3e8289aSAndrew Geissler        hp/ci-authorized \
77a4d19dcbSPatrick Williams        hpe/ci-authorized \
78a4d19dcbSPatrick Williams        ibm/ci-authorized \
796481ea40SAndrew Geissler        iei/ci-authorized \
80a4d19dcbSPatrick Williams        individual/ci-authorized \
81*cf184a41SAndrew Geissler        ingrasys/ci-authorized \
82a4d19dcbSPatrick Williams        inspur/ci-authorized \
83a4d19dcbSPatrick Williams        intel/ci-authorized \
84a4d19dcbSPatrick Williams        inventec/ci-authorized \
85e13a7111SAndrew Geissler        lenovo/ci-authorized \
86ec22c46fSAndrew Geissler        microsoft/ci-authorized \
87478503b1SAndrew Geissler        mitac/ci-authorized \
885f4d98c0SAndrew Geissler        multicoreware/ci-authorized \
89a56c7012SAndrew Geissler        nineelements/ci-authorized \
90a4d19dcbSPatrick Williams        nuvoton/ci-authorized \
91a4d19dcbSPatrick Williams        nvidia/ci-authorized \
92a4d19dcbSPatrick Williams        openbmc/ci-authorized \
9346182106SAndrew Geissler        pcpartner/ci-authorized \
94bc73b757SPatrick Williams        phytium/ci-authorized \
957d88622fSAndrew Geissler        plexus/ci-authorized \
96a4d19dcbSPatrick Williams        quanta/ci-authorized \
977031b334SJae Hyun Yoo        quic/ci-authorized \
98a4d19dcbSPatrick Williams        rcs/ci-authorized \
9923b47b25SAndrew Geissler        sipearl/ci-authorized \
100a4d19dcbSPatrick Williams        supermicro/ci-authorized \
101b8730367SAndrew Geissler        tencent/ci-authorized \
1022b2f4f17SAndrew Geissler        triple-crown/ci-authorized \
103fd0e80d0SJordan Chang        ufispace/ci-authorized \
104eb4961b1SPatrick Williams        vaisala/ci-authorized \
105a4d19dcbSPatrick Williams        wistron/ci-authorized \
106a4d19dcbSPatrick Williams        wiwynn/ci-authorized \
107a4d19dcbSPatrick Williams    )
1083c88e2d1SAndrew Geissler
109a4d19dcbSPatrick Williamsrm -f "$WORKSPACE/users.txt"
110a4d19dcbSPatrick Williamsfor g in "${GERRIT_CI_GROUPS[@]}"; do
111a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
112a4d19dcbSPatrick Williams        >> "$WORKSPACE/users.txt"
113a4d19dcbSPatrick Williamsdone
1143c88e2d1SAndrew Geissler
1153c88e2d1SAndrew Geissler# grep for the specific username word in the file
116a4d19dcbSPatrick Williamsif grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
117a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
118a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1198ac8e093SPatrick Williams        --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \
120a4d19dcbSPatrick Williams        "--message='User approved, CI ok to start'"
1212e93e4e6SPatrick Williams
1222e93e4e6SPatrick Williams    # Immediately erase the score to prevent infinite triggers.
1232e93e4e6SPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
1242e93e4e6SPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1258ac8e093SPatrick Williams        --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
1262e93e4e6SPatrick Williams
1273c88e2d1SAndrew Geissler    exit 0
1283c88e2d1SAndrew Geisslerfi
1293c88e2d1SAndrew Geissler
1303c88e2d1SAndrew Geisslerecho "${COMMITTER_USERNAME} is not on the approved list."
131a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
132a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
133f8ee6f07SPatrick Williams    --ok-to-test=0 -t autogenerated:jenkins \
134a4d19dcbSPatrick Williams    "--message='User not approved, see admin, no CI'"
1353c88e2d1SAndrew Geissler
1363c88e2d1SAndrew Geisslerexit 0
137