-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcmd-analytics.ts
More file actions
162 lines (144 loc) · 3.94 KB
/
cmd-analytics.ts
File metadata and controls
162 lines (144 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import assert from 'node:assert'
import { logger } from '@socketsecurity/registry/lib/logger'
import { displayAnalytics } from './display-analytics'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { handleBadInput } from '../../utils/handle-bad-input'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk'
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
const { DRY_RUN_BAIL_TEXT } = constants
const config: CliCommandConfig = {
commandName: 'analytics',
description: `Look up analytics data`,
hidden: false,
flags: {
...commonFlags,
...outputFlags,
file: {
type: 'string',
shortFlag: 'f',
default: '-',
description:
'Path to a local file to save the output. Only valid with --json/--markdown. Defaults to stdout.'
},
repo: {
type: 'string',
shortFlag: 'r',
default: '',
description: 'Name of the repository. Only valid when scope=repo'
},
scope: {
type: 'string',
shortFlag: 's',
default: 'org',
description:
"Scope of the analytics data - either 'org' or 'repo', default: org"
},
time: {
type: 'number',
shortFlag: 't',
default: 7,
description: 'Time filter - either 7, 30 or 90, default: 7'
}
},
help: (command, { flags }) => `
Usage
$ ${command} --scope=<scope> --time=<time filter>
Default parameters are set to show the organization-level analytics over the
last 7 days.
Options
${getFlagListOutput(flags, 6)}
Examples
$ ${command} --scope=org --time=7
$ ${command} --scope=org --time=30
$ ${command} --scope=repo --repo=test-repo --time=30
`
}
export const cmdAnalytics = {
description: config.description,
hidden: config.hidden,
run: run
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})
const { file, json, markdown, repo, scope, time } = cli.flags
const apiToken = getDefaultToken()
const wasBadInput = handleBadInput(
{
test: scope === 'org' || scope === 'repo',
message: 'Scope must be "repo" or "org"',
pass: 'ok',
fail: 'bad'
},
{
test: time === 7 || time === 30 || time === 90,
message: 'The time filter must either be 7, 30 or 90',
pass: 'ok',
fail: 'bad'
},
{
nook: true,
test: scope === 'org' || repo,
message: 'When scope=repo, repo name should be set through --repo',
pass: 'ok',
fail: 'missing'
},
{
nook: true,
test: file === '-' || json || markdown,
message:
'The `--file` flag is only valid when using `--json` or `--markdown`',
pass: 'ok',
fail: 'bad'
},
{
nook: true,
test: !json || !markdown,
message:
'The `--json` and `--markdown` flags can not be used at the same time',
pass: 'ok',
fail: 'bad'
},
{
nook: true,
test: apiToken,
message:
'You need to be logged in to use this command. See `socket login`.',
pass: 'ok',
fail: 'missing API token'
}
)
if (wasBadInput) {
return
}
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}
assert(assertScope(scope))
assert(assertTime(time))
return await displayAnalytics({
scope,
time,
repo: String(repo || ''),
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
filePath: String(file || '')
})
}
function assertScope(scope: unknown): scope is 'org' | 'repo' {
return scope === 'org' || scope === 'repo'
}
function assertTime(time: unknown): time is 7 | 30 | 90 {
return time === 7 || time === 30 || time === 90
}