Skip to content
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

[fix] Refactor to add auth error handling #177

Merged
merged 2 commits into from
Aug 22, 2024
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
14 changes: 10 additions & 4 deletions src/commands/audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
handleUnsuccessfulApiResponse
} from '../utils/api-helpers'
import { printFlagList } from '../utils/formatting'
import { FREE_API_KEY, getDefaultKey, setupSdk } from '../utils/sdk'
import { getDefaultKey, setupSdk } from '../utils/sdk'

import type { CliSubcommand } from '../utils/meow-with-subcommands'
import type { Ora } from 'ora'

Check warning on line 15 in src/commands/audit-log.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../utils/errors'

Check warning on line 16 in src/commands/audit-log.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/errors` import should occur before import of `../utils/formatting`

export const auditLog: CliSubcommand = {
description: 'Look up the audit log for an organization',
Expand All @@ -21,8 +22,12 @@

const input = setupCommand(name, auditLog.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinner = ora(`Looking up audit log for ${input.orgSlug}\n`).start()
await fetchOrgAuditLog(input.orgSlug, input, spinner)
await fetchOrgAuditLog(input.orgSlug, input, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -132,9 +137,10 @@
async function fetchOrgAuditLog(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getAuditLogEvents(orgSlug, input),
`Looking up audit log for ${orgSlug}\n`
Expand Down
19 changes: 12 additions & 7 deletions src/commands/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import { printFlagList } from '../utils/formatting'
import { getDefaultKey, setupSdk } from '../utils/sdk'

import type { CliSubcommand } from '../utils/meow-with-subcommands'

Check warning on line 15 in src/commands/dependencies.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import type { Ora } from 'ora'
import { AuthError } from '../utils/errors'

Check warning on line 16 in src/commands/dependencies.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/errors` import should occur before import of `../utils/formatting`

export const dependencies: CliSubcommand = {
description:
Expand All @@ -23,9 +23,7 @@

const input = setupCommand(name, dependencies.description, argv, importMeta)
if (input) {
const spinnerText = 'Searching dependencies...'
const spinner = ora(spinnerText).start()
await searchDeps(input, spinner)
await searchDeps(input)
}
}
}
Expand Down Expand Up @@ -100,10 +98,17 @@
}

async function searchDeps(
{ limit, offset, outputJson }: CommandContext,
spinner: Ora
{ limit, offset, outputJson }: CommandContext
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Searching dependencies...'
const spinner = ora(spinnerText).start()

const socketSdk = await setupSdk(apiKey)

const result = await handleApiCall(
socketSdk.searchDependencies({ limit, offset }),
'Searching dependencies'
Expand Down
15 changes: 8 additions & 7 deletions src/commands/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
} from '../utils/api-helpers'
import { getDefaultKey, setupSdk } from '../utils/sdk'

import type { CliSubcommand } from '../utils/meow-with-subcommands'

Check warning on line 11 in src/commands/organization.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../utils/errors'

Check warning on line 12 in src/commands/organization.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/errors` import should occur before import of `../utils/sdk`

export const organizations: CliSubcommand = {
description: 'List organizations associated with the API key used',
Expand Down Expand Up @@ -42,6 +43,9 @@

async function fetchOrganizations(): Promise<void> {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const socketSdk = await setupSdk(apiKey)
const spinner = ora('Fetching organizations...').start()

Expand All @@ -57,13 +61,10 @@
spinner.stop()

const organizations = Object.values(result.data.organizations)
if (apiKey) {
console.log(
`List of organizations associated with your API key: ${chalk.italic(apiKey)}`
)
} else {
console.log('List of organizations associated with your API key.')
}

console.log(
`List of organizations associated with your API key: ${chalk.italic(apiKey)}`
)

for (const o of organizations) {
console.log(`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { meowWithSubcommands } from '../../utils/meow-with-subcommands'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'

const description = 'Project report related commands'
const description = '[Deprecated] Project report related commands'

export const report: CliSubcommand = {
description,
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'

Check warning on line 14 in src/commands/repos/create.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../../utils/errors'

Check warning on line 15 in src/commands/repos/create.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../../utils/errors` import should occur before import of `../../utils/formatting`

export const create: CliSubcommand = {
description: 'Create a repository in an organization',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} create`
const input = setupCommand(name, create.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Creating repository... \n'
const spinner = ora(spinnerText).start()
await createRepo(input.orgSlug, input, spinner)
await createRepo(input.orgSlug, input, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -145,9 +150,10 @@
async function createRepo(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.createOrgRepo(orgSlug, input),
'creating repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'

Check warning on line 12 in src/commands/repos/delete.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../../utils/errors'

Check warning on line 13 in src/commands/repos/delete.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../../utils/errors` import should occur before import of `../../utils/sdk`

export const del: CliSubcommand = {
description: 'Delete a repository in an organization',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} del`
const input = setupCommand(name, del.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Deleting repository... \n'
const spinner = ora(spinnerText).start()
await deleteRepository(input.orgSlug, input.repoName, spinner)
await deleteRepository(input.orgSlug, input.repoName, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -71,9 +76,10 @@
async function deleteRepository(
orgSlug: string,
repoName: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.deleteOrgRepo(orgSlug, repoName),
'deleting repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'
import { AuthError } from '../../utils/errors'

export const list: CliSubcommand = {
description: 'List repositories in an organization',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} list`
const input = setupCommand(name, list.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Listing repositories... \n'
const spinner = ora(spinnerText).start()
await listOrgRepos(input.orgSlug, input, spinner)
await listOrgRepos(input.orgSlug, input, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -129,9 +134,10 @@ function setupCommand(
async function listOrgRepos(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getOrgRepoList(orgSlug, input),
'listing repositories'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'
import { AuthError } from '../../utils/errors'

export const update: CliSubcommand = {
description: 'Update a repository in an organization',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} update`
const input = setupCommand(name, update.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Updating repository... \n'
const spinner = ora(spinnerText).start()
await updateRepository(input.orgSlug, input, spinner)
await updateRepository(input.orgSlug, input, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -145,9 +150,10 @@ function setupCommand(
async function updateRepository(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.updateOrgRepo(orgSlug, input.name, input),
'updating repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'
import { AuthError } from '../../utils/errors'

export const view: CliSubcommand = {
description: 'View repositories in an organization',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} view`
const input = setupCommand(name, view.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Fetching repository... \n'
const spinner = ora(spinnerText).start()
await viewRepository(input.orgSlug, input.repositoryName, spinner)
await viewRepository(input.orgSlug, input.repositoryName, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -89,9 +94,10 @@ function setupCommand(
async function viewRepository(
orgSlug: string,
repoName: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getOrgRepo(orgSlug, repoName),
'fetching repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/scan/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'
import { AuthError } from '../../utils/errors'

export const create: CliSubcommand = {
description: 'Create a scan',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} create`
const input = await setupCommand(name, create.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Creating a scan... \n'
const spinner = ora(spinnerText).start()
await createFullScan(input, spinner)
await createFullScan(input, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -203,9 +208,10 @@ async function setupCommand(

async function createFullScan(
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const {
orgSlug,
repoName,
Expand Down
12 changes: 9 additions & 3 deletions src/commands/scan/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'
import { AuthError } from '../../utils/errors'

export const del: CliSubcommand = {
description: 'Delete a scan',
async run(argv, importMeta, { parentName }) {
const name = `${parentName} del`
const input = setupCommand(name, del.description, argv, importMeta)
if (input) {
const apiKey = getDefaultKey()
if(!apiKey){
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
}
const spinnerText = 'Deleting scan...'
const spinner = ora(spinnerText).start()
await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner)
await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner, apiKey)
}
}
}
Expand Down Expand Up @@ -87,9 +92,10 @@ function setupCommand(
async function deleteOrgFullScan(
orgSlug: string,
fullScanId: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.deleteOrgFullScan(orgSlug, fullScanId),
'Deleting scan'
Expand Down
Loading
Loading