From 3dbd67a4ae8368e7cc8b03761ccf9eec7024bd29 Mon Sep 17 00:00:00 2001 From: Mohamed Habib Date: Tue, 3 Sep 2024 05:37:44 -0700 Subject: [PATCH] fetch org from db (#1688) --- backend/bootstrap/main.go | 3 ++- backend/controllers/github.go | 36 ++++++++++++++++++++++++++++------- backend/models/storage.go | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/backend/bootstrap/main.go b/backend/bootstrap/main.go index 79eda2139..c32f2da4f 100644 --- a/backend/bootstrap/main.go +++ b/backend/bootstrap/main.go @@ -87,7 +87,8 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController githubGroup := r.Group("/github") githubGroup.Use(middleware.GetWebMiddleware()) - githubGroup.GET("/callback", diggerController.GithubAppCallbackPage) + // authless endpoint because we no longer rely on orgId + r.GET("/github/callback", diggerController.GithubAppCallbackPage) githubGroup.GET("/repos", diggerController.GithubReposPage) githubGroup.GET("/setup", controllers.GithubAppSetup) githubGroup.GET("/exchange-code", diggerController.GithubSetupExchangeCode) diff --git a/backend/controllers/github.go b/backend/controllers/github.go index 06b9dedea..f872882d7 100644 --- a/backend/controllers/github.go +++ b/backend/controllers/github.go @@ -1067,19 +1067,41 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) { // TODO: Lookup org in GithubAppInstallation by installationID if found use that installationID otherwise // create a new org for this installationID // retrive org for current orgID - orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY) - if !exists { - log.Printf("Unable to retrive orgId: %v", err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrive orgId"}) + installationIdInt64, err := strconv.ParseInt(installationId, 10, 64) + if err != nil { + log.Printf("strconv.ParseInt error: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "installationId could not be parsed"}) return } - org, err := models.DB.GetOrganisationById(orgId) + + var link *models.GithubAppInstallationLink + link, err = models.DB.GetGithubAppInstallationLink(installationIdInt64) if err != nil { - log.Printf("Error fetching organisation: %v", err) - c.JSON(http.StatusInternalServerError, gin.H{"error": "Error fetching organisation"}) + log.Printf("Error getting GetGithubAppInstallationLink: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "error getting github app link"}) return } + if link == nil { + log.Printf("Failed to find GithubAppInstallationLink create a link and an org %v", installationId) + name := fmt.Sprintf("dggr-def-%v", uuid.NewString()[:8]) + org, err := models.DB.CreateOrganisation(name, "digger", "digger") + if err != nil { + log.Printf("Error with CreateOrganisation: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error with CreateOrganisation"}) + return + } + link, err = models.DB.CreateGithubInstallationLink(org, installationId64) + if err != nil { + log.Printf("Error with CreateGithubInstallationLink: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Error with CreateGithubInstallationLink"}) + return + } + } + + org := link.Organisation + orgId := link.OrganisationId + // create a github installation link (org ID matched to installation ID) _, err = models.DB.CreateGithubInstallationLink(org, installationId64) if err != nil { diff --git a/backend/models/storage.go b/backend/models/storage.go index 29b41fe63..0d9f0315a 100644 --- a/backend/models/storage.go +++ b/backend/models/storage.go @@ -460,7 +460,7 @@ func (db *Database) GetGithubApp(gitHubAppId any) (*GithubApp, error) { func (db *Database) CreateGithubInstallationLink(org *Organisation, installationId int64) (*GithubAppInstallationLink, error) { l := GithubAppInstallationLink{} // check if there is already a link to another org, and throw an error in this case - result := db.GormDB.Where("github_installation_id = ? AND status=?", installationId, GithubAppInstallationLinkActive).Find(&l) + result := db.GormDB.Preload("Organisation").Where("github_installation_id = ? AND status=?", installationId, GithubAppInstallationLinkActive).Find(&l) if result.Error != nil { if !errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, result.Error