forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.js
More file actions
62 lines (55 loc) · 1.71 KB
/
clean.js
File metadata and controls
62 lines (55 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* SPDX-FileCopyrightText: 2016-present Kriasoft <hello@kriasoft.com> */
/* SPDX-License-Identifier: MIT */
import { Octokit } from "@octokit/rest";
import envars from "envars";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { $, nothrow } from "zx";
import { deleteTestKVNamespace, deleteTestSubdomain } from "./cloudflare.js";
// Load environment variables
const __dirname = path.dirname(fileURLToPath(import.meta.url));
envars.config({ env: "test", cwd: path.resolve(__dirname, "../env") });
// Initialize GitHub client
const env = process.env;
const [owner, repo] = env.GITHUB_REPOSITORY.split("/");
const gh = new Octokit({ auth: env.GITHUB_TOKEN });
// Get the list of merged PRs
const { data: pulls } = await gh.pulls.list({
owner,
repo,
state: "closed",
sort: "updated",
direction: "desc",
base: "main",
per_page: 30,
});
// Cleans up transient deployments for merged PRs
for (const pr of pulls.slice(3)) {
console.log("[", pr.number, "]", pr.title);
const [res] = await Promise.all([
gh.repos.listDeployments({
owner,
repo,
environment: `${pr.number}-test`,
}),
deleteTestSubdomain(pr.number),
deleteTestKVNamespace(pr.number),
nothrow(
$`gcloud functions delete api_${pr.number} --project=${env.GOOGLE_CLOUD_PROJECT} --region=${env.GOOGLE_CLOUD_REGION} --verbosity=none --quiet`,
),
]);
for (const deployment of res.data) {
await gh.repos.createDeploymentStatus({
mediaType: { previews: ["ant-man", "flash"] },
owner,
repo,
deployment_id: deployment.id,
state: "inactive",
});
await gh.repos.deleteDeployment({
owner,
repo,
deployment_id: deployment.id,
});
}
}