From 380520fed6b373071e64fcdee8679becdd381dd9 Mon Sep 17 00:00:00 2001 From: Martin Thomson Date: Tue, 9 Jul 2024 11:48:51 +1000 Subject: [PATCH] Add a script to detect bad GitHub usernames --- get-email.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ upload.mk | 6 ++--- 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100755 get-email.sh diff --git a/get-email.sh b/get-email.sh new file mode 100755 index 00000000..a96ebeb6 --- /dev/null +++ b/get-email.sh @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# Given a tag and a draft, attempt to work out who is responsible for the submission. +# +# This tries several things, starting from a simple UPLOAD_EMAIL environment variable. +# Then it looks at the email of the person that tagged the commit, if that is set. +# Then it starts looking for who made the commit or who authored the commit. +# For each of these, GitHub could use a @users.noreply.github.com address. +# In which case, this attempts to use the GitHub API to get their email. +# Finally, this picks the first listed author email address from the draft. + +tried=() +emailok() { + tried+=("$1") + if [ -n "$2" ]; then + echo "$2" + exit 0 + fi +} + +# Exit on errors, +set -e +# ... but don't print anything; we're using tokens. +set +x + +emailok "\$UPLOAD_EMAIL environment variable" "$UPLOAD_EMAIL" + +tag="$1" +draft="$2" + +for k in taggeremail committeremail authoremail; do + tagger="$(git tag --list --format '%('"$k"')' "$tag" | sed -e 's/^$//')" + ghuser="${tagger%@users.noreply.github.com}" + if [ "$ghuser" = "$email" ]; then + emailok "git $k" "$tagger" + elif [ -n "$GITHUB_API_TOKEN" ]; then + script="import json,sys +e=json.load(sys.stdin).get('email') +if isinstance(e, str): print(e)" + userjson="$(curl -SsLf \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GITHUB_API_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/users/$ghuser" 2>/dev/null)" + apiok=$? + emailok "github API for $k ($ghuser)" \ + "$([ "$apiok" -eq 0 ] && echo "$userjson" | python -c "$script" 2>/dev/null)" + fi +done + +if [ -n "$draft" ]; then + emailok "draft author" "$(xmllint --xpath '/rfc/front/author[1]/address/email/text()' "$draft" 2>/dev/null)" +fi + +# Give up +{ + echo "Unable to find email to use for submission." + echo "Tried:" + for t in "${tried[@]}"; do + echo " $t" + done +} 1>&2 +exit 1 diff --git a/upload.mk b/upload.mk index 14621054..cfd79f8b 100644 --- a/upload.mk +++ b/upload.mk @@ -25,10 +25,8 @@ endif .%.upload: %.xml set -ex; tag="$(notdir $(basename $<))"; \ - email="$(UPLOAD_EMAIL)"; \ - [ -z "$$email" ] && email="$$(git tag --list --format '%(taggeremail)' "$$tag" | sed -e 's/^$$//')"; \ - [ -z "$$email" ] && email=$$(xmllint --xpath '/rfc/front/author[1]/address/email/text()' $< 2>/dev/null); \ - [ -z "$$email" ] && ! echo "Unable to find email to use for submission." 1>&2; \ + email="$$($(LIBDIR)/get-email.sh "$$tag" "$<")"; \ + [ -z "$$email" ] && exit 1; \ replaces() { \ [ "$${1##*-}" = "00" ] || return; \ file="$$(git ls-files "$${1%-[0-9][0-9]}.*")"; \