Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: retrieve integration meta data from new endpoint #5647

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d596237
chore: add flag to split logic - in progress
lemusthelroy May 17, 2024
12cdb9d
chore: link
lemusthelroy May 17, 2024
8d0ec67
chore: in progress with ff split
lemusthelroy May 21, 2024
ebeebdd
chore: correct flag
lemusthelroy May 21, 2024
f271f78
chore: debug tests
lemusthelroy May 21, 2024
a39c459
chore: debug tests
lemusthelroy May 21, 2024
876602b
chore: test coverage added
lemusthelroy May 21, 2024
9e5dec2
chore: remove logs
lemusthelroy May 21, 2024
16e8eae
chore: change local dep
lemusthelroy May 21, 2024
18cc081
chore: remove flag comment
lemusthelroy May 21, 2024
2db6580
chore: user v2 as flag var name
lemusthelroy May 21, 2024
7fa8c19
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
lemusthelroy May 21, 2024
4dd7b86
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
khendrikse May 22, 2024
3fae809
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
khendrikse May 23, 2024
2e34fa7
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
khendrikse May 24, 2024
f510e19
chore: add link to linear issue
lemusthelroy May 24, 2024
7bf6d8f
chore: make error func return never
lemusthelroy May 24, 2024
d56db4a
chore: remove else
lemusthelroy May 24, 2024
3f8f4e1
chore: fix typing by return error func that returns never
lemusthelroy May 24, 2024
83b6ead
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
lemusthelroy May 29, 2024
1f4992f
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
lemusthelroy May 29, 2024
5606943
Merge branch 'main' into lemusthelroy/ct-1160-point-build-system-to-n…
kodiakhq[bot] May 29, 2024
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
82 changes: 63 additions & 19 deletions packages/config/src/api/site_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type GetSiteInfoOpts = {
offline?: boolean
api?: NetlifyAPI
context?: string
featureFlags?: Record<string, boolean>
testOpts?: TestOptions
}
/**
Expand All @@ -37,30 +36,58 @@ export const getSiteInfo = async function ({
}: GetSiteInfoOpts) {
const { env: testEnv = false } = testOpts

if (api === undefined || mode === 'buildbot' || testEnv) {
if (api === undefined || testEnv || offline) {
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
const siteInfo = siteId === undefined ? {} : { id: siteId }

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

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

const promises = [
getSite(api, siteId, siteFeatureFlagPrefix),
getAccounts(api),
getAddons(api, siteId),
getIntegrations({ siteId, testOpts, offline }),
]
const siteInfo = await getSite(api, siteId, siteFeatureFlagPrefix)
const featureFlags = siteInfo.feature_flags

const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)
const useV1Endpoint = !featureFlags?.cli_integration_installations_meta
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved

if (siteInfo.use_envelope) {
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId, context })
if (useV1Endpoint) {
if (mode === 'buildbot') {
const siteInfo = siteId === undefined ? {} : { id: siteId }

siteInfo.build_settings.env = envelope
}
const integrations = await getIntegrations({ siteId, testOpts, offline, featureFlags })

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

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

const [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 }
} else {
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
const promises = [
getAccounts(api),
getAddons(api, siteId),
getIntegrations({ siteId, testOpts, offline, accountId: siteInfo.account_id, featureFlags }),
]

return { siteInfo, accounts, addons, integrations }
const [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 | null = null) {
Expand All @@ -73,6 +100,8 @@ const getSite = async function (api: NetlifyAPI, siteId: string, siteFeatureFlag
return { ...site, id: siteId }
} catch (error) {
throwUserError(`Failed retrieving site data for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
// Added to satisfy TypeScript only an object can be returned
return { id: siteId }
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -82,6 +111,8 @@ const getAccounts = async function (api: NetlifyAPI) {
return Array.isArray(accounts) ? accounts : []
} catch (error) {
throwUserError(`Failed retrieving user account: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
// Added to satisfy TypeScript only an array can be returned
return []
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -95,19 +126,25 @@ const getAddons = async function (api: NetlifyAPI, siteId: string) {
return Array.isArray(addons) ? addons : []
} catch (error) {
throwUserError(`Failed retrieving addons for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
// Added to satisfy TypeScript only an array can be returned
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
return []
}
}

type GetIntegrationsOpts = {
siteId?: string
accountId?: string
testOpts: TestOptions
offline: boolean
featureFlags?: Record<string, boolean>
}

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

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

const useV1Endpoint = !featureFlags?.cli_integration_installations_meta

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

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
lemusthelroy marked this conversation as resolved.
Show resolved Hide resolved
return []
}
}
1 change: 0 additions & 1 deletion packages/config/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const resolveConfig = async function (opts) {
mode,
offline,
siteFeatureFlagPrefix,
featureFlags,
testOpts,
})

Expand Down
114 changes: 109 additions & 5 deletions packages/config/tests/api/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,37 @@ 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: [],
}

const siteInfoWithFeatureFlag = (flag) => {
return {
path: SITE_INFO_PATH,
response: {
ssl_url: 'test',
name: 'test-name',
build_settings: { repo_url: 'test' },
account_id: 'account1',
feature_flags: {
[flag]: true,
},
},
}
}

const SITE_INFO_BUILD_SETTINGS = {
path: SITE_INFO_PATH,
response: {
Expand Down Expand Up @@ -307,36 +333,114 @@ 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 not returned if no api', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
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',
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)
})

// new tests
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',
})
.runConfigServer([SITE_INTEGRATIONS_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
.runConfigServer([
siteInfoWithFeatureFlag('cli_integration_installations_meta'),
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',
})
.runConfigServer([
siteInfoWithFeatureFlag('cli_integration_installations_meta'),
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
Loading