Fetch Bash Scripts by Repo Topics #6204
This file contains hidden or 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
| name: Fetch Bash Scripts by Repo Topics | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: '*/10 * * * *' | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| fetch-scripts: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v5 | |
| - name: Install dependencies | |
| run: | | |
| sudo apt-get update -y | |
| sudo apt-get install -y jq gh git | |
| - name: Fetch topics and collect Bash scripts | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| set -euo pipefail | |
| echo "Fetching topics for $REPO ..." | |
| topics_json=$(curl -s -H "Accept: application/vnd.github+json" \ | |
| -H "Authorization: token $GITHUB_TOKEN" \ | |
| "https://api.github.com/repos/$REPO/topics") | |
| topics_count=$(echo "$topics_json" | jq -r '.names | length') | |
| if [ "$topics_count" -eq 0 ]; then | |
| echo "No topics found for $REPO. Exiting." | |
| echo "ANY_CHANGES=false" >> $GITHUB_ENV | |
| exit 0 | |
| fi | |
| topics=($(echo "$topics_json" | jq -r '.names[]')) | |
| echo "Found topics: ${topics[*]}" | |
| mkdir -p scripts | |
| any_changes=false | |
| for topic in "${topics[@]}"; do | |
| echo "Searching Bash repos with topic: $topic" | |
| search_url="https://api.github.com/search/repositories?q=topic:${topic}+language:bash&sort=stars&order=desc" | |
| repos=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "$search_url" | jq -r '.items[:5][] | .clone_url') | |
| for repo in $repos; do | |
| echo "Cloning $repo" | |
| git clone --depth 1 "$repo" temp_repo || { echo "clone failed: $repo"; continue; } | |
| for file in temp_repo/*.sh; do | |
| [ -f "$file" ] || continue | |
| dest="scripts/$(basename "$repo")-$(basename "$file")" | |
| if [ ! -f "$dest" ]; then | |
| cp "$file" "$dest" | |
| any_changes=true | |
| fi | |
| done | |
| rm -rf temp_repo | |
| done | |
| done | |
| if [ "$any_changes" = true ]; then | |
| echo "ANY_CHANGES=true" >> $GITHUB_ENV | |
| else | |
| echo "ANY_CHANGES=false" >> $GITHUB_ENV | |
| fi | |
| - name: Create Pull Request (if changes) | |
| if: env.ANY_CHANGES == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| set -euo pipefail | |
| echo "Checking for existing open auto-bash-update PRs..." | |
| existing_heads=$(gh pr list --state open --json headRefName --jq '.[].headRefName' || true) | |
| if echo "$existing_heads" | grep -E '^auto-bash-update' >/dev/null 2>&1; then | |
| echo "Found existing auto-bash-update PR. Skipping creation." | |
| exit 0 | |
| fi | |
| branch="auto-bash-update-$(date +%s)" | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@github.com" | |
| git checkout -b "$branch" | |
| git add scripts/ | |
| git commit -m "Auto fetch new Bash scripts by repo topics" || { echo "Nothing to commit."; exit 0; } | |
| git push origin "$branch" | |
| pr_body="Hey @DisabledAbel 👋%0A%0AThis PR adds new Bash scripts fetched from repositories that share the same topics as the repository.%0APlease review and approve before merging.%0A%0A*(Generated automatically by GitHub Actions)*" | |
| gh pr create \ | |
| --title "Auto fetch new Bash scripts" \ | |
| --body "$pr_body" \ | |
| --base main \ | |
| --head "$branch" | |
| echo "✅ PR created for branch $branch" | |
| - name: No new scripts | |
| if: env.ANY_CHANGES != 'true' | |
| run: echo "No new scripts found. No PR created." |