From ead7a61a78cddd8de622237808f49040b96a1b46 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Mon, 17 Oct 2022 20:59:59 +0200 Subject: [PATCH] feat(danger): check that a changelog entry is not added to an already released section --- CHANGELOG.md | 2 +- danger/dangerfile.js | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85350e1..6a4ce21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Features - Updater - add `changelog-entry` option to disable adding a changelog entry ([#43](https://github.com/getsentry/github-workflows/pull/43)) -- Danger - check a changelog entry is not added to an already released section ([#44](https://github.com/getsentry/github-workflows/pull/44)) +- Danger - check that a changelog entry is not added to an already released section ([#44](https://github.com/getsentry/github-workflows/pull/44)) ## 2.2.2 diff --git a/danger/dangerfile.js b/danger/dangerfile.js index e25551b..844af91 100644 --- a/danger/dangerfile.js +++ b/danger/dangerfile.js @@ -79,19 +79,32 @@ async function checkChangelog() { changelogFile ); - const hasChangelogEntry = RegExp(`#${danger.github.pr.number}\\b`).test( + const changelogMatch = RegExp(`^(.*)\n[^\n]+#${danger.github.pr.number}\\b`, 's').exec( changelogContents ); - if (hasChangelogEntry) { - return; + // check if a changelog entry exists + if (!changelogMatch) { + return reportMissingChangelog(changelogFile); } - // Report missing changelog entry - fail( - "Please consider adding a changelog entry for the next release.", - changelogFile - ); + // Check if the entry is added to an Unreleased section (or rather, check that it's not added to a released one) + const textBeforeEntry = changelogMatch[1] + const section = RegExp('^(## +v?[0-9.]+)([\-\n _]|$)', 'm').exec(textBeforeEntry) + if (section) { + const lineNr = 1 + textBeforeEntry.split(/\r\n|\r|\n/).length + fail( + `The changelog entry seems to be part of an already released section \`${section[1]}\`. + Consider moving the entry to the appropriate subsection of the \`## Unreleased\` section, please.`, + changelogFile, + lineNr + ); + } +} + +/// Report missing changelog entry +function reportMissingChangelog(changelogFile) { + fail("Please consider adding a changelog entry for the next release.", changelogFile); const prTitleFormatted = danger.github.pr.title .split(": ")