Skip to content

Commit aac9829

Browse files
branch cleanup
1 parent 15e4b10 commit aac9829

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

danger/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ jobs:
2929
* type: string
3030
* required: false
3131
* default: `${{ github.token }}`
32+
* extra-dangerfile: Path to an additional dangerfile to run custom checks.
33+
* extra-install-packages: Additional packages that are required by the extra-dangerfile, you can find a list of packages here: https://packages.debian.org/search?suite=bookworm&keywords=curl.
3234

3335
## Outputs
3436

danger/action.yml

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ inputs:
77
description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}'
88
required: false
99
default: ${{ github.token }}
10+
extra-dangerfile:
11+
description: 'Path to additional dangerfile to run after the main checks'
12+
type: string
13+
required: false
14+
extra-install-packages:
15+
description: 'Additional apt packages to install in the DangerJS container (space-separated package names)'
16+
type: string
17+
required: false
1018

1119
outputs:
1220
outcome:
@@ -22,12 +30,31 @@ runs:
2230
token: ${{ inputs.api-token }}
2331
fetch-depth: 0
2432

33+
# Read the Danger version from the properties file
34+
- name: Get Danger version
35+
id: config
36+
shell: pwsh
37+
run: Get-Content '${{ github.action_path }}/danger.properties' | Tee-Object $env:GITHUB_OUTPUT -Append
38+
39+
# Validate extra-install-packages to prevent code injection
40+
- name: Validate package names
41+
if: ${{ inputs.extra-install-packages }}
42+
shell: bash
43+
run: |
44+
packages="${{ inputs.extra-install-packages }}"
45+
# Only allow alphanumeric characters, hyphens, periods, plus signs, underscores, and spaces
46+
if ! echo "$packages" | grep -E '^[a-zA-Z0-9._+-]+( [a-zA-Z0-9._+-]+)*$' > /dev/null; then
47+
echo "::error::Invalid package names in extra-install-packages. Only alphanumeric characters, hyphens, periods, plus signs, underscores, and spaces are allowed."
48+
exit 1
49+
fi
50+
2551
# Using a pre-built docker image in GitHub container registry instead of NPM to reduce possible attack vectors.
26-
- name: Run DangerJS
27-
id: danger
52+
- name: Setup container
2853
shell: bash
2954
run: |
30-
docker run \
55+
# Start a detached container with all necessary volumes and environment variables
56+
docker run -td --name danger \
57+
--entrypoint /bin/bash \
3158
--volume ${{ github.workspace }}:/github/workspace \
3259
--volume ${{ github.action_path }}:${{ github.action_path }} \
3360
--volume ${{ github.event_path }}:${{ github.event_path }} \
@@ -36,5 +63,22 @@ runs:
3663
-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 \
3764
-e GITHUB_TOKEN="${{ inputs.api-token }}" \
3865
-e DANGER_DISABLE_TRANSPILATION="true" \
39-
ghcr.io/danger/danger-js:11.3.1 \
40-
--failOnErrors --dangerfile ${{ github.action_path }}/dangerfile.js
66+
-e EXTRA_DANGERFILE_INPUT="${{ inputs.extra-dangerfile }}" \
67+
ghcr.io/danger/danger-js:${{ steps.config.outputs.version }} \
68+
-c "sleep infinity"
69+
70+
- name: Setup additional packages
71+
if: ${{ inputs.extra-install-packages }}
72+
shell: bash
73+
run: |
74+
docker exec --user root danger apt-get update
75+
echo "Installing packages: ${{ inputs.extra-install-packages }}"
76+
docker exec --user root danger sh -c "apt-get install -y ${{ inputs.extra-install-packages }}"
77+
echo "All additional packages installed successfully."
78+
79+
- name: Run DangerJS
80+
id: danger
81+
shell: bash
82+
run: |
83+
trap "docker rm -f danger || true" EXIT
84+
docker exec --user $(id -u) danger danger ci --fail-on-errors --dangerfile ${{ github.action_path }}/dangerfile.js

danger/dangerfile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { getFlavorConfig, extractPRFlavor } = require('./dangerfile-utils.js');
2+
const fs = require('fs');
23

34
const headRepoName = danger.github.pr.head.repo.git_url;
45
const baseRepoName = danger.github.pr.base.repo.git_url;
@@ -186,10 +187,36 @@ async function checkActionsArePinned() {
186187
}
187188
}
188189

190+
async function CheckFromExternalChecks() {
191+
// Get the external dangerfile path from environment variable (passed via workflow input)
192+
// Priority: EXTRA_DANGERFILE (absolute path) -> EXTRA_DANGERFILE_INPUT (relative path)
193+
const customPath = process.env.EXTRA_DANGERFILE || process.env.EXTRA_DANGERFILE_INPUT;
194+
console.log(`::debug:: Checking from external checks: ${customPath}`);
195+
if (customPath) {
196+
try {
197+
const extraModule = require(`/github/workspace/${customPath}`);
198+
await extraModule({
199+
fail: fail,
200+
warn: warn,
201+
message: message,
202+
markdown: markdown,
203+
danger: danger,
204+
});
205+
} catch (err) {
206+
if (err.message && err.message.includes('Cannot use import statement outside a module')) {
207+
warn(`External dangerfile uses ES6 imports. Please convert to CommonJS syntax (require/module.exports) or use .mjs extension with proper module configuration.\nFile: ${customPath}`);
208+
} else {
209+
warn(`Could not load custom Dangerfile: ${customPath}\n${err}`);
210+
}
211+
}
212+
}
213+
}
214+
189215
async function checkAll() {
190216
await checkDocs();
191217
await checkChangelog();
192218
await checkActionsArePinned();
219+
await CheckFromExternalChecks();
193220
}
194221

195222
schedule(checkAll);

0 commit comments

Comments
 (0)