-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsite_info.ts
213 lines (187 loc) · 6.32 KB
/
site_info.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { NetlifyAPI } from 'netlify'
import fetch from 'node-fetch'
import type { RequestInit } from 'node-fetch'
import { getEnvelope } from '../env/envelope.js'
import { throwUserError } from '../error.js'
import {
EXTENSION_API_BASE_URL,
EXTENSION_API_STAGING_BASE_URL,
NETLIFY_API_BASE_URL,
NETLIFY_API_STAGING_BASE_URL,
} from '../integrations.js'
import { ERROR_CALL_TO_ACTION } from '../log/messages.js'
import { IntegrationResponse } from '../types/api.js'
import { ModeOption, TestOptions } from '../types/options.js'
type GetSiteInfoOpts = {
siteId: string
accountId?: string
mode: ModeOption
offline?: boolean
api?: NetlifyAPI
context?: string
featureFlags?: Record<string, boolean>
testOpts?: TestOptions
siteFeatureFlagPrefix: string
token: string
extensionApiBaseUrl: string
}
/**
* Retrieve Netlify Site information, if available.
* Used to retrieve local build environment variables and UI build settings.
* This is not used in production builds since the buildbot passes this
* information instead.
* Requires knowing the `siteId` and having the access `token`.
* Silently ignore API errors. For example the network connection might be down,
* but local builds should still work regardless.
*/
export const getSiteInfo = async function ({
api,
siteId,
accountId,
mode,
context,
offline = false,
testOpts = {},
siteFeatureFlagPrefix,
token,
featureFlags = {},
extensionApiBaseUrl,
}: GetSiteInfoOpts) {
const { env: testEnv = false } = testOpts
if (api === undefined || mode === 'buildbot' || testEnv) {
const siteInfo: { id?: string; account_id?: string } = {}
if (siteId !== undefined) siteInfo.id = siteId
if (accountId !== undefined) siteInfo.account_id = accountId
const integrations =
mode === 'buildbot' && !offline
? await getIntegrations({
siteId,
testOpts,
offline,
accountId,
token,
featureFlags,
extensionApiBaseUrl,
mode,
})
: []
return { siteInfo, accounts: [], addons: [], integrations }
}
const promises = [
getSite(api, siteId, siteFeatureFlagPrefix),
getAccounts(api),
getAddons(api, siteId),
getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags, extensionApiBaseUrl, mode }),
]
const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)
if (siteInfo.use_envelope) {
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId, context })
siteInfo.build_settings.env = envelope
}
return { siteInfo, accounts, addons, integrations }
}
const getSite = async function (api: NetlifyAPI, siteId: string, siteFeatureFlagPrefix: string) {
if (siteId === undefined) {
return {}
}
try {
const site = await (api as any).getSite({ siteId, feature_flags: siteFeatureFlagPrefix })
return { ...site, id: siteId }
} catch (error) {
throwUserError(`Failed retrieving site data for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}
}
const getAccounts = async function (api: NetlifyAPI) {
try {
const accounts = await (api as any).listAccountsForUser()
return Array.isArray(accounts) ? accounts : []
} catch (error) {
throwUserError(`Failed retrieving user account: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}
}
const getAddons = async function (api: NetlifyAPI, siteId: string) {
if (siteId === undefined) {
return []
}
try {
const addons = await (api as any).listServiceInstancesForSite({ siteId })
return Array.isArray(addons) ? addons : []
} catch (error) {
throwUserError(`Failed retrieving addons for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}
}
type GetIntegrationsOpts = {
siteId?: string
accountId?: string
testOpts: TestOptions
offline: boolean
token?: string
featureFlags?: Record<string, boolean>
extensionApiBaseUrl: string
mode: ModeOption
}
const getIntegrations = async function ({
siteId,
accountId,
testOpts,
offline,
token,
featureFlags,
extensionApiBaseUrl,
mode,
}: GetIntegrationsOpts): Promise<IntegrationResponse[]> {
if (!siteId || offline) {
return []
}
const sendBuildBotTokenToJigsaw = featureFlags?.send_build_bot_token_to_jigsaw
const { host: originalHost, setBaseUrl } = testOpts
// TODO(kh): I am adding this purely for local staging development.
// We should remove this once we have fixed https://github.com/netlify/cli/blob/b5a5c7525edd28925c5c2e3e5f0f00c4261eaba5/src/lib/build.ts#L125
let host = originalHost
// If there is a host, we use it to fetch the integrations
// we check if the host is staging or production and set the host accordingly,
// sadly necessary because of https://github.com/netlify/cli/blob/b5a5c7525edd28925c5c2e3e5f0f00c4261eaba5/src/lib/build.ts#L125
if (originalHost) {
if (originalHost?.includes(NETLIFY_API_STAGING_BASE_URL)) {
host = EXTENSION_API_STAGING_BASE_URL
} else if (originalHost?.includes(NETLIFY_API_BASE_URL)) {
host = EXTENSION_API_BASE_URL
} else {
host = `http://${originalHost}`
}
}
const baseUrl = new URL(host ?? extensionApiBaseUrl)
// We only use this for testing
if (host && setBaseUrl) {
setBaseUrl(extensionApiBaseUrl)
}
// if accountId isn't present, use safe v1 endpoint
const url = accountId
? `${baseUrl}team/${accountId}/integrations/installations/meta/${siteId}`
: `${baseUrl}site/${siteId}/integrations/safe`
try {
const requestOptions = {} as RequestInit
// This is used to identify where the request is coming from
requestOptions.headers = {
'netlify-config-mode': mode,
}
if (sendBuildBotTokenToJigsaw && token) {
requestOptions.headers = {
...requestOptions.headers,
'netlify-sdk-build-bot-token': token,
}
}
const response = await fetch(url, requestOptions)
if (!response.ok) {
throw new Error(`Unexpected status code ${response.status} from fetching extensions`)
}
const bodyText = await response.text()
if (bodyText === '') {
return []
}
const integrations = await JSON.parse(bodyText)
return Array.isArray(integrations) ? integrations : []
} catch (error) {
return throwUserError(`Failed retrieving extensions for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}
}