-
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.
* modify drift app
- Loading branch information
Showing
18 changed files
with
817 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.idea/ | ||
**/.env | ||
**/.env* | ||
.DS_Store | ||
venv/ | ||
**/__pycache__/ | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.