Skip to content

Commit

Permalink
fix: fail build when extension failed to be retrieved (#5915)
Browse files Browse the repository at this point in the history
* fix: fail build when extension failed to be retrieved

* fix(config): fail build if fetching extensions returns a non 200 response

* fix(config): update snapshot

* chore: refactor

* chore: merge with main

* fix: reset package-lock.json to match origin/main
  • Loading branch information
YujohnNattrass authored Dec 11, 2024
1 parent 7e99c26 commit 20c7359
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/config/src/api/site_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const getIntegrations = async function ({
accountId,
testOpts,
offline,
}: GetIntegrationsOpts): Promise<IntegrationResponse[]> {
}: GetIntegrationsOpts): Promise<IntegrationResponse[] | undefined> {
if (!siteId || offline) {
return []
}
Expand All @@ -130,15 +130,21 @@ const getIntegrations = async function ({
? `${baseUrl}team/${accountId}/integrations/installations/meta/${siteId}`
: `${baseUrl}site/${siteId}/integrations/safe`

let response
try {
const response = await fetch(url)
response = await fetch(url)
if (response.status !== 200) {
throw new Error(`Unexpected status code ${response.status} from fetching extensions`)
}
} catch (error) {
throwUserError(`Failed retrieving extensions for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}

const integrations = await response.json()
return Array.isArray(integrations) ? integrations : []
try {
if (Number(response.headers.get(`content-length`)) === 0) return []
const responseBody = await response.json()
return Array.isArray(responseBody) ? responseBody : []
} catch (error) {
// 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 []
throwUserError(`Failed to parse extensions for site ${siteId}: ${error.message}. ${ERROR_CALL_TO_ACTION}`)
}
}
6 changes: 6 additions & 0 deletions packages/config/tests/api/snapshots/tests.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,12 @@ Generated by [AVA](https://avajs.dev).
"token": "test"␊
}`

## Integrations teamintegrationsInstallationsMeta API error

> Snapshot 1
'Failed retrieving extensions for site test: Unexpected status code 500 from fetching extensions. Double-check your login status with \'netlify status\' or contact support with details of your error.'

## baseRelDir is true if build.base is overridden

> Snapshot 1
Expand Down
Binary file modified packages/config/tests/api/snapshots/tests.js.snap
Binary file not shown.
22 changes: 22 additions & 0 deletions packages/config/tests/api/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const TEAM_INSTALLATIONS_META_RESPONSE = {
],
}

const TEAM_INSTALLATIONS_META_RESPONSE_ERROR = {
path: '/team/account1/integrations/installations/meta/test',
response: { error: 'Internal Server Error' },
status: 500,
}

const SITE_INTEGRATIONS_EMPTY_RESPONSE = {
path: '/site/test/integrations/safe',
response: [],
Expand Down Expand Up @@ -413,6 +419,22 @@ test('Integrations are returned if accountId is present and mode is dev', async
t.assert(config.integrations[0].has_build === true)
})

test('Integrations teamintegrationsInstallationsMeta API error', async (t) => {
const { output } = await new Fixture('./fixtures/base')
.withFlags({
siteId: 'test',
mode: 'buildbot',
token: 'test',
accountId: 'account1',
featureFlags: {
cli_integration_installations_meta: true,
},
})
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE_ERROR, FETCH_INTEGRATIONS_EMPTY_RESPONSE])

t.snapshot(normalizeOutput(output))
})

test('baseRelDir is true if build.base is overridden', async (t) => {
const fixturesDir = normalize(`${fileURLToPath(test.meta.file)}/../fixtures`)

Expand Down

0 comments on commit 20c7359

Please sign in to comment.