-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathdeploy.ts
459 lines (400 loc) · 16.8 KB
/
deploy.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import fs from 'fs'
import { resolve } from 'path'
import { env, exit } from 'process'
import { OptionValues } from 'commander'
import inquirer from 'inquirer'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'js-y... Remove this comment to see the full error message
import yaml from 'js-yaml'
import fetch from 'node-fetch'
import { z } from 'zod'
import { getBuildOptions } from '../../lib/build.js'
import { chalk, getToken, log } from '../../utils/command-helpers.js'
import { getSiteInformation } from '../../utils/dev.js'
import BaseCommand from '../base-command.js'
import { checkOptions } from '../build/build.js'
import { deploy as siteDeploy } from '../deploy/deploy.js'
function getIntegrationAPIUrl() {
return env.INTEGRATION_URL || 'https://api.netlifysdk.com'
}
// @ts-expect-error TS(7006) FIXME: Parameter 'localScopes' implicitly has an 'any' ty... Remove this comment to see the full error message
export function areScopesEqual(localScopes, remoteScopes) {
if (localScopes.length !== remoteScopes.length) {
return false
}
// @ts-expect-error TS(7006) FIXME: Parameter 'scope' implicitly has an 'any' type.
return localScopes.every((scope) => remoteScopes.includes(scope))
}
// @ts-expect-error TS(7006) FIXME: Parameter 'localScopes' implicitly has an 'any' ty... Remove this comment to see the full error message
function logScopeConfirmationMessage(localScopes, remoteScopes) {
log(chalk.yellow(`This integration is already registered. The current required scopes are:`))
for (const scope of remoteScopes) {
log(chalk.green(`- ${scope}`))
}
log(chalk.yellow('and will be updated to:'))
for (const scope of localScopes) {
log(chalk.green(`- ${scope}`))
}
log(chalk.yellow('if you continue. This will only affect future installations of the integration.'))
}
// @ts-expect-error TS(7006) FIXME: Parameter 'registeredIntegrationScopes' implicitly... Remove this comment to see the full error message
function formatScopesToWrite(registeredIntegrationScopes) {
let scopesToWrite = {}
for (const scope of registeredIntegrationScopes) {
const [resource, permission] = scope.split(':')
if (resource === 'all') {
scopesToWrite = { all: true }
break
} else {
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
if (!scopesToWrite[resource]) {
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
scopesToWrite[resource] = []
}
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
scopesToWrite[resource].push(permission)
}
}
return scopesToWrite
}
// @ts-expect-error TS(7006) FIXME: Parameter 'scopes' implicitly has an 'any' type.
function formatScopesForRemote(scopes) {
const scopesToWrite = []
if (scopes.all) {
scopesToWrite.push('all')
} else {
const scopeResources = Object.keys(scopes)
scopeResources.forEach((resource) => {
const permissionsRequested = scopes[resource]
// @ts-expect-error TS(7006) FIXME: Parameter 'permission' implicitly has an 'any' typ... Remove this comment to see the full error message
permissionsRequested.forEach((permission) => {
scopesToWrite.push(`${resource}:${permission}`)
})
})
}
return scopesToWrite.join(',')
}
// @ts-expect-error TS(7006) FIXME: Parameter 'name' implicitly has an 'any' type.
function verifyRequiredFieldsAreInConfig(name, description, scopes, integrationLevel) {
const missingFields = []
if (!name) {
missingFields.push('name')
}
if (!description) {
missingFields.push('description')
}
if (!scopes) {
missingFields.push('scopes')
}
if (!integrationLevel) {
missingFields.push('integrationLevel')
}
if (missingFields.length !== 0) {
log(
chalk.yellow(
`You are missing the following fields for the integration to be deployed: ${missingFields.join(
', ',
)}. Please add a these fields as an entry to the integration.yaml file and try again.`,
),
)
log(
chalk.yellow(
'For more information on the required fields, please see the documentation: https://ntl.fyi/create-private-integration',
),
)
return false
}
return true
}
// @ts-expect-error TS(7006) FIXME: Parameter 'workingDir' implicitly has an 'any' typ... Remove this comment to see the full error message
export async function registerIntegration(workingDir, siteId, accountId, localIntegrationConfig, token) {
const { description, integrationLevel, name, scopes, slug } = localIntegrationConfig
log(chalk.yellow(`An integration associated with the site ID ${siteId} is not registered.`))
const registerPrompt = await inquirer.prompt([
{
type: 'confirm',
name: 'registerIntegration',
message: `Would you like to register this site as a private integration now?`,
default: false,
},
])
if (!registerPrompt.registerIntegration) {
log(
chalk.white(
"Cancelling deployment. Please run 'netlify int deploy' again when you are ready to register the integration.",
),
)
log(
chalk.white(
"You can also register the integration through the Netlify UI on the 'Integrations' > 'Create private integration' page",
),
)
exit(1)
}
if (!verifyRequiredFieldsAreInConfig(name, description, scopes, integrationLevel)) {
exit(1)
}
log(chalk.white('Registering the integration...'))
const { body, statusCode } = await fetch(`${getIntegrationAPIUrl()}/${accountId}/integrations`, {
method: 'POST',
headers: {
'netlify-token': token,
},
body: JSON.stringify({
name,
slug,
description,
hostSiteId: siteId,
scopes: formatScopesForRemote(scopes),
integrationLevel,
}),
}).then(async (res) => {
const response = await res.json()
return { body: response, statusCode: res.status }
})
if (statusCode !== 201) {
log(chalk.red(`There was an error registering the integration:`))
log()
log(chalk.red(`-----------------------------------------------`))
// @ts-expect-error TS(18046) - 'body' is of type 'unknown'
log(chalk.red(body.msg))
log(chalk.red(`-----------------------------------------------`))
log()
log(chalk.red(`Please try again. If the problem persists, please contact support.`))
exit(1)
}
// @ts-expect-error TS(18046) - 'body' is of type 'unknown'
log(chalk.green(`Successfully registered the integration with the slug: ${body.slug}`))
const updatedIntegrationConfig = yaml.dump({
// @ts-expect-error TS(18046) - 'body' is of type 'unknown'
config: { name, description, slug: body.slug, scopes, integrationLevel },
})
const filePath = resolve(workingDir, 'integration.yaml')
await fs.promises.writeFile(filePath, updatedIntegrationConfig)
log(chalk.yellow('Your integration.yaml file has been updated. Please commit and push these changes.'))
}
export async function updateIntegration(
// @ts-expect-error TS(7006) FIXME: Parameter 'workingDir' implicitly has an 'any' typ... Remove this comment to see the full error message
workingDir,
// @ts-expect-error TS(7006) FIXME: Parameter 'options' implicitly has an 'any' type.
options,
// @ts-expect-error TS(7006) FIXME: Parameter 'siteId' implicitly has an 'any' type.
siteId,
// @ts-expect-error TS(7006) FIXME: Parameter 'accountId' implicitly has an 'any' type... Remove this comment to see the full error message
accountId,
// @ts-expect-error TS(7006) FIXME: Parameter 'localIntegrationConfig' implicitly has ... Remove this comment to see the full error message
localIntegrationConfig,
// @ts-expect-error TS(7006) FIXME: Parameter 'token' implicitly has an 'any' type.
token,
// @ts-expect-error TS(7006) FIXME: Parameter 'registeredIntegration' implicitly has a... Remove this comment to see the full error message
registeredIntegration,
) {
let { description, integrationLevel, name, scopes, slug } = localIntegrationConfig
let integrationSlug = slug
if (slug !== registeredIntegration.slug) {
// Update the project's integration.yaml file with the remote slug since that will
// be considered the source of truth and is a value that can't be edited by the user.
// Let the user know they need to commit and push the changes.
integrationSlug = registeredIntegration.slug
}
if (!name) {
// Disabling this lint rule because the destructuring was not assigning the variable correct and leading to a bug
// eslint-disable-next-line prefer-destructuring
name = registeredIntegration.name
}
if (!description) {
// eslint-disable-next-line prefer-destructuring
description = registeredIntegration.description
}
if (!integrationLevel) {
// eslint-disable-next-line prefer-destructuring
integrationLevel = registeredIntegration.integrationLevel
}
// This is returned as a comma separated string and will be easier to manage here as an array
const registeredIntegrationScopes = registeredIntegration.scopes.split(',')
const scopeResources = Object.keys(scopes)
// @ts-expect-error TS(7034) FIXME: Variable 'localScopes' implicitly has type 'any[]'... Remove this comment to see the full error message
let localScopes = []
if (scopeResources.includes('all')) {
localScopes = ['all']
} else {
scopeResources.forEach((resource) => {
const permissionsRequested = scopes[resource]
// @ts-expect-error TS(7006) FIXME: Parameter 'permission' implicitly has an 'any' typ... Remove this comment to see the full error message
permissionsRequested.forEach((permission) => {
localScopes.push(`${resource}:${permission}`)
})
})
}
// @ts-expect-error TS(7005) FIXME: Variable 'localScopes' implicitly has an 'any[]' t... Remove this comment to see the full error message
if (!areScopesEqual(localScopes, registeredIntegrationScopes)) {
// @ts-expect-error TS(7005) FIXME: Variable 'localScopes' implicitly has an 'any[]' t... Remove this comment to see the full error message
logScopeConfirmationMessage(localScopes, registeredIntegrationScopes)
const scopePrompt = await inquirer.prompt([
{
type: 'confirm',
name: 'updateScopes',
message: `Do you want to update the scopes?`,
default: false,
},
])
let scopesToWrite
if (scopePrompt.updateScopes) {
// Update the scopes in remote
scopesToWrite = scopes
const { statusCode, updateResponse } = await fetch(
`${getIntegrationAPIUrl()}/${accountId}/integrations/${integrationSlug}`,
{
method: 'PUT',
headers: {
'netlify-token': token,
},
body: JSON.stringify({
name,
description,
hostSiteId: siteId,
// @ts-expect-error TS(7005) FIXME: Variable 'localScopes' implicitly has an 'any[]' t... Remove this comment to see the full error message
scopes: localScopes.join(','),
integrationLevel,
}),
},
).then(async (res) => {
const response = await res.json()
return { updateResponse: response, statusCode: res.status }
})
if (statusCode !== 200) {
log(
chalk.red(`There was an error updating the integration: ${updateResponse}`),
chalk.red('Please try again. If the problem persists, please contact support.'),
)
exit(1)
}
} else {
const useRegisteredScopesPrompt = await inquirer.prompt([
{
type: 'confirm',
name: 'useRegisteredScopes',
message: `Do you want to save the scopes registered for your integration in your local configuration file?`,
default: false,
},
])
if (useRegisteredScopesPrompt.useRegisteredScopes) {
// Use the scopes that are already registered
log(chalk.white('Saving the currently registered scopes to the integration.yaml file.'))
scopesToWrite = formatScopesToWrite(registeredIntegrationScopes)
}
if (!useRegisteredScopesPrompt.useRegisteredScopes && options.prod) {
log(chalk.red('Unable to deploy your integration to production without updating the registered scopes.'))
exit(1)
}
}
const updatedIntegrationConfig = yaml.dump({
config: { name, description, slug: integrationSlug, scopes: scopesToWrite, integrationLevel },
})
const filePath = resolve(workingDir, 'integration.yaml')
await fs.promises.writeFile(filePath, updatedIntegrationConfig)
log(chalk.yellow('Changes to the integration.yaml file are complete. Please commit and push these changes.'))
}
}
const possibleFiles = ['integration.yaml', 'integration.yml', 'integration.netlify.yaml', 'integration.netlify.yml']
const IntegrationConfigurationSchema = z.object({
name: z.string().optional(),
description: z.string().optional(),
slug: z.string().regex(/^[a-z\d-]+$/, 'slug must be lowercase with dashes'),
scopes: z
.object({
all: z.boolean().optional(),
site: z.array(z.enum(['read', 'write'])).optional(),
env: z.array(z.enum(['read', 'write', 'delete'])).optional(),
user: z.array(z.enum(['read', 'write'])).optional(),
})
.optional(),
integrationLevel: z.enum(['site', 'team', 'team-and-site']).optional(),
})
// @ts-expect-error TS(7006) FIXME: Parameter 'workingDir' implicitly has an 'any' typ... Remove this comment to see the full error message
const getConfigurationFile = (workingDir) => {
const pwd = workingDir
const fileName = possibleFiles.find((configFileName) => fs.existsSync(resolve(pwd, configFileName)))
return fileName
}
// @ts-expect-error TS(7006) FIXME: Parameter 'workingDir' implicitly has an 'any' typ... Remove this comment to see the full error message
export const getConfiguration = (workingDir) => {
const pwd = workingDir
const fileName = getConfigurationFile(workingDir)
if (!fileName) {
throw new Error('No configuration file found')
}
try {
const { config } = yaml.load(fs.readFileSync(resolve(pwd, fileName), 'utf-8'))
if (!config) {
throw new Error('No configuration found')
}
const parseResult = IntegrationConfigurationSchema.safeParse(config)
if (!parseResult.success) {
console.error(parseResult.error.message)
throw new Error('Invalid Configuration')
}
return config
} catch (error) {
console.error(error)
console.error(`No configuration found in ${fileName} in ${pwd}`)
exit(1)
}
}
export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { api, cachedConfig, site, siteInfo } = command.netlify
const { id: siteId } = site
const [token] = await command.authenticate(options.auth)
const workingDir = resolve(command.workingDir)
const buildOptions = await getBuildOptions({
cachedConfig,
packagePath: command.workspacePackage,
currentDir: command.workingDir,
token,
// @ts-expect-error TS(2740)
options,
})
// Confirm that a site is linked and that the user is logged in
checkOptions(buildOptions)
const { description, integrationLevel, name, scopes, slug } = await getConfiguration(command.workingDir)
const localIntegrationConfig = { name, description, scopes, slug, integrationLevel }
// @ts-expect-error TS(2345) FIXME: Argument of type '{ api: any; site: any; siteInfo:... Remove this comment to see the full error message
const { accountId } = await getSiteInformation({
api,
site,
siteInfo,
})
const { body: registeredIntegration, statusCode } = await fetch(
`${getIntegrationAPIUrl()}/${accountId}/integrations?site_id=${siteId}`,
{
headers: {
'netlify-token': token,
},
},
).then(async (res) => {
const body = await res.json()
return { body, statusCode: res.status }
})
// The integration is registered on the remote
statusCode === 200
? await updateIntegration(
workingDir,
options,
siteId,
accountId,
localIntegrationConfig,
token,
registeredIntegration,
)
: await registerIntegration(workingDir, siteId, accountId, localIntegrationConfig, token)
// Set the prod flag to true if the integration is being initially registered because we don't want the user
// to be in a weird state where the card is appearing in the integrations list but there's no production
// version of the integration deployed
options = statusCode === 200 ? options : { ...options, prod: true }
// Deploy the integration to that site
await siteDeploy(options, command)
log(
`${chalk.cyanBright.bold(
`Your integration has been deployed. Next step is to enable it for a team or site.`,
)} https://ntl.fyi/create-private-integration`,
)
}