-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfetch-default-org-slug.mts
More file actions
60 lines (51 loc) · 1.86 KB
/
fetch-default-org-slug.mts
File metadata and controls
60 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { debug } from '@socketsecurity/lib/debug'
import { SOCKET_CLI_ORG_SLUG } from '../../env/socket-cli-org-slug.mts'
import { getConfigValueOrUndef } from '../../utils/config.mts'
import { fetchOrganization } from '../organization/fetch-organization-list.mts'
import type { CResult } from '../../types.mts'
// Use the config defaultOrg when set, otherwise discover from remote.
export async function getDefaultOrgSlug(): Promise<CResult<string>> {
const defaultOrgResult = getConfigValueOrUndef('defaultOrg')
if (defaultOrgResult) {
debug(
`use: org from "defaultOrg" value of socket/settings local app data: ${defaultOrgResult}`,
)
return { ok: true, data: defaultOrgResult }
}
if (SOCKET_CLI_ORG_SLUG) {
debug(
`use: org from SOCKET_CLI_ORG_SLUG environment variable: ${SOCKET_CLI_ORG_SLUG}`,
)
return { ok: true, data: SOCKET_CLI_ORG_SLUG }
}
const orgsCResult = await fetchOrganization()
if (!orgsCResult.ok) {
return orgsCResult
}
const { organizations } = orgsCResult.data
if (!organizations.length) {
return {
ok: false,
message: 'Failed to establish identity',
data: 'No organization associated with the Socket API token. Unable to continue.',
}
}
// Use the API slug (URL-safe identifier), not the display name. Previously
// read `.name` which broke API calls for orgs like "Alamos GmbH" whose
// display name contains spaces, producing URLs like
// `/v0/orgs/Alamos%20GmbH/full-scans` that 404'd.
const slug = organizations[0]?.slug
if (!slug) {
return {
ok: false,
message: 'Failed to establish identity',
data: 'Cannot determine the default organization for the API token. Unable to continue.',
}
}
debug(`resolve: org from Socket API: ${slug}`)
return {
ok: true,
message: 'Retrieved default org from server',
data: slug,
}
}