xref: /openbmc/openbmc-build-scripts/jenkins/userid-validation (revision 47b59dc80db2956c36b6f24aee0b7c1a6b688995)
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
51*47b59dc8SPatrick Williams# Add reviewers based on OWNERS files.
52*47b59dc8SPatrick Williams"${WORKSPACE}/openbmc-build-scripts/tools/owners" -p "${WORKSPACE}" reviewers |
53*47b59dc8SPatrick Williams{
54*47b59dc8SPatrick Williams    while read -r reviewer ;
55*47b59dc8SPatrick Williams    do
56*47b59dc8SPatrick Williams        # shellcheck disable=2086 # GERRIT_COMMAND is purposefully wordsplit.
57*47b59dc8SPatrick Williams        ${GERRIT_COMMAND}/a/changes/${GERRIT_PROJECT/\//%2F}~${GERRIT_BRANCH}~${GERRIT_CHANGE_ID}/reviewers \
58*47b59dc8SPatrick Williams            -X POST \
59*47b59dc8SPatrick Williams            -H "Content-Type: application/json" \
60*47b59dc8SPatrick Williams            -d "$reviewer" || true
61*47b59dc8SPatrick Williams    done
62*47b59dc8SPatrick Williams} || true
63*47b59dc8SPatrick Williams
643c88e2d1SAndrew Geissler# Write full list of users to a file
65a4d19dcbSPatrick WilliamsGERRIT_CI_GROUPS=( \
66a4d19dcbSPatrick Williams    alibaba/ci-authorized \
67a4d19dcbSPatrick Williams    amd/ci-authorized \
68e63bb548SAndrew Geissler    ami/ci-authorized \
69a4d19dcbSPatrick Williams    ampere/ci-authorized \
7076f25debSGilbert Chen    arm/ci-authorized \
712fde84b0SAndrew Geissler    aspeed/ci-authorized \
72a4d19dcbSPatrick Williams    bytedance/ci-authorized \
730cc575a4SAndrew Geissler    code-construct/ci-authorized \
74123ffbe9SPatrick Williams    depo/ci-authorized \
75a9a79198SAndrew Geissler    erg/ci-authorized \
76a4d19dcbSPatrick Williams    facebook/ci-authorized \
77870b41abSPatrick Williams    fii/ci-authorized \
78a4d19dcbSPatrick Williams    gager-in/ci-authorized \
79a4d19dcbSPatrick Williams    google/ci-authorized \
80a4d19dcbSPatrick Williams    hcl/ci-authorized \
81a4d19dcbSPatrick Williams    hpe/ci-authorized \
82a4d19dcbSPatrick Williams    ibm/ci-authorized \
83a4d19dcbSPatrick Williams    individual/ci-authorized \
84a4d19dcbSPatrick Williams    inspur/ci-authorized \
85a4d19dcbSPatrick Williams    intel/ci-authorized \
86a4d19dcbSPatrick Williams    inventec/ci-authorized \
87e13a7111SAndrew Geissler    lenovo/ci-authorized \
88a56c7012SAndrew Geissler    nineelements/ci-authorized \
89a4d19dcbSPatrick Williams    nuvoton/ci-authorized \
90a4d19dcbSPatrick Williams    nvidia/ci-authorized \
91a4d19dcbSPatrick Williams    openbmc/ci-authorized \
9246182106SAndrew Geissler    pcpartner/ci-authorized \
93bc73b757SPatrick Williams    phytium/ci-authorized \
94a4d19dcbSPatrick Williams    quanta/ci-authorized \
957031b334SJae Hyun Yoo    quic/ci-authorized \
96a4d19dcbSPatrick Williams    rcs/ci-authorized \
97a4d19dcbSPatrick Williams    supermicro/ci-authorized \
98a4d19dcbSPatrick Williams    wistron/ci-authorized \
99a4d19dcbSPatrick Williams    wiwynn/ci-authorized \
100a4d19dcbSPatrick Williams    yadro/ci-authorized \
101a4d19dcbSPatrick Williams)
1023c88e2d1SAndrew Geissler
103a4d19dcbSPatrick Williamsrm -f "$WORKSPACE/users.txt"
104a4d19dcbSPatrick Williamsfor g in "${GERRIT_CI_GROUPS[@]}"; do
105a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" ls-members "$g" --recursive \
106a4d19dcbSPatrick Williams        >> "$WORKSPACE/users.txt"
107a4d19dcbSPatrick Williamsdone
1083c88e2d1SAndrew Geissler
1093c88e2d1SAndrew Geissler# grep for the specific username word in the file
110a4d19dcbSPatrick Williamsif grep -q -w "${COMMITTER_USERNAME}" "$WORKSPACE/users.txt"; then
111a4d19dcbSPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
112a4d19dcbSPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1138ac8e093SPatrick Williams        --ok-to-test=1 -t autogenerated:jenkins --notify=NONE \
114a4d19dcbSPatrick Williams        "--message='User approved, CI ok to start'"
1152e93e4e6SPatrick Williams
1162e93e4e6SPatrick Williams    # Immediately erase the score to prevent infinite triggers.
1172e93e4e6SPatrick Williams    "${GERRIT_SSH_CMD[@]}" review \
1182e93e4e6SPatrick Williams        "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
1198ac8e093SPatrick Williams        --ok-to-test=0 -t autogenerated:jenkins --notify=NONE
1202e93e4e6SPatrick Williams
1213c88e2d1SAndrew Geissler    exit 0
1223c88e2d1SAndrew Geisslerfi
1233c88e2d1SAndrew Geissler
1243c88e2d1SAndrew Geisslerecho "${COMMITTER_USERNAME} is not on the approved list."
125a4d19dcbSPatrick Williams"${GERRIT_SSH_CMD[@]}" review \
126a4d19dcbSPatrick Williams    "${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER}" \
127f8ee6f07SPatrick Williams    --ok-to-test=0 -t autogenerated:jenkins \
128a4d19dcbSPatrick Williams    "--message='User not approved, see admin, no CI'"
1293c88e2d1SAndrew Geissler
1303c88e2d1SAndrew Geisslerexit 0
131