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
4 changes: 3 additions & 1 deletion packages/config/src/api/site_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type GetSiteInfoOpts = {
siteFeatureFlagPrefix: string
offline?: boolean
api?: NetlifyAPI
context?: string
featureFlags?: Record<string, boolean>
testOpts?: TestOptions
}
Expand All @@ -30,6 +31,7 @@ export const getSiteInfo = async function ({
siteId,
mode,
siteFeatureFlagPrefix,
context,
offline = false,
testOpts = {},
}: GetSiteInfoOpts) {
Expand All @@ -53,7 +55,7 @@ export const getSiteInfo = async function ({
const [siteInfo, accounts, addons, integrations] = await Promise.all(promises)

if (siteInfo.use_envelope) {
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId })
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, siteId, context })

siteInfo.build_settings.env = envelope
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export const getEnvelope = async function ({ api, accountId, siteId }) {
import type { NetlifyAPI } from 'netlify'

export const getEnvelope = async function ({
api,
accountId,
siteId,
context,
}: {
api: NetlifyAPI
accountId: string
siteId?: string
context?: string
}) {
if (accountId === undefined) {
return {}
}
try {
const environmentVariables = await api.getEnvVars({ accountId, siteId })
const environmentVariables = await (api as any).getEnvVars({ accountId, siteId, context_name: context })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding correctly based on the tests, in the absence of a context this will apply the default value which will be production meaning the current behaviour is maintained?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coooorect!


const sortedEnvVarsFromDevContext = environmentVariables
.sort((left, right) => (left.key.toLowerCase() < right.key.toLowerCase() ? -1 : 1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { NetlifyAPI } from 'netlify'
import omit from 'omit.js'

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

const generalEnv = await getGeneralEnv({ siteInfo, buildDir, branch, deployId, buildId, context })
const [accountEnv, addonsEnv, uiEnv, configFileEnv] = await getUserEnv({ api, config, siteInfo, accounts, addons })
const [accountEnv, addonsEnv, uiEnv, configFileEnv] = await getUserEnv({
api,
config,
siteInfo,
accounts,
addons,
context,
})

// Sources of environment variables, in descending order of precedence.
const sources = [
Expand Down Expand Up @@ -91,7 +99,7 @@ const getGeneralEnv = async function ({
context,
}) {
const gitEnv = await getGitEnv(buildDir, branch)
const deployUrls = getDeployUrls({ siteInfo, branch, deployId })
const deployUrls = getDeployUrls({ siteInfo: siteInfo as $TSFixMe, branch, deployId })
return removeFalsy({
SITE_ID: id,
SITE_NAME: name,
Expand All @@ -112,7 +120,11 @@ const getGeneralEnv = async function ({
}

const getDeployUrls = function ({
siteInfo: { name = DEFAULT_SITE_NAME, ssl_url: sslUrl, build_settings: { repo_url: REPOSITORY_URL } = {} },
siteInfo: {
name = DEFAULT_SITE_NAME,
ssl_url: sslUrl,
build_settings: { repo_url: REPOSITORY_URL = undefined } = {},
},
branch,
deployId,
}) {
Expand All @@ -129,18 +141,28 @@ const NETLIFY_DEFAULT_DOMAIN = '.netlify.app'
const DEFAULT_SITE_NAME = 'site-name'

// Environment variables specified by the user
const getUserEnv = async function ({ api, config, siteInfo, accounts, addons }) {
const accountEnv = await getAccountEnv({ api, siteInfo, accounts })
const getUserEnv = async function ({ api, config, siteInfo, accounts, addons, context }) {
const accountEnv = await getAccountEnv({ api, siteInfo, accounts, context })
const addonsEnv = getAddonsEnv(addons)
const uiEnv = getUiEnv({ siteInfo })
const configFileEnv = getConfigFileEnv({ config })
return [accountEnv, addonsEnv, uiEnv, configFileEnv].map(cleanUserEnv)
}

// Account-wide environment variables
const getAccountEnv = async function ({ api, siteInfo, accounts }) {
const getAccountEnv = async function ({
api,
siteInfo,
accounts,
context,
}: {
api: NetlifyAPI
siteInfo: any
accounts: any
context?: string
}) {
if (siteInfo.use_envelope) {
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug })
const envelope = await getEnvelope({ api, accountId: siteInfo.account_slug, context })
return envelope
}
const { site_env: siteEnv = {} } = accounts.find(({ slug }) => slug === siteInfo.account_slug) || {}
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const resolveConfig = async function (opts) {

const { siteInfo, accounts, addons, integrations } = await getSiteInfo({
api,
context,
siteId,
mode,
offline,
Expand Down
4 changes: 2 additions & 2 deletions packages/config/tests/env/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import test from 'ava'
const SITE_INFO_PATH = '/api/v1/sites/test'
const LIST_ACCOUNTS_PATH = '/api/v1/accounts'
const LIST_ADDONS_PATH = '/api/v1/sites/test/service-instances'
const TEAM_ENVELOPE_PATH = '/api/v1/accounts/team/env'
const SITE_ENVELOPE_PATH = '/api/v1/accounts/team/env?site_id=test'
const TEAM_ENVELOPE_PATH = '/api/v1/accounts/team/env?context_name=production'
const SITE_ENVELOPE_PATH = '/api/v1/accounts/team/env?context_name=production&site_id=test'

// List of API mock URLs, responses and status codes
const SITE_INFO_RESPONSE_URL = {
Expand Down