Skip to content

Commit 7a2525f

Browse files
authored
feat: allow jigsaw to be called with the build bot token (#6096)
* feat: allow jigsaw to be called with the build bot token * chore: pass ffs to getIntegrations
1 parent 58e4537 commit 7a2525f

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

packages/build/src/plugins/child/run.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getUtils } from './utils.js'
1010

1111
/** Run a specific plugin event handler */
1212
export const run = async function (
13-
{ event, error, constants, envChanges, featureFlags, netlifyConfig, otelCarrier },
13+
{ event, error, constants, envChanges, featureFlags, netlifyConfig, otelCarrier, extensionMetadata },
1414
{ methods, inputs, packageJson, verbose },
1515
) {
1616
setGlobalContext(propagation.extract(context.active(), otelCarrier))
@@ -31,6 +31,7 @@ export const run = async function (
3131
error,
3232
featureFlags,
3333
systemLog,
34+
extensionMetadata,
3435
}
3536

3637
const envBefore = setEnvChanges(envChanges)

packages/build/src/steps/plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const firePluginStep = async function ({
6666
netlifyConfig,
6767
constants,
6868
otelCarrier,
69+
extensionMetadata,
6970
},
7071
logs: logsA,
7172
verbose,

packages/build/src/types/netlify_plugin_options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ export interface NetlifyPluginOptions<TInputs extends PluginInputs<StringKeys<TI
2323
utils: NetlifyPluginUtils
2424
featureFlags?: Record<string, unknown>
2525
systemLog?(message: string): void
26+
/**
27+
* When an extension's event handler executes, we pass extension-specific metadata to the exension
28+
*/
29+
extensionMetadata: { extension_token?: string }
2630
}

packages/config/src/api/site_info.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NetlifyAPI } from 'netlify'
22
import fetch from 'node-fetch'
3+
import type { RequestInit } from 'node-fetch'
34

45
import { getEnvelope } from '../env/envelope.js'
56
import { throwUserError } from '../error.js'
@@ -17,6 +18,7 @@ type GetSiteInfoOpts = {
1718
featureFlags?: Record<string, boolean>
1819
testOpts?: TestOptions
1920
siteFeatureFlagPrefix: string
21+
token: string
2022
}
2123
/**
2224
* Retrieve Netlify Site information, if available.
@@ -36,6 +38,8 @@ export const getSiteInfo = async function ({
3638
offline = false,
3739
testOpts = {},
3840
siteFeatureFlagPrefix,
41+
token,
42+
featureFlags = {},
3943
}: GetSiteInfoOpts) {
4044
const { env: testEnv = false } = testOpts
4145

@@ -46,7 +50,9 @@ export const getSiteInfo = async function ({
4650
if (accountId !== undefined) siteInfo.account_id = accountId
4751

4852
const integrations =
49-
mode === 'buildbot' && !offline ? await getIntegrations({ siteId, testOpts, offline, accountId }) : []
53+
mode === 'buildbot' && !offline
54+
? await getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags })
55+
: []
5056

5157
return { siteInfo, accounts: [], addons: [], integrations }
5258
}
@@ -55,7 +61,7 @@ export const getSiteInfo = async function ({
5561
getSite(api, siteId, siteFeatureFlagPrefix),
5662
getAccounts(api),
5763
getAddons(api, siteId),
58-
getIntegrations({ siteId, testOpts, offline, accountId }),
64+
getIntegrations({ siteId, testOpts, offline, accountId, token, featureFlags }),
5965
]
6066

6167
const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)
@@ -109,18 +115,22 @@ type GetIntegrationsOpts = {
109115
accountId?: string
110116
testOpts: TestOptions
111117
offline: boolean
118+
token?: string
119+
featureFlags?: Record<string, boolean>
112120
}
113121

114122
const getIntegrations = async function ({
115123
siteId,
116124
accountId,
117125
testOpts,
118126
offline,
127+
token,
128+
featureFlags,
119129
}: GetIntegrationsOpts): Promise<IntegrationResponse[]> {
120130
if (!siteId || offline) {
121131
return []
122132
}
123-
133+
const sendBuildBotTokenToJigsaw = featureFlags?.send_build_bot_token_to_jigsaw
124134
const { host } = testOpts
125135

126136
const baseUrl = new URL(host ? `http://${host}` : `https://api.netlifysdk.com`)
@@ -131,7 +141,15 @@ const getIntegrations = async function ({
131141
: `${baseUrl}site/${siteId}/integrations/safe`
132142

133143
try {
134-
const response = await fetch(url)
144+
const requestOptions = {} as RequestInit
145+
146+
if (sendBuildBotTokenToJigsaw && token) {
147+
requestOptions.headers = {
148+
'netlify-sdk-build-bot-token': token,
149+
}
150+
}
151+
152+
const response = await fetch(url, requestOptions)
135153
if (!response.ok) {
136154
throw new Error(`Unexpected status code ${response.status} from fetching extensions`)
137155
}

packages/config/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const resolveConfig = async function (opts) {
8080
offline,
8181
featureFlags,
8282
testOpts,
83+
token,
8384
})
8485

8586
const { defaultConfig: defaultConfigA, baseRelDir: baseRelDirA } = parseDefaultConfig({

packages/config/tests/api/tests.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,32 @@ test('Integrations are returned if accountId is present and mode is dev', async
419419
t.assert(config.integrations[0].has_build === true)
420420
})
421421

422+
test('Integrations are returned and called with a netlify-sdk-build-bot-token', async (t) => {
423+
const { output, requests } = await new Fixture('./fixtures/base')
424+
.withFlags({
425+
siteId: 'test',
426+
mode: 'dev',
427+
token: 'test',
428+
accountId: 'account1',
429+
featureFlags: {
430+
send_build_bot_token_to_jigsaw: true,
431+
},
432+
})
433+
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
434+
435+
const config = JSON.parse(output)
436+
const installationsHeaders = requests.find(
437+
(request) => request.url === TEAM_INSTALLATIONS_META_RESPONSE.path,
438+
)?.headers
439+
440+
t.assert(installationsHeaders.includes('netlify-sdk-build-bot-token'))
441+
t.assert(config.integrations)
442+
t.assert(config.integrations.length === 1)
443+
t.assert(config.integrations[0].slug === 'test')
444+
t.assert(config.integrations[0].version === 'so-cool-v2')
445+
t.assert(config.integrations[0].has_build === true)
446+
})
447+
422448
test('Integrations are not returned if failed to fetch integrations', async (t) => {
423449
const { output } = await new Fixture('./fixtures/base')
424450
.withFlags({

0 commit comments

Comments
 (0)