Skip to content

Commit 8301161

Browse files
authored
fix: when fetching env from envelope, pass context (#5426)
* fix: when fetching env from envelope, pass context * fix: build * chore: update tests to show its working * chore: use TSFixme
1 parent f4914c5 commit 8301161

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

Diff for: packages/config/src/api/site_info.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type GetSiteInfoOpts = {
1313
siteFeatureFlagPrefix: string
1414
offline?: boolean
1515
api?: NetlifyAPI
16+
context?: string
1617
featureFlags?: Record<string, boolean>
1718
testOpts?: TestOptions
1819
}
@@ -30,6 +31,7 @@ export const getSiteInfo = async function ({
3031
siteId,
3132
mode,
3233
siteFeatureFlagPrefix,
34+
context,
3335
offline = false,
3436
testOpts = {},
3537
}: GetSiteInfoOpts) {
@@ -53,7 +55,7 @@ export const getSiteInfo = async function ({
5355
const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)
5456

5557
if (siteInfo.use_envelope) {
56-
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId })
58+
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId, context })
5759

5860
siteInfo.build_settings.env = envelope
5961
}

Diff for: packages/config/src/env/envelope.js renamed to packages/config/src/env/envelope.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
export const getEnvelope = async function ({ api, accountId, siteId }) {
1+
import type { NetlifyAPI } from 'netlify'
2+
3+
export const getEnvelope = async function ({
4+
api,
5+
accountId,
6+
siteId,
7+
context,
8+
}: {
9+
api: NetlifyAPI
10+
accountId: string
11+
siteId?: string
12+
context?: string
13+
}) {
214
if (accountId === undefined) {
315
return {}
416
}
517
try {
6-
const environmentVariables = await api.getEnvVars({ accountId, siteId })
18+
const environmentVariables = await (api as any).getEnvVars({ accountId, siteId, context_name: context })
719

820
const sortedEnvVarsFromDevContext = environmentVariables
921
.sort((left, right) => (left.key.toLowerCase() < right.key.toLowerCase() ? -1 : 1))

Diff for: packages/config/src/env/main.js renamed to packages/config/src/env/main.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NetlifyAPI } from 'netlify'
12
import omit from 'omit.js'
23

34
import { removeFalsy } from '../utils/remove_falsy.js'
@@ -29,7 +30,14 @@ export const getEnv = async function ({
2930
}
3031

3132
const generalEnv = await getGeneralEnv({ siteInfo, buildDir, branch, deployId, buildId, context })
32-
const [accountEnv, addonsEnv, uiEnv, configFileEnv] = await getUserEnv({ api, config, siteInfo, accounts, addons })
33+
const [accountEnv, addonsEnv, uiEnv, configFileEnv] = await getUserEnv({
34+
api,
35+
config,
36+
siteInfo,
37+
accounts,
38+
addons,
39+
context,
40+
})
3341

3442
// Sources of environment variables, in descending order of precedence.
3543
const sources = [
@@ -91,7 +99,7 @@ const getGeneralEnv = async function ({
9199
context,
92100
}) {
93101
const gitEnv = await getGitEnv(buildDir, branch)
94-
const deployUrls = getDeployUrls({ siteInfo, branch, deployId })
102+
const deployUrls = getDeployUrls({ siteInfo: siteInfo as $TSFixMe, branch, deployId })
95103
return removeFalsy({
96104
SITE_ID: id,
97105
SITE_NAME: name,
@@ -112,7 +120,11 @@ const getGeneralEnv = async function ({
112120
}
113121

114122
const getDeployUrls = function ({
115-
siteInfo: { name = DEFAULT_SITE_NAME, ssl_url: sslUrl, build_settings: { repo_url: REPOSITORY_URL } = {} },
123+
siteInfo: {
124+
name = DEFAULT_SITE_NAME,
125+
ssl_url: sslUrl,
126+
build_settings: { repo_url: REPOSITORY_URL = undefined } = {},
127+
},
116128
branch,
117129
deployId,
118130
}) {
@@ -129,18 +141,28 @@ const NETLIFY_DEFAULT_DOMAIN = '.netlify.app'
129141
const DEFAULT_SITE_NAME = 'site-name'
130142

131143
// Environment variables specified by the user
132-
const getUserEnv = async function ({ api, config, siteInfo, accounts, addons }) {
133-
const accountEnv = await getAccountEnv({ api, siteInfo, accounts })
144+
const getUserEnv = async function ({ api, config, siteInfo, accounts, addons, context }) {
145+
const accountEnv = await getAccountEnv({ api, siteInfo, accounts, context })
134146
const addonsEnv = getAddonsEnv(addons)
135147
const uiEnv = getUiEnv({ siteInfo })
136148
const configFileEnv = getConfigFileEnv({ config })
137149
return [accountEnv, addonsEnv, uiEnv, configFileEnv].map(cleanUserEnv)
138150
}
139151

140152
// Account-wide environment variables
141-
const getAccountEnv = async function ({ api, siteInfo, accounts }) {
153+
const getAccountEnv = async function ({
154+
api,
155+
siteInfo,
156+
accounts,
157+
context,
158+
}: {
159+
api: NetlifyAPI
160+
siteInfo: any
161+
accounts: any
162+
context?: string
163+
}) {
142164
if (siteInfo.use_envelope) {
143-
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug })
165+
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, context })
144166
return envelope
145167
}
146168
const { site_env: siteEnv = {} } = accounts.find(({ slug }) => slug === siteInfo.account_slug) || {}

Diff for: packages/config/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const resolveConfig = async function (opts) {
6868

6969
const { siteInfo, accounts, addons, integrations } = await getSiteInfo({
7070
api,
71+
context,
7172
siteId,
7273
mode,
7374
offline,

Diff for: packages/config/tests/env/tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import test from 'ava'
55
const SITE_INFO_PATH = '/api/v1/sites/test'
66
const LIST_ACCOUNTS_PATH = '/api/v1/accounts'
77
const LIST_ADDONS_PATH = '/api/v1/sites/test/service-instances'
8-
const TEAM_ENVELOPE_PATH = '/api/v1/accounts/team/env'
9-
const SITE_ENVELOPE_PATH = '/api/v1/accounts/team/env?site_id=test'
8+
const TEAM_ENVELOPE_PATH = '/api/v1/accounts/team/env?context_name=production'
9+
const SITE_ENVELOPE_PATH = '/api/v1/accounts/team/env?context_name=production&site_id=test'
1010

1111
// List of API mock URLs, responses and status codes
1212
const SITE_INFO_RESPONSE_URL = {

0 commit comments

Comments
 (0)