Skip to content
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
17 changes: 2 additions & 15 deletions src/commands/repos/cmd-repos-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger'

import { createRepo } from './create-repo'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { commonFlags } from '../../flags'
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'

Expand All @@ -21,7 +19,6 @@ const config: CliCommandConfig = {
hidden: false,
flags: {
...commonFlags,
...outputFlags,
repoName: {
type: 'string',
shortFlag: 'n',
Expand Down Expand Up @@ -104,22 +101,12 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await createRepo({
outputJson: Boolean(cli.flags['json']),
outputMarkdown: Boolean(cli.flags['markdown']),
orgSlug,
repoName,
description: String(cli.flags['repoDescription'] || ''),
homepage: String(cli.flags['homepage'] || ''),
default_branch: String(cli.flags['defaultBranch'] || ''),
visibility: String(cli.flags['visibility'] || 'private'),
apiToken
visibility: String(cli.flags['visibility'] || 'private')
})
}
11 changes: 1 addition & 10 deletions src/commands/repos/cmd-repos-del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { deleteRepo } from './delete-repo'
import constants from '../../constants'
import { commonFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
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'

Expand Down Expand Up @@ -74,12 +72,5 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await deleteRepo(orgSlug, repoName, apiToken)
await deleteRepo(orgSlug, repoName)
}
23 changes: 8 additions & 15 deletions src/commands/repos/cmd-repos-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { listRepos } from './list-repos'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
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'

Expand Down Expand Up @@ -96,21 +94,16 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await listRepos({
apiToken,
outputJson: Boolean(cli.flags['json']),
outputMarkdown: Boolean(cli.flags['markdown']),
orgSlug,
sort: String(cli.flags['sort'] || 'created_at'),
direction: cli.flags['direction'] === 'asc' ? 'asc' : 'desc',
orgSlug,
outputKind: cli.flags['json']
? 'json'
: cli.flags['markdown']
? 'markdown'
: 'print',
page: Number(cli.flags['page']) || 1,
per_page: Number(cli.flags['perPage']) || 30
per_page: Number(cli.flags['perPage']) || 30,
sort: String(cli.flags['sort'] || 'created_at')
})
}
15 changes: 1 addition & 14 deletions src/commands/repos/cmd-repos-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger'

import { updateRepo } from './update-repo'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { commonFlags } from '../../flags'
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'

Expand All @@ -21,7 +19,6 @@ const config: CliCommandConfig = {
hidden: false,
flags: {
...commonFlags,
...outputFlags,
repoName: {
type: 'string',
shortFlag: 'n',
Expand Down Expand Up @@ -106,17 +103,7 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await updateRepo({
apiToken,
outputJson: Boolean(cli.flags['json']),
outputMarkdown: Boolean(cli.flags['markdown']),
orgSlug,
repoName,
description: String(cli.flags['repoDescription'] || ''),
Expand Down
22 changes: 11 additions & 11 deletions src/commands/repos/cmd-repos-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { viewRepo } from './view-repo'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
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'

Expand All @@ -21,7 +19,12 @@ const config: CliCommandConfig = {
hidden: false,
flags: {
...commonFlags,
...outputFlags
...outputFlags,
repoName: {
description: 'The repository to check',
default: '',
type: 'string'
}
},
help: (command, config) => `
Usage
Expand Down Expand Up @@ -86,12 +89,9 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await viewRepo(orgSlug, repoName, apiToken)
await viewRepo(
orgSlug,
repoName,
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
)
}
50 changes: 39 additions & 11 deletions src/commands/repos/create-repo.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { setupSdk } from '../../utils/sdk'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

export async function createRepo({
default_branch,
description,
homepage,
orgSlug,
repoName,
visibility
}: {
orgSlug: string
repoName: string
description: string
homepage: string
default_branch: string
visibility: string
}): Promise<void> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await createRepoWithToken({
apiToken,
default_branch,
description,
homepage,
orgSlug,
repoName,
visibility
})
}

async function createRepoWithToken({
apiToken,
default_branch,
description,
homepage,
orgSlug,
outputJson,
outputMarkdown,
repoName,
visibility
}: {
apiToken: string
outputJson: boolean
outputMarkdown: boolean
orgSlug: string
repoName: string
description: string
Expand All @@ -31,9 +61,6 @@ export async function createRepo({
const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.createOrgRepo(orgSlug, {
outputJson,
outputMarkdown,
orgSlug,
name: repoName,
description,
homepage,
Expand All @@ -43,9 +70,10 @@ export async function createRepo({
'creating repository'
)

if (result.success) {
spinner.successAndStop('Repository created successfully')
} else {
if (!result.success) {
handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
return
}

spinner.successAndStop('Repository created successfully')
}
24 changes: 20 additions & 4 deletions src/commands/repos/delete-repo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { setupSdk } from '../../utils/sdk'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

export async function deleteRepo(
orgSlug: string,
repoName: string
): Promise<void> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await deleteRepoWithToken(orgSlug, repoName, apiToken)
}

async function deleteRepoWithToken(
orgSlug: string,
repoName: string,
apiToken: string
Expand All @@ -18,9 +33,10 @@ export async function deleteRepo(
'deleting repository'
)

if (result.success) {
spinner.successAndStop('Repository deleted successfully')
} else {
if (!result.success) {
handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner)
return
}

spinner.successAndStop('Repository deleted successfully')
}
Loading