Skip to content

Commit f74b035

Browse files
authored
SHA for snapshot depends on the event type
1 parent 051fb9a commit f74b035

File tree

4 files changed

+113
-11
lines changed

4 files changed

+113
-11
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@actions/exec": "^1.1.1",
3939
"@actions/github": "^5.0.0",
4040
"@octokit/rest": "^18.12.0",
41+
"@octokit/webhooks-types": "^6.10.0",
4142
"openapi-typescript": "^5.2.0",
4243
"packageurl-js": "0.0.6"
4344
},

src/snapshot.test.ts

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { context } from '@actions/github'
22

33
import { Manifest } from './manifest'
44
import { PackageCache } from './package-cache'
5-
import { Snapshot } from './snapshot'
5+
import { shaFromContext, Snapshot } from './snapshot'
66

77
function roundTripJSON(obj: any): object {
88
return JSON.parse(JSON.stringify(obj))
@@ -20,20 +20,16 @@ manifest.addDirectDependency(
2020
manifest.addIndirectDependency(cache.package('pkg:npm/%40actions/[email protected]'))
2121

2222
// add bogus git data to the context
23-
context.sha = '0000000000000000000000000000000000000000'
23+
context.sha = '1000000000000000000000000000000000000000'
2424
context.ref = 'foo/bar/baz'
2525

2626
describe('Snapshot', () => {
2727
it('renders expected JSON', () => {
2828
const snapshot = new Snapshot(
29-
{
30-
name: 'test detector',
31-
url: 'https://github.com/github/dependency-submission-toolkit',
32-
version: '0.0.1'
33-
},
29+
exampleDetector,
3430
context,
35-
{ id: '42', correlator: 'test' },
36-
new Date('2022-06-04T05:07:06.457Z')
31+
exampleJob,
32+
exampleDate
3733
)
3834
snapshot.addManifest(manifest)
3935
expect(roundTripJSON(snapshot)).toEqual({
@@ -49,7 +45,7 @@ describe('Snapshot', () => {
4945
},
5046
ref: 'foo/bar/baz',
5147
scanned: '2022-06-04T05:07:06.457Z',
52-
sha: '0000000000000000000000000000000000000000',
48+
sha: '1000000000000000000000000000000000000000',
5349
manifests: {
5450
test: {
5551
resolved: {
@@ -73,4 +69,74 @@ describe('Snapshot', () => {
7369
}
7470
})
7571
})
72+
73+
it('gets the correct sha from the context when given a pull request', () => {
74+
const prContext = context
75+
const expectedSha = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2'
76+
prContext.eventName = 'pull_request'
77+
prContext.payload.pull_request = {
78+
number: 1,
79+
head: {
80+
sha: expectedSha
81+
}
82+
}
83+
84+
const snapshot = new Snapshot(
85+
exampleDetector,
86+
prContext,
87+
exampleJob,
88+
exampleDate
89+
)
90+
91+
expect(snapshot.sha).toEqual(expectedSha)
92+
})
7693
})
94+
95+
describe('shaFromContext', () => {
96+
it('gets the right sha from the context when given a pull_request event', () => {
97+
const expectedSha = '1234567890123456789012345678901234567890'
98+
const prContext = context
99+
prContext.eventName = 'pull_request'
100+
prContext.payload.pull_request = {
101+
number: 1,
102+
head: {
103+
sha: expectedSha
104+
}
105+
}
106+
expect(shaFromContext(prContext)).toEqual(expectedSha)
107+
})
108+
109+
it('gets the right sha from the context when given a pull_request_review event', () => {
110+
const expectedSha = 'abcdef1234567890123456789012345678901234'
111+
const prReviewContext = context
112+
prReviewContext.eventName = 'pull_request_review'
113+
prReviewContext.payload.pull_request = {
114+
number: 1,
115+
head: {
116+
sha: expectedSha
117+
}
118+
}
119+
expect(shaFromContext(prReviewContext)).toEqual(expectedSha)
120+
})
121+
122+
it('uses the primary sha from the context when given a push event', () => {
123+
const expectedSha = 'def1234567890123456789012345678901234567'
124+
const pushContext = context
125+
pushContext.eventName = 'push'
126+
pushContext.sha = expectedSha
127+
expect(shaFromContext(pushContext)).toEqual(expectedSha)
128+
})
129+
})
130+
131+
const exampleDetector = {
132+
name: 'test detector',
133+
url: 'https://github.com/github/dependency-submission-toolkit',
134+
version: '0.0.1'
135+
}
136+
137+
const exampleJob = {
138+
id: '42',
139+
correlator: 'test'
140+
}
141+
142+
const exampleDate = new Date('2022-06-04T05:07:06.457Z')

src/snapshot.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as core from '@actions/core'
33
import * as github from '@actions/github'
44
import { Octokit } from '@octokit/rest'
55
import { RequestError } from '@octokit/request-error'
6+
import { PullRequestEvent } from '@octokit/webhooks-types'
67

78
import { Manifest } from './manifest'
89

@@ -33,6 +34,34 @@ export function jobFromContext(context: Context): Job {
3334
}
3435
}
3536

37+
/**
38+
* shaFromContext returns the sha of the commit that triggered the action, or the head sha of the PR.
39+
*
40+
* See https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request for more details
41+
* about why this function is necessary, but the short reason is that GITHUB_SHA is _not_ necessarily the head sha
42+
* of the PR when the event is pull_request (or some other related event types).
43+
*
44+
* @param {Context} context
45+
* @returns {string}
46+
*/
47+
export function shaFromContext(context: Context): string {
48+
const pullRequestEvents = [
49+
'pull_request',
50+
'pull_request_comment',
51+
'pull_request_review',
52+
'pull_request_review_comment'
53+
// Note that pull_request_target is omitted here.
54+
// That event runs in the context of the base commit of the PR,
55+
// so the snapshot should not be associated with the head commit.
56+
]
57+
if (pullRequestEvents.includes(context.eventName)) {
58+
const pr = (context.payload as PullRequestEvent).pull_request
59+
return pr.head.sha
60+
} else {
61+
return context.sha
62+
}
63+
}
64+
3665
/**
3766
* Detector provides metadata details about the detector used to generate the snapshot
3867
*/
@@ -104,7 +133,7 @@ export class Snapshot {
104133
this.detector = detector
105134
this.version = version
106135
this.job = job || jobFromContext(context)
107-
this.sha = context.sha
136+
this.sha = shaFromContext(context)
108137
this.ref = context.ref
109138
this.scanned = date.toISOString()
110139
this.manifests = {}

0 commit comments

Comments
 (0)