Skip to content

Commit 378c69b

Browse files
committed
Cleanup sdk api usage
1 parent 4915a63 commit 378c69b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+580
-357
lines changed

src/commands/analytics/fetch-org-analytics.mts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@ import { handleApiCall } from '../../utils/api.mts'
22
import { setupSdk } from '../../utils/sdk.mts'
33

44
import type { CResult } from '../../types.mts'
5+
import type { SetupSdkOptions } from '../../utils/sdk.mts'
56
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
67

8+
export type FetchOrgAnalyticsDataOptions = {
9+
sdkOptions?: SetupSdkOptions | undefined
10+
}
11+
712
export async function fetchOrgAnalyticsData(
813
time: number,
14+
options?: FetchOrgAnalyticsDataOptions | undefined,
915
): Promise<CResult<SocketSdkSuccessResult<'getOrgAnalytics'>['data']>> {
10-
const sockSdkCResult = await setupSdk()
16+
const { sdkOptions } = {
17+
__proto__: null,
18+
...options,
19+
} as FetchOrgAnalyticsDataOptions
20+
21+
const sockSdkCResult = await setupSdk(sdkOptions)
1122
if (!sockSdkCResult.ok) {
1223
return sockSdkCResult
1324
}
1425
const sockSdk = sockSdkCResult.data
1526

16-
return await handleApiCall(
17-
sockSdk.getOrgAnalytics(time.toString()),
18-
'analytics data',
19-
)
27+
return await handleApiCall(sockSdk.getOrgAnalytics(time.toString()), {
28+
desc: 'analytics data',
29+
})
2030
}

src/commands/analytics/fetch-repo-analytics.mts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@ import { handleApiCall } from '../../utils/api.mts'
22
import { setupSdk } from '../../utils/sdk.mts'
33

44
import type { CResult } from '../../types.mts'
5+
import type { SetupSdkOptions } from '../../utils/sdk.mts'
56
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
67

8+
export type RepoAnalyticsDataOptions = {
9+
sdkOptions?: SetupSdkOptions | undefined
10+
}
11+
712
export async function fetchRepoAnalyticsData(
813
repo: string,
914
time: number,
15+
options?: RepoAnalyticsDataOptions | undefined,
1016
): Promise<CResult<SocketSdkSuccessResult<'getRepoAnalytics'>['data']>> {
11-
const sockSdkCResult = await setupSdk()
17+
const { sdkOptions } = {
18+
__proto__: null,
19+
...options,
20+
} as RepoAnalyticsDataOptions
21+
22+
const sockSdkCResult = await setupSdk(sdkOptions)
1223
if (!sockSdkCResult.ok) {
1324
return sockSdkCResult
1425
}
1526
const sockSdk = sockSdkCResult.data
1627

17-
return await handleApiCall(
18-
sockSdk.getRepoAnalytics(repo, time.toString()),
19-
'analytics data',
20-
)
28+
return await handleApiCall(sockSdk.getRepoAnalytics(repo, time.toString()), {
29+
desc: 'analytics data',
30+
})
2131
}

src/commands/audit-log/fetch-audit-log.mts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@ import { handleApiCall } from '../../utils/api.mts'
22
import { setupSdk } from '../../utils/sdk.mts'
33

44
import type { CResult, OutputKind } from '../../types.mts'
5+
import type { SetupSdkOptions } from '../../utils/sdk.mts'
56
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
67

7-
export async function fetchAuditLog({
8-
logType,
9-
orgSlug,
10-
outputKind,
11-
page,
12-
perPage,
13-
}: {
14-
outputKind: OutputKind
8+
export type FetchAuditLogsConfig = {
9+
logType: string
1510
orgSlug: string
11+
outputKind: OutputKind
1612
page: number
1713
perPage: number
18-
logType: string
19-
}): Promise<CResult<SocketSdkSuccessResult<'getAuditLogEvents'>['data']>> {
20-
const sockSdkCResult = await setupSdk()
14+
}
15+
16+
export type FetchAuditLogOptions = {
17+
sdkOptions?: SetupSdkOptions | undefined
18+
}
19+
20+
export async function fetchAuditLog(
21+
config: FetchAuditLogsConfig,
22+
options?: FetchAuditLogOptions | undefined,
23+
): Promise<CResult<SocketSdkSuccessResult<'getAuditLogEvents'>['data']>> {
24+
const { sdkOptions } = { __proto__: null, ...options } as FetchAuditLogOptions
25+
26+
const sockSdkCResult = await setupSdk(sdkOptions)
2127
if (!sockSdkCResult.ok) {
2228
return sockSdkCResult
2329
}
2430
const sockSdk = sockSdkCResult.data
2531

32+
const { logType, orgSlug, outputKind, page, perPage } = {
33+
__proto__: null,
34+
...config,
35+
} as FetchAuditLogsConfig
36+
2637
return await handleApiCall(
2738
sockSdk.getAuditLogEvents(orgSlug, {
2839
// I'm not sure this is used at all.
@@ -34,6 +45,6 @@ export async function fetchAuditLog({
3445
page: String(page),
3546
per_page: String(perPage),
3647
}),
37-
`audit log for ${orgSlug}`,
48+
{ desc: `audit log for ${orgSlug}` },
3849
)
3950
}

src/commands/audit-log/handle-audit-log.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ export async function handleAuditLog({
1010
page,
1111
perPage,
1212
}: {
13+
logType: string
1314
outputKind: OutputKind
1415
orgSlug: string
1516
page: number
1617
perPage: number
17-
logType: string
1818
}): Promise<void> {
1919
const auditLogs = await fetchAuditLog({
20+
logType,
2021
orgSlug,
2122
outputKind,
2223
page,
2324
perPage,
24-
logType,
2525
})
2626

2727
await outputAuditLog(auditLogs, {

src/commands/audit-log/output-audit-log.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export async function outputAuditLog(
2626
page,
2727
perPage,
2828
}: {
29+
logType: string
2930
outputKind: OutputKind
3031
orgSlug: string
3132
page: number
3233
perPage: number
33-
logType: string
3434
},
3535
): Promise<void> {
3636
if (!result.ok) {
@@ -96,10 +96,10 @@ export async function outputAsJson(
9696
page,
9797
perPage,
9898
}: {
99+
logType: string
99100
orgSlug: string
100101
page: number
101102
perPage: number
102-
logType: string
103103
},
104104
): Promise<string> {
105105
if (!auditLogs.ok) {
@@ -112,10 +112,10 @@ export async function outputAsJson(
112112
desc: 'Audit logs for given query',
113113
// Lazily access constants.ENV.VITEST.
114114
generated: constants.ENV.VITEST ? REDACTED : new Date().toISOString(),
115-
org: orgSlug,
116115
logType,
117-
page,
118116
nextPage: auditLogs.data.nextPage,
117+
org: orgSlug,
118+
page,
119119
perPage,
120120
logs: auditLogs.data.results.map(log => {
121121
// Note: The subset is pretty arbitrary

src/commands/audit-log/output-audit-log.test.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ describe('output-audit-log', () => {
2525
"data": {
2626
"desc": "Audit logs for given query",
2727
"generated": "<redacted>",
28-
"org": "noorgslug",
2928
"logType": "",
30-
"page": 1,
3129
"nextPage": "2",
30+
"org": "noorgslug",
31+
"page": 1,
3232
"perPage": 10,
3333
"logs": [
3434
{

src/commands/ci/fetch-default-org-slug.mts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,25 @@
11
import { debugFn } from '@socketsecurity/registry/lib/debug'
22

3-
import { handleApiCall } from '../../utils/api.mts'
43
import { getConfigValueOrUndef } from '../../utils/config.mts'
5-
import { setupSdk } from '../../utils/sdk.mts'
4+
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
65

76
import type { CResult } from '../../types.mts'
87

98
// Use the config defaultOrg when set, otherwise discover from remote.
109
export async function getDefaultOrgSlug(): Promise<CResult<string>> {
1110
const defaultOrgResult = getConfigValueOrUndef('defaultOrg')
12-
1311
if (defaultOrgResult) {
1412
debugFn('notice', 'use: default org', defaultOrgResult)
1513
return { ok: true, data: defaultOrgResult }
1614
}
1715

18-
const sockSdkCResult = await setupSdk()
19-
if (!sockSdkCResult.ok) {
20-
return sockSdkCResult
21-
}
22-
const sockSdk = sockSdkCResult.data
23-
24-
const result = await handleApiCall(
25-
sockSdk.getOrganizations(),
26-
'list of organizations',
27-
)
28-
29-
if (!result.ok) {
30-
return result
16+
const orgsCResult = await fetchOrganization()
17+
if (!orgsCResult.ok) {
18+
return orgsCResult
3119
}
3220

33-
const { organizations } = result.data
21+
const { organizations } = orgsCResult.data
3422
const keys = Object.keys(organizations)
35-
3623
if (!keys.length) {
3724
return {
3825
ok: false,
@@ -42,7 +29,6 @@ export async function getDefaultOrgSlug(): Promise<CResult<string>> {
4229
}
4330

4431
const slug = (organizations as any)[keys[0]!]?.name ?? undefined
45-
4632
if (!slug) {
4733
return {
4834
ok: false,

src/commands/ci/handle-ci.mts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
1111
// description: 'Alias for "report create --view --strict"',
1212
// argv: ['report', 'create', '--view', '--strict']
1313
// }
14-
const result = await getDefaultOrgSlug()
15-
if (!result.ok) {
16-
process.exitCode = result.code ?? 1
14+
const orgSlugCResult = await getDefaultOrgSlug()
15+
if (!orgSlugCResult.ok) {
16+
process.exitCode = orgSlugCResult.code ?? 1
1717
// Always assume json mode.
18-
logger.log(serializeResultJson(result))
18+
logger.log(serializeResultJson(orgSlugCResult))
1919
return
2020
}
2121

22+
const orgSlug = orgSlugCResult.data
2223
const cwd = process.cwd()
2324
// Lazily access constants.SOCKET_DEFAULT_BRANCH.
2425
const branchName = (await gitBranch(cwd)) || constants.SOCKET_DEFAULT_BRANCH
@@ -35,7 +36,7 @@ export async function handleCi(autoManifest: boolean): Promise<void> {
3536
cwd,
3637
defaultBranch: false,
3738
interactive: false,
38-
orgSlug: result.data,
39+
orgSlug,
3940
outputKind: 'json',
4041
// When 'pendingHead' is true, it requires 'branchName' set and 'tmp' false.
4142
pendingHead: true,

src/commands/config/discover-config-value.mts

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { handleApiCall } from '../../utils/api.mts'
21
import { isSupportedConfigKey } from '../../utils/config.mts'
3-
import { hasDefaultToken, setupSdk } from '../../utils/sdk.mts'
2+
import { hasDefaultToken } from '../../utils/sdk.mts'
3+
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
44

55
import type { CResult } from '../../types.mts'
66

@@ -130,54 +130,32 @@ export async function discoverConfigValue(
130130
async function getDefaultOrgFromToken(): Promise<
131131
string[] | string | undefined
132132
> {
133-
const sockSdkCResult = await setupSdk()
134-
if (!sockSdkCResult.ok) {
133+
const orgsCResult = await fetchOrganization()
134+
if (!orgsCResult.ok) {
135135
return undefined
136136
}
137-
const sockSdk = sockSdkCResult.data
138-
139-
const result = await handleApiCall(
140-
sockSdk.getOrganizations(),
141-
'list of organizations',
142-
)
143-
144-
if (result.ok) {
145-
const arr = Array.from(Object.values(result.data.organizations)).map(
146-
({ slug }) => slug,
147-
)
148-
if (arr.length === 0) {
149-
return undefined
150-
}
151-
if (arr.length === 1) {
152-
return arr[0]
153-
}
154-
return arr
155-
}
156137

157-
return undefined
138+
const { organizations } = orgsCResult.data
139+
const slugs = Array.from(Object.values(organizations)).map(o => o.slug)
140+
if (slugs.length === 0) {
141+
return undefined
142+
}
143+
if (slugs.length === 1) {
144+
return slugs[0]
145+
}
146+
return slugs
158147
}
159148

160149
async function getEnforceableOrgsFromToken(): Promise<string[] | undefined> {
161-
const sockSdkCResult = await setupSdk()
162-
if (!sockSdkCResult.ok) {
150+
const orgsCResult = await fetchOrganization()
151+
if (!orgsCResult.ok) {
163152
return undefined
164153
}
165-
const sockSdk = sockSdkCResult.data
166-
167-
const result = await handleApiCall(
168-
sockSdk.getOrganizations(),
169-
'list of organizations',
170-
)
171-
172-
if (result.ok) {
173-
const arr = Array.from(Object.values(result.data.organizations)).map(
174-
({ slug }) => slug,
175-
)
176-
if (arr.length === 0) {
177-
return undefined
178-
}
179-
return arr
180-
}
181154

182-
return undefined
155+
const { organizations } = orgsCResult.data
156+
const slugs = Array.from(Object.values(organizations)).map(o => o.slug)
157+
if (!slugs.length) {
158+
return undefined
159+
}
160+
return slugs
183161
}

0 commit comments

Comments
 (0)