@@ -2,7 +2,7 @@ import { context } from '@actions/github'
22
33import { Manifest } from './manifest'
44import { PackageCache } from './package-cache'
5- import { Snapshot } from './snapshot'
5+ import { shaFromContext , Snapshot } from './snapshot'
66
77function roundTripJSON ( obj : any ) : object {
88 return JSON . parse ( JSON . stringify ( obj ) )
@@ -20,20 +20,17 @@ manifest.addDirectDependency(
2020manifest . 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 '
2424context . ref = 'foo/bar/baz'
25+ context . eventName = 'push'
2526
2627describe ( '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' )
0 commit comments