Skip to content

Final fixes for v1 release #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,27 @@ import { logger } from '@socketsecurity/registry/lib/logger'

import { cmdAnalytics } from './commands/analytics/cmd-analytics.mts'
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log.mts'
import { cmdCdxgen } from './commands/cdxgen/cmd-cdxgen.mts'
import { cmdCI } from './commands/ci/cmd-ci.mts'
import { cmdConfig } from './commands/config/cmd-config.mts'
import { cmdScanCreate } from './commands/dependencies/cmd-dependencies.mts'
import { cmdDiffScan } from './commands/diff-scan/cmd-diff-scan.mts'
import { cmdFix } from './commands/fix/cmd-fix.mts'
import { cmdInfo } from './commands/info/cmd-info.mts'
import { cmdInstall } from './commands/install/cmd-install.mts'
import { cmdJson } from './commands/json/cmd-json.mts'
import { cmdLogin } from './commands/login/cmd-login.mts'
import { cmdLogout } from './commands/logout/cmd-logout.mts'
import { cmdManifestCdxgen } from './commands/manifest/cmd-manifest-cdxgen.mts'
import { cmdManifest } from './commands/manifest/cmd-manifest.mts'
import { cmdNpm } from './commands/npm/cmd-npm.mts'
import { cmdNpx } from './commands/npx/cmd-npx.mts'
import { cmdOops } from './commands/oops/cmd-oops.mts'
import { cmdOptimize } from './commands/optimize/cmd-optimize.mts'
import { cmdOrganizationDependencies } from './commands/organization/cmd-organization-dependencies.mts'
import { cmdOrganizationPolicyLicense } from './commands/organization/cmd-organization-policy-license.mts'
import { cmdOrganizationPolicyPolicy } from './commands/organization/cmd-organization-policy-security.mts'
import { cmdOrganizationPolicySecurity } from './commands/organization/cmd-organization-policy-security.mts'
import { cmdOrganization } from './commands/organization/cmd-organization.mts'
import { cmdPackage } from './commands/package/cmd-package.mts'
import { cmdRawNpm } from './commands/raw-npm/cmd-raw-npm.mts'
import { cmdRawNpx } from './commands/raw-npx/cmd-raw-npx.mts'
import { cmdReport } from './commands/report/cmd-report.mts'
import { cmdRepos } from './commands/repos/cmd-repos.mts'
import { cmdRepository } from './commands/repository/cmd-repository.mts'
import { cmdScan } from './commands/scan/cmd-scan.mts'
import { cmdThreatFeed } from './commands/threat-feed/cmd-threat-feed.mts'
import { cmdUninstall } from './commands/uninstall/cmd-uninstall.mts'
Expand Down Expand Up @@ -62,10 +59,8 @@ void (async () => {
await meowWithSubcommands(
{
ci: cmdCI,
cdxgen: cmdCdxgen,
config: cmdConfig,
fix: cmdFix,
info: cmdInfo,
install: cmdInstall,
json: cmdJson,
login: cmdLogin,
Expand All @@ -78,14 +73,11 @@ void (async () => {
package: cmdPackage,
'raw-npm': cmdRawNpm,
'raw-npx': cmdRawNpx,
report: cmdReport,
wrapper: cmdWrapper,
scan: cmdScan,
'audit-log': cmdAuditLog,
repos: cmdRepos,
dependencies: cmdScanCreate,
repos: cmdRepository,
analytics: cmdAnalytics,
'diff-scan': cmdDiffScan,
'threat-feed': cmdThreatFeed,
manifest: cmdManifest,
uninstall: cmdUninstall,
Expand All @@ -97,13 +89,28 @@ void (async () => {
hidden: true,
argv: ['audit-log'],
},
auditLog: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
auditLogs: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
['audit-logs']: {
description: cmdAuditLog.description,
hidden: true,
argv: ['audit-log'],
},
cdxgen: {
description: cmdManifestCdxgen.description,
hidden: true,
argv: ['manifest', 'cdxgen'],
},
deps: {
description: cmdScanCreate.description,
description: cmdOrganizationDependencies.description,
hidden: true,
argv: ['dependencies'],
},
Expand Down Expand Up @@ -148,22 +155,22 @@ void (async () => {
argv: ['package'],
},
repo: {
description: cmdRepos.description,
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
repository: {
description: cmdRepos.description,
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
repositories: {
description: cmdRepos.description,
description: cmdRepository.description,
hidden: true,
argv: ['repos'],
},
security: {
description: cmdOrganizationPolicyPolicy.description,
description: cmdOrganizationPolicySecurity.description,
hidden: true,
argv: ['organization', 'policy', 'security'],
},
Expand Down
117 changes: 32 additions & 85 deletions src/commands/analytics/cmd-analytics.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { handleAnalytics } from './handle-analytics.mts'
import constants from '../../constants.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { checkCommandInput } from '../../utils/check-input.mts'
import { isTestingV1 } from '../../utils/config.mts'
import { getOutputKind } from '../../utils/get-output-kind.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import { getFlagListOutput } from '../../utils/output-formatting.mts'
Expand All @@ -23,58 +22,32 @@ const config: CliCommandConfig = {
...outputFlags,
file: {
type: 'string',
shortFlag: 'f',
description:
'Filepath to save output when given. Only valid with --json/--markdown.',
},
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: 30,
description: 'Time filter - either 7, 30 or 90, default: 30',
description: 'Path to store result, only valid with --json/--markdown',
},
},
help: (command, { flags }) =>
`
Usage
$ ${command} ${isTestingV1() ? '[ org | repo <reponame>] [time]' : '--scope=<scope> --time=<time filter>'}
$ ${command} [options] [ "org" | "repo" <reponame>] [TIME]

API Token Requirements
- Quota: 1 unit
- Permissions: report:write

${isTestingV1() ? '' : 'Default parameters are set to show the organization-level analytics over the'}
${isTestingV1() ? '' : 'last 30 days.'}

${isTestingV1() ? 'The scope is either org or repo level, defaults to org.' : ''}
The scope is either org or repo level, defaults to org.

${isTestingV1() ? 'When scope is repo, a repo slug must be given as well.' : ''}
When scope is repo, a repo slug must be given as well.

${isTestingV1() ? 'The time argument must be number 7, 30, or 90 and defaults to 30.' : ''}
The TIME argument must be number 7, 30, or 90 and defaults to 30.

Options
${getFlagListOutput(flags, 6)}

Examples
$ ${command} ${isTestingV1() ? 'org 7' : '--scope=org --time=7'}
$ ${command} ${isTestingV1() ? 'repo test-repo 30' : '--scope=org --time=30'}
$ ${command} ${isTestingV1() ? '90' : '--scope=repo --repo=test-repo --time=30'}
`
// Drop consecutive empty lines. Temporarily necessary to deal with v1 prep.
.replace(/\n(?: *\n)+/g, '\n\n'),
$ ${command} org 7
$ ${command} repo test-repo 30
$ ${command} 90
`,
}

export const cmdAnalytics = {
Expand All @@ -98,7 +71,7 @@ async function run(
const { file, json, markdown } = cli.flags
const outputKind = getOutputKind(json, markdown)

// In v1 mode support:
// Supported inputs:
// - [] (no args)
// - ['org']
// - ['org', '30']
Expand All @@ -107,84 +80,59 @@ async function run(
// - ['30']
// Validate final values in the next step
let scope = 'org'
let time = isTestingV1() ? '30' : 30
let time = '30'
let repoName = ''
if (isTestingV1()) {
if (cli.input[0] === 'org') {
if (cli.input[1]) {
time = cli.input[1]
}
} else if (cli.input[0] === 'repo') {
scope = 'repo'
if (cli.input[1]) {
repoName = cli.input[1]
}
if (cli.input[2]) {
time = cli.input[2]
}
} else if (cli.input[0]) {
time = cli.input[0]
if (cli.input[0] === 'org') {
if (cli.input[1]) {
time = cli.input[1]
}
} else {
if (cli.flags['scope']) {
scope = String(cli.flags['scope'] || '')
} else if (cli.input[0] === 'repo') {
scope = 'repo'
if (cli.input[1]) {
repoName = cli.input[1]
}
if (scope === 'repo') {
repoName = String(cli.flags['repo'] || '')
}
if (cli.flags['time']) {
time = Number(cli.flags['time'] || 30)
if (cli.input[2]) {
time = cli.input[2]
}
} else if (cli.input[0]) {
time = cli.input[0]
}

const hasApiToken = hasDefaultToken()

const noLegacy =
!cli.flags['scope'] && !cli.flags['repo'] && !cli.flags['time']

const wasValidInput = checkCommandInput(
outputKind,
{
// In v1 this can't go wrong anymore since the unknown value goes to time
nook: !isTestingV1(),
test: scope === 'org' || scope === 'repo',
message: 'Scope must be "repo" or "org"',
pass: 'ok',
fail: 'bad',
},
{
nook: true,
// Before v1 there were no args, only flags
test: isTestingV1() || cli.input.length === 0,
message: 'This command does not accept any arguments (use flags instead)',
test: noLegacy,
message: 'Legacy flags are no longer supported. See v1 migration guide.',
pass: 'ok',
fail: `bad`,
fail: `received legacy flags`,
},
{
nook: true,
test: scope === 'org' || !!repoName,
message: isTestingV1()
? 'When scope=repo, repo name should be the second argument'
: 'When scope=repo, repo name should be set through --repo',
message: 'When scope=repo, repo name should be the second argument',
pass: 'ok',
fail: 'missing',
},
{
nook: true,
test:
scope === 'org' ||
!isTestingV1() ||
(repoName !== '7' && repoName !== '30' && repoName !== '90'),
message: 'When scope is repo, the second arg should be repo, not time',
pass: 'ok',
fail: 'missing',
},
{
test: isTestingV1()
? time === '7' || time === '30' || time === '90'
: time === 7 || time === 30 || time === 90,
test: time === '7' || time === '30' || time === '90',
message: 'The time filter must either be 7, 30 or 90',
pass: 'ok',
fail: isTestingV1()
? 'invalid range set, see --help for command arg details.'
: 'bad',
fail: 'invalid range set, see --help for command arg details.',
},
{
nook: true,
Expand Down Expand Up @@ -222,8 +170,7 @@ async function run(

return await handleAnalytics({
scope,
time:
time === '90' || time === 90 ? 90 : time === '30' || time === 30 ? 30 : 7,
time: time === '90' ? 90 : time === '30' ? 30 : 7,
repo: repoName,
outputKind,
filePath: String(file || ''),
Expand Down
Loading
Loading