Skip to content

Commit 162dc01

Browse files
committed
Update and cleanup repos command
1 parent 5a261bf commit 162dc01

File tree

10 files changed

+226
-87
lines changed

10 files changed

+226
-87
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { createRepo } from './create-repo'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -104,13 +102,6 @@ async function run(
104102
return
105103
}
106104

107-
const apiToken = getDefaultToken()
108-
if (!apiToken) {
109-
throw new AuthError(
110-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
111-
)
112-
}
113-
114105
await createRepo({
115106
outputJson: Boolean(cli.flags['json']),
116107
outputMarkdown: Boolean(cli.flags['markdown']),
@@ -119,7 +110,6 @@ async function run(
119110
description: String(cli.flags['repoDescription'] || ''),
120111
homepage: String(cli.flags['homepage'] || ''),
121112
default_branch: String(cli.flags['defaultBranch'] || ''),
122-
visibility: String(cli.flags['visibility'] || 'private'),
123-
apiToken
113+
visibility: String(cli.flags['visibility'] || 'private')
124114
})
125115
}

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { deleteRepo } from './delete-repo'
77
import constants from '../../constants'
88
import { commonFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -74,12 +72,5 @@ async function run(
7472
return
7573
}
7674

77-
const apiToken = getDefaultToken()
78-
if (!apiToken) {
79-
throw new AuthError(
80-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
81-
)
82-
}
83-
84-
await deleteRepo(orgSlug, repoName, apiToken)
75+
await deleteRepo(orgSlug, repoName)
8576
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { listRepos } from './list-repos'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -96,21 +94,16 @@ async function run(
9694
return
9795
}
9896

99-
const apiToken = getDefaultToken()
100-
if (!apiToken) {
101-
throw new AuthError(
102-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
103-
)
104-
}
105-
10697
await listRepos({
107-
apiToken,
108-
outputJson: Boolean(cli.flags['json']),
109-
outputMarkdown: Boolean(cli.flags['markdown']),
110-
orgSlug,
111-
sort: String(cli.flags['sort'] || 'created_at'),
11298
direction: cli.flags['direction'] === 'asc' ? 'asc' : 'desc',
99+
orgSlug,
100+
outputKind: cli.flags['json']
101+
? 'json'
102+
: cli.flags['markdown']
103+
? 'markdown'
104+
: 'print',
113105
page: Number(cli.flags['page']) || 1,
114-
per_page: Number(cli.flags['perPage']) || 30
106+
per_page: Number(cli.flags['perPage']) || 30,
107+
sort: String(cli.flags['sort'] || 'created_at')
115108
})
116109
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { updateRepo } from './update-repo'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -106,15 +104,7 @@ async function run(
106104
return
107105
}
108106

109-
const apiToken = getDefaultToken()
110-
if (!apiToken) {
111-
throw new AuthError(
112-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
113-
)
114-
}
115-
116107
await updateRepo({
117-
apiToken,
118108
outputJson: Boolean(cli.flags['json']),
119109
outputMarkdown: Boolean(cli.flags['markdown']),
120110
orgSlug,

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66
import { viewRepo } from './view-repo'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
109
import { meowOrExit } from '../../utils/meow-with-subcommands'
1110
import { getFlagListOutput } from '../../utils/output-formatting'
12-
import { getDefaultToken } from '../../utils/sdk'
1311

1412
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
1513

@@ -21,7 +19,12 @@ const config: CliCommandConfig = {
2119
hidden: false,
2220
flags: {
2321
...commonFlags,
24-
...outputFlags
22+
...outputFlags,
23+
repoName: {
24+
description: 'The repository to check',
25+
default: '',
26+
type: 'string'
27+
}
2528
},
2629
help: (command, config) => `
2730
Usage
@@ -86,12 +89,9 @@ async function run(
8689
return
8790
}
8891

89-
const apiToken = getDefaultToken()
90-
if (!apiToken) {
91-
throw new AuthError(
92-
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
93-
)
94-
}
95-
96-
await viewRepo(orgSlug, repoName, apiToken)
92+
await viewRepo(
93+
orgSlug,
94+
repoName,
95+
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
96+
)
9797
}

src/commands/repos/create-repo.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
11
import constants from '../../constants'
22
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3-
import { setupSdk } from '../../utils/sdk'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
45

56
export async function createRepo({
7+
default_branch,
8+
description,
9+
homepage,
10+
orgSlug,
11+
outputJson,
12+
outputMarkdown,
13+
repoName,
14+
visibility
15+
}: {
16+
outputJson: boolean
17+
outputMarkdown: boolean
18+
orgSlug: string
19+
repoName: string
20+
description: string
21+
homepage: string
22+
default_branch: string
23+
visibility: string
24+
}): Promise<void> {
25+
const apiToken = getDefaultToken()
26+
if (!apiToken) {
27+
throw new AuthError(
28+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
29+
)
30+
}
31+
32+
await createRepoWithToken({
33+
apiToken,
34+
default_branch,
35+
description,
36+
homepage,
37+
orgSlug,
38+
outputJson,
39+
outputMarkdown,
40+
repoName,
41+
visibility
42+
})
43+
}
44+
45+
async function createRepoWithToken({
646
apiToken,
747
default_branch,
848
description,
@@ -43,9 +83,10 @@ export async function createRepo({
4383
'creating repository'
4484
)
4585

46-
if (result.success) {
47-
spinner.successAndStop('Repository created successfully')
48-
} else {
86+
if (!result.success) {
4987
handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
88+
return
5089
}
90+
91+
spinner.successAndStop('Repository created successfully')
5192
}

src/commands/repos/delete-repo.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import constants from '../../constants'
22
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
3-
import { setupSdk } from '../../utils/sdk'
3+
import { AuthError } from '../../utils/errors'
4+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
45

56
export async function deleteRepo(
7+
orgSlug: string,
8+
repoName: string
9+
): Promise<void> {
10+
const apiToken = getDefaultToken()
11+
if (!apiToken) {
12+
throw new AuthError(
13+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
14+
)
15+
}
16+
17+
await deleteRepoWithToken(orgSlug, repoName, apiToken)
18+
}
19+
20+
async function deleteRepoWithToken(
621
orgSlug: string,
722
repoName: string,
823
apiToken: string
@@ -18,9 +33,10 @@ export async function deleteRepo(
1833
'deleting repository'
1934
)
2035

21-
if (result.success) {
22-
spinner.successAndStop('Repository deleted successfully')
23-
} else {
36+
if (!result.success) {
2437
handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner)
38+
return
2539
}
40+
41+
spinner.successAndStop('Repository deleted successfully')
2642
}

src/commands/repos/list-repos.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,67 @@ import { logger } from '@socketsecurity/registry/lib/logger'
66

77
import constants from '../../constants'
88
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
9-
import { setupSdk } from '../../utils/sdk'
9+
import { AuthError } from '../../utils/errors'
10+
import { getDefaultToken, setupSdk } from '../../utils/sdk'
1011

1112
export async function listRepos({
12-
apiToken,
1313
direction,
1414
orgSlug,
15-
outputJson,
16-
outputMarkdown,
15+
outputKind,
1716
page,
1817
per_page,
1918
sort
2019
}: {
21-
outputJson: boolean
22-
outputMarkdown: boolean
20+
direction: string
2321
orgSlug: string
22+
outputKind: 'json' | 'markdown' | 'print'
23+
page: number
24+
per_page: number
2425
sort: string
26+
}): Promise<void> {
27+
const apiToken = getDefaultToken()
28+
if (!apiToken) {
29+
throw new AuthError(
30+
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
31+
)
32+
}
33+
34+
await listReposWithToken({
35+
apiToken,
36+
direction,
37+
orgSlug,
38+
outputKind,
39+
page,
40+
per_page,
41+
sort
42+
})
43+
}
44+
45+
async function listReposWithToken({
46+
apiToken,
47+
direction,
48+
orgSlug,
49+
outputKind,
50+
page,
51+
per_page,
52+
sort
53+
}: {
54+
apiToken: string
2555
direction: string
26-
per_page: number
56+
orgSlug: string
57+
outputKind: 'json' | 'markdown' | 'print'
2758
page: number
28-
apiToken: string
59+
per_page: number
60+
sort: string
2961
}): Promise<void> {
3062
// Lazily access constants.spinner.
3163
const { spinner } = constants
3264

33-
spinner.start('Listing repositories...')
65+
spinner.start('Fetching list of repositories...')
3466

3567
const socketSdk = await setupSdk(apiToken)
3668
const result = await handleApiCall(
3769
socketSdk.getOrgRepoList(orgSlug, {
38-
outputJson,
39-
outputMarkdown,
40-
orgSlug,
4170
sort,
4271
direction,
4372
per_page,
@@ -51,9 +80,9 @@ export async function listRepos({
5180
return
5281
}
5382

54-
spinner.stop()
83+
spinner.stop('Fetch complete.')
5584

56-
if (outputJson) {
85+
if (outputKind === 'json') {
5786
const data = result.data.results.map(o => ({
5887
id: o.id,
5988
name: o.name,

0 commit comments

Comments
 (0)