Skip to content

Commit f8d1d53

Browse files
authored
Feat: Danger reusable workflow (#34)
* feat: add dangerJS workflow * chore: rename test workflows * refactor: fail danger on errors * refactor: use danger-js docker image * chore: update docs * chore: update danger workflow version to v2 * chore: update changelog for v2.1.0
1 parent 67e6a71 commit f8d1d53

8 files changed

Lines changed: 176 additions & 4 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This isn't a reusable workflow but an actual CI action for this repo itself - to test the workflows.
2+
name: Danger Workflow Tests
3+
4+
on:
5+
pull_request:
6+
types: [opened, synchronize, reopened, edited, ready_for_review]
7+
8+
jobs:
9+
danger:
10+
uses: ./.github/workflows/danger.yml
11+
with:
12+
_workflow_version: ${{ github.sha }}
13+
14+
test-outputs:
15+
runs-on: ubuntu-latest
16+
needs: danger
17+
steps:
18+
- run: "[[ '${{ needs.danger.outputs.outcome }}' == 'success' ]]"

.github/workflows/danger.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Runs DangerJS with a pre-configured set of rules on a Pull Request.
2+
on:
3+
workflow_call:
4+
inputs:
5+
_workflow_version:
6+
description: 'Internal: specify github-workflows (this repo) revision to use when checking out scripts.'
7+
type: string
8+
required: false
9+
default: v2 # Note: update when publishing a new version
10+
outputs:
11+
outcome:
12+
description: Whether the Danger run finished successfully. Possible values are success, failure, cancelled, or skipped.
13+
value: ${{ jobs.danger.outputs.outcome }}
14+
15+
jobs:
16+
danger:
17+
runs-on: ubuntu-latest
18+
outputs:
19+
outcome: ${{ steps.danger.outcome }}
20+
steps:
21+
- uses: actions/checkout@v3
22+
with:
23+
fetch-depth: 0
24+
25+
- name: Download dangerfile.js
26+
run: wget https://raw.githubusercontent.com/getsentry/github-workflows/${{ inputs._workflow_version }}/danger/dangerfile.js -P ${{ runner.temp }}
27+
28+
# Using a pre-built docker image in GitHub container registry instaed of NPM to reduce possible attack vectors.
29+
- name: Run DangerJS
30+
id: danger
31+
run: |
32+
docker run \
33+
--volume ${{ github.workspace }}:/github/workspace \
34+
--volume ${{ runner.temp }}:${{ runner.temp }} \
35+
--workdir /github/workspace \
36+
--user $UID \
37+
-e "INPUT_ARGS" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e GITHUB_ACTIONS=true -e CI=true \
38+
-e GITHUB_TOKEN="${{ github.token }}" \
39+
-e DANGER_DISABLE_TRANSPILATION="true" \
40+
ghcr.io/danger/danger-js:11.1.2 \
41+
--failOnErrors --dangerfile ${{ runner.temp }}/dangerfile.js

.github/workflows/updater-scripts-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This isn't a reusable workflow but an actual CI action for this repo itself - to test scripts.
2-
name: Script Tests
2+
name: Updater Script Tests
33

44
on:
55
push:

.github/workflows/updater-workflow-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This isn't a reusable workflow but an actual CI action for this repo itself - to test the workflows.
2-
name: Workflow Tests
2+
name: Updater Workflow Tests
33

44
on:
55
push:

.github/workflows/versioning.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Keep the major version tags up-to-date
1+
name: Sync tags with releases
22

33
on:
44
release:

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.0
6+
7+
### Features
8+
9+
- New reusable workflow, `danger.yml`, to check Pull Requests with predefined rules ([#34](https://github.com/getsentry/github-workflows/pull/34))
10+
511
## 2.0.0
612

713
### Changes

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ jobs:
7777

7878
* `api-token`: GH authentication token to create PRs with & push.
7979
If you provide the usual `${{ github.token }}`, no followup CI will run on the created PR.
80-
If you want CI to run on the PRs created by the Updater, you need to provide custom user-specific auth token.
80+
If you want CI to run on the PRs created by the Updater, you need to provide custom user-specific auth token.
81+
82+
## Danger
83+
84+
Runs DangerJS on Pull Reqeusts in your repository. This uses custom set of rules defined in [this dangerfile](danger/dangerfile.js).
85+
86+
```yaml
87+
name: Danger
88+
89+
on:
90+
pull_request:
91+
types: [opened, synchronize, reopened, edited, ready_for_review]
92+
93+
jobs:
94+
danger:
95+
uses: getsentry/github-workflows/.github/workflows/danger.yml@v2
96+
```

danger/dangerfile.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// e.g. "feat" if PR title is "Feat : add more useful stuff"
2+
// or "ci" if PR branch is "ci/update-danger"
3+
function getPrFlavor() {
4+
if (danger.github && danger.github.pr) {
5+
var separator = undefined;
6+
if (danger.github.pr.title) {
7+
const parts = danger.github.pr.title.split(":");
8+
if (parts.length > 1) {
9+
return parts[0].toLowerCase().trim();
10+
}
11+
}
12+
if (danger.github.pr.head && danger.github.pr.head.ref) {
13+
const parts = danger.github.pr.head.ref.split("/");
14+
if (parts.length > 1) {
15+
return parts[0].toLowerCase();
16+
}
17+
}
18+
}
19+
return "";
20+
}
21+
22+
async function checkDocs() {
23+
if (getPrFlavor().startsWith("feat")) {
24+
message(
25+
'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.'
26+
);
27+
}
28+
}
29+
30+
async function checkChangelog() {
31+
const changelogFile = "CHANGELOG.md";
32+
33+
// 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+
}
41+
}
42+
43+
// Check if current PR has an entry in changelog
44+
const changelogContents = await danger.github.utils.fileContents(
45+
changelogFile
46+
);
47+
48+
const hasChangelogEntry = RegExp(`#${danger.github.pr.number}\\b`).test(
49+
changelogContents
50+
);
51+
52+
if (hasChangelogEntry) {
53+
return;
54+
}
55+
56+
// Report missing changelog entry
57+
fail(
58+
"Please consider adding a changelog entry for the next release.",
59+
changelogFile
60+
);
61+
62+
const prTitleFormatted = danger.github.pr.title
63+
.split(": ")
64+
.slice(-1)[0]
65+
.trim()
66+
.replace(/\.+$/, "");
67+
68+
markdown(
69+
`
70+
### Instructions and example for changelog
71+
72+
Please add an entry to \`CHANGELOG.md\` to the "Unreleased" section. Make sure the entry includes this PR's number.
73+
74+
Example:
75+
76+
\`\`\`markdown
77+
## Unreleased
78+
79+
- ${prTitleFormatted} ([#${danger.github.pr.number}](${danger.github.pr.html_url}))
80+
\`\`\`
81+
82+
If none of the above apply, you can opt out of this check by adding \`#skip-changelog\` to the PR description.`.trim()
83+
);
84+
}
85+
86+
async function checkAll() {
87+
await checkDocs();
88+
await checkChangelog();
89+
}
90+
91+
schedule(checkAll);

0 commit comments

Comments
 (0)