This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
337 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"checkJs": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
name: snaplet-netlify-plugin-preview-db | ||
|
||
inputs: | ||
- name: databaseEnvVar | ||
required: false | ||
description: Database environment variable name | ||
default: "DATABASE_URL" | ||
|
||
- name: databaseCreateCommand | ||
required: false | ||
description: Command used to generate the instant database | ||
description: Command used to create the preview database | ||
default: "snaplet db create --git --latest" | ||
|
||
- name: databaseDeleteCommand | ||
required: false | ||
description: Command used to delete the preview database | ||
default: "snaplet db delete --git" | ||
|
||
- name: databaseUrlCommand | ||
required: false | ||
description: Command used to get the instant database url | ||
description: Command used to get the preview database url | ||
default: "snaplet db url --git" | ||
|
||
- name: databaseUrlEnvKey | ||
required: false | ||
description: Preview database environment variable key | ||
default: "DATABASE_URL" | ||
|
||
- name: reset | ||
required: false | ||
description: Reset the database state on each commit | ||
description: Reset the preview database state on each commit | ||
default: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { github } from "./github.js"; | ||
|
||
/** | ||
* @param {{ commitRef: string }} options | ||
*/ | ||
export async function getAssociatedPullRequests(options) { | ||
/** @type {{ head: { ref: string }}[]} */ | ||
const associatedPullRequests = await github(`commits/${options.commitRef}/pulls`); | ||
|
||
return associatedPullRequests; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import fetch from "node-fetch"; | ||
|
||
const [OWNER, REPOSITORY] = new URL(process.env.REPOSITORY_URL).pathname.slice(1).split("/"); | ||
|
||
/** | ||
* @template T | ||
* @param {string} path | ||
* @returns {Promise<T>} | ||
*/ | ||
export async function github(path, options) { | ||
const response = await fetch(`https://api.github.com/repos/${OWNER}/${REPOSITORY}/${path}`, { | ||
headers: { | ||
Accept: "application/vnd.github+json", | ||
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`, | ||
"Content-Type": "application/json", | ||
}, | ||
...options | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`); | ||
} | ||
|
||
// @ts-ignore | ||
return await response.json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { createPreviewDatabase } from "./snaplet/previewDatabase/createPreviewDatabase.js" | ||
import { installSnapletCLI } from "./snaplet/installSnapletCLI.js"; | ||
import { setEnvironmentVariable } from "./netlify/environmentVariable/setEnvironmentVariable.js"; | ||
|
||
/** | ||
* @param {{ | ||
* utils: { run: { command: (cmd: string, options: Record<string, any>) => { stdout: string } } }, | ||
* inputs: { databaseCreateCommand: string, databaseUrlCommand: string, databaseUrlEnvKey: string, reset: boolean } | ||
* }} config | ||
*/ | ||
export async function handleDeployPreview({ | ||
utils: { run }, | ||
inputs: { | ||
databaseCreateCommand, | ||
databaseUrlCommand, | ||
databaseUrlEnvKey, | ||
reset, | ||
}, | ||
}) { | ||
await installSnapletCLI({ run }); | ||
|
||
const databaseUrl = await createPreviewDatabase({ run }, { | ||
databaseCreateCommand, | ||
databaseUrlCommand, | ||
reset, | ||
}); | ||
|
||
await setEnvironmentVariable({ | ||
siteId: process.env.SITE_ID, | ||
branch: process.env.HEAD, | ||
key: databaseUrlEnvKey, | ||
value: databaseUrl, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { installSnapletCLI } from "./snaplet/installSnapletCLI.js"; | ||
import { getAssociatedPullRequests } from "./github/getAssociatedPullRequests.js"; | ||
import { deletePreviewDatabase } from "./snaplet/previewDatabase/deletePreviewDatabase.js"; | ||
import { deleteEnvironmentVariable } from "./netlify/environmentVariable/deleteEnvironmentVariable.js"; | ||
|
||
/** | ||
* @param {{ | ||
* utils: { run: { command: (cmd: string, options: Record<string, any>) => { stdout: string } } }, | ||
* inputs: { databaseDeleteCommand: string, databaseUrlCommand: string, databaseUrlEnvKey: string } | ||
* }} config | ||
*/ | ||
export async function handleDeployProduction({ | ||
utils: { run }, | ||
inputs: { | ||
databaseDeleteCommand, | ||
databaseUrlCommand, | ||
databaseUrlEnvKey, | ||
}, | ||
}) { | ||
const associatedPullRequests = await getAssociatedPullRequests({ commitRef: process.env.COMMIT_REF }); | ||
|
||
if (associatedPullRequests.length > 0) { | ||
await installSnapletCLI({ run }); | ||
|
||
await Promise.all(associatedPullRequests.map(async (pullRequest) => { | ||
const branch = pullRequest.head.ref; | ||
|
||
await Promise.all([ | ||
deleteEnvironmentVariable({ branch, key: databaseUrlEnvKey, siteId: process.env.SITE_ID }), | ||
deletePreviewDatabase({ run }, { branch, databaseDeleteCommand, databaseUrlCommand }), | ||
]); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,26 @@ | ||
import { createPreviewDatabase } from "./createPreviewDatabase.js" | ||
import { installSnapletCLI } from "./installSnapletCLI.js"; | ||
import { setEnvironmentVariable } from "./setEnvironmentVariable.js"; | ||
import { handleDeployPreview } from "./handleDeployPreview.js"; | ||
import { handleDeployProduction } from "./handleDeployProduction.js"; | ||
|
||
export async function onPreBuild({ | ||
utils: { run }, | ||
constants, | ||
netlifyConfig, | ||
inputs: { | ||
databaseEnvVar, | ||
databaseCreateCommand, | ||
databaseUrlCommand, | ||
reset, | ||
}, | ||
}) { | ||
if (process.env.CONTEXT === "deploy-preview") { | ||
await installSnapletCLI({ run }); | ||
|
||
const databaseUrl = await createPreviewDatabase({ run }, { | ||
databaseCreateCommand, | ||
databaseUrlCommand, | ||
reset, | ||
}); | ||
|
||
await setEnvironmentVariable({ | ||
siteId: constants.SITE_ID, | ||
branch: netlifyConfig.build.environment.BRANCH, | ||
key: databaseEnvVar, | ||
value: databaseUrl, | ||
}); | ||
/** | ||
* @param {{ | ||
* utils: { run: { command: (cmd: string, options: Record<string, any>) => { stdout: string } } }, | ||
* inputs: { databaseCreateCommand: string, databaseDeleteCommand: string, databaseUrlCommand: string, databaseUrlEnvKey: string, reset: boolean } | ||
* }} config | ||
*/ | ||
export async function onPreBuild(config) { | ||
switch (process.env.CONTEXT) { | ||
case "deploy-preview": | ||
await handleDeployPreview(config); | ||
break; | ||
case "production": | ||
try { | ||
await handleDeployProduction(config); | ||
} catch (e) { | ||
// We don't want the production build to fail because of resources clean up | ||
console.error(`Failed to clean up resources for this commit`); | ||
console.error(e); | ||
} | ||
break; | ||
default: | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
src/netlify/environmentVariable/deleteEnvironmentVariable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getAccountId } from "./getAccountId.js"; | ||
import { netlify } from "../netlify.js"; | ||
import { getEnvironmentVariable } from "./getEnvironmentVariable.js"; | ||
|
||
/** | ||
* @param {{ siteId: string, branch: string, key: string }} options | ||
*/ | ||
export async function deleteEnvironmentVariable(options) { | ||
const { siteId, branch, key } = options; | ||
|
||
const accountId = await getAccountId({ siteId }); | ||
|
||
const environmentVariable = await getEnvironmentVariable({ accountId, siteId, key }); | ||
|
||
const environmentVariableValue = environmentVariable?.values?.find(v => v.context === "branch" && v.context_parameter === branch); | ||
|
||
if (environmentVariableValue) { | ||
console.log(`Deleting environment variable ${key} for branch ${branch}...`); | ||
await netlify( | ||
`accounts/${accountId}/env/${key}/value/${environmentVariableValue.id}?site_id=${siteId}`, | ||
{ | ||
method: "DELETE", | ||
} | ||
); | ||
console.log(`Environment variable ${key} for branch ${branch} deleted.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { netlify } from "../netlify.js"; | ||
|
||
/** | ||
* @param {{ siteId: string }} options | ||
*/ | ||
export async function getAccountId(options) { | ||
const { siteId } = options; | ||
|
||
/** @type [{ account_name: string, account_slug: string }, { id: string, name: string, slug: string }[]] */ | ||
const [{ account_name, account_slug }, accounts] = await Promise.all([netlify(`sites/${siteId}`), netlify("accounts")]); | ||
|
||
const account = accounts.find(account => account.name === account_name && account.slug === account_slug); | ||
|
||
return account.id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { netlify } from "../netlify.js"; | ||
|
||
/** | ||
* @param {{ accountId: string, siteId: string, key: string }} options | ||
* @returns {Promise<{ values: { id: string, context: string, context_parameter?: string }[]} | null>} | ||
*/ | ||
export async function getEnvironmentVariable(options) { | ||
const { accountId, siteId, key } = options; | ||
|
||
try { | ||
/** @type {{ values: { id: string, context: string, context_parameter?: string }[]}} */ | ||
const environmentVariable = await netlify(`accounts/${accountId}/env/${key}?site_id=${siteId}`); | ||
|
||
return environmentVariable; | ||
} catch (_) { | ||
return null; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/netlify/environmentVariable/isEnvironmentVariableDeployed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getEnvironmentVariable } from "./getEnvironmentVariable.js"; | ||
|
||
/** | ||
* @param {{ accountId: string, siteId: string, key: string, branch: string }} options | ||
*/ | ||
export async function isEnvironmentVariableDeployed(options) { | ||
const { accountId, siteId, key, branch } = options; | ||
|
||
let environmentVariableIsDeployed = false; | ||
|
||
const environmentVariable = await getEnvironmentVariable({ accountId, siteId, key }); | ||
|
||
if (environmentVariable) { | ||
environmentVariableIsDeployed = environmentVariable.values.some(v => v.context === "branch" && v.context_parameter === branch); | ||
} | ||
|
||
return environmentVariableIsDeployed; | ||
} |
Oops, something went wrong.