From 19e620c8c6e539fa84b6c1211e7d381d73d2a54c Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Tue, 24 Mar 2026 20:02:50 +0000 Subject: [PATCH] ci: Automatically comment on pull requests missing changelog entries --- .forgejo/workflows/check-changelog.yml | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 .forgejo/workflows/check-changelog.yml diff --git a/.forgejo/workflows/check-changelog.yml b/.forgejo/workflows/check-changelog.yml new file mode 100644 index 000000000..b5fd04469 --- /dev/null +++ b/.forgejo/workflows/check-changelog.yml @@ -0,0 +1,97 @@ +name: Check Changelog + +on: + pull_request_target: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: + contents: read + pull-requests: write + issues: write + +jobs: + check-changelog: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + persist-credentials: false + sparse-checkout: . + + - name: Check for changelog entry + id: check_files + run: | + git fetch origin ${{ github.base_ref }} + + # Check for Added (A) or Modified (M) files in changelog.d + CHANGELOG_CHANGES=$(git diff --name-status origin/${{ github.base_ref }} HEAD -- changelog.d/) + + SRC_CHANGES=$(git diff --name-status origin/${{ github.base_ref }} HEAD -- src/) + + echo "Changes in changelog.d/:" + echo "$CHANGELOG_CHANGES" + echo "Changes in src/:" + echo "$SRC_CHANGES" + + if echo "$CHANGELOG_CHANGES" | grep -q "^[AM]"; then + echo "has_changelog=true" >> $GITHUB_OUTPUT + else + echo "has_changelog=false" >> $GITHUB_OUTPUT + fi + + if [ -n "$SRC_CHANGES" ]; then + echo "src_changed=true" >> $GITHUB_OUTPUT + else + echo "src_changed=false" >> $GITHUB_OUTPUT + fi + + - name: Manage PR Comment + uses: https://github.com/actions/github-script@v8 + env: + HAS_CHANGELOG: ${{ steps.check_files.outputs.has_changelog }} + SRC_CHANGED: ${{ steps.check_files.outputs.src_changed }} + with: + script: | + const hasChangelog = process.env.HAS_CHANGELOG === 'true'; + const srcChanged = process.env.SRC_CHANGED === 'true'; + const commentSignature = ''; + const commentBody = `${commentSignature}\nPlease add a changelog fragment to \`changelog.d/\` describing your changes.`; + + const { data: currentUser } = await github.rest.users.getAuthenticated(); + + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + }); + + const botComment = comments.find(comment => + comment.user.id === currentUser.id && + comment.body.includes(commentSignature) + ); + + const shouldWarn = srcChanged && !hasChangelog; + + if (!shouldWarn) { + if (botComment) { + console.log('Changelog found or not required. Deleting existing warning comment.'); + await github.rest.issues.deleteComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: botComment.id, + }); + } + } else { + if (!botComment) { + console.log('Changelog missing and required. Creating warning comment.'); + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: commentBody, + }); + } + }