-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to detect bad GitHub usernames
- Loading branch information
1 parent
960f70c
commit 380520f
Showing
2 changed files
with
64 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <user>@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/^<//;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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters