From b299f98161a02fe335c7e06ff4c93ce8fc24137a Mon Sep 17 00:00:00 2001 From: Muhammad Atif Ali Date: Tue, 13 Feb 2024 15:23:44 +0300 Subject: [PATCH] ci: automate version bumps in module README.md files (#139) Co-authored-by: Mathias Fredriksson --- .github/workflows/update-readme.yaml | 41 ++++++++++++++++++++++++++++ update-version.sh | 37 +++++++++++++++++-------- 2 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/update-readme.yaml diff --git a/.github/workflows/update-readme.yaml b/.github/workflows/update-readme.yaml new file mode 100644 index 0000000..ffda695 --- /dev/null +++ b/.github/workflows/update-readme.yaml @@ -0,0 +1,41 @@ +name: Update README on Tag + +on: + workflow_dispatch: + push: + tags: + - 'v*' + +jobs: + update-readme: + permissions: + contents: read + pull-requests: write + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Get the latest tag + id: get-latest-tag + run: echo "TAG=$(git describe --tags --abbrev=0 | sed 's/^v//')" >> $GITHUB_OUTPUT + + - name: Run update script + run: ./update-version.sh + + - name: Create Pull Request + id: create-pr + uses: peter-evans/create-pull-request@v5 + with: + commit-message: 'chore: bump version to ${{ env.TAG }} in README.md files' + title: 'chore: bump version to ${{ env.TAG }} in README.md files' + body: 'This is an auto-generated PR to update README.md files of all modules with the new tag ${{ env.TAG }}' + branch: 'update-readme-branch' + env: + TAG: ${{ steps.get-latest-tag.outputs.TAG }} + + - name: Auto-approve + uses: hmarr/auto-approve-action@v4 + if: github.ref == 'refs/heads/update-readme-branch' diff --git a/update-version.sh b/update-version.sh index 2fadb57..433f614 100755 --- a/update-version.sh +++ b/update-version.sh @@ -6,16 +6,31 @@ set -euo pipefail -LATEST_TAG=$(git describe --abbrev=0 --tags | sed 's/^v//') || exit $? +current_tag=$(git describe --tags --abbrev=0) +previous_tag=$(git describe --tags --abbrev=0 $current_tag^) +mapfile -t changed_files < <(git diff --name-only "$previous_tag" "$current_tag" | xargs dirname | sort -u | grep -v '^\.') -find . -name README.md | while read -r file; do - tmpfile=$(mktemp /tmp/tempfile.XXXXXX) - awk -v tag="$LATEST_TAG" '{ - if ($1 == "version" && $2 == "=") { - sub(/"[^"]*"/, "\"" tag "\"") - print - } else { - print - } - }' "$file" > "$tmpfile" && mv "$tmpfile" "$file" +changed_dirs=() +for file in $changed_files; do + dir=$(dirname "$file") + changed_dirs+=("$dir") done +changed_dirs=($(printf "%s\n" "${changed_dirs[@]}" | sort -u)) + +LATEST_TAG=$(git describe --abbrev=0 --tags | sed 's/^v//') || exit $? + +for dir in "${changed_dirs[@]}"; do + if [[ -f "$dir/README.md" ]]; then + echo "Bumping version in $dir/README.md" + file="$dir/README.md" + tmpfile=$(mktemp /tmp/tempfile.XXXXXX) + awk -v tag="$LATEST_TAG" '{ + if ($1 == "version" && $2 == "=") { + sub(/"[^"]*"/, "\"" tag "\"") + print + } else { + print + } + }' "$file" > "$tmpfile" && mv "$tmpfile" "$file" + fi +done \ No newline at end of file