Skip to content

Instantly share code, notes, and snippets.

@joesturge
Created September 19, 2024 13:09
Show Gist options
  • Select an option

  • Save joesturge/70430527c6f4b83bdaa452d68e4129de to your computer and use it in GitHub Desktop.

Select an option

Save joesturge/70430527c6f4b83bdaa452d68e4129de to your computer and use it in GitHub Desktop.
Count the number of merged dependabot PRs per member of a github team
#!/bin/bash
# Check for arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <team-url>"
exit 1
fi
# Extract organization and team slug from the URL
TEAM_URL=$1
ORG=$(echo "$TEAM_URL" | cut -d '/' -f 5)
TEAM=$(echo "$TEAM_URL" | cut -d '/' -f 7)
# Get current date and calculate the date two weeks ago
CURRENT_DATE=$(date -u +"%Y-%m-%d")
TWO_WEEKS_AGO=$(date -u -d "13 days ago" +"%Y-%m-%d")
# Get the list of team members, handle errors if unauthorized or not found
TEAM_MEMBERS=$(gh api "/orgs/$ORG/teams/$TEAM/members" --jq '.[].login' 2>/dev/null)
if [ -z "$TEAM_MEMBERS" ]; then
echo "Failed to retrieve team members. Check the team URL and permissions."
exit 1
fi
# Get all repositories the team has access to
REPOS=$(gh api "/orgs/$ORG/teams/$TEAM/repos" --jq '.[].full_name' 2>/dev/null)
if [ -z "$REPOS" ]; then
echo "Failed to retrieve repositories for the team. Check permissions."
exit 1
fi
# Initialize an associative array to accumulate PR counts for each member
declare -A pr_count
# Loop through each repository and each team member to count PRs
for REPO in $REPOS; do
echo "Checking repository: $REPO"
for MEMBER in $TEAM_MEMBERS; do
COUNT=$(gh pr list --repo "$REPO" --state merged \
--search "author:dependabot[bot] merged:$TWO_WEEKS_AGO..$CURRENT_DATE" \
--json mergedBy \
--jq "[.[] | select(.mergedBy.login == \"$MEMBER\")] | length")
# Accumulate count per member across repos
pr_count[$MEMBER]=$((pr_count[$MEMBER]+COUNT))
done
done
# Output the sorted result (highest count at the top)
echo "Dependabot PRs merged by team members across all repositories in the last two weeks:"
for MEMBER in "${!pr_count[@]}"; do
echo "$MEMBER: ${pr_count[$MEMBER]}"
done | sort -nr -k2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment