Skip to content

Commit ad91c93

Browse files
authored
Apply handle pattern to repos (#378)
1 parent 30045f0 commit ad91c93

22 files changed

+458
-259
lines changed

src/commands/repos/cmd-repos-create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { createRepo } from './create-repo'
6+
import { handleCreateRepo } from './handle-create-repo'
77
import constants from '../../constants'
88
import { commonFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -101,7 +101,7 @@ async function run(
101101
return
102102
}
103103

104-
await createRepo({
104+
await handleCreateRepo({
105105
orgSlug,
106106
repoName,
107107
description: String(cli.flags['repoDescription'] || ''),

src/commands/repos/cmd-repos-del.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { deleteRepo } from './delete-repo'
6+
import { handleDeleteRepo } from './handle-delete-repo'
77
import constants from '../../constants'
88
import { commonFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -72,5 +72,5 @@ async function run(
7272
return
7373
}
7474

75-
await deleteRepo(orgSlug, repoName)
75+
await handleDeleteRepo(orgSlug, repoName)
7676
}

src/commands/repos/cmd-repos-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { listRepos } from './list-repos'
6+
import { handleListRepos } from './handle-list-repos'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -94,7 +94,7 @@ async function run(
9494
return
9595
}
9696

97-
await listRepos({
97+
await handleListRepos({
9898
direction: cli.flags['direction'] === 'asc' ? 'asc' : 'desc',
9999
orgSlug,
100100
outputKind: cli.flags['json']

src/commands/repos/cmd-repos-update.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { updateRepo } from './update-repo'
6+
import { handleUpdateRepo } from './handle-update-repo'
77
import constants from '../../constants'
88
import { commonFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -103,7 +103,7 @@ async function run(
103103
return
104104
}
105105

106-
await updateRepo({
106+
await handleUpdateRepo({
107107
orgSlug,
108108
repoName,
109109
description: String(cli.flags['repoDescription'] || ''),

src/commands/repos/cmd-repos-view.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'
33

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

6-
import { viewRepo } from './view-repo'
6+
import { handleViewRepo } from './handle-view-repo'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -56,7 +56,8 @@ async function run(
5656
parentName
5757
})
5858

59-
const repoName = cli.flags['repoName']
59+
const { json, markdown, repoName } = cli.flags
60+
6061
const [orgSlug = ''] = cli.input
6162

6263
if (!repoName || typeof repoName !== 'string' || !orgSlug) {
@@ -89,9 +90,9 @@ async function run(
8990
return
9091
}
9192

92-
await viewRepo(
93+
await handleViewRepo(
9394
orgSlug,
9495
repoName,
95-
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
96+
json ? 'json' : markdown ? 'markdown' : 'text'
9697
)
9798
}

src/commands/repos/create-repo.ts renamed to src/commands/repos/fetch-create-repo.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
33
import { AuthError } from '../../utils/errors'
44
import { getDefaultToken, setupSdk } from '../../utils/sdk'
55

6-
export async function createRepo({
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchCreateRepo({
79
default_branch,
810
description,
911
homepage,
@@ -17,16 +19,15 @@ export async function createRepo({
1719
homepage: string
1820
default_branch: string
1921
visibility: string
20-
}): Promise<void> {
22+
}): Promise<SocketSdkReturnType<'createOrgRepo'>['data'] | undefined> {
2123
const apiToken = getDefaultToken()
2224
if (!apiToken) {
2325
throw new AuthError(
2426
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
2527
)
2628
}
2729

28-
await createRepoWithToken({
29-
apiToken,
30+
return await fetchCreateRepoWithToken(apiToken, {
3031
default_branch,
3132
description,
3233
homepage,
@@ -36,29 +37,31 @@ export async function createRepo({
3637
})
3738
}
3839

39-
async function createRepoWithToken({
40-
apiToken,
41-
default_branch,
42-
description,
43-
homepage,
44-
orgSlug,
45-
repoName,
46-
visibility
47-
}: {
48-
apiToken: string
49-
orgSlug: string
50-
repoName: string
51-
description: string
52-
homepage: string
53-
default_branch: string
54-
visibility: string
55-
}): Promise<void> {
40+
async function fetchCreateRepoWithToken(
41+
apiToken: string,
42+
{
43+
default_branch,
44+
description,
45+
homepage,
46+
orgSlug,
47+
repoName,
48+
visibility
49+
}: {
50+
orgSlug: string
51+
repoName: string
52+
description: string
53+
homepage: string
54+
default_branch: string
55+
visibility: string
56+
}
57+
): Promise<SocketSdkReturnType<'createOrgRepo'>['data'] | undefined> {
5658
// Lazily access constants.spinner.
5759
const { spinner } = constants
5860

59-
spinner.start('Creating repository...')
60-
6161
const socketSdk = await setupSdk(apiToken)
62+
63+
spinner.start('Sending request ot create a repository...')
64+
6265
const result = await handleApiCall(
6366
socketSdk.createOrgRepo(orgSlug, {
6467
name: repoName,
@@ -70,10 +73,12 @@ async function createRepoWithToken({
7073
'creating repository'
7174
)
7275

76+
spinner.successAndStop('Received response requesting to create a repository.')
77+
7378
if (!result.success) {
7479
handleUnsuccessfulApiResponse('createOrgRepo', result)
7580
return
7681
}
7782

78-
spinner.successAndStop('Repository created successfully')
83+
return result.data
7984
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import constants from '../../constants'
2+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
5+
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchDeleteRepo(
9+
orgSlug: string,
10+
repoName: string
11+
): Promise<SocketSdkReturnType<'deleteOrgRepo'>['data'] | undefined> {
12+
const apiToken = getDefaultToken()
13+
if (!apiToken) {
14+
throw new AuthError(
15+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
16+
)
17+
}
18+
19+
return await fetchDeleteRepoWithToken(orgSlug, repoName, apiToken)
20+
}
21+
22+
async function fetchDeleteRepoWithToken(
23+
orgSlug: string,
24+
repoName: string,
25+
apiToken: string
26+
): Promise<SocketSdkReturnType<'deleteOrgRepo'>['data'] | undefined> {
27+
// Lazily access constants.spinner.
28+
const { spinner } = constants
29+
30+
const socketSdk = await setupSdk(apiToken)
31+
32+
spinner.start('Sending request to delete a repository...')
33+
34+
const result = await handleApiCall(
35+
socketSdk.deleteOrgRepo(orgSlug, repoName),
36+
'deleting repository'
37+
)
38+
39+
spinner.successAndStop('Received response requesting to delete a repository.')
40+
41+
if (!result.success) {
42+
handleUnsuccessfulApiResponse('deleteOrgRepo', result)
43+
return
44+
}
45+
46+
return result.data
47+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import constants from '../../constants'
2+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
5+
6+
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
7+
8+
export async function fetchListRepos({
9+
direction,
10+
orgSlug,
11+
page,
12+
per_page,
13+
sort
14+
}: {
15+
direction: string
16+
orgSlug: string
17+
page: number
18+
per_page: number
19+
sort: string
20+
}): Promise<SocketSdkReturnType<'getOrgRepoList'>['data'] | undefined> {
21+
const apiToken = getDefaultToken()
22+
if (!apiToken) {
23+
throw new AuthError(
24+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
25+
)
26+
}
27+
28+
return await fetchListReposWithToken(apiToken, {
29+
direction,
30+
orgSlug,
31+
page,
32+
per_page,
33+
sort
34+
})
35+
}
36+
37+
async function fetchListReposWithToken(
38+
apiToken: string,
39+
{
40+
direction,
41+
orgSlug,
42+
page,
43+
per_page,
44+
sort
45+
}: {
46+
direction: string
47+
orgSlug: string
48+
page: number
49+
per_page: number
50+
sort: string
51+
}
52+
): Promise<SocketSdkReturnType<'getOrgRepoList'>['data'] | undefined> {
53+
// Lazily access constants.spinner.
54+
const { spinner } = constants
55+
56+
const socketSdk = await setupSdk(apiToken)
57+
58+
spinner.start('Fetching list of repositories...')
59+
60+
const result = await handleApiCall(
61+
socketSdk.getOrgRepoList(orgSlug, {
62+
sort,
63+
direction,
64+
per_page: String(per_page),
65+
page: String(page)
66+
}),
67+
'listing repositories'
68+
)
69+
70+
spinner.successAndStop('Received response for repository list.')
71+
72+
if (!result.success) {
73+
handleUnsuccessfulApiResponse('getOrgRepoList', result)
74+
return
75+
}
76+
77+
return result.data
78+
}

0 commit comments

Comments
 (0)