|
| 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