Skip to content

Commit 23f2713

Browse files
authored
Add get-repo-orgs-secrets-count (#76)
* Add get-repo-orgs-secrets-count List all non public repos and the number of organization secret available to each * Renamed get-repo-organizations-secrets-count.sh * reordered readme * yet another readme reorder
1 parent d1c07c7 commit 23f2713

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

gh-cli/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,25 @@ Generates a CSV with 4 columns:
906906

907907
Get repositories not using actions, by files committed in the `.github/workflows` directory
908908

909+
### get-repositories-organization-secrets-count.sh
910+
911+
Gets the list of organization secrets that are available by repository (all repositories).
912+
913+
Public repositories are ignored and not listed.
914+
915+
A repository can only use a max of [100 organization secrets](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#limits-for-secrets) that are available to it. The purpose of this script is to get list of repositories and the number of organization secrets available to them mostly to figure out if you are hitting the limit and not all secrets are really available to the repository (only first 100 secrets sorted by secret name are available).
916+
917+
usage:
918+
919+
```shell
920+
get-repositories-organization-secrets-count.sh my-org-name
921+
Public repo i-am-public Skipping it
922+
923+
Secrets count for my-org-name by repo:
924+
repo1: 102 secrets
925+
repo2: 103 secrets
926+
```
927+
909928
### get-repositories-using-actions.sh
910929

911930
Get repositories using actions, by files committed in the `.github/workflows` directory
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)