Skip to content

Commit 9a7eb49

Browse files
committed
Handle gh install updates separately from new installs
1 parent d07e605 commit 9a7eb49

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

apps/webapp/app/routes/_app.github.callback/route.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type LoaderFunctionArgs } from "@remix-run/node";
22
import { z } from "zod";
33
import { validateGitHubAppInstallSession } from "~/services/gitHubSession.server";
4-
import { linkGitHubAppInstallation } from "~/services/gitHub.server";
4+
import { linkGitHubAppInstallation, updateGitHubAppInstallation } from "~/services/gitHub.server";
55
import { logger } from "~/services/logger.server";
66
import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server";
77
import { tryCatch } from "@trigger.dev/core";
@@ -76,8 +76,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
7676
}
7777

7878
switch (callbackData.setup_action) {
79-
case "install":
80-
case "update": {
79+
case "install": {
8180
const [error] = await tryCatch(
8281
linkGitHubAppInstallation(callbackData.installation_id, organizationId)
8382
);
@@ -92,6 +91,19 @@ export async function loader({ request }: LoaderFunctionArgs) {
9291
return redirectWithSuccessMessage(redirectTo, request, "GitHub App installed successfully");
9392
}
9493

94+
case "update": {
95+
const [error] = await tryCatch(updateGitHubAppInstallation(callbackData.installation_id));
96+
97+
if (error) {
98+
logger.error("Failed to update GitHub App installation", {
99+
error,
100+
});
101+
return redirectWithErrorMessage(redirectTo, request, "Failed to update GitHub App");
102+
}
103+
104+
return redirectWithSuccessMessage(redirectTo, request, "GitHub App updated successfully");
105+
}
106+
95107
case "request": {
96108
// This happens when a non-admin user requests installation
97109
// The installation_id won't be available until an admin approves

apps/webapp/app/services/gitHub.server.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,50 @@ export async function linkGitHubAppInstallation(
5656
});
5757
}
5858

59+
/**
60+
* Links a GitHub App installation to a Trigger organization
61+
*/
62+
export async function updateGitHubAppInstallation(installationId: number): Promise<void> {
63+
if (!githubApp) {
64+
throw new Error("GitHub App is not enabled");
65+
}
66+
67+
const octokit = await githubApp.getInstallationOctokit(installationId);
68+
const { data: installation } = await octokit.rest.apps.getInstallation({
69+
installation_id: installationId,
70+
});
71+
72+
const existingInstallation = await prisma.githubAppInstallation.findFirst({
73+
where: { appInstallationId: installationId },
74+
});
75+
76+
if (!existingInstallation) {
77+
throw new Error("GitHub App installation not found");
78+
}
79+
80+
const repositorySelection = installation.repository_selection === "all" ? "ALL" : "SELECTED";
81+
82+
// repos are updated asynchronously via webhook events
83+
await prisma.githubAppInstallation.update({
84+
where: { id: existingInstallation?.id },
85+
data: {
86+
appInstallationId: installationId,
87+
targetId: installation.target_id,
88+
targetType: installation.target_type,
89+
accountHandle: installation.account
90+
? "login" in installation.account
91+
? installation.account.login
92+
: "slug" in installation.account
93+
? installation.account.slug
94+
: "-"
95+
: "-",
96+
permissions: installation.permissions,
97+
suspendedAt: existingInstallation?.suspendedAt,
98+
repositorySelection,
99+
},
100+
});
101+
}
102+
59103
async function fetchInstallationRepositories(octokit: Octokit, installationId: number) {
60104
const iterator = octokit.paginate.iterator(octokit.rest.apps.listReposAccessibleToInstallation, {
61105
installation_id: installationId,

0 commit comments

Comments
 (0)