Skip to content

Commit 4de18e4

Browse files
authored
Merge pull request #34 from diggerhq/feat/trigger-apply-button
Trigger apply button
2 parents 7084211 + ad1538f commit 4de18e4

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client'
2+
3+
import { Button } from "@/components/Button";
4+
import { triggerApplyAction } from "@/data/user/projects";
5+
import { useSAToastMutation } from "@/hooks/useSAToastMutation";
6+
7+
export function TriggerApplyButton({ projectId }: { projectId: string }) {
8+
9+
const triggerApplyMutation = useSAToastMutation(
10+
async () => {
11+
const result = await triggerApplyAction({
12+
projectId: projectId,
13+
});
14+
return result;
15+
},
16+
{
17+
loadingMessage: "Triggering apply...",
18+
successMessage: "Apply run scheduled successfully!",
19+
errorMessage: "Failed to schedule Apply run",
20+
}
21+
);
22+
23+
const onClick = async () => {
24+
try {
25+
await triggerApplyMutation.mutateAsync();
26+
} catch (error) {
27+
console.error("Error updating project settings:", error);
28+
}
29+
};
30+
31+
return (
32+
<Button variant="default" onClick={onClick} size="sm">
33+
Trigger Apply
34+
</Button>
35+
)
36+
}

src/app/(dynamic-pages)/(authenticated-pages)/(application-pages)/project/[projectSlug]/(specific-project-pages)/layout.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { projectSlugParamSchema } from "@/utils/zod-schemas/params";
66
import { AlertCircleIcon, ExternalLink, GitBranch } from "lucide-react";
77
import Link from "next/link";
88
import { Suspense } from "react";
9+
import { TriggerApplyButton } from "./TriggerApplyButton";
910

1011

1112
async function ProjectPageHeading({ projectId, branch }: { projectId: string, branch: string | null }) {
1213
const projectTitle = await getProjectTitleById(projectId);
1314
return (
1415
<PageHeading
1516
title={projectTitle}
16-
subTitle={<p className="flex items-center"><GitBranch /> {branch || 'main'}</p>}
17+
subTitle={<span className="flex items-center"><GitBranch /> {branch || 'main'}</span>}
1718
/>
1819
);
1920
}
@@ -25,8 +26,6 @@ export default async function ProjectPagesLayout({ params, children }: { params:
2526
const projectId = (await getSlimProjectBySlug(projectSlug)).id;
2627
const project = await getProjectById(projectId);
2728

28-
29-
3029
const tabs = [
3130
{
3231
label: 'Runs',
@@ -60,8 +59,13 @@ export default async function ProjectPagesLayout({ params, children }: { params:
6059
)}
6160

6261
<Suspense>
63-
<ProjectPageHeading projectId={project.id} branch={project.branch} />
62+
<div className="flex justify-between items-center space-y-4 max-w-5xl">
63+
<ProjectPageHeading projectId={project.id} branch={project.branch} />
64+
<TriggerApplyButton projectId={project.id} />
65+
</div>
6466
</Suspense>
67+
68+
6569
<Suspense>
6670
<TabsNavigationV2 tabs={tabs} />
6771
</Suspense>

src/data/user/projects.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -956,4 +956,35 @@ export async function deleteProject(projectId: string): Promise<SAPayload<unknow
956956
}
957957

958958
return { status: 'success', data };
959+
}
960+
961+
export async function triggerApplyAction({ projectId }: { projectId: string }): Promise<SAPayload<unknown>> {
962+
const triggerApplyUrl = process.env.DIGGER_TRIGGER_APPLY_URL;
963+
const webhookSecret = process.env.DIGGER_WEBHOOK_SECRET;
964+
if (!triggerApplyUrl) {
965+
throw new Error('DIGGER_TRIGGER_APPLY_URL env variable is not set');
966+
}
967+
if (!webhookSecret) {
968+
throw new Error('DIGGER_WEBHOOK_SECRET env variable is not set');
969+
}
970+
971+
const user = await serverGetLoggedInUser();
972+
const payload = {
973+
user_id: user.id,
974+
project_id: projectId,
975+
}
976+
const response = await fetch(
977+
triggerApplyUrl,
978+
{
979+
method: 'POST',
980+
body: JSON.stringify(payload),
981+
headers: {
982+
Authorization: `Bearer ${webhookSecret}`,
983+
},
984+
},
985+
);
986+
if (!response.ok) {
987+
throw new Error(`Digger api responded with status: ${response.status}`);
988+
}
989+
return { status: 'success', data: {} };
959990
}

0 commit comments

Comments
 (0)