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
4 changes: 2 additions & 2 deletions src/commands/repos/cmd-repos-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { createRepo } from './create-repo'
import { handleCreateRepo } from './handle-create-repo'
import constants from '../../constants'
import { commonFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -101,7 +101,7 @@ async function run(
return
}

await createRepo({
await handleCreateRepo({
orgSlug,
repoName,
description: String(cli.flags['repoDescription'] || ''),
Expand Down
4 changes: 2 additions & 2 deletions src/commands/repos/cmd-repos-del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { deleteRepo } from './delete-repo'
import { handleDeleteRepo } from './handle-delete-repo'
import constants from '../../constants'
import { commonFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -72,5 +72,5 @@ async function run(
return
}

await deleteRepo(orgSlug, repoName)
await handleDeleteRepo(orgSlug, repoName)
}
4 changes: 2 additions & 2 deletions src/commands/repos/cmd-repos-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { listRepos } from './list-repos'
import { handleListRepos } from './handle-list-repos'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -94,7 +94,7 @@ async function run(
return
}

await listRepos({
await handleListRepos({
direction: cli.flags['direction'] === 'asc' ? 'asc' : 'desc',
orgSlug,
outputKind: cli.flags['json']
Expand Down
4 changes: 2 additions & 2 deletions src/commands/repos/cmd-repos-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { updateRepo } from './update-repo'
import { handleUpdateRepo } from './handle-update-repo'
import constants from '../../constants'
import { commonFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -103,7 +103,7 @@ async function run(
return
}

await updateRepo({
await handleUpdateRepo({
orgSlug,
repoName,
description: String(cli.flags['repoDescription'] || ''),
Expand Down
9 changes: 5 additions & 4 deletions src/commands/repos/cmd-repos-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { viewRepo } from './view-repo'
import { handleViewRepo } from './handle-view-repo'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -56,7 +56,8 @@ async function run(
parentName
})

const repoName = cli.flags['repoName']
const { json, markdown, repoName } = cli.flags

const [orgSlug = ''] = cli.input

if (!repoName || typeof repoName !== 'string' || !orgSlug) {
Expand Down Expand Up @@ -89,9 +90,9 @@ async function run(
return
}

await viewRepo(
await handleViewRepo(
orgSlug,
repoName,
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
json ? 'json' : markdown ? 'markdown' : 'text'
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

export async function createRepo({
import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchCreateRepo({
default_branch,
description,
homepage,
Expand All @@ -17,16 +19,15 @@ export async function createRepo({
homepage: string
default_branch: string
visibility: string
}): Promise<void> {
}): Promise<SocketSdkReturnType<'createOrgRepo'>['data'] | undefined> {
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,
return await fetchCreateRepoWithToken(apiToken, {
default_branch,
description,
homepage,
Expand All @@ -36,29 +37,31 @@ export async function createRepo({
})
}

async function createRepoWithToken({
apiToken,
default_branch,
description,
homepage,
orgSlug,
repoName,
visibility
}: {
apiToken: string
orgSlug: string
repoName: string
description: string
homepage: string
default_branch: string
visibility: string
}): Promise<void> {
async function fetchCreateRepoWithToken(
apiToken: string,
{
default_branch,
description,
homepage,
orgSlug,
repoName,
visibility
}: {
orgSlug: string
repoName: string
description: string
homepage: string
default_branch: string
visibility: string
}
): Promise<SocketSdkReturnType<'createOrgRepo'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

spinner.start('Creating repository...')

const socketSdk = await setupSdk(apiToken)

spinner.start('Sending request ot create a repository...')

const result = await handleApiCall(
socketSdk.createOrgRepo(orgSlug, {
name: repoName,
Expand All @@ -70,10 +73,12 @@ async function createRepoWithToken({
'creating repository'
)

spinner.successAndStop('Received response requesting to create a repository.')

if (!result.success) {
handleUnsuccessfulApiResponse('createOrgRepo', result)
return
}

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

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchDeleteRepo(
orgSlug: string,
repoName: string
): Promise<SocketSdkReturnType<'deleteOrgRepo'>['data'] | undefined> {
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.'
)
}

return await fetchDeleteRepoWithToken(orgSlug, repoName, apiToken)
}

async function fetchDeleteRepoWithToken(
orgSlug: string,
repoName: string,
apiToken: string
): Promise<SocketSdkReturnType<'deleteOrgRepo'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

const socketSdk = await setupSdk(apiToken)

spinner.start('Sending request to delete a repository...')

const result = await handleApiCall(
socketSdk.deleteOrgRepo(orgSlug, repoName),
'deleting repository'
)

spinner.successAndStop('Received response requesting to delete a repository.')

if (!result.success) {
handleUnsuccessfulApiResponse('deleteOrgRepo', result)
return
}

return result.data
}
78 changes: 78 additions & 0 deletions src/commands/repos/fetch-list-repos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchListRepos({
direction,
orgSlug,
page,
per_page,
sort
}: {
direction: string
orgSlug: string
page: number
per_page: number
sort: string
}): Promise<SocketSdkReturnType<'getOrgRepoList'>['data'] | undefined> {
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.'
)
}

return await fetchListReposWithToken(apiToken, {
direction,
orgSlug,
page,
per_page,
sort
})
}

async function fetchListReposWithToken(
apiToken: string,
{
direction,
orgSlug,
page,
per_page,
sort
}: {
direction: string
orgSlug: string
page: number
per_page: number
sort: string
}
): Promise<SocketSdkReturnType<'getOrgRepoList'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants

const socketSdk = await setupSdk(apiToken)

spinner.start('Fetching list of repositories...')

const result = await handleApiCall(
socketSdk.getOrgRepoList(orgSlug, {
sort,
direction,
per_page: String(per_page),
page: String(page)
}),
'listing repositories'
)

spinner.successAndStop('Received response for repository list.')

if (!result.success) {
handleUnsuccessfulApiResponse('getOrgRepoList', result)
return
}

return result.data
}
Loading