Skip to content

Commit cfc29cb

Browse files
authored
Merge pull request #42 from github/juxtin/sha-from-context
Get the right SHA for the snapshot depending on the event type
2 parents 051fb9a + 00b48ea commit cfc29cb

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-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: 77 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,17 @@ 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'
25+
context.eventName = 'push'
2526

2627
describe('Snapshot', () => {
2728
it('renders expected JSON', () => {
2829
const snapshot = new Snapshot(
29-
{
30-
name: 'test detector',
31-
url: 'https://github.com/github/dependency-submission-toolkit',
32-
version: '0.0.1'
33-
},
30+
exampleDetector,
3431
context,
35-
{ id: '42', correlator: 'test' },
36-
new Date('2022-06-04T05:07:06.457Z')
32+
exampleJob,
33+
exampleDate
3734
)
3835
snapshot.addManifest(manifest)
3936
expect(roundTripJSON(snapshot)).toEqual({
@@ -49,7 +46,7 @@ describe('Snapshot', () => {
4946
},
5047
ref: 'foo/bar/baz',
5148
scanned: '2022-06-04T05:07:06.457Z',
52-
sha: '0000000000000000000000000000000000000000',
49+
sha: '1000000000000000000000000000000000000000',
5350
manifests: {
5451
test: {
5552
resolved: {
@@ -73,4 +70,74 @@ describe('Snapshot', () => {
7370
}
7471
})
7572
})
73+
74+
it('gets the correct sha from the context when given a pull request', () => {
75+
const prContext = context
76+
const expectedSha = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2'
77+
prContext.eventName = 'pull_request'
78+
prContext.payload.pull_request = {
79+
number: 1,
80+
head: {
81+
sha: expectedSha
82+
}
83+
}
84+
85+
const snapshot = new Snapshot(
86+
exampleDetector,
87+
prContext,
88+
exampleJob,
89+
exampleDate
90+
)
91+
92+
expect(snapshot.sha).toEqual(expectedSha)
93+
})
7694
})
95+
96+
describe('shaFromContext', () => {
97+
it('gets the right sha from the context when given a pull_request event', () => {
98+
const expectedSha = '1234567890123456789012345678901234567890'
99+
const prContext = context
100+
prContext.eventName = 'pull_request'
101+
prContext.payload.pull_request = {
102+
number: 1,
103+
head: {
104+
sha: expectedSha
105+
}
106+
}
107+
expect(shaFromContext(prContext)).toEqual(expectedSha)
108+
})
109+
110+
it('gets the right sha from the context when given a pull_request_review event', () => {
111+
const expectedSha = 'abcdef1234567890123456789012345678901234'
112+
const prReviewContext = context
113+
prReviewContext.eventName = 'pull_request_review'
114+
prReviewContext.payload.pull_request = {
115+
number: 1,
116+
head: {
117+
sha: expectedSha
118+
}
119+
}
120+
expect(shaFromContext(prReviewContext)).toEqual(expectedSha)
121+
})
122+
123+
it('uses the primary sha from the context when given a push event', () => {
124+
const expectedSha = 'def1234567890123456789012345678901234567'
125+
const pushContext = context
126+
pushContext.eventName = 'push'
127+
pushContext.sha = expectedSha
128+
expect(shaFromContext(pushContext)).toEqual(expectedSha)
129+
})
130+
})
131+
132+
const exampleDetector = {
133+
name: 'test detector',
134+
url: 'https://github.com/github/dependency-submission-toolkit',
135+
version: '0.0.1'
136+
}
137+
138+
const exampleJob = {
139+
id: '42',
140+
correlator: 'test'
141+
}
142+
143+
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)