Skip to content

Commit 8a69483

Browse files
Merge pull request #121 from SocketDev/cg/addReposCommand
Add repos command
2 parents d6f7dd9 + 3a61d87 commit 8a69483

File tree

9 files changed

+759
-5
lines changed

9 files changed

+759
-5
lines changed

lib/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './report/index.js'
1010
export * from './wrapper/index.js'
1111
export * from './scan/index.js'
1212
export * from './audit-log/index.js'
13+
export * from './repos/index.js'

lib/commands/repos/create.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* eslint-disable no-console */
2+
3+
import chalk from 'chalk'
4+
import meow from 'meow'
5+
import ora from 'ora'
6+
7+
import { outputFlags } from '../../flags/index.js'
8+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
9+
import { prepareFlags } from '../../utils/flags.js'
10+
import { printFlagList } from '../../utils/formatting.js'
11+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
12+
13+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
14+
export const create = {
15+
description: 'Create a repository in an organization',
16+
async run (argv, importMeta, { parentName }) {
17+
const name = parentName + ' create'
18+
19+
const input = setupCommand(name, create.description, argv, importMeta)
20+
if (input) {
21+
const spinnerText = 'Creating repository... \n'
22+
const spinner = ora(spinnerText).start()
23+
await createRepo(input.orgSlug, input, spinner)
24+
}
25+
}
26+
}
27+
28+
const repositoryCreationFlags = prepareFlags({
29+
repoName: {
30+
type: 'string',
31+
shortFlag: 'n',
32+
default: '',
33+
description: 'Repository name',
34+
},
35+
repoDescription: {
36+
type: 'string',
37+
shortFlag: 'd',
38+
default: '',
39+
description: 'Repository description',
40+
},
41+
homepage: {
42+
type: 'string',
43+
shortFlag: 'h',
44+
default: '',
45+
description: 'Repository url',
46+
},
47+
defaultBranch: {
48+
type: 'string',
49+
shortFlag: 'b',
50+
default: 'main',
51+
description: 'Repository default branch',
52+
},
53+
visibility: {
54+
type: 'string',
55+
shortFlag: 'v',
56+
default: 'private',
57+
description: 'Repository visibility (Default Private)',
58+
}
59+
})
60+
61+
// Internal functions
62+
63+
/**
64+
* @typedef CommandContext
65+
* @property {boolean} outputJson
66+
* @property {boolean} outputMarkdown
67+
* @property {string} orgSlug
68+
* @property {string} name
69+
* @property {string} description
70+
* @property {string} homepage
71+
* @property {string} default_branch
72+
* @property {string} visibility
73+
*/
74+
75+
/**
76+
* @param {string} name
77+
* @param {string} description
78+
* @param {readonly string[]} argv
79+
* @param {ImportMeta} importMeta
80+
* @returns {void|CommandContext}
81+
*/
82+
function setupCommand (name, description, argv, importMeta) {
83+
const flags = {
84+
...outputFlags,
85+
...repositoryCreationFlags
86+
}
87+
88+
const cli = meow(`
89+
Usage
90+
$ ${name} <org slug>
91+
92+
Options
93+
${printFlagList(flags, 6)}
94+
95+
Examples
96+
$ ${name} FakeOrg --repoName=test-repo
97+
`, {
98+
argv,
99+
description,
100+
importMeta,
101+
flags
102+
})
103+
104+
const {
105+
json: outputJson,
106+
markdown: outputMarkdown,
107+
repoName,
108+
repoDescription,
109+
homepage,
110+
defaultBranch,
111+
visibility
112+
} = cli.flags
113+
114+
const [orgSlug = ''] = cli.input
115+
116+
if (!orgSlug) {
117+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug \n`)
118+
cli.showHelp()
119+
return
120+
}
121+
122+
if (!repoName) {
123+
console.error(`${chalk.bgRed('Input error')}: Repository name is required. \n`)
124+
cli.showHelp()
125+
return
126+
}
127+
128+
return {
129+
outputJson,
130+
outputMarkdown,
131+
orgSlug,
132+
name: repoName,
133+
description: repoDescription,
134+
homepage,
135+
default_branch: defaultBranch,
136+
visibility
137+
}
138+
}
139+
140+
/**
141+
* @typedef RepositoryData
142+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'createOrgRepo'>["data"]} data
143+
*/
144+
145+
/**
146+
* @param {string} orgSlug
147+
* @param {CommandContext} input
148+
* @param {import('ora').Ora} spinner
149+
* @returns {Promise<void|RepositoryData>}
150+
*/
151+
async function createRepo (orgSlug, input, spinner) {
152+
const socketSdk = await setupSdk(getDefaultKey())
153+
const result = await handleApiCall(socketSdk.createOrgRepo(orgSlug, input), 'creating repository')
154+
155+
if (!result.success) {
156+
return handleUnsuccessfulApiResponse('createOrgRepo', result, spinner)
157+
}
158+
159+
spinner.stop()
160+
161+
console.log('\n✅ Repository created successfully \n')
162+
163+
return {
164+
data: result.data
165+
}
166+
}

lib/commands/repos/delete.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* eslint-disable no-console */
2+
3+
import chalk from 'chalk'
4+
import meow from 'meow'
5+
import ora from 'ora'
6+
7+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
8+
import { getDefaultKey, setupSdk } from '../../utils/sdk.js'
9+
10+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
11+
export const del = {
12+
description: 'Delete a repository in an organization',
13+
async run (argv, importMeta, { parentName }) {
14+
const name = parentName + ' del'
15+
16+
const input = setupCommand(name, del.description, argv, importMeta)
17+
if (input) {
18+
const spinnerText = 'Deleting repository... \n'
19+
const spinner = ora(spinnerText).start()
20+
await deleteRepository(input.orgSlug, input.repoName, spinner)
21+
}
22+
}
23+
}
24+
25+
// Internal functions
26+
27+
/**
28+
* @typedef CommandContext
29+
* @property {string} orgSlug
30+
* @property {string} repoName
31+
*/
32+
33+
/**
34+
* @param {string} name
35+
* @param {string} description
36+
* @param {readonly string[]} argv
37+
* @param {ImportMeta} importMeta
38+
* @returns {void|CommandContext}
39+
*/
40+
function setupCommand (name, description, argv, importMeta) {
41+
const cli = meow(`
42+
Usage
43+
$ ${name} <org slug> <repo slug>
44+
45+
Examples
46+
$ ${name} FakeOrg test-repo
47+
`, {
48+
argv,
49+
description,
50+
importMeta
51+
})
52+
53+
const [orgSlug = '', repoName = ''] = cli.input
54+
55+
if (!orgSlug || !repoName) {
56+
console.error(`${chalk.bgRed('Input error')}: Please provide an organization slug and repository slug \n`)
57+
cli.showHelp()
58+
return
59+
}
60+
61+
return {
62+
orgSlug,
63+
repoName
64+
}
65+
}
66+
67+
/**
68+
* @typedef RepositoryData
69+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'deleteOrgRepo'>["data"]} data
70+
*/
71+
72+
/**
73+
* @param {string} orgSlug
74+
* @param {string} repoName
75+
* @param {import('ora').Ora} spinner
76+
* @returns {Promise<void|RepositoryData>}
77+
*/
78+
async function deleteRepository (orgSlug, repoName, spinner) {
79+
const socketSdk = await setupSdk(getDefaultKey())
80+
const result = await handleApiCall(socketSdk.deleteOrgRepo(orgSlug, repoName), 'deleting repository')
81+
82+
if (!result.success) {
83+
return handleUnsuccessfulApiResponse('deleteOrgRepo', result, spinner)
84+
}
85+
86+
spinner.stop()
87+
88+
console.log('\n✅ Repository deleted successfully \n')
89+
90+
return {
91+
data: result.data
92+
}
93+
}

lib/commands/repos/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { create } from './create.js'
2+
import { del } from './delete.js'
3+
import { list } from './list.js'
4+
import { update } from './update.js'
5+
import { view } from './view.js'
6+
import { meowWithSubcommands } from '../../utils/meow-with-subcommands.js'
7+
8+
const description = 'Repositories related commands'
9+
10+
/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */
11+
export const repo = {
12+
description,
13+
run: async (argv, importMeta, { parentName }) => {
14+
await meowWithSubcommands(
15+
{
16+
create,
17+
view,
18+
list,
19+
del,
20+
update
21+
},
22+
{
23+
argv,
24+
description,
25+
importMeta,
26+
name: parentName + ' repo',
27+
}
28+
)
29+
}
30+
}

0 commit comments

Comments
 (0)