|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + echo >&2 "Usage: $0 ORG ACTIVITY_REPO MEMBER_REPO DIR" |
| 6 | + exit 1 |
| 7 | +} |
| 8 | + |
| 9 | +ORG=${1:-$(usage)} |
| 10 | +ACTIVITY_REPO=${2:-$(usage)} |
| 11 | +MEMBER_REPO=${3:-$(usage)} |
| 12 | +DIR=${4:-$(usage)} |
| 13 | + |
| 14 | +tmp=$(mktemp -d) |
| 15 | + |
| 16 | +shopt -s nullglob |
| 17 | + |
| 18 | +# One month plus a bit of leeway |
| 19 | +epochOneMonthAgo=$(( $(date --date='1 month ago' +%s) - 60 * 60 * 12 )) |
| 20 | +mainBranch=$(git branch --show-current) |
| 21 | + |
| 22 | +mkdir -p "$DIR" |
| 23 | +cd "$DIR" |
| 24 | +for login in *; do |
| 25 | + gh api -X GET /repos/"$ORG"/"$ACTIVITY_REPO"/activity -f time_period=year -f actor="$login" -f per_page=100 \ |
| 26 | + --jq ".[] | \"- \(.timestamp) [\(.activity_type) on \(.ref | ltrimstr(\"refs/heads/\"))](https://github.com/$ORG/$ACTIVITY_REPO/compare/\(.before)...\(.after))\"" \ |
| 27 | + > "$tmp/$login" |
| 28 | + activityCount=$(wc -l <"$tmp/$login") |
| 29 | + |
| 30 | + branchName=retire-$login |
| 31 | + prInfo=$(gh api -X GET /repos/"$ORG"/"$MEMBER_REPO"/pulls -f head="$ORG":"$branchName" --jq '.[0]') |
| 32 | + if [[ -n "$prInfo" ]]; then |
| 33 | + # If there is a PR already |
| 34 | + prNumber=$(jq .number <<< "$prInfo") |
| 35 | + epochCreatedAt=$(date --date="$(jq -r .created_at <<< "$prInfo")" +%s) |
| 36 | + if (( epochCreatedAt < epochOneMonthAgo )); then |
| 37 | + echo "$login has a retirement PR due, comment with a reminder to merge" |
| 38 | + { |
| 39 | + if (( activityCount > 0 )); then |
| 40 | + echo "One month has passed, @$login has been active again:" |
| 41 | + cat "$tmp/$login" |
| 42 | + echo "" |
| 43 | + echo "This PR may be merged and implemented by:" |
| 44 | + else |
| 45 | + echo "One month has passed, to this PR should now be merged and implemented by:" |
| 46 | + fi |
| 47 | + echo "- Adding @$login to the [Retired Nixpkgs Contributors team](https://github.com/orgs/NixOS/teams/retired-nixpkgs-contributors)" |
| 48 | + echo "- Removing @$login from the [Nixpkgs Committers team](https://github.com/orgs/NixOS/teams/nixpkgs-committers)" |
| 49 | + } | gh api --method POST /repos/"$ORG"/"$MEMBER_REPO"/issues/"$prNumber"/comments -F "body=@-" >/dev/null |
| 50 | + else |
| 51 | + echo "$login has a retirement PR pending" |
| 52 | + fi |
| 53 | + elif (( activityCount <= 0 )); then |
| 54 | + echo "$login has become inactive, opening a PR" |
| 55 | + # If there is no PR yet, but they have become inactive |
| 56 | + git switch -C "$branchName" |
| 57 | + git rm "$login" |
| 58 | + git commit -m "Automatic retirement of @$login" |
| 59 | + git push -f origin "$branchName" |
| 60 | + { |
| 61 | + echo "This is an automated PR to retire @$login as a Nixpkgs committers due to not using their commit access for 1 year." |
| 62 | + echo "" |
| 63 | + echo "Make a comment with your motivation to keep commit access, otherwise this PR will be merged and implemented in 1 month." |
| 64 | + } | gh api \ |
| 65 | + --method POST \ |
| 66 | + /repos/"$ORG"/"$MEMBER_REPO"/pulls \ |
| 67 | + -f "title=Automatic retirement of @$login" \ |
| 68 | + -F "body=@-" \ |
| 69 | + -f "head=$ORG:$branchName" \ |
| 70 | + -f "base=$mainBranch" >/dev/null |
| 71 | + git checkout "$mainBranch" |
| 72 | + else |
| 73 | + echo "$login is active with $activityCount activities" |
| 74 | + fi |
| 75 | +done |
0 commit comments