-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dbmodels | ||
|
||
import ( | ||
"errors" | ||
"github.com/diggerhq/digger/ee/drift/model" | ||
"gorm.io/gorm" | ||
"log" | ||
) | ||
|
||
type DriftStatus string | ||
|
||
var DriftStatusNewDrift = "new drift" | ||
var DriftStatusNoDrift = "no drift" | ||
var DriftStatusAcknowledgeDrift = "acknowledged drift" | ||
|
||
// GetProjectByName return project for specified org and repo | ||
// if record doesn't exist return nil | ||
func (db *Database) GetProjectByName(orgId any, repo *model.Repo, name string) (*model.Project, error) { | ||
log.Printf("GetProjectByName, org id: %v, project name: %v\n", orgId, name) | ||
var project model.Project | ||
|
||
err := db.GormDB. | ||
Joins("INNER JOIN repos ON projects.repo_id = repos.id"). | ||
Where("repos.id = ?", repo.ID). | ||
Where("projects.name = ?", name).First(&project).Error | ||
|
||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
log.Printf("Unknown error occurred while fetching database, %v\n", err) | ||
return nil, err | ||
} | ||
|
||
return &project, nil | ||
} | ||
|
||
func (db *Database) CreateProject(name string, repo *model.Repo) (*model.Project, error) { | ||
project := &model.Project{ | ||
Name: name, | ||
RepoID: repo.ID, | ||
DriftStatus: DriftStatusNewDrift, | ||
} | ||
result := db.GormDB.Save(project) | ||
if result.Error != nil { | ||
log.Printf("Failed to create project: %v, error: %v\n", name, result.Error) | ||
return nil, result.Error | ||
} | ||
log.Printf("Project %s, (id: %v) has been created successfully\n", name, project.ID) | ||
return project, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package dbmodels | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/diggerhq/digger/ee/drift/model" | ||
configuration "github.com/diggerhq/digger/libs/digger_config" | ||
"gorm.io/gorm" | ||
"log" | ||
) | ||
|
||
// GetRepo returns digger repo by organisationId and repo name (diggerhq-digger) | ||
// it will return an empty object if record doesn't exist in database | ||
func (db *Database) GetRepo(orgIdKey any, repoName string) (*model.Repo, error) { | ||
var repo model.Repo | ||
|
||
err := db.GormDB.Where("organisation_id = ? AND repos.name=?", orgIdKey, repoName).First(&repo).Error | ||
|
||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, nil | ||
} | ||
log.Printf("Failed to find digger repo for orgId: %v, and repoName: %v, error: %v\n", orgIdKey, repoName, err) | ||
return nil, err | ||
} | ||
return &repo, nil | ||
} | ||
|
||
func (db *Database) RefreshProjectsFromRepo(orgId string, config configuration.DiggerConfigYaml, repo *model.Repo) error { | ||
log.Printf("UpdateRepoDiggerConfig, repo: %v\n", repo) | ||
|
||
err := db.GormDB.Transaction(func(tx *gorm.DB) error { | ||
for _, dc := range config.Projects { | ||
projectName := dc.Name | ||
p, err := db.GetProjectByName(orgId, repo, projectName) | ||
if err != nil { | ||
return fmt.Errorf("error retriving project by name: %v", err) | ||
} | ||
if p == nil { | ||
_, err := db.CreateProject(projectName, repo) | ||
if err != nil { | ||
return fmt.Errorf("could not create project: %v", err) | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error while updating projects from config: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tasks | ||
|
||
import ( | ||
"fmt" | ||
utils3 "github.com/diggerhq/digger/backend/utils" | ||
"github.com/diggerhq/digger/ee/drift/dbmodels" | ||
"github.com/diggerhq/digger/ee/drift/utils" | ||
dg_configuration "github.com/diggerhq/digger/libs/digger_config" | ||
utils2 "github.com/diggerhq/digger/next/utils" | ||
"log" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func LoadProjectsFromGithubRepo(gh utils2.GithubClientProvider, installationId string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) error { | ||
link, err := dbmodels.DB.GetGithubAppInstallationLink(installationId) | ||
if err != nil { | ||
log.Printf("Error getting GetGithubAppInstallationLink: %v", err) | ||
return fmt.Errorf("error getting github app link") | ||
} | ||
|
||
orgId := link.OrganisationID | ||
diggerRepoName := strings.ReplaceAll(repoFullName, "/", "-") | ||
repo, err := dbmodels.DB.GetRepo(orgId, diggerRepoName) | ||
if err != nil { | ||
log.Printf("Error getting Repo: %v", err) | ||
return fmt.Errorf("error getting github app link") | ||
} | ||
if repo == nil { | ||
log.Printf("Repo not found: Org: %v | repo: %v", orgId, diggerRepoName) | ||
return fmt.Errorf("Repo not found: Org: %v | repo: %v", orgId, diggerRepoName) | ||
} | ||
|
||
installationId64, err := strconv.ParseInt(installationId, 10, 64) | ||
if err != nil { | ||
log.Printf("failed to convert installation id %v to int64", installationId) | ||
return fmt.Errorf("failed to convert installation id to int64") | ||
} | ||
_, token, err := utils.GetGithubService(gh, installationId64, repoFullName, repoOwner, repoName) | ||
if err != nil { | ||
log.Printf("Error getting github service: %v", err) | ||
return fmt.Errorf("error getting github service") | ||
} | ||
|
||
err = utils3.CloneGitRepoAndDoAction(cloneUrl, branch, *token, func(dir string) error { | ||
config, err := dg_configuration.LoadDiggerConfigYaml(dir, true, nil) | ||
if err != nil { | ||
log.Printf("ERROR load digger.yml: %v", err) | ||
return fmt.Errorf("error loading digger.yml %v", err) | ||
} | ||
dbmodels.DB.RefreshProjectsFromRepo(link.OrganisationID, *config, repo) | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error while cloning repo: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"github.com/bradleyfalzon/ghinstallation/v2" | ||
"github.com/diggerhq/digger/ee/drift/dbmodels" | ||
github2 "github.com/diggerhq/digger/libs/ci/github" | ||
"github.com/diggerhq/digger/next/utils" | ||
"github.com/google/go-github/v61/github" | ||
"log" | ||
net "net/http" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func GetGithubClient(gh utils.GithubClientProvider, installationId int64, repoFullName string) (*github.Client, *string, error) { | ||
repo, err := dbmodels.DB.GetRepoByInstllationIdAndRepoFullName(strconv.FormatInt(installationId, 10), repoFullName) | ||
if err != nil { | ||
log.Printf("Error getting repo: %v", err) | ||
return nil, nil, fmt.Errorf("Error getting repo: %v", err) | ||
} | ||
|
||
ghClient, token, err := gh.Get(repo.GithubAppID, installationId) | ||
return ghClient, token, err | ||
} | ||
|
||
func GetGithubService(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) { | ||
ghClient, token, err := GetGithubClient(gh, installationId, repoFullName) | ||
if err != nil { | ||
log.Printf("Error creating github app client: %v", err) | ||
return nil, nil, fmt.Errorf("Error creating github app client: %v", err) | ||
} | ||
|
||
ghService := github2.GithubService{ | ||
Client: ghClient, | ||
RepoName: repoName, | ||
Owner: repoOwner, | ||
} | ||
|
||
return &ghService, token, nil | ||
} | ||
|
||
type DiggerGithubRealClientProvider struct { | ||
} | ||
|
||
func (gh DiggerGithubRealClientProvider) NewClient(netClient *net.Client) (*github.Client, error) { | ||
ghClient := github.NewClient(netClient) | ||
return ghClient, nil | ||
} | ||
|
||
func (gh DiggerGithubRealClientProvider) Get(githubAppId int64, installationId int64) (*github.Client, *string, error) { | ||
githubAppPrivateKey := "" | ||
githubAppPrivateKeyB64 := os.Getenv("GITHUB_APP_PRIVATE_KEY_BASE64") | ||
if githubAppPrivateKeyB64 != "" { | ||
decodedBytes, err := base64.StdEncoding.DecodeString(githubAppPrivateKeyB64) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error initialising github app installation: please set GITHUB_APP_PRIVATE_KEY_BASE64 env variable\n") | ||
} | ||
githubAppPrivateKey = string(decodedBytes) | ||
} else { | ||
githubAppPrivateKey = os.Getenv("GITHUB_APP_PRIVATE_KEY") | ||
if githubAppPrivateKey != "" { | ||
log.Printf("WARNING: GITHUB_APP_PRIVATE_KEY will be deprecated in future releases, " + | ||
"please use GITHUB_APP_PRIVATE_KEY_BASE64 instead") | ||
} else { | ||
return nil, nil, fmt.Errorf("error initialising github app installation: please set GITHUB_APP_PRIVATE_KEY_BASE64 env variable\n") | ||
} | ||
} | ||
|
||
tr := net.DefaultTransport | ||
itr, err := ghinstallation.New(tr, githubAppId, installationId, []byte(githubAppPrivateKey)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error initialising github app installation: %v\n", err) | ||
} | ||
|
||
token, err := itr.Token(context.Background()) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error initialising git app token: %v\n", err) | ||
} | ||
ghClient, err := gh.NewClient(&net.Client{Transport: itr}) | ||
if err != nil { | ||
log.Printf("error creating new client: %v", err) | ||
} | ||
return ghClient, &token, nil | ||
} |