Skip to content

Instantly share code, notes, and snippets.

@ryankanno
Last active October 15, 2024 16:34
Show Gist options
  • Save ryankanno/76d3ab25362fb942186026444fed5039 to your computer and use it in GitHub Desktop.
Save ryankanno/76d3ab25362fb942186026444fed5039 to your computer and use it in GitHub Desktop.
Bash script using GH CLI + jq to report status of Dependabot automated security fixes for all your repositories.
#!/usr/bin/env bash
QUOTE_CHAR='"'
function process_field() {
local field="$1"
local q="$QUOTE_CHAR"
# Check if the field is already quoted
if [[ "$field" == "$q"*"$q" ]]; then
# Field is already quoted, just echo it back
echo "$field"
elif [[ "$field" == *"$q"* || "$field" == *,* || "$field" == *$'\n'* ]]; then
# Field contains quotes, commas, or newlines, so it needs quoting
# First, escape any existing quotes
field="${field//$q/$q$q}"
# Now add outer quotes
echo "$q$field$q"
else
# Field doesn't need quoting, just echo it back
echo "$field"
fi
}
function add_row() {
local output=""
for field in "$@"; do
output+="$(process_field "$field"),"
done
echo "${output%,}" # Remove trailing comma
}
USER="__USERNAME_GOES_HERE__"
REPO_LIMIT=1000
ALL_REPOS_RESPONSE=$(gh repo list "$USER" --source --no-archived --limit "$REPO_LIMIT" --json name,updatedAt,url,visibility,pullRequests --jq ".[]")
add_row "Repository" "URL" "UpdatedAt" "Visibility" "PRs" "DependabotSecurityEnabled" "DependabotSecurityPaused"
for REPO_RESPONSE in $ALL_REPOS_RESPONSE; do
REPO=$(echo "$REPO_RESPONSE" | jq -r '.name')
UPDATED_AT=$(echo "$REPO_RESPONSE" | jq -r '.updatedAt')
URL=$(echo "$REPO_RESPONSE" | jq -r '.url')
VISIBILITY=$(echo "$REPO_RESPONSE" | jq -r '.visibility' | awk '{print tolower($0)}')
PRS=$(echo "$REPO_RESPONSE" | jq -r '.pullRequests.totalCount')
AUTOMATED_SECURITY_FIX=$(gh api "repos/$USER/$REPO/automated-security-fixes")
IS_ENABLED=$(echo "$AUTOMATED_SECURITY_FIX" | jq '.enabled')
IS_PAUSED=$(echo "$AUTOMATED_SECURITY_FIX" | jq '.paused')
ENABLED_TEXT=$([ "$IS_ENABLED" != "true" ] && echo 0 || echo 1)
PAUSED_TEXT=$([ "$IS_PAUSED" != "true" ] && echo 0 || echo 1)
add_row "\"$REPO\"" "\"$URL\"" "$UPDATED_AT" "\"$VISIBILITY\"" $PRS $ENABLED_TEXT $PAUSED_TEXT
done
@ryankanno
Copy link
Author

Make sure to replace USERNAME_GOES_HERE with your username.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment