@@ -2480,139 +2480,8 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
24802480
24812481 slog .Debug ("Validating GitHub callback" , "installationId" , installationId64 , "clientId" , clientId )
24822482
2483- if setupAction = = "update" {
2483+ if setupAction ! = "update" {
24842484
2485- // if the setupAction is equal to update, keep the same logic but don't validate as we don't have the code query parameter.
2486-
2487- // TODO: Lookup org in GithubAppInstallation by installationID if found use that installationID otherwise
2488- // create a new org for this installationID
2489- // retrieve org for current orgID
2490- installationIdInt64 , err := strconv .ParseInt (installationId , 10 , 64 )
2491- if err != nil {
2492- slog .Error ("Failed to parse installation ID as int64" ,
2493- "installationId" , installationId ,
2494- "error" , err ,
2495- )
2496- c .JSON (http .StatusInternalServerError , gin.H {"error" : "installationId could not be parsed" })
2497- return
2498- }
2499-
2500- slog .Debug ("Looking up GitHub app installation link" , "installationId" , installationIdInt64 )
2501-
2502- var link * models.GithubAppInstallationLink
2503- link , err = models .DB .GetGithubAppInstallationLink (installationIdInt64 )
2504- if err != nil {
2505- slog .Error ("Error getting GitHub app installation link" ,
2506- "installationId" , installationIdInt64 ,
2507- "error" , err ,
2508- )
2509- c .JSON (http .StatusInternalServerError , gin.H {"error" : "error getting github app link" })
2510- return
2511- }
2512-
2513- if link == nil {
2514- slog .Info ("No existing link found, creating new organization and link" ,
2515- "installationId" , installationId ,
2516- )
2517-
2518- name := fmt .Sprintf ("dggr-def-%v" , uuid .NewString ()[:8 ])
2519- externalId := uuid .NewString ()
2520-
2521- slog .Debug ("Creating new organization" ,
2522- "name" , name ,
2523- "externalId" , externalId ,
2524- )
2525-
2526- org , err := models .DB .CreateOrganisation (name , "digger" , externalId )
2527- if err != nil {
2528- slog .Error ("Error creating organization" ,
2529- "name" , name ,
2530- "error" , err ,
2531- )
2532- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Error with CreateOrganisation" })
2533- return
2534- }
2535-
2536- slog .Debug ("Creating GitHub installation link" ,
2537- "orgId" , org .ID ,
2538- "installationId" , installationId64 ,
2539- )
2540-
2541- link , err = models .DB .CreateGithubInstallationLink (org , installationId64 )
2542- if err != nil {
2543- slog .Error ("Error creating GitHub installation link" ,
2544- "orgId" , org .ID ,
2545- "installationId" , installationId64 ,
2546- "error" , err ,
2547- )
2548- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Error with CreateGithubInstallationLink" })
2549- return
2550- }
2551-
2552- slog .Info ("Created new organization and installation link" ,
2553- "orgId" , org .ID ,
2554- "installationId" , installationId64 ,
2555- )
2556- } else {
2557- slog .Info ("Found existing installation link" ,
2558- "orgId" , link .OrganisationId ,
2559- "installationId" , installationId64 ,
2560- )
2561- }
2562-
2563- org := link .Organisation
2564- orgId := link .OrganisationId
2565-
2566- // create a github installation link (org ID matched to installation ID)
2567- _ , err = models .DB .CreateGithubInstallationLink (org , installationId64 )
2568- if err != nil {
2569- slog .Error ("Error creating GitHub installation link" ,
2570- "orgId" , orgId ,
2571- "installationId" , installationId64 ,
2572- "error" , err ,
2573- )
2574- c .JSON (http .StatusInternalServerError , gin.H {"error" : "Error updating GitHub installation" })
2575- return
2576- }
2577-
2578- // we get repos accessible to this installation
2579- slog .Debug ("Listing repositories for installation" , "installationId" , installationId64 )
2580-
2581- // resets all existing installations (soft delete)
2582- slog .Debug ("Resetting existing GitHub installations" ,
2583- "installationId" , installationId ,
2584- )
2585-
2586- var AppInstallation models.GithubAppInstallation
2587- err = models .DB .GormDB .Model (& AppInstallation ).Where ("github_installation_id=?" , installationId ).Update ("status" , models .GithubAppInstallDeleted ).Error
2588- if err != nil {
2589- slog .Error ("Failed to update GitHub installations" ,
2590- "installationId" , installationId ,
2591- "error" , err ,
2592- )
2593- c .String (http .StatusInternalServerError , "Failed to update github installations: %v" , err )
2594- return
2595- }
2596-
2597- // reset all existing repos (soft delete)
2598- slog .Debug ("Soft deleting existing repositories" ,
2599- "orgId" , orgId ,
2600- )
2601-
2602- var ExistingRepos []models.Repo
2603- err = models .DB .GormDB .Delete (ExistingRepos , "organisation_id=?" , orgId ).Error
2604- if err != nil {
2605- slog .Error ("Could not delete repositories" ,
2606- "orgId" , orgId ,
2607- "error" , err ,
2608- )
2609- c .String (http .StatusInternalServerError , "could not delete repos: %v" , err )
2610- return
2611- }
2612-
2613- c .HTML (http .StatusOK , "github_success.tmpl" , gin.H {})
2614-
2615- } else {
26162485 code := c .Request .URL .Query ()["code" ][0 ]
26172486 result , installation , err := validateGithubCallback (d .GithubClientProvider , clientId , clientSecret , code , installationId64 )
26182487 if ! result {
@@ -2832,6 +2701,9 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
28322701 )
28332702
28342703 c .HTML (http .StatusOK , "github_success.tmpl" , gin.H {})
2704+
2705+ } else {
2706+ c .HTML (http .StatusBadRequest , "github_success.tmpl" , gin.H {})
28352707 }
28362708}
28372709
0 commit comments