Browse Source

functions for deletion and webhook deletion

Ivan Galakhov 4 years ago
parent
commit
2a5e54345e

+ 8 - 0
internal/repository/gorm/auth.go

@@ -1124,3 +1124,11 @@ func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountI
 
 	return ret, nil
 }
+
+func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
+	if err := repo.db.Where("account_id = ?", accountID).Delete(ints.GithubAppInstallation{}).Error; err != nil {
+		return err
+	}
+
+	return nil
+}

+ 1 - 0
internal/repository/integrations.go

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

+ 14 - 3
internal/repository/memory/auth.go

@@ -2,8 +2,6 @@ package test
 
 import (
 	"errors"
-	"fmt"
-
 	"github.com/porter-dev/porter/internal/repository"
 	"gorm.io/gorm"
 
@@ -466,7 +464,6 @@ func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(id uint)
 }
 
 func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
-	fmt.Println("calling memory handler")
 
 	if !repo.canQuery {
 		return nil, errors.New("cannot write database")
@@ -480,3 +477,17 @@ func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountI
 
 	return nil, gorm.ErrRecordNotFound
 }
+
+func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
+	if !repo.canQuery {
+		return errors.New("cannot write database")
+	}
+
+	for i, installation := range repo.githubAppInstallations {
+		if installation != nil && installation.AccountID == accountID {
+			repo.githubAppInstallations[i] = nil
+		}
+	}
+
+	return nil
+}

+ 6 - 2
server/api/integration_handler.go

@@ -2,7 +2,6 @@ package api
 
 import (
 	"encoding/json"
-	"fmt"
 	"github.com/google/go-github/github"
 	"gorm.io/gorm"
 	"io/ioutil"
@@ -419,7 +418,12 @@ func (app *App) HandleGithubAppEvent(w http.ResponseWriter, r *http.Request) {
 			}
 		}
 		if *e.Action == "deleted" {
-			fmt.Println("deletion event")
+			err := app.Repo.GithubAppInstallation.DeleteGithubAppInstallationByAccountID(*e.Installation.Account.ID)
+
+			if err != nil {
+				app.handleErrorInternal(err, w)
+				return
+			}
 		}
 	}