Skip to content

Commit 6c2ea97

Browse files
feat: add script to search repositories by custom properties using GitHub's search API (#102)
1 parent 33f3af3 commit 6c2ea97

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

gh-cli/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,25 @@ Code search in an organization.
12271227

12281228
See the [docs](https://docs.github.com/en/rest/search?apiVersion=2022-11-28#search-code) and [StackOverflow](https://stackoverflow.com/questions/24132790/how-to-search-for-code-in-github-with-github-api) for more information.
12291229

1230+
### search-repositories-by-custom-property.sh
1231+
1232+
Search for repositories in an organization with specific custom properties using GitHub's search API with custom property filters.
1233+
1234+
The script automatically adds the 'props.' prefix to property names and supports multiple search formats:
1235+
1236+
- Single property: `RepoType:IssueOps`
1237+
- Multiple properties (AND logic): `RepoType:IssueOps Environment:Production`
1238+
- Exclusion search: `no:RepoType` (finds repos without the RepoType property)
1239+
- Mixed queries: `RepoType:IssueOps&Environment:Production`
1240+
1241+
Example usage:
1242+
1243+
```shell
1244+
./search-repositories-by-custom-property.sh joshjohanning-org 'RepoType:IssueOps'
1245+
./search-repositories-by-custom-property.sh joshjohanning-org 'no:RepoType'
1246+
./search-repositories-by-custom-property.sh joshjohanning-org 'RepoType:IssueOps Environment:Production'
1247+
```
1248+
12301249
### set-branch-protection-status-checks.sh
12311250

12321251
Set the branch protection status checks - and optionally create a branch protection rule if it doesn't exist or set the required status checks setting on an existing branch protection rule if it isn't set
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
3+
# Search for repositories in an organization with specific custom properties
4+
# Uses GitHub's search API with custom property filters
5+
6+
if [ -z "$1" ]; then
7+
echo "Usage: $0 <org> [property_query]"
8+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org"
9+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org 'RepoType:IssueOps'"
10+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org 'Environment:Production'"
11+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org 'RepoType:IssueOps&Environment:Production'"
12+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org 'RepoType:IssueOps Environment:Production'"
13+
echo "Example: ./search-repositories-by-custom-property.sh joshjohanning-org 'no:RepoType' # Repos without repo_type property"
14+
echo "Note that you can't add the same property twice in the same search query"
15+
exit 1
16+
fi
17+
18+
org="$1"
19+
property_query="${2:-RepoType:IssueOps}"
20+
21+
# Add 'props.' prefix automatically to each property if not already present
22+
# Handle multiple properties separated by & or space
23+
if [[ "$property_query" != *props.* ]]; then
24+
# Replace property names with props. prefix for multiple formats
25+
# Handle & separated: RepoType:IssueOps&Environment:Production
26+
# Handle space separated: RepoType:IssueOps Environment:Production
27+
# Handle no: qualifier: no:repo_type becomes no:props.repo_type
28+
# First handle regular properties, then fix the no: qualifier placement
29+
property_query=$(echo "$property_query" | sed -E 's/(^|[[:space:]&])([A-Za-z][A-Za-z0-9_]*):([^&[:space:]]+)/\1props.\2:\3/g')
30+
property_query=$(echo "$property_query" | sed -E 's/props\.no:/no:props./g')
31+
fi
32+
33+
echo "Searching for repositories in $org with property: $property_query"
34+
echo ""
35+
36+
# Use the search API to find repositories with custom properties
37+
# The search query combines org filter with custom property filter
38+
search_query="org:$org $property_query"
39+
# Replace spaces with + for proper GitHub search API formatting
40+
search_query=$(echo "$search_query" | sed 's/ /+/g')
41+
42+
echo "search query: $search_query"
43+
44+
# Make the API call - don't URL encode the plus signs, GitHub expects them as literal +
45+
response=$(gh api --paginate "search/repositories?q=$search_query" 2>&1)
46+
exit_code=$?
47+
48+
if [ $exit_code -ne 0 ]; then
49+
if echo "$response" | grep -q "HTTP 403\|rate limit"; then
50+
echo "Error: Rate limit exceeded or authentication issue"
51+
echo "Make sure you have a valid GitHub token and try again later"
52+
exit 1
53+
elif echo "$response" | grep -q "HTTP 422"; then
54+
echo "Error: Invalid search query"
55+
echo "Check that the custom property name and value are correct"
56+
exit 1
57+
else
58+
echo "Error: Search failed"
59+
echo "$response"
60+
exit 1
61+
fi
62+
fi
63+
64+
# Parse and display results
65+
# With --paginate, gh returns all items as a single JSON array when there are multiple pages
66+
# For single page, it returns the normal response object
67+
if echo "$response" | jq -e 'type == "array"' >/dev/null 2>&1; then
68+
# Multiple pages - response is an array of page objects
69+
actual_count=$(echo "$response" | jq 'map(.items) | add | length' 2>/dev/null | head -1)
70+
total_count=$(echo "$response" | jq -r '.[0].total_count' 2>/dev/null | head -1)
71+
echo "Found $actual_count repo(s) (out of $total_count total matching):"
72+
echo ""
73+
echo "$response" | jq -r 'map(.items) | add | .[].full_name'
74+
else
75+
# Single page - normal response object
76+
total_count=$(echo "$response" | jq -r '.total_count' 2>/dev/null | head -1)
77+
echo "Found $total_count repo(s) matching the criteria:"
78+
echo ""
79+
if [ "$total_count" -gt 0 ] 2>/dev/null; then
80+
echo "$response" | jq -r '.items[] | .full_name'
81+
else
82+
echo "No repositories found with the specified custom property."
83+
echo ""
84+
echo "This could mean:"
85+
echo "- No repositories have this custom property set"
86+
echo "- The property name or value is incorrect"
87+
echo "- You don't have access to repositories with this property"
88+
fi
89+
fi

0 commit comments

Comments
 (0)