Created
February 2, 2024 00:02
-
-
Save nate-double-u/b31ef0642091dbb465672fdaa15bf3e8 to your computer and use it in GitHub Desktop.
Find stale PRs where the author hasn't commented in 2 weeks
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
#!/bin/bash | |
# Script provided by OpenAI ChatGPT on February 1, 2024. | |
# Repository in the format "owner/repo" | |
REPO="owner/repo" | |
# Period in days to check for author's inactivity in comments | |
INACTIVE_DAYS=14 | |
# Calculate date 2 weeks ago in ISO8601 format for macOS | |
INACTIVE_DATE=$(date -v-${INACTIVE_DAYS}d +%Y-%m-%dT%H:%M:%SZ) | |
# Fetch all open PRs including author login and createdAt date | |
PRs=$(gh pr list -R $REPO --json number,url,author,createdAt --state open) | |
# Loop through each PR to check for the last comment by the PR author | |
echo "$PRs" | jq -c '.[]' | while read -r pr; do | |
PR_NUMBER=$(echo $pr | jq '.number') | |
PR_URL=$(echo $pr | jq -r '.url') | |
AUTHOR_LOGIN=$(echo $pr | jq -r '.author.login') | |
PR_CREATED_AT=$(echo $pr | jq -r '.createdAt') | |
# Skip PRs opened within the inactivity period | |
PR_CREATED_AT_SEC=$(date -jf "%Y-%m-%dT%H:%M:%SZ" "$PR_CREATED_AT" +%s) | |
INACTIVE_DATE_SEC=$(date -jf "%Y-%m-%dT%H:%M:%SZ" "$INACTIVE_DATE" +%s) | |
if [[ "$PR_CREATED_AT_SEC" -gt "$INACTIVE_DATE_SEC" ]]; then | |
continue | |
fi | |
# Fetch all comments for the PR | |
COMMENTS=$(gh api -X GET repos/$REPO/issues/$PR_NUMBER/comments --paginate | jq -r --arg AUTHOR_LOGIN "$AUTHOR_LOGIN" '.[] | select(.user.login == $AUTHOR_LOGIN) | .created_at') | |
# Find the latest comment date made by the author | |
LAST_AUTHOR_COMMENT_DATE=$(echo "$COMMENTS" | sort -r | head -n 1) | |
if [[ -z "$LAST_AUTHOR_COMMENT_DATE" ]]; then | |
# No comments made by the author | |
echo "PR #$PR_NUMBER: $PR_URL - Author ($AUTHOR_LOGIN) has not commented since PR creation on $PR_CREATED_AT" | |
open -a /Applications/Firefox.app "$PR_URL" | |
else | |
# Convert LAST_AUTHOR_COMMENT_DATE to seconds since the epoch for comparison | |
LAST_AUTHOR_COMMENT_DATE_SEC=$(date -jf "%Y-%m-%dT%H:%M:%SZ" "$LAST_AUTHOR_COMMENT_DATE" +%s) | |
if [[ "$LAST_AUTHOR_COMMENT_DATE_SEC" -lt "$INACTIVE_DATE_SEC" ]]; then | |
echo "PR #$PR_NUMBER: $PR_URL - Author's ($AUTHOR_LOGIN) last comment on $LAST_AUTHOR_COMMENT_DATE is more than $INACTIVE_DAYS days old" | |
open -a /Applications/Firefox.app "$PR_URL" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is written to run on a mac with
gh
, Firefox andjq
installed.