-
Notifications
You must be signed in to change notification settings - Fork 40
Refactor many commands into consistency #330
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // https://github.com/SocketDev/socket-python-cli/blob/6d4fc56faee68d3a4764f1f80f84710635bdaf05/socketsecurity/socketcli.py | ||
| import meowOrExit from 'meow' | ||
|
|
||
| import { runAction } from './run-action.ts' | ||
| import { type CliCommandConfig } from '../../utils/meow-with-subcommands' | ||
| import { getFlagListOutput } from '../../utils/output-formatting.ts' | ||
|
|
||
| const config: CliCommandConfig = { | ||
| commandName: 'action', | ||
| description: 'Socket action command', // GitHub Action ? | ||
| hidden: true, | ||
| flags: { | ||
| // This flag is unused | ||
| // socketSecurityApiKey: { // deprecate this asap. | ||
| // type: 'string', | ||
| // default: 'env var SOCKET_SECURITY_API_KEY', | ||
| // description: 'Socket API token' | ||
| // }, | ||
| githubEventBefore: { | ||
| type: 'string', | ||
| default: '', | ||
| description: 'Before marker' | ||
| }, | ||
| githubEventAfter: { | ||
| type: 'string', | ||
| default: '', | ||
| description: 'After marker' | ||
| } | ||
| }, | ||
| help: (parentName, { commandName, flags }) => ` | ||
| Usage | ||
| $ ${parentName} ${commandName} [options] | ||
|
|
||
| Options | ||
| ${getFlagListOutput(flags, 6)} | ||
| ` | ||
| } | ||
|
|
||
| export const cmdAction = { | ||
| description: config.description, | ||
| hidden: config.hidden, | ||
| run: run | ||
| } | ||
|
|
||
| async function run( | ||
| argv: readonly string[], | ||
| importMeta: ImportMeta, | ||
| { parentName }: { parentName: string } | ||
| ): Promise<void> { | ||
| const cli = meowOrExit(config.help(parentName, config), { | ||
| argv, | ||
| description: config.description, | ||
| importMeta, | ||
| flags: config.flags | ||
| }) | ||
|
|
||
| const githubEventBefore = String(cli.flags['githubEventBefore'] || '') | ||
| const githubEventAfter = String(cli.flags['githubEventAfter'] || '') | ||
|
|
||
| await runAction(githubEventBefore, githubEventAfter) | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // https://github.com/SocketDev/socket-python-cli/blob/6d4fc56faee68d3a4764f1f80f84710635bdaf05/socketsecurity/socketcli.py | ||
|
|
||
| import micromatch from 'micromatch' | ||
| import { simpleGit } from 'simple-git' | ||
|
|
||
| import { SocketSdk } from '@socketsecurity/sdk' | ||
|
|
||
| import { Core } from './core' | ||
| import { GitHub } from './core/github' | ||
| import * as Messages from './core/messages' | ||
| import * as SCMComments from './core/scm_comments' | ||
| import { getDefaultToken } from '../../utils/sdk' | ||
|
|
||
| // TODO: is this a github action handler? | ||
| export async function runAction( | ||
| githubEventBefore: string, | ||
| githubEventAfter: string | ||
| ) { | ||
| //TODO | ||
| const socket = new SocketSdk(getDefaultToken()!) | ||
|
|
||
| const git = simpleGit() | ||
| const changedFiles = ( | ||
| await git.diff( | ||
| process.env['GITHUB_EVENT_NAME'] === 'pull_request' | ||
| ? ['--name-only', 'HEAD^1', 'HEAD'] | ||
| : ['--name-only', githubEventBefore, githubEventAfter] | ||
| ) | ||
| ).split('\n') | ||
|
|
||
| console.log({ changedFiles }) | ||
| // supportedFiles have 3-level deep globs | ||
| const patterns = Object.values(await socket.getReportSupportedFiles()) | ||
| .flatMap((i: Record<string, any>) => Object.values(i)) | ||
| .flatMap((i: Record<string, any>) => Object.values(i)) | ||
| .flatMap((i: Record<string, any>) => Object.values(i)) | ||
|
|
||
| const files = micromatch(changedFiles, patterns) | ||
|
|
||
| const scm = new GitHub() | ||
|
|
||
| if (scm.checkEventType() === 'comment') { | ||
| console.log('Comment initiated flow') | ||
| const comments = await scm.getCommentsForPR() | ||
| await scm.removeCommentAlerts({ comments }) | ||
| } else if (scm.checkEventType() === 'diff') { | ||
| console.log('Push initiated flow') | ||
| const core = new Core({ owner: scm.owner, repo: scm.repo, files, socket }) | ||
| const diff = await core.createNewDiff({}) | ||
| const comments = await scm.getCommentsForPR() | ||
| diff.newAlerts = SCMComments.removeAlerts({ | ||
| comments, | ||
| newAlerts: diff.newAlerts | ||
| }) | ||
| const overviewComment = Messages.dependencyOverviewTemplate(diff) | ||
| const securityComment = Messages.securityCommentTemplate(diff) | ||
| let newSecurityComment = true | ||
| let newOverviewComment = true | ||
| let updateOldSecurityComment = comments.security !== undefined | ||
| let updateOldOverviewComment = comments.overview !== undefined | ||
| if (diff.newAlerts.length === 0) { | ||
| if (!updateOldSecurityComment) { | ||
| newSecurityComment = false | ||
| console.log('No new alerts or security issue comment disabled') | ||
| } else { | ||
| console.log('Updated security comment with no new alerts') | ||
| } | ||
| } | ||
| if (diff.newPackages.length === 0 && diff.removedPackages.length === 0) { | ||
| if (!updateOldOverviewComment) { | ||
| newOverviewComment = false | ||
| console.log( | ||
| 'No new/removed packages or Dependency Overview comment disabled' | ||
| ) | ||
| } else { | ||
| console.log('Updated overview comment with no dependencies') | ||
| } | ||
| } | ||
| await scm.addSocketComments({ | ||
| securityComment, | ||
| overviewComment, | ||
| comments, | ||
| newSecurityComment, | ||
| newOverviewComment | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.