Skip to content

Commit 86702fc

Browse files
committed
Cleanup org name and slug use
1 parent b672927 commit 86702fc

File tree

4 files changed

+32
-48
lines changed

4 files changed

+32
-48
lines changed

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export async function discoverConfigValue(
5959
}
6060
}
6161

62-
const org = await getDefaultOrgFromToken()
63-
if (!org?.length) {
62+
const orgSlugs = await getDefaultOrgFromToken()
63+
if (!orgSlugs?.length) {
6464
return {
6565
success: false,
6666
value: undefined,
@@ -69,17 +69,17 @@ export async function discoverConfigValue(
6969
}
7070
}
7171

72-
if (Array.isArray(org)) {
72+
if (Array.isArray(orgSlugs)) {
7373
return {
7474
success: true,
75-
value: org,
75+
value: orgSlugs,
7676
message: 'These are the orgs that the current API token can access.'
7777
}
7878
}
7979

8080
return {
8181
success: true,
82-
value: org,
82+
value: orgSlugs,
8383
message: 'This is the org that belongs to the current API token.'
8484
}
8585
}
@@ -95,8 +95,8 @@ export async function discoverConfigValue(
9595
}
9696
}
9797

98-
const orgs = await getEnforceableOrgsFromToken()
99-
if (!orgs?.length) {
98+
const orgSlugs = await getEnforceableOrgsFromToken()
99+
if (!orgSlugs?.length) {
100100
return {
101101
success: false,
102102
value: undefined,
@@ -107,7 +107,7 @@ export async function discoverConfigValue(
107107

108108
return {
109109
success: true,
110-
value: orgs,
110+
value: orgSlugs,
111111
message: 'These are the orgs whose security policy you can enforce.'
112112
}
113113
}
@@ -131,25 +131,11 @@ export async function discoverConfigValue(
131131
async function getDefaultOrgFromToken(): Promise<
132132
string[] | string | undefined
133133
> {
134-
const sockSdk = await setupSdk()
135-
const result = await handleApiCall(
136-
sockSdk.getOrganizations(),
137-
'looking up organizations'
138-
)
139-
140-
if (result.success) {
141-
const arr = Array.from(Object.values(result.data.organizations)).map(
142-
({ slug }) => slug
143-
)
144-
if (arr.length === 0) {
145-
return undefined
146-
}
147-
if (arr.length === 1) {
148-
return arr[0]
149-
}
150-
return arr
134+
const orgSlugs = await getEnforceableOrgsFromToken()
135+
const length = orgSlugs?.length
136+
if (length) {
137+
return length === 1 ? orgSlugs![0] : orgSlugs!
151138
}
152-
153139
return undefined
154140
}
155141

@@ -159,16 +145,12 @@ async function getEnforceableOrgsFromToken(): Promise<string[] | undefined> {
159145
sockSdk.getOrganizations(),
160146
'looking up organizations'
161147
)
162-
163148
if (result.success) {
164-
const arr = Array.from(Object.values(result.data.organizations)).map(
165-
({ slug }) => slug
166-
)
167-
if (arr.length === 0) {
168-
return undefined
149+
const organizations = Object.values(result.data.organizations)
150+
const orgSlugs = organizations.map(o => o.slug)
151+
if (orgSlugs.length) {
152+
return orgSlugs
169153
}
170-
return arr
171154
}
172-
173155
return undefined
174156
}

src/commands/login/attempt-login.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,26 @@ export async function attemptLogin(
3434

3535
spinner.start('Verifying API key...')
3636

37-
let orgs: SocketSdkReturnType<'getOrganizations'>['data']
37+
let data: SocketSdkReturnType<'getOrganizations'>['data']
3838
try {
3939
const sdk = await setupSdk(apiToken, apiBaseUrl, apiProxy)
4040
const result = await sdk.getOrganizations()
4141
if (!result.success) {
4242
throw new AuthError()
4343
}
44-
orgs = result.data
44+
data = result.data
4545
spinner.success('API key verified')
4646
} catch {
4747
spinner.errorAndStop('Invalid API key')
4848
return
4949
}
5050

51-
const enforcedChoices: OrgChoices = Object.values(orgs.organizations)
52-
.filter(org => org?.plan === 'enterprise')
53-
.map(org => ({
54-
name: org.name ?? 'undefined',
55-
value: org.id
51+
const organizations = Object.values(data.organizations)
52+
const enforcedChoices: OrgChoices = organizations
53+
.filter(o => o?.plan === 'enterprise')
54+
.map(o => ({
55+
name: o.name ?? o.slug,
56+
value: o.id
5657
}))
5758

5859
let enforcedOrgs: string[] = []

src/commands/organization/output-organization-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function outputOrganizationList(
2020
logger.log(
2121
JSON.stringify(
2222
organizations.map(o => ({
23-
name: o.name,
23+
name: o.name ?? o.slug,
2424
id: o.id,
2525
plan: o.plan
2626
})),
@@ -70,7 +70,7 @@ export async function outputOrganizationList(
7070
// Just dump
7171
for (const o of organizations) {
7272
logger.log(
73-
`- Name: ${colors.bold(o.name ?? 'undefined')}, ID: ${colors.bold(o.id)}, Plan: ${colors.bold(o.plan)}`
73+
`- Name: ${colors.bold(o.name ?? o.slug)}, ID: ${colors.bold(o.id)}, Plan: ${colors.bold(o.plan)}`
7474
)
7575
}
7676
}

src/commands/scan/suggest-org-slug.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ export async function suggestOrgSlug(): Promise<string | void> {
1313
// Ignore a failed request here. It was not the primary goal of
1414
// running this command and reporting it only leads to end-user confusion.
1515
if (result.success) {
16+
const organizations = Object.values(result.data.organizations)
1617
const proceed = await select<string>({
1718
message:
1819
'Missing org name; do you want to use any of these orgs for this scan?',
1920
choices: [
20-
...Object.values(result.data.organizations).map(org => {
21-
const slug = org.name ?? 'undefined'
21+
...organizations.map(o => {
22+
const displayName = o.name ?? o.slug
2223
return {
23-
name: `Yes [${slug}]`,
24-
value: slug,
25-
description: `Use "${slug}" as the organization`
24+
name: `Yes [${displayName}]`,
25+
value: displayName,
26+
description: `Use "${displayName}" as the organization`
2627
}
2728
}),
2829
{

0 commit comments

Comments
 (0)