Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pull_request.head.sha for GitHub dependency graph submissions where appropriate #883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,858 changes: 1,938 additions & 1,920 deletions dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

3,858 changes: 1,938 additions & 1,920 deletions dist/post/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
"@actions/http-client": "2.1.1",
"@actions/tool-cache": "2.0.1",
"@octokit/rest": "19.0.13",
"@octokit/webhooks-types": "^7.3.0",
"string-argv": "0.3.2"
},
"devDependencies": {
"@types/node": "16.18.38",
"@types/jest": "29.5.4",
"@types/node": "16.18.38",
"@types/unzipper": "0.10.6",
"@typescript-eslint/parser": "6.4.1",
"@vercel/ncc": "0.36.1",
Expand All @@ -52,7 +53,7 @@
"eslint-plugin-jest": "27.2.3",
"eslint-plugin-prettier": "5.0.0",
"jest": "29.6.3",
"js-yaml": "4.1.0",
"js-yaml": "4.1.0",
"patch-package": "8.0.0",
"prettier": "3.0.2",
"ts-jest": "29.1.1",
Expand Down
22 changes: 22 additions & 0 deletions src/dependency-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as github from '@actions/github'
import * as glob from '@actions/glob'
import * as toolCache from '@actions/tool-cache'
import {Octokit} from '@octokit/rest'
import {Context} from '@actions/github/lib/context'
import type {PullRequestEvent} from '@octokit/webhooks-types'

import * as path from 'path'
import fs from 'fs'
Expand All @@ -20,9 +22,11 @@ export function setup(option: DependencyGraphOption): void {

core.info('Enabling dependency graph generation')
const jobCorrelator = getJobCorrelator()
const sha = shaFromContext(github.context)
core.exportVariable('GITHUB_DEPENDENCY_GRAPH_ENABLED', 'true')
core.exportVariable('GITHUB_JOB_CORRELATOR', jobCorrelator)
core.exportVariable('GITHUB_JOB_ID', github.context.runId)
core.exportVariable('GITHUB_SHA', sha)
core.exportVariable(
'DEPENDENCY_GRAPH_REPORT_DIR',
path.resolve(layout.workspaceDirectory(), 'dependency-graph-reports')
Expand Down Expand Up @@ -151,6 +155,24 @@ function getRelativePathFromWorkspace(file: string): string {
return path.relative(workspaceDirectory, file)
}

export function shaFromContext(context: Context): string {
const pullRequestEvents = [
'pull_request',
'pull_request_comment',
'pull_request_review',
'pull_request_review_comment'
// Note that pull_request_target is omitted here.
// That event runs in the context of the base commit of the PR,
// so the snapshot should not be associated with the head commit.
]
if (pullRequestEvents.includes(context.eventName)) {
const pr = (context.payload as PullRequestEvent).pull_request
return pr.head.sha
} else {
return context.sha
}
}

export function getJobCorrelator(): string {
return constructJobCorrelator(github.context.workflow, github.context.job, getJobMatrix())
}
Expand Down