From 6fc8bc001dc77fd57c733391595aafce7b98c771 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Wed, 4 Oct 2023 13:20:29 +0200 Subject: [PATCH] Add Danger changelog checker --- .github/workflows/danger.yml | 16 ++++++++++ dangerfile.js | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .github/workflows/danger.yml create mode 100644 dangerfile.js diff --git a/.github/workflows/danger.yml b/.github/workflows/danger.yml new file mode 100644 index 00000000..68e3645b --- /dev/null +++ b/.github/workflows/danger.yml @@ -0,0 +1,16 @@ +name: "Danger" +on: + pull_request: + types: [opened, synchronize, reopened, edited, ready_for_review] + +jobs: + build: + name: Changelog + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: npx danger ci + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/dangerfile.js b/dangerfile.js new file mode 100644 index 00000000..91f473ad --- /dev/null +++ b/dangerfile.js @@ -0,0 +1,60 @@ +async function checkChangelog() { + const changelogFile = "CHANGELOG.md"; + + // Check if skipped + const skipChangelog = + danger.github && (danger.github.pr.body + "").includes("#skip-changelog"); + + if (skipChangelog) { + return; + } + + // Check if current PR has an entry in changelog + const changelogContents = await danger.github.utils.fileContents( + changelogFile + ); + + const hasChangelogEntry = RegExp(`#${danger.github.pr.number}\\b`).test( + changelogContents + ); + + if (hasChangelogEntry) { + return; + } + + // Report missing changelog entry + fail( + "Please consider adding a changelog entry for the next release.", + changelogFile + ); + + const prTitleFormatted = danger.github.pr.title + .split(": ") + .slice(-1)[0] + .trim() + .replace(/\.+$/, ""); + + markdown( + ` +### Instructions and example for changelog +Please add an entry to \`CHANGELOG.md\` to the "Next" section. Make sure the entry includes this PR's number. +Example: +\`\`\`markdown +## Next +- ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url})) +\`\`\` +If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description.`.trim() + ); +} + +async function checkAll() { + const isDraft = danger.github.pr.mergeable_state === "draft"; + + if (isDraft) { + return; + } + + await checkChangelog(); +} + +schedule(checkAll);