From 3e506a42650aa64392fcc413f41bd1641d869285 Mon Sep 17 00:00:00 2001 From: KshitijThareja Date: Mon, 12 Feb 2024 19:52:01 +0000 Subject: [PATCH] Add update changelog workflow --- .github/pull_request_template.md | 2 - .github/workflows/changelog.yml | 13 +++++- .github/workflows/update_changelog.yml | 62 ++++++++++++++++++++------ 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 312485865..2c6933da8 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -22,7 +22,6 @@ Addresses #*PR# HERE* items in CHANGELOG.md as examples --> -- [#PR no] - **Description:** Summary of change(s) - **Products impact:** Choose from - none (for internal updates) / bugfix / new API / updated API / removed API. If it's 'none', use "-" for all items below to indicate they are not relevant. - **Addresses:** Link(s) to GH issue(s) addressed. Include KDS links as well as links to related issues in a consumer product repository too. @@ -31,7 +30,6 @@ Addresses #*PR# HERE* - **Impacts a11y:** Does this change improve a11y or adds new features that can be used to improve it? Choose from: yes / no - **Guidance:** Why and how to introduce this update to a consumer? Required for breaking changes, appreciated for changes with a11y impact, and welcomed for non-breaking changes when relevant. -[#PR no]: PR link ## Steps to test diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index 51786cf0d..7050adb7a 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -1,8 +1,8 @@ -name: Check PR Description +name: Check PR for Changelog on: pull_request: - types: [opened, edited, synchronize, reopened] + types: [opened, edited, reopened] jobs: check-description: @@ -18,8 +18,10 @@ jobs: description=$(jq -r ".pull_request.body" "$GITHUB_EVENT_PATH") if [[ "$description" == *"## Changelog"* ]]; then echo "containsChangelog=true" >> $GITHUB_OUTPUT + echo "true" > containsChangelog.txt else echo "containsChangelog=false" >> $GITHUB_OUTPUT + echo "false" > containsChangelog.txt fi - name: Exit with appropriate code run: | @@ -30,3 +32,10 @@ jobs: echo "PR does not contain Changelog section" exit 1 fi + - name: Upload artifact + if: always() + uses: actions/upload-artifact@v4 + with: + name: pr-description-info + path: containsChangelog.txt + if-no-files-found: error \ No newline at end of file diff --git a/.github/workflows/update_changelog.yml b/.github/workflows/update_changelog.yml index d8bb12457..1ceeb8fa9 100644 --- a/.github/workflows/update_changelog.yml +++ b/.github/workflows/update_changelog.yml @@ -1,32 +1,68 @@ name: Update Changelog.md on: + workflow_run: + workflows: ["Check PR for Changelog"] + types: + - completed pull_request: types: - closed jobs: update-changelog: + if: github.event.pull_request.merged == true runs-on: ubuntu-latest permissions: write-all + steps: - - name: Checkout code - uses: actions/checkout@v4 + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: pr-description-info + + - name: Check if PR is from dependabot + id: check-dependabot + run: | + is_dependabot=$(jq -r '.pull_request.user.login' $GITHUB_EVENT_PATH) + echo "is_dependabot=$is_dependabot" >> $GITHUB_OUTPUT + + + - name: Read containsChangelog value + id: read-contains-changelog + run: | + containsChangelog=$(cat pr-description-info/containsChangelog.txt) + echo "Value of containsChangelog: $containsChangelog" + echo "containsChangelog=$containsChangelog" >> $GITHUB_OUTPUT - name: Extract Changelog from PR description + if: steps.read-contains-changelog.outputs.containsChangelog == 'true' id: extract-changelog run: | - changelog=$(grep -Pzo "(?s)(.*?)" ${{ github.event.before }}..${{ github.sha }} || true) - echo "::set-output name=changelog::${changelog}" - + description=$(jq -r ".pull_request.body" "$GITHUB_EVENT_PATH") + changelog_section=$(echo "$description" | awk '/## Changelog/{flag=1; next} /##/{flag=0} flag' | sed '//d' | sed '//d' | sed '1d;$d') + echo "changelog=${changelog_section}" >> $GITHUB_OUTPUT + - name: Update Changelog in main branch - if: ${{ github.event.pull_request.merged == true }} + if: steps.check-dependabot.outputs.is_dependabot == 'dependabot[bot]' || steps.read-contains-changelog.outputs.containsChangelog == 'true' run: | - echo "${{ steps.extract-changelog.outputs.changelog }}" | sed -e '1s/^/### PR details\n/' >> CHANGELOG.md - git config user.name "${{ github.actor }}" - git config user.email "${{ github.actor }}@users.noreply.github.com" - git add CHANGELOG.md - git commit -m "Automatically update changelog [skip ci]" - git push - exit 0 + changelog_content="${{ steps.extract-changelog.outputs.changelog }}" + if [ "${{ steps.check-dependabot.outputs.is_dependabot }}" == "dependabot[bot]" ]; then + changelog_content="**Description:** ${{ github.event.pull_request.title }}\n- **Products impact:** Dev Dependency upgrade\n- **Addresses:** -\n- **Components:** -\n- **Breaking:** -\n- **Impacts a11y:** -\n- **Guidance:** -\n\n" + fi + + pr_number=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH") + pr_link="[#${pr_number}]" + pr_link_ref="[#${pr_number}]: ${{ github.event.pull_request.html_url }}" + changelog_content="${pr_link}\n${changelog_content}\n${pr_link_ref}\n" + changelog_content=$(echo "$changelog_content" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//') + sed -i "//a\\$changelog_content" CHANGELOG.md + + - name: Commit updated CHANGELOG + if: steps.check-dependabot.outputs.is_dependabot == 'dependabot[bot]' || steps.read-contains-changelog.outputs.containsChangelog == 'true' + uses: stefanzweifel/git-auto-commit-action@v4 + with: + branch: ${{ github.event.pull_request.base.ref }} + commit_message: "Update CHANGELOG.md w/ PR #${{ github.event.pull_request.number }}" + file_pattern: CHANGELOG.md \ No newline at end of file