11import chalk from 'chalk'
2- // @ts -ignore
3- import chalkTable from 'chalk-table'
42import meow from 'meow'
53import ora from 'ora'
4+ import fetch from 'node-fetch'
65
76import { outputFlags } from '../../flags'
8- import {
9- handleApiCall ,
10- handleUnsuccessfulApiResponse
11- } from '../../utils/api-helpers'
7+ // import {
8+ // handleApiCall,
9+ // handleUnsuccessfulApiResponse
10+ // } from '../../utils/api-helpers'
1211import { printFlagList } from '../../utils/formatting'
13- import { getDefaultKey , setupSdk } from '../../utils/sdk'
12+ import { getDefaultKey } from '../../utils/sdk'
1413
1514import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1615import type { Ora } from 'ora'
@@ -26,65 +25,42 @@ export const get: CliSubcommand = {
2625 if ( ! apiKey ) {
2726 throw new AuthError ( "User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key." )
2827 }
29- const spinnerText = 'Listing scans ... \n'
28+ const spinnerText = 'Getting diff scan ... \n'
3029 const spinner = ora ( spinnerText ) . start ( )
31- await listOrgFullScan ( input . orgSlug , input , spinner , apiKey )
30+ await getDiffScan ( input . before , input . after , spinner , apiKey )
3231 }
3332 }
3433}
3534
36- const listFullScanFlags : { [ key : string ] : any } = {
37- sort : {
35+ const getDiffScanFlags : { [ key : string ] : any } = {
36+ before : {
3837 type : 'string' ,
39- shortFlag : 's' ,
40- default : 'created_at' ,
41- description :
42- 'Sorting option (`name` or `created_at`) - default is `created_at`'
38+ shortFlag : 'b' ,
39+ default : '' ,
40+ description : 'The full scan ID of the base scan'
4341 } ,
44- direction : {
42+ after : {
4543 type : 'string' ,
46- shortFlag : 'd' ,
47- default : 'desc' ,
48- description : 'Direction option (`desc` or `asc`) - Default is `desc`'
49- } ,
50- perPage : {
51- type : 'number' ,
52- shortFlag : 'pp' ,
53- default : 30 ,
54- description : 'Results per page - Default is 30'
44+ shortFlag : 'a' ,
45+ default : '' ,
46+ description : 'The full scan ID of the head scan'
5547 } ,
56- page : {
57- type : 'number ' ,
48+ preview : {
49+ type : 'boolean ' ,
5850 shortFlag : 'p' ,
59- default : 1 ,
60- description : 'Page number - Default is 1 '
51+ default : true ,
52+ description : 'A boolean flag to persist or not the diff scan result '
6153 } ,
62- fromTime : {
63- type : 'string' ,
64- shortFlag : 'f' ,
65- default : '' ,
66- description : 'From time - as a unix timestamp'
67- } ,
68- untilTime : {
69- type : 'string' ,
70- shortFlag : 'u' ,
71- default : '' ,
72- description : 'Until time - as a unix timestamp'
73- }
7454}
7555
7656// Internal functions
7757
7858type CommandContext = {
7959 outputJson : boolean
8060 outputMarkdown : boolean
81- orgSlug : string
82- sort : string
83- direction : string
84- per_page : number
85- page : number
86- from_time : string
87- until_time : string
61+ before : string
62+ after : string
63+ preview : boolean
8864}
8965
9066function setupCommand (
@@ -95,19 +71,19 @@ function setupCommand(
9571) : CommandContext | undefined {
9672 const flags : { [ key : string ] : any } = {
9773 ...outputFlags ,
98- ...listFullScanFlags
74+ ...getDiffScanFlags
9975 }
10076
10177 const cli = meow (
10278 `
10379 Usage
104- $ ${ name } <org slug>
80+ $ ${ name }
10581
10682 Options
10783 ${ printFlagList ( flags , 6 ) }
10884
10985 Examples
110- $ ${ name } FakeOrg
86+ $ ${ name }
11187 ` ,
11288 {
11389 argv,
@@ -120,80 +96,57 @@ function setupCommand(
12096 const {
12197 json : outputJson ,
12298 markdown : outputMarkdown ,
123- sort,
124- direction,
125- perPage,
126- page,
127- fromTime,
128- untilTime
99+ before,
100+ after,
101+ preview,
129102 } = cli . flags
130103
131- if ( ! cli . input [ 0 ] ) {
104+ if ( ! before || ! after ) {
132105 console . error (
133- `${ chalk . bgRed ( 'Input error' ) } : Please specify an organization slug .\n`
106+ `${ chalk . bgRed ( 'Input error' ) } : Please specify a before and after full scan ID .\n`
134107 )
135108 cli . showHelp ( )
136109 return
137110 }
138111
139- const { 0 : orgSlug = '' } = cli . input
140-
141112 return < CommandContext > {
142113 outputJson,
143114 outputMarkdown,
144- orgSlug,
145- sort,
146- direction,
147- per_page : perPage ,
148- page,
149- from_time : fromTime ,
150- until_time : untilTime
115+ before,
116+ after,
117+ preview
151118 }
152119}
153120
154- async function listOrgFullScan (
155- orgSlug : string ,
156- input : CommandContext ,
121+ async function getDiffScan (
122+ before : string ,
123+ after : string ,
157124 spinner : Ora ,
158125 apiKey : string
159126) : Promise < void > {
160- const socketSdk = await setupSdk ( apiKey )
161- const result = await handleApiCall (
162- socketSdk . getOrgFullScanList ( orgSlug , input ) ,
163- 'Listing scans'
164- )
127+ // const socketSdk = await setupSdk(apiKey)
128+ // const result = await handleApiCall(
129+ // socketSdk.getOrgFullScanList(orgSlug, input),
130+ // 'Listing scans'
131+ // )
132+
133+ const response = await fetch ( `https://api.socket.dev/v0/orgs/SocketDev/full-scans/diff?before=${ before } &after=${ after } &preview` , {
134+ method : 'GET' ,
135+ headers : {
136+ 'Authorization' : 'Basic ' + btoa ( `${ apiKey } :${ apiKey } ` )
137+ }
138+ } ) ;
139+ const data = await response . json ( ) ;
165140
166- if ( ! result . success ) {
167- handleUnsuccessfulApiResponse ( 'getOrgFullScanList' , result , spinner )
168- return
169- }
141+ // if (!result.success) {
142+ // handleUnsuccessfulApiResponse('getOrgFullScanList', result, spinner)
143+ // return
144+ // }
170145 spinner . stop ( )
171146
172- console . log ( `\n Listing scans for: ${ orgSlug } \n` )
173-
174- const options = {
175- columns : [
176- { field : 'id' , name : chalk . magenta ( 'ID' ) } ,
177- { field : 'report_url' , name : chalk . magenta ( 'Scan URL' ) } ,
178- { field : 'branch' , name : chalk . magenta ( 'Branch' ) } ,
179- { field : 'created_at' , name : chalk . magenta ( 'Created at' ) }
180- ]
181- }
182-
183- const formattedResults = result . data . results . map ( d => {
184- return {
185- id : d . id ,
186- report_url : chalk . underline ( `${ d . html_report_url } ` ) ,
187- created_at : d . created_at
188- ? new Date ( d . created_at ) . toLocaleDateString ( 'en-us' , {
189- year : 'numeric' ,
190- month : 'numeric' ,
191- day : 'numeric'
192- } )
193- : '' ,
194- branch : d . branch
195- }
196- } )
147+ // before: dfc4cf0c-aefd-4081-9e4e-7385257f26e2
148+ // after: 922e45f5-8a7b-4b16-95a5-e98ad00470f1
197149
198- console . log ( `${ chalkTable ( options , formattedResults ) } \n` )
150+ console . log ( `\n Diff scan result: \n` )
151+ console . log ( data ) ;
199152}
0 commit comments