Skip to content

Commit

Permalink
Danger PR number as markdown link (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Jun 14, 2022
1 parent 1deb7a6 commit 01ad443
Showing 1 changed file with 44 additions and 67 deletions.
111 changes: 44 additions & 67 deletions dangerfile.js
@@ -1,81 +1,58 @@
const PR_NUMBER = danger.github.pr.number;
const PR_LINK = `(#${PR_NUMBER})`;

const CHANGELOG_SUMMARY_TITLE = `Instructions and example for changelog`;
const CHANGELOG_BODY = `Please add an entry to \`CHANGELOG.md\` to the "Unreleased" section under the following heading:
To the changelog entry, please add a link to this PR (consider a more descriptive message):`;

const CHANGELOG_END_BODY = `If none of the above apply, you can opt out by adding _#skip-changelog_ to the PR description.`;

function getCleanTitleWithPrLink() {
const title = danger.github.pr.title;
return title.split(": ").slice(-1)[0].trim().replace(/\.+$/, "") + PR_LINK;
}

function getChangelogDetailsHtml() {
return `
<details>
<summary><b>\`${CHANGELOG_SUMMARY_TITLE}\`$</b></summary>
\`${CHANGELOG_BODY}\`
\`\`\`md
- ${getCleanTitleWithPrLink()}
\`\`\`
\`${CHANGELOG_END_BODY}\`
</details>
`;
}

function getChangelogDetailsTxt() {
return CHANGELOG_SUMMARY_TITLE + '\n' +
CHANGELOG_BODY + '\n' +
getCleanTitleWithPrLink() + '\n' +
CHANGELOG_END_BODY;
}

function HasPermissionToComment(){
return danger.github.pr.head.repo.git_url == danger.github.pr.base.repo.git_url;
}

async function containsChangelog(path) {
const contents = await danger.github.utils.fileContents(path);
return contents.includes(PR_LINK);
async function checkDocs() {
if (danger.github.pr.title.startsWith("feat:")) {
message(
'Do not forget to update <a href="https://github.com/getsentry/sentry-docs">Sentry-docs</a> with your feature once the pull request gets approved.'
);
}
}

async function checkChangelog() {
const changelogFile = "CHANGELOG.md";

// Check if skipped
const skipChangelog =
danger.github && (danger.github.pr.body + "").includes("#skip-changelog");

if (skipChangelog) {
return;
}

const hasChangelog = await containsChangelog("CHANGELOG.md");
// 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 (!hasChangelog)
{
if (HasPermissionToComment())
{
fail("Please consider adding a changelog entry for the next release.");
markdown(getChangelogDetailsHtml());
}
else
{
//Fallback
console.log("Please consider adding a changelog entry for the next release.");
console.log(getChangelogDetailsTxt());
process.exitCode = 1;
}
if (hasChangelogEntry) {
return;
}
}

async function checkIfFeature() {
const title = danger.github.pr.title;
if (title.startsWith('feat:') && HasPermissionToComment()){
message('Do not forget to update <a href="https://github.com/getsentry/sentry-docs">Sentry-docs</a> with your feature once the pull request gets approved.');
}
// 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 "Unreleased" section. Make sure the entry includes this PR's number.
Example:
\`\`\`markdown
## Unreleased
- ${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() {
Expand All @@ -86,7 +63,7 @@ async function checkAll() {
return;
}

await checkIfFeature();
await checkDocs();
await checkChangelog();
}

Expand Down

0 comments on commit 01ad443

Please sign in to comment.