Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add before-uninstall to cleanup buildhooks and build tokens #48

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 1 addition & 1 deletion extension.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
config:
slug: yl9z8yms-migrate-csp-extension
slug: zqi474os-csp-testing-v2

# set this to true to add a global require shim to esbuild. Workaround for https://github.com/evanw/esbuild/issues/1921
use_cjs_shims: false
Expand Down
43 changes: 43 additions & 0 deletions src/endpoints/before-uninstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { withNetlifySDKContext } from "@netlify/sdk/ui/functions";
import { siteConfigSchema } from "..";

export default withNetlifySDKContext(async (_req, context) => {
if (!context.teamId) {
return new Response("teamId is required", { status: 400 });
}

const sites = await context.client.getSites();

for (const site of sites) {
const siteConfig = await context.client.getSiteConfiguration(
context.teamId,
site.id
);
if (siteConfig) {
const parsedConfig = siteConfigSchema.safeParse(siteConfig.config);
if (parsedConfig.success) {
// delete the env and build hook
if (parsedConfig?.data?.buildHook?.id) {
try {
await context.client.deleteBuildHook(
site.id,
parsedConfig.data.buildHook.id
);
} catch (e) {
console.error(
`Failed to delete build hook ${parsedConfig.data.buildHook.id} for site ${site.id}`
);
}
}

try {
await context.client.removeBuildToken(context.teamId, site.id);
} catch (e) {
console.error(`Failed to remove build token for site ${site.id}`);
}
}
}
}

return new Response("Uninstall complete", { status: 200 });
});