Skip to content

Commit

Permalink
fetch org from db (#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Sep 3, 2024
1 parent ca920c5 commit 3dbd67a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
3 changes: 2 additions & 1 deletion backend/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 29 additions & 7 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion backend/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3dbd67a

Please sign in to comment.