@@ -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+
59103async function fetchInstallationRepositories ( octokit : Octokit , installationId : number ) {
60104 const iterator = octokit . paginate . iterator ( octokit . rest . apps . listReposAccessibleToInstallation , {
61105 installation_id : installationId ,
0 commit comments