Skip to content

Commit

Permalink
Danger PR number as markdown link (#1745)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia authored Sep 29, 2021
1 parent b8854fe commit 1a40920
Showing 1 changed file with 47 additions and 65 deletions.
112 changes: 47 additions & 65 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,63 @@
const PR_NUMBER = danger.github.pr.number;
const PR_LINK = `(#${PR_NUMBER})`;
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.'
);
}
}

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:
async function checkChangelog() {
const changelogFile = "CHANGELOG.md";

To the changelog entry, please add a link to this PR (consider a more descriptive message):`;
// Check if skipped
const skipChangelog =
danger.github && (danger.github.pr.body + "").includes("#skip-changelog");

const CHANGELOG_END_BODY = `If none of the above apply, you can opt out by adding _#skip-changelog_ to the PR description.`;
if (skipChangelog) {
return;
}

function getCleanTitleWithPrLink() {
const title = danger.github.pr.title;
return title.split(": ").slice(-1)[0].trim().replace(/\.+$/, "") + PR_LINK;
}
// Check if current PR has an entry in changelog
const changelogContents = await danger.github.utils.fileContents(
changelogFile
);

function getChangelogDetailsHtml() {
return `
<details>
<summary><b>\`${CHANGELOG_SUMMARY_TITLE}\`$</b></summary>
const hasChangelogEntry = RegExp(`#${danger.github.pr.number}\\b`).test(
changelogContents
);

\`${CHANGELOG_BODY}\`
if (hasChangelogEntry) {
return;
}

\`\`\`md
- ${getCleanTitleWithPrLink()}
\`\`\`
// Report missing changelog entry
fail(
"Please consider adding a changelog entry for the next release.",
changelogFile
);

\`${CHANGELOG_END_BODY}\`
</details>
`;
}
const prTitleFormatted = danger.github.pr.title
.split(": ")
.slice(-1)[0]
.trim()
.replace(/\.+$/, "");

function getChangelogDetailsTxt() {
return CHANGELOG_SUMMARY_TITLE + '\n' +
CHANGELOG_BODY + '\n' +
getCleanTitleWithPrLink() + '\n' +
CHANGELOG_END_BODY;
}
markdown(
`
### Instructions and example for changelog
function HasPermissionToComment(){
return danger.github.pr.head.repo.git_url == danger.github.pr.base.repo.git_url;
}
Please add an entry to \`CHANGELOG.md\` to the "Unreleased" section. Make sure the entry includes this PR's number.
async function containsChangelog(path) {
const contents = await danger.github.utils.fileContents(path);
return contents.includes(PR_LINK);
}
Example:
async function checkChangelog() {
const skipChangelog =
danger.github && (danger.github.pr.body + "").includes("#skip-changelog");
if (skipChangelog) {
return;
}
\`\`\`markdown
## Unreleased
const hasChangelog = await containsChangelog("CHANGELOG.md");

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;
}
}
}
- ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url}))
\`\`\`
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.');
}
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 +68,7 @@ async function checkAll() {
return;
}

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

Expand Down

0 comments on commit 1a40920

Please sign in to comment.