Sfoglia il codice sorgente

schema/migration changes and webhook insertion flow

Ivan Galakhov 4 anni fa
parent
commit
88c5cf1976

+ 1 - 0
cmd/app/main.go

@@ -72,6 +72,7 @@ func main() {
 		&ints.ClusterTokenCache{},
 		&ints.RegTokenCache{},
 		&ints.HelmRepoTokenCache{},
+		&ints.GithubAppInstallation{},
 	)
 
 	if err != nil {

+ 1 - 0
cmd/migrate/main.go

@@ -57,6 +57,7 @@ func main() {
 		&ints.ClusterTokenCache{},
 		&ints.RegTokenCache{},
 		&ints.HelmRepoTokenCache{},
+		&ints.GithubAppInstallation{},
 	)
 
 	if err != nil {

+ 4 - 4
internal/models/integrations/github_app.go

@@ -8,20 +8,20 @@ type GithubAppInstallation struct {
 	gorm.Model
 
 	// Can belong to either a user or an organization
-	AccountID string `json:"account_id" gorm:"unique"`
+	AccountID int64 `json:"account_id" gorm:"unique"`
 
 	// Installation ID (used for authentication)
-	InstallationID string `json:"installation_id"`
+	InstallationID int64 `json:"installation_id"`
 }
 
 type GithubAppInstallationExternal struct {
 	ID uint `json:"id"`
 
 	// Can belong to either a user or an organization
-	AccountID string `json:"account_id"`
+	AccountID int64 `json:"account_id"`
 
 	// Installation ID (used for authentication)
-	InstallationID string `json:"installation_id"`
+	InstallationID int64 `json:"installation_id"`
 }
 
 func (r *GithubAppInstallation) Externalize() *GithubAppInstallationExternal {

+ 2 - 1
internal/repository/gorm/auth.go

@@ -1114,7 +1114,8 @@ func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(id uint)
 	return ret, nil
 }
 
-func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID string) (*ints.GithubAppInstallation, error) {
+func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
+
 	ret := &ints.GithubAppInstallation{}
 
 	if err := repo.db.Where("account_id = ?", accountID).First(&ret).Error; err != nil {

+ 1 - 0
internal/repository/gorm/helpers_test.go

@@ -72,6 +72,7 @@ func setupTestEnv(tester *tester, t *testing.T) {
 		&ints.ClusterTokenCache{},
 		&ints.RegTokenCache{},
 		&ints.HelmRepoTokenCache{},
+		&ints.GithubAppInstallation{},
 	)
 
 	if err != nil {

+ 21 - 20
internal/repository/gorm/repository.go

@@ -9,25 +9,26 @@ import (
 // gorm.DB for querying the database
 func NewRepository(db *gorm.DB, key *[32]byte) *repository.Repository {
 	return &repository.Repository{
-		User:             NewUserRepository(db),
-		Session:          NewSessionRepository(db),
-		Project:          NewProjectRepository(db),
-		Release:          NewReleaseRepository(db),
-		GitRepo:          NewGitRepoRepository(db, key),
-		Cluster:          NewClusterRepository(db, key),
-		HelmRepo:         NewHelmRepoRepository(db, key),
-		Registry:         NewRegistryRepository(db, key),
-		Infra:            NewInfraRepository(db, key),
-		GitActionConfig:  NewGitActionConfigRepository(db),
-		Invite:           NewInviteRepository(db),
-		AuthCode:         NewAuthCodeRepository(db),
-		DNSRecord:        NewDNSRecordRepository(db),
-		PWResetToken:     NewPWResetTokenRepository(db),
-		KubeIntegration:  NewKubeIntegrationRepository(db, key),
-		BasicIntegration: NewBasicIntegrationRepository(db, key),
-		OIDCIntegration:  NewOIDCIntegrationRepository(db, key),
-		OAuthIntegration: NewOAuthIntegrationRepository(db, key),
-		GCPIntegration:   NewGCPIntegrationRepository(db, key),
-		AWSIntegration:   NewAWSIntegrationRepository(db, key),
+		User:                  NewUserRepository(db),
+		Session:               NewSessionRepository(db),
+		Project:               NewProjectRepository(db),
+		Release:               NewReleaseRepository(db),
+		GitRepo:               NewGitRepoRepository(db, key),
+		Cluster:               NewClusterRepository(db, key),
+		HelmRepo:              NewHelmRepoRepository(db, key),
+		Registry:              NewRegistryRepository(db, key),
+		Infra:                 NewInfraRepository(db, key),
+		GitActionConfig:       NewGitActionConfigRepository(db),
+		Invite:                NewInviteRepository(db),
+		AuthCode:              NewAuthCodeRepository(db),
+		DNSRecord:             NewDNSRecordRepository(db),
+		PWResetToken:          NewPWResetTokenRepository(db),
+		KubeIntegration:       NewKubeIntegrationRepository(db, key),
+		BasicIntegration:      NewBasicIntegrationRepository(db, key),
+		OIDCIntegration:       NewOIDCIntegrationRepository(db, key),
+		OAuthIntegration:      NewOAuthIntegrationRepository(db, key),
+		GCPIntegration:        NewGCPIntegrationRepository(db, key),
+		AWSIntegration:        NewAWSIntegrationRepository(db, key),
+		GithubAppInstallation: NewGithubAppInstallationRepository(db),
 	}
 }

+ 1 - 1
internal/repository/integrations.go

@@ -58,5 +58,5 @@ type GCPIntegrationRepository interface {
 type GithubAppInstallationRepository interface {
 	CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error)
 	ReadGithubAppInstallation(id uint) (*ints.GithubAppInstallation, error)
-	ReadGithubAppInstallationByAccountID(accountID string) (*ints.GithubAppInstallation, error)
+	ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error)
 }

+ 4 - 1
internal/repository/memory/auth.go

@@ -2,6 +2,7 @@ package test
 
 import (
 	"errors"
+	"fmt"
 
 	"github.com/porter-dev/porter/internal/repository"
 	"gorm.io/gorm"
@@ -464,7 +465,9 @@ func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(id uint)
 	return repo.githubAppInstallations[int(id-1)], nil
 }
 
-func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID string) (*ints.GithubAppInstallation, error) {
+func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
+	fmt.Println("calling memory handler")
+
 	if !repo.canQuery {
 		return nil, errors.New("cannot write database")
 	}

+ 24 - 5
server/api/integration_handler.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"github.com/google/go-github/github"
+	"gorm.io/gorm"
 	"io/ioutil"
 	"net/http"
 	"net/url"
@@ -397,11 +398,29 @@ func (app *App) HandleGithubAppEvent(w http.ResponseWriter, r *http.Request) {
 
 	switch e := event.(type) {
 	case *github.InstallationEvent:
-		fmt.Println("installation event")
-		fmt.Println("we want to insert the pair:")
-		fmt.Println(*e.Installation.ID)
-		fmt.Println(*e.Installation.Account.ID)
-		//fmt.Println(*e.Installation.Account.Login)
+		if *e.Action == "created" {
+			_, err := app.Repo.GithubAppInstallation.ReadGithubAppInstallationByAccountID(*e.Installation.Account.ID)
+
+			if err != nil && err == gorm.ErrRecordNotFound {
+				// insert account/installation pair into databse
+				_, err := app.Repo.GithubAppInstallation.CreateGithubAppInstallation(&ints.GithubAppInstallation{
+					AccountID:      *e.Installation.Account.ID,
+					InstallationID: *e.Installation.ID,
+				})
+
+				if err != nil {
+					app.handleErrorInternal(err, w)
+				}
+
+				return
+			} else if err != nil {
+				app.handleErrorInternal(err, w)
+				return
+			}
+		}
+		if *e.Action == "deleted" {
+			fmt.Println("deletion event")
+		}
 	}
 
 }