Skip to content

Commit daafc1f

Browse files
CopilotTylerJDev
andauthored
Harden Dependabot workflow metadata handling
Co-authored-by: TylerJDev <26746305+TylerJDev@users.noreply.github.com>
1 parent a56b0ec commit daafc1f

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

.github/workflows/check-for-changeset.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
exit 0
2929
fi
3030
31-
changesets=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" --paginate --jq '.[].filename' | grep -E '^\.changeset/[^/]+\.md$' | grep -Ev '^\.changeset/(README\.md|dependabot-[0-9]+\.md)$' || true)
31+
changesets=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" --paginate --jq '.[].filename' | grep -E '^\.changeset/[^/]+\.md$' | grep -Ev '^\.changeset/(README\.md|dependabot-[0-9]+\.md|auto-release-[^.]+\.md)$' || true)
3232
if [ -n "$changesets" ]; then
3333
echo 'The ".changeset/*.md" file pattern matched the changed files of the pull request.'
3434
exit 0

.github/workflows/dependabot-changeset.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ jobs:
5656
run: node scripts/dependabot-changeset.mjs
5757
env:
5858
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
59-
DEPENDABOT_METADATA_JSON: ${{ toJson(steps.metadata.outputs) }}
59+
DEPENDABOT_DEPENDENCY_NAMES: ${{ steps.metadata.outputs.dependency-names }}
60+
DEPENDABOT_DEPENDENCY_TYPE: ${{ steps.metadata.outputs.dependency-type }}
61+
DEPENDABOT_UPDATE_TYPE: ${{ steps.metadata.outputs.update-type }}
62+
DEPENDABOT_UPDATED_DEPENDENCIES_JSON: ${{ steps.metadata.outputs.updated-dependencies-json }}
63+
DEPENDABOT_ALERT_STATE: ${{ steps.metadata.outputs.alert-state }}
64+
DEPENDABOT_GHSA_ID: ${{ steps.metadata.outputs.ghsa-id }}
65+
DEPENDABOT_CVSS: ${{ steps.metadata.outputs.cvss }}

scripts/dependabot-changeset.mjs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,23 @@ export async function applyDependabotChangeset({event, metadata, token}) {
190190
return {action: existing ? 'updated' : 'created', path: changesetPath, policy}
191191
}
192192

193+
function metadataFromEnv(env) {
194+
if (env.DEPENDABOT_METADATA_JSON) return JSON.parse(env.DEPENDABOT_METADATA_JSON)
195+
196+
return {
197+
'dependency-names': env.DEPENDABOT_DEPENDENCY_NAMES ?? '',
198+
'dependency-type': env.DEPENDABOT_DEPENDENCY_TYPE ?? '',
199+
'update-type': env.DEPENDABOT_UPDATE_TYPE ?? '',
200+
'updated-dependencies-json': env.DEPENDABOT_UPDATED_DEPENDENCIES_JSON ?? '',
201+
'alert-state': env.DEPENDABOT_ALERT_STATE ?? '',
202+
'ghsa-id': env.DEPENDABOT_GHSA_ID ?? '',
203+
cvss: env.DEPENDABOT_CVSS ?? '',
204+
}
205+
}
206+
193207
if (import.meta.url === `file://${process.argv[1]}`) {
194208
const event = JSON.parse(process.env.GITHUB_EVENT_JSON ?? readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
195-
const metadata = JSON.parse(process.env.DEPENDABOT_METADATA_JSON)
209+
const metadata = metadataFromEnv(process.env)
196210
const token = process.env.GITHUB_TOKEN
197211
if (!token) throw new Error('GITHUB_TOKEN is required.')
198212

0 commit comments

Comments
 (0)