@@ -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,17 @@ 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
+ context . eventName = 'push'
25
26
26
27
describe ( 'Snapshot' , ( ) => {
27
28
it ( 'renders expected JSON' , ( ) => {
28
29
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 ,
34
31
context ,
35
- { id : '42' , correlator : 'test' } ,
36
- new Date ( '2022-06-04T05:07:06.457Z' )
32
+ exampleJob ,
33
+ exampleDate
37
34
)
38
35
snapshot . addManifest ( manifest )
39
36
expect ( roundTripJSON ( snapshot ) ) . toEqual ( {
@@ -49,7 +46,7 @@ describe('Snapshot', () => {
49
46
} ,
50
47
ref : 'foo/bar/baz' ,
51
48
scanned : '2022-06-04T05:07:06.457Z' ,
52
- sha : '0000000000000000000000000000000000000000 ' ,
49
+ sha : '1000000000000000000000000000000000000000 ' ,
53
50
manifests : {
54
51
test : {
55
52
resolved : {
@@ -73,4 +70,74 @@ describe('Snapshot', () => {
73
70
}
74
71
} )
75
72
} )
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
+ } )
76
94
} )
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' )
0 commit comments