Skip to content

Commit 8d04dd6

Browse files
feat: add script to retrieve repositories created from a specific template using GraphQL (#153)
1 parent 165c88a commit 8d04dd6

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

gh-cli/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,17 @@ Generates a CSV with 4 columns:
12341234
- url template - The autolink url template
12351235
- autonumeric - If the autolink is autonumeric or not (true/false)
12361236

1237+
### get-repositories-created-from-template.sh
1238+
1239+
Gets all repositories in an organization or user account that were created from a specific repository template using GraphQL. Tries the organization endpoint first, then falls back to the user endpoint.
1240+
1241+
```shell
1242+
./get-repositories-created-from-template.sh joshjohanning-org joshjohanning/nodejs-actions-starter-template
1243+
```
1244+
1245+
> [!NOTE]
1246+
> The `templateRepository` field is only populated if the template repository still exists and is accessible
1247+
12371248
### get-repositories-not-using-actions.sh
12381249

12391250
Get repositories not using actions, by files committed in the `.github/workflows` directory
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
# Gets all repositories in an organization or user account that were created from a specific
4+
# repository template
5+
#
6+
# Note: Uses the GraphQL API to query all repositories in a single paginated call. Tries the
7+
# organization endpoint first, then falls back to the user endpoint. The templateRepository
8+
# field is only populated if the template repository still exists and is accessible.
9+
#
10+
# Usage:
11+
# ./get-repositories-created-from-template.sh <org-or-user> <template-repo-full-name> [hostname]
12+
#
13+
# Example:
14+
# ./get-repositories-created-from-template.sh joshjohanning-org joshjohanning/nodejs-actions-starter-template
15+
# ./get-repositories-created-from-template.sh joshjohanning joshjohanning/nodejs-actions-starter-template
16+
# ./get-repositories-created-from-template.sh joshjohanning-org joshjohanning/nodejs-actions-starter-template ghes.example.com
17+
18+
if [ -z "$2" ]; then
19+
echo "Usage: $0 <org-or-user> <template-repo-full-name> [hostname]"
20+
echo "Example: $0 joshjohanning-org joshjohanning/nodejs-actions-starter-template"
21+
exit 1
22+
fi
23+
24+
org="$1"
25+
template_repo="$2"
26+
hostname="${3:-github.com}"
27+
28+
echo "Finding repositories in '$org' created from template '$template_repo'..."
29+
echo ""
30+
31+
# try organization first, fall back to user
32+
results=$(gh api graphql --hostname "$hostname" --paginate -F owner="$org" -f query='
33+
query ($owner: String!, $endCursor: String) {
34+
organization(login: $owner) {
35+
repositories(first: 100, after: $endCursor) {
36+
nodes {
37+
nameWithOwner
38+
templateRepository {
39+
nameWithOwner
40+
}
41+
}
42+
pageInfo {
43+
endCursor
44+
hasNextPage
45+
}
46+
}
47+
}
48+
}' --jq "[.data.organization.repositories.nodes[] | select(.templateRepository.nameWithOwner == \"$template_repo\") | .nameWithOwner] | .[]" 2>/dev/null)
49+
50+
if [ $? -ne 0 ] || [ -z "$results" ]; then
51+
results=$(gh api graphql --hostname "$hostname" --paginate -F owner="$org" -f query='
52+
query ($owner: String!, $endCursor: String) {
53+
user(login: $owner) {
54+
repositories(first: 100, after: $endCursor, ownerAffiliations: OWNER) {
55+
nodes {
56+
nameWithOwner
57+
templateRepository {
58+
nameWithOwner
59+
}
60+
}
61+
pageInfo {
62+
endCursor
63+
hasNextPage
64+
}
65+
}
66+
}
67+
}' --jq "[.data.user.repositories.nodes[] | select(.templateRepository.nameWithOwner == \"$template_repo\") | .nameWithOwner] | .[]")
68+
fi
69+
70+
echo "$results"

0 commit comments

Comments
 (0)