|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +if [ -z "$1" ]; then |
| 4 | + echo "Usage: $0 <org>" |
| 5 | + exit 1 |
| 6 | +fi |
| 7 | + |
| 8 | +org="$1" |
| 9 | + |
| 10 | +declare -A repos |
| 11 | +while IFS= read -r repo_json; do |
| 12 | + visibility=$(echo "$repo_json" | jq -r '.visibility') |
| 13 | + repo_name=$(echo "$repo_json" | jq -r '.name') |
| 14 | + |
| 15 | + if [ "$visibility" = "public" ]; then |
| 16 | + echo "Public repo $repo_name Skipping it" |
| 17 | + continue |
| 18 | + fi |
| 19 | + |
| 20 | + repos["$repo_name"]=0 |
| 21 | +done < <(gh api "orgs/$org/repos" --paginate --jq '.[] | {name: .name, visibility: .visibility}') |
| 22 | + |
| 23 | +# Increment secrets count for all repos |
| 24 | +function incrementAllRepos() { |
| 25 | + for repo in "${!repos[@]}"; do |
| 26 | + ((repos["$repo"]++)) |
| 27 | + done |
| 28 | +} |
| 29 | + |
| 30 | +# Given a secret name increment secrets count for selected repos |
| 31 | +function incrementSelectedRepos() { |
| 32 | + secret_name="$1" |
| 33 | + |
| 34 | + while IFS= read -r repo_json; do |
| 35 | + repo_name=$(echo "$repo_json" | jq -r '.name') |
| 36 | + |
| 37 | + repos["$repo_name"]=$((repos["$repo_name"] + 1)) |
| 38 | + done < <(gh api "orgs/$org/actions/secrets/$secret_name/repositories" --paginate --jq '.repositories[] | {name: .name}') |
| 39 | +} |
| 40 | + |
| 41 | +while read -r secret_json; do |
| 42 | + |
| 43 | + secret_name=$(echo "$secret_json" | jq -r '.name') |
| 44 | + visibility=$(echo "$secret_json" | jq -r '.visibility') |
| 45 | + |
| 46 | + if [ "$visibility" = "public" ]; then |
| 47 | + echo "$secret_name is available to public repos. Skipping it" |
| 48 | + continue |
| 49 | + fi |
| 50 | + |
| 51 | + if [ "$visibility" = "private" ] || [ "$visibility" = "all" ]; then |
| 52 | + incrementAllRepos |
| 53 | + elif [ "$visibility" = "selected" ]; then |
| 54 | + incrementSelectedRepos "$secret_name" |
| 55 | + fi |
| 56 | + |
| 57 | +done < <(gh api "orgs/$org/actions/secrets" --paginate --jq '.secrets[] | {name: .name, visibility: .visibility}') |
| 58 | + |
| 59 | +# dump count of secrets for each repo |
| 60 | + |
| 61 | +echo -e "\nSecrets count for $org by repo:" |
| 62 | +for repo in "${!repos[@]}"; do |
| 63 | + echo "$repo: ${repos["$repo"]} secrets" |
| 64 | +done |
| 65 | + |
0 commit comments