Skip to content

Commit 96e3769

Browse files
authored
Merge pull request #330 from SocketDev/refactor_a_lot
Refactor many commands into consistency
2 parents de5e5ae + b801cb2 commit 96e3769

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2596
-2486
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"clean": "run-p --aggregate-output clean:*",
4747
"clean:dist": "del-cli 'dist' 'test/dist'",
4848
"clean:node_modules": "del-cli '**/node_modules'",
49+
"fix": "npm run lint:fix ; npm run check:lint -- --fix",
4950
"knip:dependencies": "knip --dependencies",
5051
"knip:exports": "knip --include exports,duplicates",
5152
"lint": "oxlint -c=./.oxlintrc.json --ignore-path=./.oxlintignore --tsconfig=./tsconfig.json .",

src/cli.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { messageWithCauses, stackWithCauses } from 'pony-cause'
77
import updateNotifier from 'tiny-updater'
88
import colors from 'yoctocolors-cjs'
99

10-
import { actionCommand } from './commands/action'
11-
import { analyticsCommand } from './commands/analytics/analytics-command'
12-
import { auditLogCommand } from './commands/audit-log'
13-
import { cdxgenCommand } from './commands/cdxgen'
14-
import { dependenciesCommand } from './commands/dependencies'
15-
import { diffScanCommand } from './commands/diff-scan'
16-
import { fixCommand } from './commands/fix'
17-
import { infoCommand } from './commands/info'
10+
import { cmdAction } from './commands/action/cmd-action.ts'
11+
import { cmdAnalytics } from './commands/analytics/cmd-analytics.ts'
12+
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log.ts'
13+
import { cmdCdxgen } from './commands/cdxgen/cmd-cdxgen.ts'
14+
import { cmdScanCreate } from './commands/dependencies/cmd-dependencies.ts'
15+
import { cmdDiffScan } from './commands/diff-scan/cmd-diff-scan.ts'
16+
import { cmdFix } from './commands/fix/cmd-fix.ts'
17+
import { cmdInfo } from './commands/info/cmd-info.ts'
1818
import { loginCommand } from './commands/login'
1919
import { logoutCommand } from './commands/logout'
2020
import { manifestCommand } from './commands/manifest'
@@ -24,8 +24,8 @@ import { optimizeCommand } from './commands/optimize'
2424
import { organizationCommand } from './commands/organization'
2525
import { rawNpmCommand } from './commands/raw-npm'
2626
import { rawNpxCommand } from './commands/raw-npx'
27-
import { reportCommand } from './commands/report'
28-
import { reposCommand } from './commands/repos'
27+
import { cmdReport } from './commands/report/cmd-report.ts'
28+
import { cmdRepos } from './commands/repos/cmd-repos.ts'
2929
import { cmdScan } from './commands/scan/cmd-scan.ts'
3030
import { threatFeedCommand } from './commands/threat-feed'
3131
import { wrapperCommand } from './commands/wrapper'
@@ -47,10 +47,10 @@ void (async () => {
4747
try {
4848
await meowWithSubcommands(
4949
{
50-
action: actionCommand,
51-
cdxgen: cdxgenCommand,
52-
fix: fixCommand,
53-
info: infoCommand,
50+
action: cmdAction,
51+
cdxgen: cmdCdxgen,
52+
fix: cmdFix,
53+
info: cmdInfo,
5454
login: loginCommand,
5555
logout: logoutCommand,
5656
npm: npmCommand,
@@ -59,14 +59,14 @@ void (async () => {
5959
organization: organizationCommand,
6060
'raw-npm': rawNpmCommand,
6161
'raw-npx': rawNpxCommand,
62-
report: reportCommand,
62+
report: cmdReport,
6363
wrapper: wrapperCommand,
6464
scan: cmdScan,
65-
'audit-log': auditLogCommand,
66-
repos: reposCommand,
67-
dependencies: dependenciesCommand,
68-
analytics: analyticsCommand,
69-
'diff-scan': diffScanCommand,
65+
'audit-log': cmdAuditLog,
66+
repos: cmdRepos,
67+
dependencies: cmdScanCreate,
68+
analytics: cmdAnalytics,
69+
'diff-scan': cmdDiffScan,
7070
'threat-feed': threatFeedCommand,
7171
manifest: manifestCommand
7272
},

src/commands/action/cmd-action.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// https://github.com/SocketDev/socket-python-cli/blob/6d4fc56faee68d3a4764f1f80f84710635bdaf05/socketsecurity/socketcli.py
2+
import meowOrExit from 'meow'
3+
4+
import { runAction } from './run-action.ts'
5+
import { type CliCommandConfig } from '../../utils/meow-with-subcommands'
6+
import { getFlagListOutput } from '../../utils/output-formatting.ts'
7+
8+
const config: CliCommandConfig = {
9+
commandName: 'action',
10+
description: 'Socket action command', // GitHub Action ?
11+
hidden: true,
12+
flags: {
13+
// This flag is unused
14+
// socketSecurityApiKey: { // deprecate this asap.
15+
// type: 'string',
16+
// default: 'env var SOCKET_SECURITY_API_KEY',
17+
// description: 'Socket API token'
18+
// },
19+
githubEventBefore: {
20+
type: 'string',
21+
default: '',
22+
description: 'Before marker'
23+
},
24+
githubEventAfter: {
25+
type: 'string',
26+
default: '',
27+
description: 'After marker'
28+
}
29+
},
30+
help: (parentName, { commandName, flags }) => `
31+
Usage
32+
$ ${parentName} ${commandName} [options]
33+
34+
Options
35+
${getFlagListOutput(flags, 6)}
36+
`
37+
}
38+
39+
export const cmdAction = {
40+
description: config.description,
41+
hidden: config.hidden,
42+
run: run
43+
}
44+
45+
async function run(
46+
argv: readonly string[],
47+
importMeta: ImportMeta,
48+
{ parentName }: { parentName: string }
49+
): Promise<void> {
50+
const cli = meowOrExit(config.help(parentName, config), {
51+
argv,
52+
description: config.description,
53+
importMeta,
54+
flags: config.flags
55+
})
56+
57+
const githubEventBefore = String(cli.flags['githubEventBefore'] || '')
58+
const githubEventAfter = String(cli.flags['githubEventAfter'] || '')
59+
60+
await runAction(githubEventBefore, githubEventAfter)
61+
}

src/commands/action/index.ts

Lines changed: 0 additions & 108 deletions
This file was deleted.

src/commands/action/run-action.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// https://github.com/SocketDev/socket-python-cli/blob/6d4fc56faee68d3a4764f1f80f84710635bdaf05/socketsecurity/socketcli.py
2+
3+
import micromatch from 'micromatch'
4+
import { simpleGit } from 'simple-git'
5+
6+
import { SocketSdk } from '@socketsecurity/sdk'
7+
8+
import { Core } from './core'
9+
import { GitHub } from './core/github'
10+
import * as Messages from './core/messages'
11+
import * as SCMComments from './core/scm_comments'
12+
import { getDefaultToken } from '../../utils/sdk'
13+
14+
// TODO: is this a github action handler?
15+
export async function runAction(
16+
githubEventBefore: string,
17+
githubEventAfter: string
18+
) {
19+
//TODO
20+
const socket = new SocketSdk(getDefaultToken()!)
21+
22+
const git = simpleGit()
23+
const changedFiles = (
24+
await git.diff(
25+
process.env['GITHUB_EVENT_NAME'] === 'pull_request'
26+
? ['--name-only', 'HEAD^1', 'HEAD']
27+
: ['--name-only', githubEventBefore, githubEventAfter]
28+
)
29+
).split('\n')
30+
31+
console.log({ changedFiles })
32+
// supportedFiles have 3-level deep globs
33+
const patterns = Object.values(await socket.getReportSupportedFiles())
34+
.flatMap((i: Record<string, any>) => Object.values(i))
35+
.flatMap((i: Record<string, any>) => Object.values(i))
36+
.flatMap((i: Record<string, any>) => Object.values(i))
37+
38+
const files = micromatch(changedFiles, patterns)
39+
40+
const scm = new GitHub()
41+
42+
if (scm.checkEventType() === 'comment') {
43+
console.log('Comment initiated flow')
44+
const comments = await scm.getCommentsForPR()
45+
await scm.removeCommentAlerts({ comments })
46+
} else if (scm.checkEventType() === 'diff') {
47+
console.log('Push initiated flow')
48+
const core = new Core({ owner: scm.owner, repo: scm.repo, files, socket })
49+
const diff = await core.createNewDiff({})
50+
const comments = await scm.getCommentsForPR()
51+
diff.newAlerts = SCMComments.removeAlerts({
52+
comments,
53+
newAlerts: diff.newAlerts
54+
})
55+
const overviewComment = Messages.dependencyOverviewTemplate(diff)
56+
const securityComment = Messages.securityCommentTemplate(diff)
57+
let newSecurityComment = true
58+
let newOverviewComment = true
59+
let updateOldSecurityComment = comments.security !== undefined
60+
let updateOldOverviewComment = comments.overview !== undefined
61+
if (diff.newAlerts.length === 0) {
62+
if (!updateOldSecurityComment) {
63+
newSecurityComment = false
64+
console.log('No new alerts or security issue comment disabled')
65+
} else {
66+
console.log('Updated security comment with no new alerts')
67+
}
68+
}
69+
if (diff.newPackages.length === 0 && diff.removedPackages.length === 0) {
70+
if (!updateOldOverviewComment) {
71+
newOverviewComment = false
72+
console.log(
73+
'No new/removed packages or Dependency Overview comment disabled'
74+
)
75+
} else {
76+
console.log('Updated overview comment with no dependencies')
77+
}
78+
}
79+
await scm.addSocketComments({
80+
securityComment,
81+
overviewComment,
82+
comments,
83+
newSecurityComment,
84+
newOverviewComment
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)