@@ -2,7 +2,7 @@ import { context } from '@actions/github'
2
2
3
3
import { Manifest } from './manifest'
4
4
import { PackageCache } from './package-cache'
5
- import { Snapshot } from './snapshot'
5
+ import { shaFromContext , Snapshot } from './snapshot'
6
6
7
7
function roundTripJSON ( obj : any ) : object {
8
8
return JSON . parse ( JSON . stringify ( obj ) )
@@ -20,20 +20,16 @@ manifest.addDirectDependency(
20
20
manifest . addIndirectDependency ( cache . package ( 'pkg:npm/%40actions/[email protected] ' ) )
21
21
22
22
// add bogus git data to the context
23
- context . sha = '0000000000000000000000000000000000000000 '
23
+ context . sha = '1000000000000000000000000000000000000000 '
24
24
context . ref = 'foo/bar/baz'
25
25
26
26
describe ( 'Snapshot' , ( ) => {
27
27
it ( 'renders expected JSON' , ( ) => {
28
28
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 ,
34
30
context ,
35
- { id : '42' , correlator : 'test' } ,
36
- new Date ( '2022-06-04T05:07:06.457Z' )
31
+ exampleJob ,
32
+ exampleDate
37
33
)
38
34
snapshot . addManifest ( manifest )
39
35
expect ( roundTripJSON ( snapshot ) ) . toEqual ( {
@@ -49,7 +45,7 @@ describe('Snapshot', () => {
49
45
} ,
50
46
ref : 'foo/bar/baz' ,
51
47
scanned : '2022-06-04T05:07:06.457Z' ,
52
- sha : '0000000000000000000000000000000000000000 ' ,
48
+ sha : '1000000000000000000000000000000000000000 ' ,
53
49
manifests : {
54
50
test : {
55
51
resolved : {
@@ -73,4 +69,74 @@ describe('Snapshot', () => {
73
69
}
74
70
} )
75
71
} )
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
+ } )
76
93
} )
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' )
0 commit comments