1
1
import chalk from 'chalk'
2
- // @ts -ignore
3
- import chalkTable from 'chalk-table'
4
2
import meow from 'meow'
5
3
import ora from 'ora'
4
+ import fetch from 'node-fetch'
6
5
7
6
import { 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'
12
11
import { printFlagList } from '../../utils/formatting'
13
- import { getDefaultKey , setupSdk } from '../../utils/sdk'
12
+ import { getDefaultKey } from '../../utils/sdk'
14
13
15
14
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
16
15
import type { Ora } from 'ora'
@@ -26,65 +25,42 @@ export const get: CliSubcommand = {
26
25
if ( ! apiKey ) {
27
26
throw new AuthError ( "User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key." )
28
27
}
29
- const spinnerText = 'Listing scans ... \n'
28
+ const spinnerText = 'Getting diff scan ... \n'
30
29
const spinner = ora ( spinnerText ) . start ( )
31
- await listOrgFullScan ( input . orgSlug , input , spinner , apiKey )
30
+ await getDiffScan ( input . before , input . after , spinner , apiKey )
32
31
}
33
32
}
34
33
}
35
34
36
- const listFullScanFlags : { [ key : string ] : any } = {
37
- sort : {
35
+ const getDiffScanFlags : { [ key : string ] : any } = {
36
+ before : {
38
37
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'
43
41
} ,
44
- direction : {
42
+ after : {
45
43
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'
55
47
} ,
56
- page : {
57
- type : 'number ' ,
48
+ preview : {
49
+ type : 'boolean ' ,
58
50
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 '
61
53
} ,
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
- }
74
54
}
75
55
76
56
// Internal functions
77
57
78
58
type CommandContext = {
79
59
outputJson : boolean
80
60
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
88
64
}
89
65
90
66
function setupCommand (
@@ -95,19 +71,19 @@ function setupCommand(
95
71
) : CommandContext | undefined {
96
72
const flags : { [ key : string ] : any } = {
97
73
...outputFlags ,
98
- ...listFullScanFlags
74
+ ...getDiffScanFlags
99
75
}
100
76
101
77
const cli = meow (
102
78
`
103
79
Usage
104
- $ ${ name } <org slug>
80
+ $ ${ name }
105
81
106
82
Options
107
83
${ printFlagList ( flags , 6 ) }
108
84
109
85
Examples
110
- $ ${ name } FakeOrg
86
+ $ ${ name }
111
87
` ,
112
88
{
113
89
argv,
@@ -120,80 +96,57 @@ function setupCommand(
120
96
const {
121
97
json : outputJson ,
122
98
markdown : outputMarkdown ,
123
- sort,
124
- direction,
125
- perPage,
126
- page,
127
- fromTime,
128
- untilTime
99
+ before,
100
+ after,
101
+ preview,
129
102
} = cli . flags
130
103
131
- if ( ! cli . input [ 0 ] ) {
104
+ if ( ! before || ! after ) {
132
105
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`
134
107
)
135
108
cli . showHelp ( )
136
109
return
137
110
}
138
111
139
- const { 0 : orgSlug = '' } = cli . input
140
-
141
112
return < CommandContext > {
142
113
outputJson,
143
114
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
151
118
}
152
119
}
153
120
154
- async function listOrgFullScan (
155
- orgSlug : string ,
156
- input : CommandContext ,
121
+ async function getDiffScan (
122
+ before : string ,
123
+ after : string ,
157
124
spinner : Ora ,
158
125
apiKey : string
159
126
) : 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 ( ) ;
165
140
166
- if ( ! result . success ) {
167
- handleUnsuccessfulApiResponse ( 'getOrgFullScanList' , result , spinner )
168
- return
169
- }
141
+ // if (!result.success) {
142
+ // handleUnsuccessfulApiResponse('getOrgFullScanList', result, spinner)
143
+ // return
144
+ // }
170
145
spinner . stop ( )
171
146
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
197
149
198
- console . log ( `${ chalkTable ( options , formattedResults ) } \n` )
150
+ console . log ( `\n Diff scan result: \n` )
151
+ console . log ( data ) ;
199
152
}
0 commit comments