Created
May 17, 2024 20:18
-
-
Save Goos/95ae8ae229f863133a7d375539d4f7e4 to your computer and use it in GitHub Desktop.
Shell script for searching through github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# ./search_github.sh "some query" "my-org" "my-token" | vi - | |
# Define the search query | |
search_query="$1" | |
org="$2" | |
token="$3" | |
# GitHub API URL | |
api_url="https://api.github.com/search/code" | |
# Initial page | |
page=1 | |
# Loop until 'curl' exits with a non-zero status or no more results | |
while true; do | |
# Fetch the results from GitHub API | |
response=$( | |
curl -s -L -G \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
-H "Authorization: Bearer $token" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
--data-urlencode "page=$page" \ | |
--data-urlencode "q=org:$org NOT fork:only $search_query" \ | |
"$api_url" | |
) | |
# Check if 'curl' was successful | |
if [ $? -ne 0 ]; then | |
echo "Error fetching data from GitHub API." | |
exit 1 | |
fi | |
# Extract repositories from the response | |
urls=$(echo "$response" | jq '.items[] .html_url') | |
# Check if no repositories were found | |
if [ -z "$urls" ]; then | |
break | |
fi | |
# Print the repositories | |
echo "$urls" | |
# Increment the page number | |
((page++)) | |
sleep 2 | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment