Skip to content

Commit f953a4d

Browse files
pvdzjdalton
andauthored
Update and cleanup repos command (#355)
* Update and cleanup repos command * Drop output flags where they are currently not used --------- Co-authored-by: John-David Dalton <jdalton@users.noreply.github.com>
1 parent fdb956d commit f953a4d

File tree

10 files changed

+216
-108
lines changed

10 files changed

+216
-108
lines changed

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger'
55

66
import { createRepo } from './create-repo'
77
import constants from '../../constants'
8-
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
8+
import { commonFlags } from '../../flags'
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,6 @@ const config: CliCommandConfig = {
2119
hidden: false,
2220
flags: {
2321
...commonFlags,
24-
...outputFlags,
2522
repoName: {
2623
type: 'string',
2724
shortFlag: 'n',
@@ -104,22 +101,12 @@ async function run(
104101
return
105102
}
106103

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-
114104
await createRepo({
115-
outputJson: Boolean(cli.flags['json']),
116-
outputMarkdown: Boolean(cli.flags['markdown']),
117105
orgSlug,
118106
repoName,
119107
description: String(cli.flags['repoDescription'] || ''),
120108
homepage: String(cli.flags['homepage'] || ''),
121109
default_branch: String(cli.flags['defaultBranch'] || ''),
122-
visibility: String(cli.flags['visibility'] || 'private'),
123-
apiToken
110+
visibility: String(cli.flags['visibility'] || 'private')
124111
})
125112
}

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: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import { logger } from '@socketsecurity/registry/lib/logger'
55

66
import { updateRepo } from './update-repo'
77
import constants from '../../constants'
8-
import { commonFlags, outputFlags } from '../../flags'
9-
import { AuthError } from '../../utils/errors'
8+
import { commonFlags } from '../../flags'
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,6 @@ const config: CliCommandConfig = {
2119
hidden: false,
2220
flags: {
2321
...commonFlags,
24-
...outputFlags,
2522
repoName: {
2623
type: 'string',
2724
shortFlag: 'n',
@@ -106,17 +103,7 @@ async function run(
106103
return
107104
}
108105

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-
116106
await updateRepo({
117-
apiToken,
118-
outputJson: Boolean(cli.flags['json']),
119-
outputMarkdown: Boolean(cli.flags['markdown']),
120107
orgSlug,
121108
repoName,
122109
description: String(cli.flags['repoDescription'] || ''),

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: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
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+
repoName,
12+
visibility
13+
}: {
14+
orgSlug: string
15+
repoName: string
16+
description: string
17+
homepage: string
18+
default_branch: string
19+
visibility: string
20+
}): Promise<void> {
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+
await createRepoWithToken({
29+
apiToken,
30+
default_branch,
31+
description,
32+
homepage,
33+
orgSlug,
34+
repoName,
35+
visibility
36+
})
37+
}
38+
39+
async function createRepoWithToken({
640
apiToken,
741
default_branch,
842
description,
943
homepage,
1044
orgSlug,
11-
outputJson,
12-
outputMarkdown,
1345
repoName,
1446
visibility
1547
}: {
1648
apiToken: string
17-
outputJson: boolean
18-
outputMarkdown: boolean
1949
orgSlug: string
2050
repoName: string
2151
description: string
@@ -31,9 +61,6 @@ export async function createRepo({
3161
const socketSdk = await setupSdk(apiToken)
3262
const result = await handleApiCall(
3363
socketSdk.createOrgRepo(orgSlug, {
34-
outputJson,
35-
outputMarkdown,
36-
orgSlug,
3764
name: repoName,
3865
description,
3966
homepage,
@@ -43,9 +70,10 @@ export async function createRepo({
4370
'creating repository'
4471
)
4572

46-
if (result.success) {
47-
spinner.successAndStop('Repository created successfully')
48-
} else {
73+
if (!result.success) {
4974
handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
75+
return
5076
}
77+
78+
spinner.successAndStop('Repository created successfully')
5179
}

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
}

0 commit comments

Comments
 (0)