Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions packages/config/src/api/site_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { ModeOption, TestOptions } from '../types/options.js'

type GetSiteInfoOpts = {
siteId: string
accountId?: string
mode: ModeOption
siteFeatureFlagPrefix: string
offline?: boolean
api?: NetlifyAPI
context?: string
Expand All @@ -29,14 +29,47 @@ type GetSiteInfoOpts = {
export const getSiteInfo = async function ({
api,
siteId,
accountId,
mode,
siteFeatureFlagPrefix,
context,
offline = false,
testOpts = {},
featureFlags = {},
}: GetSiteInfoOpts) {
const { env: testEnv = false } = testOpts

const useV2Endpoint = !!accountId && featureFlags.cli_integration_installations_meta

if (useV2Endpoint) {
if (api === undefined || mode === 'buildbot' || testEnv) {
const siteInfo = siteId === undefined ? {} : { id: siteId }

const integrations =
mode === 'buildbot' && !offline
? await getIntegrations({ siteId, testOpts, offline, useV2Endpoint, accountId })
: []

return { siteInfo, accounts: [], addons: [], integrations }
}

const promises = [
getSite(api, siteId),
getAccounts(api),
getAddons(api, siteId),
getIntegrations({ siteId, testOpts, offline, useV2Endpoint, accountId }),
]

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 }
}

if (api === undefined || mode === 'buildbot' || testEnv) {
const siteInfo = siteId === undefined ? {} : { id: siteId }

Expand All @@ -46,7 +79,7 @@ export const getSiteInfo = async function ({
}

const promises = [
getSite(api, siteId, siteFeatureFlagPrefix),
getSite(api, siteId),
getAccounts(api),
getAddons(api, siteId),
getIntegrations({ siteId, testOpts, offline }),
Expand All @@ -63,13 +96,13 @@ export const getSiteInfo = async function ({
return { siteInfo, accounts, addons, integrations }
}

const getSite = async function (api: NetlifyAPI, siteId: string, siteFeatureFlagPrefix: string | null = null) {
const getSite = async function (api: NetlifyAPI, siteId: string) {
if (siteId === undefined) {
return {}
}

try {
const site = await (api as any).getSite({ siteId, feature_flags: siteFeatureFlagPrefix })
const site = await (api as any).getSite({ siteId })
return { ...site, id: siteId }
} catch (error) {
throwUserError(`Failed retrieving site data for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
Expand Down Expand Up @@ -100,14 +133,18 @@ const getAddons = async function (api: NetlifyAPI, siteId: string) {

type GetIntegrationsOpts = {
siteId?: string
accountId?: string
testOpts: TestOptions
offline: boolean
useV2Endpoint?: boolean
}

const getIntegrations = async function ({
siteId,
accountId,
testOpts,
offline,
useV2Endpoint,
}: GetIntegrationsOpts): Promise<IntegrationResponse[]> {
if (!siteId || offline) {
return []
Expand All @@ -117,13 +154,19 @@ const getIntegrations = async function ({

const baseUrl = new URL(host ? `http://${host}` : `https://api.netlifysdk.com`)

const url = useV2Endpoint
? `${baseUrl}team/${accountId}/integrations/installations/meta`
: `${baseUrl}site/${siteId}/integrations/safe`

try {
const response = await fetch(`${baseUrl}site/${siteId}/integrations/safe`)
const response = await fetch(url)

const integrations = await response.json()
return Array.isArray(integrations) ? integrations : []
} catch (error) {
// for now, we'll just ignore errors, as this is early days
// Integrations should not block the build if they fail to load
// TODO: We should consider blocking the build as integrations are a critical part of the build process
// https://linear.app/netlify/issue/CT-1214/implement-strategy-in-builds-to-deal-with-integrations-that-we-fail-to
return []
}
}
4 changes: 4 additions & 0 deletions packages/config/src/bin/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ The NETLIFY_AUTH_TOKEN environment variable can be used as well.`,
string: true,
describe: `Netlify Site ID.`,
},
accountId: {
string: true,
describe: 'Netlify Account ID. This will only be available in buildbot mode.',
},
context: {
string: true,
describe: `Build context.
Expand Down
18 changes: 4 additions & 14 deletions packages/config/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,8 @@ import { getRedirectsPath, addRedirects } from './redirects.js'
* `config` together with related properties such as the `configPath`.
*/
export const resolveConfig = async function (opts) {
const {
cachedConfig,
cachedConfigPath,
host,
scheme,
packagePath,
pathPrefix,
testOpts,
token,
offline,
siteFeatureFlagPrefix,
...optsA
} = addDefaultOpts(opts) as $TSFixMe
const { cachedConfig, cachedConfigPath, host, scheme, packagePath, pathPrefix, testOpts, token, offline, ...optsA } =
addDefaultOpts(opts) as $TSFixMe
// `api` is not JSON-serializable, so we cannot cache it inside `cachedConfig`
const api = getApiClient({ token, offline, host, scheme, pathPrefix, testOpts })

Expand All @@ -57,6 +46,7 @@ export const resolveConfig = async function (opts) {
base,
branch,
siteId,
accountId,
deployId,
buildId,
baseRelDir,
Expand All @@ -70,9 +60,9 @@ export const resolveConfig = async function (opts) {
api,
context,
siteId,
accountId,
mode,
offline,
siteFeatureFlagPrefix,
featureFlags,
testOpts,
})
Expand Down
85 changes: 80 additions & 5 deletions packages/config/tests/api/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ const SITE_INTEGRATIONS_RESPONSE = {
],
}

const TEAM_INSTALLATIONS_META_RESPONSE = {
path: '/team/account1/integrations/installations/meta',
response: [
{
slug: 'test',
version: 'so-cool',
has_build: true,
},
],
}

const SITE_INTEGRATIONS_EMPTY_RESPONSE = {
path: '/site/test/integrations/safe',
response: [],
Expand Down Expand Up @@ -307,36 +318,100 @@ test('In integration dev mode, integration specified in config is returned and b
t.assert(config.integrations[0].version === undefined)
})

test('Integrations are returned if feature flag is true, mode buildbot', async (t) => {
test('Integrations are not returned if offline', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
offline: true,
siteId: 'test',
mode: 'buildbot',
})
.runConfigServer([SITE_INTEGRATIONS_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

const config = JSON.parse(output)

t.assert(config.integrations)
t.assert(config.integrations.length === 0)
})

test('Integrations are returned if feature flag is false and mode is buildbot', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
siteId: 'test',
mode: 'buildbot',
accountId: 'account1',
token: 'test',
})
.runConfigServer([SITE_INFO_DATA, SITE_INTEGRATIONS_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

const config = JSON.parse(output)

t.assert(config.integrations)
t.assert(config.integrations.length === 1)
t.assert(config.integrations[0].slug === 'test')
t.assert(config.integrations[0].version === 'so-cool')
t.assert(config.integrations[0].has_build === true)
})

test('Integrations are not returned if offline', async (t) => {
test('Integrations are returned if feature flag is false and mode is dev', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
siteId: 'test',
mode: 'dev',
token: 'test',
})
.runConfigServer([SITE_INFO_DATA, SITE_INTEGRATIONS_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

const config = JSON.parse(output)

t.assert(config.integrations)
t.assert(config.integrations.length === 1)
t.assert(config.integrations[0].slug === 'test')
t.assert(config.integrations[0].version === 'so-cool')
t.assert(config.integrations[0].has_build === true)
})

test('Integrations are returned if flag is true for site and mode is buildbot', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
offline: true,
siteId: 'test',
mode: 'buildbot',
token: 'test',
accountId: 'account1',
featureFlags: {
cli_integration_installations_meta: true,
},
})
.runConfigServer([SITE_INTEGRATIONS_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

const config = JSON.parse(output)

t.assert(config.integrations)
t.assert(config.integrations.length === 0)
t.assert(config.integrations.length === 1)
t.assert(config.integrations[0].slug === 'test')
t.assert(config.integrations[0].version === 'so-cool')
t.assert(config.integrations[0].has_build === true)
})

test('Integrations are returned if flag is true for site and mode is dev', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
siteId: 'test',
mode: 'dev',
token: 'test',
accountId: 'account1',
featureFlags: {
cli_integration_installations_meta: true,
},
})
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

const config = JSON.parse(output)

t.assert(config.integrations)
t.assert(config.integrations.length === 1)
t.assert(config.integrations[0].slug === 'test')
t.assert(config.integrations[0].version === 'so-cool')
t.assert(config.integrations[0].has_build === true)
})

test('baseRelDir is true if build.base is overridden', async (t) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/config/tests/cli/snapshots/tests.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Generated by [AVA](https://avajs.dev).
The NETLIFY_AUTH_TOKEN␊
environment variable can be used as well. [string]␊
--siteId Netlify Site ID. [string]␊
--accountId Netlify Account ID. This will only be available in buildbot␊
mode. [string]␊
--context Build context.␊
Default: 'production' [string]␊
--branch Repository branch.␊
Expand Down
Binary file modified packages/config/tests/cli/snapshots/tests.js.snap
Binary file not shown.