blob: 32d79cc4881dbefa40d4484f8e03f25c13e58d69 [file] [log] [blame]
Martin Rothdb02f112023-09-27 12:06:01 -06001#!/usr/bin/env bash
2# shellcheck disable=SC2310,SC2312
3# The above line must be directly after the shebang line.
4# Disables these warnings:
5# SC2310 - This function is invoked in an 'if' condition so set -e will be disabled.
6# Invoke separately if failures should cause the script to exit.
7# SC2312 - Consider invoking this command separately to avoid masking its return value
8
9#
10# SPDX-License-Identifier: GPL-2.0-only
11#
12
13#
14# Description:
15# Identifies new users in gerrit so that they can be greeted and marked
16# as new users.
17#
18
19VERSION="1.00"
20PROGRAM=$0
21PROGNAME="$(basename "${PROGRAM}")"
22
23COMMIT_COUNT=5 # Consider a user to be new until they have this many commits
24AGE="1week"
25#AGE="3months"
26KNOWN_AUTHOR_FILE="./authorlist.txt"
27NEW_AUTHOR_FILE="./new_authors.txt"
28GERRIT_USER="$1"
29GERRIT_REPO="review.coreboot.org"
30
31show_version() {
32 echo "${PROGNAME} version ${VERSION}"
33 echo
34}
35
36usage() {
37 echo "Usage: ${PROGNAME} <gerrit username>"
38 echo "example: ${PROGNAME} martinlroth"
39}
40
41main() {
42 local commit_count
43 local patchlist
44 local owner
45 local owner_email
46 local commit_id
47 local new_users=0
48
49 show_version
50
51 if ! jq --version >/dev/null 2>&1; then
52 echo "Error: jq is not installed. Please install it with your package manager."
53 exit 1
54 fi
55
56 if [[ -z ${GERRIT_USER} ]]; then
57 echo "Error: Please specify gerrit username on the command line." >&2
58 fi
59 if [[ -z ${GERRIT_USER} || ${GERRIT_USER} == "--help" || ${GERRIT_USER} == "-h" ]]; then
60 usage
61 exit 1
62 fi
63
64 echo "List of known users with more than 5 commits: ${KNOWN_AUTHOR_FILE}"
65 echo "List of new user's patches: ${NEW_AUTHOR_FILE}"
66 echo "Key: . = author in known user list or new user commit already seen"
67 echo " : = author getting added to known user list"
68 echo
69
70 touch "${NEW_AUTHOR_FILE}" "${KNOWN_AUTHOR_FILE}"
71
72 # Get all coreboot patches that aren't by known users, newer than the age set above
73 patchlist="$(ssh -p 29418 "${GERRIT_USER}@${GERRIT_REPO}" gerrit query --format=JSON --no-limit --current-patch-set "repo:coreboot AND status:open NOT age:${AGE} NOT ownerin:Reviewers NOT ownerin:\\\"Core Developers\\\"" |
74 jq -Mr '.currentPatchSet.uploader.name + ", " + .currentPatchSet.uploader.email + ", " + .currentPatchSet.revision + ", " + (.number | tostring)' |
75 grep -v "null\|^,");"
76
77 if [[ -z ${patchlist} ]]; then
78 echo "Error: No patches returned." >&2
79 exit 1
80 else
81 echo "$(wc -l <<<"${patchlist}") patches found."
82 fi
83
84 # Loop through all patches found, looking for any that are not by a known author.
85 # If an author with more than 5 patches is found, add them to a list to filter out in the future.
86 # If we find a new patch, print it, then add it to a list so that it isn't reported again.
87 IFS=$'\n'
88 for patch in ${patchlist}; do
89 owner="$(echo "${patch}" | cut -f1 -d',')"
90 owner_email="$(echo "${patch}" | cut -f2 -d',')"
91 commit_id="$(echo "${patch}" | cut -f4 -d',')"
92
93 if grep -qi "${owner}" "${KNOWN_AUTHOR_FILE}" || grep -qi "${owner_email}" "${KNOWN_AUTHOR_FILE}" || grep -q "${commit_id}" "${NEW_AUTHOR_FILE}"; then
94 printf "."
95 continue
96 fi
97
98 # Get the author's commit count
99 # shellcheck disable=SC2126 # (Consider using grep -c instead of grep | wc)
100 commit_count="$(ssh -p 29418 "${GERRIT_USER}@${GERRIT_REPO}" gerrit query --format=JSON "limit:6 AND repo:coreboot AND owner:${owner_email} and status:merged" |
101 jq -Mr '.owner.name + ", " + .owner.email' |
102 grep -v "null\|^," |
103 wc -l)"
104 if [[ ${commit_count} -ge ${COMMIT_COUNT} ]]; then
105 printf ":"
106 echo "${owner}, ${owner_email}" >>"${KNOWN_AUTHOR_FILE}"
107 continue
108 fi
109
110 printf "\n%s looks to be a patch by a new author.\n" "${patch}"
111 echo "${patch}" >>"${NEW_AUTHOR_FILE}"
112 new_users+=1
113 done
114 printf "\n"
115 if [[ ${new_users} -eq 0 ]]; then
116 echo "No new patches by new users found."
117 fi
118}
119
120main