Skip to content

Commit c06302f

Browse files
authored
fix: show GH annotations when running from forks (#37)
* fix: show GH annotations when running from forks We can't post to the PR in that case. * fix: dangerfile - avoid string.replaceAll() * chore: update changelog
1 parent f8d1d53 commit c06302f

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
## 2.1.1
6+
7+
### Fixes
8+
9+
- Show GitHub annotations when running from forks - can't post a PR comment in that case ([#37](https://github.com/getsentry/github-workflows/pull/37))
10+
511
## 2.1.0
612

713
### Features

danger/dangerfile.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
1+
const headRepoName = danger.github.pr.head.repo.git_url;
2+
const baseRepoName = danger.github.pr.base.repo.git_url;
3+
const isFork = headRepoName != baseRepoName;
4+
5+
if (isFork) {
6+
console.log(
7+
"::warning::Running from a forked repo. Danger won't be able to post comments and workflow status on the main repo, printing directly."
8+
);
9+
10+
// Override DangerJS default functions to print to console & create annotations instead.
11+
const log = function (type, message, file, line) {
12+
message = message.replace(/%/g, "%25");
13+
message = message.replace(/\n/g, "%0A");
14+
message = message.replace(/\r/g, "%0D");
15+
console.log(`::${type} file=${file},line=${line}::${message}`);
16+
};
17+
18+
const dangerFail = fail;
19+
fail = function (message, file, line) {
20+
log("error", message, file, line);
21+
dangerFail(message, file, line);
22+
};
23+
24+
warn = function (message, file, line) {
25+
log("warning", message, file, line);
26+
};
27+
28+
message = function (message, file, line) {
29+
log("notice", message, file, line);
30+
};
31+
32+
markdown = function (message, file, line) {
33+
log("notice", message, file, line);
34+
};
35+
}
36+
137
// e.g. "feat" if PR title is "Feat : add more useful stuff"
238
// or "ci" if PR branch is "ci/update-danger"
3-
function getPrFlavor() {
39+
const prFlavor = (function () {
440
if (danger.github && danger.github.pr) {
5-
var separator = undefined;
641
if (danger.github.pr.title) {
742
const parts = danger.github.pr.title.split(":");
843
if (parts.length > 1) {
@@ -17,10 +52,11 @@ function getPrFlavor() {
1752
}
1853
}
1954
return "";
20-
}
55+
})();
56+
console.log(`::debug:: PR Flavor: '${prFlavor}'`);
2157

2258
async function checkDocs() {
23-
if (getPrFlavor().startsWith("feat")) {
59+
if (prFlavor.startsWith("feat")) {
2460
message(
2561
'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.'
2662
);
@@ -31,13 +67,11 @@ async function checkChangelog() {
3167
const changelogFile = "CHANGELOG.md";
3268

3369
// Check if skipped
34-
if (danger.github && danger.github.pr) {
35-
if (
36-
["ci", "chore(deps)"].includes(getPrFlavor()) ||
37-
(danger.github.pr.body + "").includes("#skip-changelog")
38-
) {
39-
return;
40-
}
70+
if (
71+
["ci", "chore(deps)"].includes(prFlavor) ||
72+
(danger.github.pr.body + "").includes("#skip-changelog")
73+
) {
74+
return;
4175
}
4276

4377
// Check if current PR has an entry in changelog
@@ -69,7 +103,7 @@ async function checkChangelog() {
69103
`
70104
### Instructions and example for changelog
71105
72-
Please add an entry to \`CHANGELOG.md\` to the "Unreleased" section. Make sure the entry includes this PR's number.
106+
Please add an entry to \`${changelogFile}\` to the "Unreleased" section. Make sure the entry includes this PR's number.
73107
74108
Example:
75109
@@ -79,7 +113,8 @@ Example:
79113
- ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url}))
80114
\`\`\`
81115
82-
If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description.`.trim()
116+
If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description.`.trim(),
117+
changelogFile
83118
);
84119
}
85120

0 commit comments

Comments
 (0)