diff --git a/.github/scripts/commit_and_pr.sh b/.github/scripts/commit_and_pr.sh new file mode 100755 index 0000000..be01361 --- /dev/null +++ b/.github/scripts/commit_and_pr.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +# Copyright 2024 - 2025 Khalil Estell and the libhal contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +# Get the branch name from the commit SHA +BRANCH_NAME="generate-index-${GITHUB_SHA:0:8}" + +# Create and switch to a new branch +git checkout -b "$BRANCH_NAME" + +# Configure git user +git config --local user.name 'libhal-bot' +git config --local user.email 'libhal-bot@users.noreply.github.com' + +# Add and commit changes +git add libraries.json +git commit -m "Update libraries.json from CI" + +# Push to remote with force (to update existing PRs) +git push origin "$BRANCH_NAME" --force-with-lease + +# Check if PR already exists for this branch and update it if needed +PR_NUMBER=$(gh pr list --state open --head "$BRANCH_NAME" --json number --jq '.[].number' 2>/dev/null || echo "") + +if [ -z "$PR_NUMBER" ]; then + # Create new PR using GitHub CLI with proper token handling + gh pr create --title "Update libraries.json from CI" --body "Automated update from CI workflow" --head "$BRANCH_NAME" --base "main" +else + echo "PR $PR_NUMBER already exists and is updated" +fi \ No newline at end of file diff --git a/.github/workflows/generate_index.yml b/.github/workflows/generate_index.yml new file mode 100644 index 0000000..8c43d0a --- /dev/null +++ b/.github/workflows/generate_index.yml @@ -0,0 +1,40 @@ +name: Generate API Index +on: + push: + branches: + - main + pull_request: + +jobs: + generate-index: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install dependencies + run: | + pip install gitpython requests + + - name: Generate libraries.json + run: python3 .github/scripts/generate_index.py . + + - name: Check for changes + id: git-check + run: | + if [ -z "$(git status --porcelain)" ]; then + echo "changes=false" >> $GITHUB_ENV + else + echo "changes=true" >> $GITHUB_ENV + fi + + - name: Create PR branch and push changes (if any) + if: env.changes == 'true' + run: .github/scripts/commit_and_pr.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/libraries.json b/libraries.json deleted file mode 100644 index 02f4c50..0000000 --- a/libraries.json +++ /dev/null @@ -1,62 +0,0 @@ -[ - { - "name": "libhal", - "versions": [ - { - "version": "4.10.0", - "url": "https://libhal.github.io/api/libhal/4.10.0" - }, - { - "version": "main", - "url": "https://libhal.github.io/api/libhal/main" - } - ], - "latest_url": "https://libhal.github.io/api/libhal/main" - }, - { - "name": "libhal-arm-mcu", - "versions": [ - { - "version": "1.10.0", - "url": "https://libhal.github.io/api/libhal-arm-mcu/1.10.0" - }, - { - "version": "1.12.0", - "url": "https://libhal.github.io/api/libhal-arm-mcu/1.12.0" - }, - { - "version": "1.9.0", - "url": "https://libhal.github.io/api/libhal-arm-mcu/1.9.0" - }, - { - "version": "main", - "url": "https://libhal.github.io/api/libhal-arm-mcu/main" - } - ], - "latest_url": "https://libhal.github.io/api/libhal-arm-mcu/main" - }, - { - "name": "libhal-util", - "versions": [ - { - "version": "5.4.3", - "url": "https://libhal.github.io/api/libhal-util/5.4.3" - }, - { - "version": "main", - "url": "https://libhal.github.io/api/libhal-util/main" - } - ], - "latest_url": "https://libhal.github.io/api/libhal-util/main" - }, - { - "name": "strong_ptr", - "versions": [ - { - "version": "main", - "url": "https://libhal.github.io/api/strong_ptr/main" - } - ], - "latest_url": "https://libhal.github.io/api/strong_ptr/main" - } -] \ No newline at end of file