index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* eslint-disable @typescript-eslint/no-var-requires */
  2. const { Octokit } = require('@octokit/rest')
  3. const { get } = require('lodash')
  4. const ENV_PRODUCTION = 'PRODUCTION'
  5. const ENV_STAGING = 'STAGING'
  6. const ENV_DEVELOPMENT = 'DEVELOPMENT'
  7. module.exports = {
  8. onPreBuild: async ({ inputs: { production_branch, app_env_prefix }, utils }) => {
  9. const { CONTEXT, REVIEW_ID, REPOSITORY_URL, SITE_NAME } = process.env
  10. // === get env based on branch/PR ===
  11. let env = ENV_PRODUCTION
  12. if (SITE_NAME === 'atlas-app-mocked') {
  13. env = ENV_DEVELOPMENT
  14. } else if (CONTEXT === 'branch-deploy') {
  15. env = ENV_STAGING
  16. } else if (CONTEXT === 'deploy-preview') {
  17. const productionPull = await isProductionPull({
  18. productionBranch: production_branch,
  19. repoUrl: REPOSITORY_URL,
  20. pullNumber: REVIEW_ID,
  21. })
  22. if (!productionPull) {
  23. env = ENV_STAGING
  24. }
  25. }
  26. const pluginSummary = `Using ${env} variables`
  27. console.log(pluginSummary)
  28. // === expose all matching env variables ===
  29. let pluginText = ''
  30. Object.keys(process.env).forEach((varKey) => {
  31. if (varKey.startsWith(env)) {
  32. const varValue = process.env[varKey]
  33. const appVarKey = app_env_prefix ? `${app_env_prefix}${varKey.replace(`${env}_`, '')}` : varKey
  34. const varLine = `Exposing ${varKey} as ${appVarKey}=${varValue}`
  35. pluginText = `${pluginText}\n${varLine}`
  36. console.log(varLine)
  37. process.env[appVarKey] = varValue
  38. }
  39. })
  40. // === expose env ===
  41. const appEnvKey = `${app_env_prefix}ENV`
  42. const envLine = `Exposing ${appEnvKey}=${env}`
  43. pluginText = `${pluginText}\n${envLine}`
  44. console.log(envLine)
  45. process.env[appEnvKey] = env
  46. utils.status.show({
  47. title: 'Env variables set',
  48. summary: pluginSummary,
  49. text: pluginText,
  50. })
  51. },
  52. }
  53. const isProductionPull = async ({ productionBranch, repoUrl, pullNumber }) => {
  54. try {
  55. const baseBranch = await getPullBaseBranch({ repoUrl, pullNumber })
  56. return baseBranch === productionBranch
  57. } catch (e) {
  58. console.log("Couldn't load base branch: ", e)
  59. }
  60. return true
  61. }
  62. const getRepoOwnerAndName = (repoUrl) => {
  63. const segments = repoUrl.split('/')
  64. const owner = segments[segments.length - 2]
  65. const name = segments[segments.length - 1]
  66. return { owner, name }
  67. }
  68. const getPullBaseBranch = async ({ repoUrl, pullNumber, timeout = 2000 }) => {
  69. const { owner, name } = getRepoOwnerAndName(repoUrl)
  70. const octokit = new Octokit()
  71. const response = await octokit.pulls.get({
  72. owner,
  73. repo: name,
  74. pull_number: pullNumber,
  75. request: {
  76. timeout,
  77. },
  78. })
  79. if (response.status !== 200) {
  80. throw new Error('Failed to fetch pull')
  81. }
  82. const base = get(response, 'data.base.ref', null)
  83. if (!base) {
  84. throw new Error('Failed to access base branch')
  85. }
  86. return base
  87. }