Skip to content

Commit 9f9efb3

Browse files
committed
ignore webhook if apps folder does not have changes
1 parent efd21e5 commit 9f9efb3

File tree

6 files changed

+329
-1
lines changed

6 files changed

+329
-1
lines changed

app/lib/octokit.server.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Octokit } from "@octokit/rest";
2+
import { GITHUB_API_TOKEN } from "config/env.server";
3+
4+
export const octokit = new Octokit({ auth: GITHUB_API_TOKEN });

app/models/pull-request.server.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { octokit } from "~/lib/octokit.server";
2+
3+
interface Repository {
4+
owner: string;
5+
name: string;
6+
}
7+
8+
interface FindPullRequestFilesArgs {
9+
repository: Repository;
10+
prNumber: number;
11+
}
12+
export async function findPullRequestFiles({
13+
repository,
14+
prNumber,
15+
}: FindPullRequestFilesArgs) {
16+
const files = await octokit.pulls.listFiles({
17+
owner: repository.owner,
18+
repo: repository.name,
19+
pull_number: prNumber,
20+
});
21+
return files.data;
22+
}

app/routes/github.webhook/pull-request-webhook.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { json } from "@remix-run/node";
22
import { z } from "zod";
33
import { DeleteDeploymentJob } from "~/jobs/delete-deployment-job.server";
44
import { PushJob } from "~/jobs/push-job.server";
5-
import { upsertBranch } from "~/models/branch.server";
5+
import { findBranch, upsertBranch } from "~/models/branch.server";
6+
import { findPullRequestFiles } from "~/models/pull-request.server";
67
import { type Repository } from "~/models/repository.server";
78

89
type GithubPullRequestEvent = z.infer<typeof GithubPullRequestEventSchema>;
@@ -14,6 +15,11 @@ export async function processPullRequestWebhook(
1415
if (!repository.deployOnlyOnPullRequest)
1516
return json({ message: "No action taken" });
1617

18+
const [files, branch] = await findPullRequestData();
19+
20+
if (!branch && !hasAppsFolderChanges())
21+
return json({ message: "No action taken" });
22+
1723
switch (webhook.payload.action) {
1824
case "opened":
1925
case "synchronize": {
@@ -28,6 +34,20 @@ export async function processPullRequestWebhook(
2834
return json({ message: "No action taken" });
2935
}
3036

37+
function findPullRequestData() {
38+
return Promise.all([
39+
findPullRequestFiles({
40+
repository,
41+
prNumber: webhook.payload.number,
42+
}),
43+
findBranch(branchName()),
44+
]);
45+
}
46+
47+
function hasAppsFolderChanges() {
48+
return files.some((file) => file.filename.startsWith("apps/"));
49+
}
50+
3151
async function deploy() {
3252
await upsertBranch({
3353
branchName: branchName(),

config/env.server.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const STRAPI_TRANSFER_TOKEN_SALT =
2323

2424
export const STRAPI_ADMIN_BACKEND_URL =
2525
process.env.STRAPI_ADMIN_BACKEND_URL ?? "";
26+
27+
export const GITHUB_API_TOKEN = process.env.GITHUB_API_TOKEN;

0 commit comments

Comments
 (0)