Skip to content

Commit 2982abe

Browse files
committed
Cleanup enterprise detection and organization use
1 parent ef3f347 commit 2982abe

29 files changed

Lines changed: 202 additions & 154 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import type { SetupSdkOptions } from '../../utils/sdk.mts'
66
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type FetchOrgAnalyticsDataOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
sdkOpts?: SetupSdkOptions | undefined
1010
}
1111

1212
export async function fetchOrgAnalyticsData(
1313
time: number,
1414
options?: FetchOrgAnalyticsDataOptions | undefined,
1515
): Promise<CResult<SocketSdkSuccessResult<'getOrgAnalytics'>['data']>> {
16-
const { sdkOptions } = {
16+
const { sdkOpts } = {
1717
__proto__: null,
1818
...options,
1919
} as FetchOrgAnalyticsDataOptions
2020

21-
const sockSdkCResult = await setupSdk(sdkOptions)
21+
const sockSdkCResult = await setupSdk(sdkOpts)
2222
if (!sockSdkCResult.ok) {
2323
return sockSdkCResult
2424
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import type { SetupSdkOptions } from '../../utils/sdk.mts'
66
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type RepoAnalyticsDataOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
sdkOpts?: SetupSdkOptions | undefined
1010
}
1111

1212
export async function fetchRepoAnalyticsData(
1313
repo: string,
1414
time: number,
1515
options?: RepoAnalyticsDataOptions | undefined,
1616
): Promise<CResult<SocketSdkSuccessResult<'getRepoAnalytics'>['data']>> {
17-
const { sdkOptions } = {
17+
const { sdkOpts } = {
1818
__proto__: null,
1919
...options,
2020
} as RepoAnalyticsDataOptions
2121

22-
const sockSdkCResult = await setupSdk(sdkOptions)
22+
const sockSdkCResult = await setupSdk(sdkOpts)
2323
if (!sockSdkCResult.ok) {
2424
return sockSdkCResult
2525
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ export type FetchAuditLogsConfig = {
1414
}
1515

1616
export type FetchAuditLogOptions = {
17-
sdkOptions?: SetupSdkOptions | undefined
17+
sdkOpts?: SetupSdkOptions | undefined
1818
}
1919

2020
export async function fetchAuditLog(
2121
config: FetchAuditLogsConfig,
2222
options?: FetchAuditLogOptions | undefined,
2323
): Promise<CResult<SocketSdkSuccessResult<'getAuditLogEvents'>['data']>> {
24-
const { sdkOptions } = { __proto__: null, ...options } as FetchAuditLogOptions
24+
const { sdkOpts } = { __proto__: null, ...options } as FetchAuditLogOptions
2525

26-
const sockSdkCResult = await setupSdk(sdkOptions)
26+
const sockSdkCResult = await setupSdk(sdkOpts)
2727
if (!sockSdkCResult.ok) {
2828
return sockSdkCResult
2929
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isSupportedConfigKey } from '../../utils/config.mts'
2+
import { getOrgSlugs } from '../../utils/organization.mts'
23
import { hasDefaultToken } from '../../utils/sdk.mts'
34
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
45

@@ -136,10 +137,10 @@ async function getDefaultOrgFromToken(): Promise<
136137
}
137138

138139
const { organizations } = orgsCResult.data
139-
const slugs = Array.from(Object.values(organizations)).map(o => o.slug)
140-
if (slugs.length === 0) {
140+
if (organizations.length === 0) {
141141
return undefined
142142
}
143+
const slugs = getOrgSlugs(organizations)
143144
if (slugs.length === 1) {
144145
return slugs[0]
145146
}
@@ -153,9 +154,5 @@ async function getEnforceableOrgsFromToken(): Promise<string[] | undefined> {
153154
}
154155

155156
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
157+
return organizations.length ? getOrgSlugs(organizations) : undefined
161158
}

src/commands/login/attempt-login.mts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import terminalLink from 'terminal-link'
22

3+
import { joinAnd } from '@socketsecurity/registry/lib/arrays'
34
import { logger } from '@socketsecurity/registry/lib/logger'
45
import { confirm, password, select } from '@socketsecurity/registry/lib/prompts'
56

67
import { applyLogin } from './apply-login.mts'
78
import constants from '../../constants.mts'
8-
import { handleApiCall } from '../../utils/api.mts'
99
import {
1010
getConfigValueOrUndef,
1111
isReadOnlyConfig,
1212
updateConfigValue,
1313
} from '../../utils/config.mts'
1414
import { failMsgWithBadge } from '../../utils/fail-msg-with-badge.mts'
15+
import { getEnterpriseOrgs, getOrgSlugs } from '../../utils/organization.mts'
1516
import { setupSdk } from '../../utils/sdk.mts'
1617
import { setupTabCompletion } from '../install/setup-tab-completion.mts'
18+
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
1719

1820
import type { Choice, Separator } from '@socketsecurity/registry/lib/prompts'
1921

@@ -50,8 +52,9 @@ export async function attemptLogin(
5052

5153
const sockSdk = sockSdkCResult.data
5254

53-
const orgsCResult = await handleApiCall(sockSdk.getOrganizations(), {
55+
const orgsCResult = await fetchOrganization({
5456
desc: 'token verification',
57+
sdk: sockSdk,
5558
})
5659
if (!orgsCResult.ok) {
5760
process.exitCode = 1
@@ -60,27 +63,31 @@ export async function attemptLogin(
6063
}
6164

6265
const { organizations } = orgsCResult.data
63-
const orgSlugs = Object.values(organizations).map(obj => obj.slug)
6466

65-
logger.success(`API token verified: ${orgSlugs}`)
67+
const orgSlugs = getOrgSlugs(organizations)
6668

67-
const enforcedChoices: OrgChoices = Object.values(organizations)
68-
.filter(org => org?.plan === 'enterprise')
69-
.map(org => ({
70-
name: org.name ?? 'undefined',
71-
value: org.id,
72-
}))
69+
logger.success(`API token verified: ${joinAnd(orgSlugs)}`)
70+
71+
const enterpriseOrgs = getEnterpriseOrgs(organizations)
72+
73+
const enforcedChoices: OrgChoices = enterpriseOrgs.map(org => ({
74+
name: org.name ?? 'undefined',
75+
value: org.id,
76+
}))
7377

7478
let enforcedOrgs: string[] = []
7579
if (enforcedChoices.length > 1) {
7680
const id = await select({
7781
message:
7882
"Which organization's policies should Socket enforce system-wide?",
79-
choices: enforcedChoices.concat({
80-
name: 'None',
81-
value: '',
82-
description: 'Pick "None" if this is a personal device',
83-
}),
83+
choices: [
84+
...enforcedChoices,
85+
{
86+
name: 'None',
87+
value: '',
88+
description: 'Pick "None" if this is a personal device',
89+
},
90+
],
8491
})
8592
if (id === undefined) {
8693
logger.fail('Canceled by user')

src/commands/organization/fetch-dependencies.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ export type FetchDependenciesConfig = {
1111
}
1212

1313
export type FetchDependenciesOptions = {
14-
sdkOptions?: SetupSdkOptions | undefined
14+
sdkOpts?: SetupSdkOptions | undefined
1515
}
1616

1717
export async function fetchDependencies(
1818
config: FetchDependenciesConfig,
1919
options?: FetchDependenciesOptions | undefined,
2020
): Promise<CResult<SocketSdkSuccessResult<'searchDependencies'>['data']>> {
21-
const { sdkOptions } = {
21+
const { sdkOpts } = {
2222
__proto__: null,
2323
...options,
2424
} as FetchDependenciesOptions
2525

26-
const sockSdkCResult = await setupSdk(sdkOptions)
26+
const sockSdkCResult = await setupSdk(sdkOpts)
2727
if (!sockSdkCResult.ok) {
2828
return sockSdkCResult
2929
}

src/commands/organization/fetch-license-policy.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import type { SetupSdkOptions } from '../../utils/sdk.mts'
66
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type FetchLicensePolicyOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
sdkOpts?: SetupSdkOptions | undefined
1010
}
1111

1212
export async function fetchLicensePolicy(
1313
orgSlug: string,
1414
options?: FetchLicensePolicyOptions | undefined,
1515
): Promise<CResult<SocketSdkSuccessResult<'getOrgLicensePolicy'>['data']>> {
16-
const { sdkOptions } = {
16+
const { sdkOpts } = {
1717
__proto__: null,
1818
...options,
1919
} as FetchLicensePolicyOptions
2020

21-
const sockSdkCResult = await setupSdk(sdkOptions)
21+
const sockSdkCResult = await setupSdk(sdkOpts)
2222
if (!sockSdkCResult.ok) {
2323
return sockSdkCResult
2424
}

src/commands/organization/fetch-organization-list.mts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,59 @@ import { setupSdk } from '../../utils/sdk.mts'
33

44
import type { CResult } from '../../types.mts'
55
import type { SetupSdkOptions } from '../../utils/sdk.mts'
6-
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
6+
import type { SocketSdk, SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type FetchOrganizationOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
desc?: string | undefined
10+
sdk?: SocketSdk | undefined
11+
sdkOpts?: SetupSdkOptions | undefined
1012
}
1113

14+
export type EnterpriseOrganization = Omit<Organization, 'plan'> & {
15+
plan: 'enterprise'
16+
}
17+
18+
export type EnterpriseOrganizations = EnterpriseOrganization[]
19+
20+
export type Organization =
21+
SocketSdkSuccessResult<'getOrganizations'>['data']['organizations'][string]
22+
23+
export type Organizations = Organization[]
24+
25+
export type OrganizationsData = { organizations: Organizations }
26+
27+
export type OrganizationsCResult = CResult<OrganizationsData>
28+
1229
export async function fetchOrganization(
1330
options?: FetchOrganizationOptions | undefined,
14-
): Promise<CResult<SocketSdkSuccessResult<'getOrganizations'>['data']>> {
15-
const { sdkOptions } = {
31+
): Promise<OrganizationsCResult> {
32+
const {
33+
desc = 'organization list',
34+
sdk,
35+
sdkOpts,
36+
} = {
1637
__proto__: null,
1738
...options,
1839
} as FetchOrganizationOptions
1940

20-
const sockSdkCResult = await setupSdk(sdkOptions)
21-
if (!sockSdkCResult.ok) {
22-
return sockSdkCResult
41+
let sockSdk = sdk
42+
if (!sockSdk) {
43+
const sockSdkCResult = await setupSdk(sdkOpts)
44+
if (!sockSdkCResult.ok) {
45+
return sockSdkCResult
46+
}
47+
sockSdk = sockSdkCResult.data
2348
}
24-
const sockSdk = sockSdkCResult.data
2549

26-
return await handleApiCall(sockSdk.getOrganizations(), {
27-
desc: 'organization list',
28-
})
50+
const orgsCResult = await handleApiCall(sockSdk.getOrganizations(), { desc })
51+
if (!orgsCResult.ok) {
52+
return orgsCResult
53+
}
54+
55+
return {
56+
...orgsCResult,
57+
data: {
58+
organizations: Object.values(orgsCResult.data.organizations),
59+
},
60+
}
2961
}

src/commands/organization/fetch-quota.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import type { SetupSdkOptions } from '../../utils/sdk.mts'
66
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type FetchQuotaOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
sdkOpts?: SetupSdkOptions | undefined
1010
}
1111

1212
export async function fetchQuota(
1313
options?: FetchQuotaOptions | undefined,
1414
): Promise<CResult<SocketSdkSuccessResult<'getQuota'>['data']>> {
15-
const { sdkOptions } = { __proto__: null, ...options } as FetchQuotaOptions
15+
const { sdkOpts } = { __proto__: null, ...options } as FetchQuotaOptions
1616

17-
const sockSdkCResult = await setupSdk(sdkOptions)
17+
const sockSdkCResult = await setupSdk(sdkOpts)
1818
if (!sockSdkCResult.ok) {
1919
return sockSdkCResult
2020
}

src/commands/organization/fetch-security-policy.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import type { SetupSdkOptions } from '../../utils/sdk.mts'
66
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
77

88
export type FetchSecurityPolicyOptions = {
9-
sdkOptions?: SetupSdkOptions | undefined
9+
sdkOpts?: SetupSdkOptions | undefined
1010
}
1111

1212
export async function fetchSecurityPolicy(
1313
orgSlug: string,
1414
options?: FetchSecurityPolicyOptions | undefined,
1515
): Promise<CResult<SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']>> {
16-
const { sdkOptions } = {
16+
const { sdkOpts } = {
1717
__proto__: null,
1818
...options,
1919
} as FetchSecurityPolicyOptions
2020

21-
const sockSdkCResult = await setupSdk(sdkOptions)
21+
const sockSdkCResult = await setupSdk(sdkOpts)
2222
if (!sockSdkCResult.ok) {
2323
return sockSdkCResult
2424
}

0 commit comments

Comments
 (0)