-
Notifications
You must be signed in to change notification settings - Fork 561
include projects api #2006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
include projects api #2006
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,50 @@ import ( | |
"gorm.io/gorm" | ||
) | ||
|
||
func ListProjects(c *gin.Context) { | ||
func ListProjectsApi(c *gin.Context) { | ||
// assume all exists as validated in middleware | ||
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) | ||
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) | ||
|
||
var org models.Organisation | ||
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error | ||
if err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
slog.Info("Organisation not found", "organisationId", organisationId, "source", organisationSource) | ||
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) | ||
} else { | ||
slog.Error("Error fetching organisation", "organisationId", organisationId, "source", organisationSource, "error", err) | ||
c.String(http.StatusInternalServerError, "Error fetching organisation") | ||
} | ||
return | ||
} | ||
|
||
var projects []models.Project | ||
|
||
err = models.DB.GormDB.Preload("Organisation"). | ||
Where("projects.organisation_id = ?", org.ID). | ||
Order("name"). | ||
Find(&projects).Error | ||
|
||
if err != nil { | ||
slog.Error("Error fetching projects", "organisationId", organisationId, "orgId", org.ID, "error", err) | ||
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database") | ||
return | ||
} | ||
|
||
marshalledRepos := make([]interface{}, 0) | ||
|
||
for _, p := range projects { | ||
marshalled := p.MapToJsonStruct() | ||
marshalledRepos = append(marshalledRepos, marshalled) | ||
} | ||
|
||
slog.Info("Successfully fetched projects", "organisationId", organisationId, "orgId", org.ID, "projectCount", len(projects)) | ||
|
||
response := make(map[string]interface{}) | ||
response["result"] = marshalledRepos | ||
|
||
c.JSON(http.StatusOK, response) | ||
} | ||
|
||
func FindProjectsForRepo(c *gin.Context) { | ||
|
@@ -353,11 +395,10 @@ func ReportProjectsForRepo(c *gin.Context) { | |
) | ||
|
||
project := models.Project{ | ||
Name: request.Name, | ||
ConfigurationYaml: request.ConfigurationYaml, | ||
OrganisationID: org.ID, | ||
RepoFullName: repo.RepoFullName, | ||
Organisation: org, | ||
Name: request.Name, | ||
OrganisationID: org.ID, | ||
RepoFullName: repo.RepoFullName, | ||
Organisation: org, | ||
} | ||
Comment on lines
397
to
402
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: ConfigurationYaml field has been removed from project creation. Ensure this doesn't break any existing code that expects this field. |
||
|
||
err = models.DB.GormDB.Create(&project).Error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- Modify "projects" table | ||
ALTER TABLE "public"."projects" ADD COLUMN "directory" text NULL; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,2 @@ | ||||||
-- Modify "projects" table | ||||||
ALTER TABLE "public"."projects" DROP COLUMN "configuration_yaml"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider using IF EXISTS clause to prevent errors if column was already dropped in some environments
Suggested change
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainVerify that all references to configuration_yaml have been removed. This migration drops the
🏁 Script executed: #!/bin/bash
# Search for any remaining references to configuration_yaml or ConfigurationYaml
echo "Searching for configuration_yaml references..."
rg -i "configuration_yaml|configurationyaml" --type go --type sql
echo "Searching for ConfigurationYaml field references..."
rg "ConfigurationYaml" --type go -A 3 -B 3 Length of output: 9506 Ensure Complete Removal of Please address the following before merging this destructive migration:
🤖 Prompt for AI Agents
|
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,13 @@ | ||||||||||||||
package models | ||||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"log/slog" | ||||||||||||||
"os" | ||||||||||||||
|
||||||||||||||
slogGorm "github.com/orandin/slog-gorm" | ||||||||||||||
sloggorm "github.com/imdatngo/slog-gorm/v2" | ||||||||||||||
"gorm.io/driver/postgres" | ||||||||||||||
_ "gorm.io/driver/postgres" | ||||||||||||||
"gorm.io/gorm" | ||||||||||||||
"log/slog" | ||||||||||||||
"os" | ||||||||||||||
"time" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
type Database struct { | ||||||||||||||
|
@@ -20,8 +20,19 @@ var DEFAULT_ORG_NAME = "digger" | |||||||||||||
var DB *Database | ||||||||||||||
|
||||||||||||||
func ConnectDatabase() { | ||||||||||||||
slogger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Set log level based on DIGGER_LOG_LEVEL environment variable instead of hardcoding to LevelInfo
Suggested change
|
||||||||||||||
|
||||||||||||||
cfg := sloggorm.NewConfig(slogger.Handler()). | ||||||||||||||
WithGroupKey("db"). | ||||||||||||||
WithSlowThreshold(time.Second). | ||||||||||||||
WithIgnoreRecordNotFoundError(true) | ||||||||||||||
|
||||||||||||||
if os.Getenv("DIGGER_LOG_LEVEL") == "DEBUG" { | ||||||||||||||
cfg.WithTraceAll(true) | ||||||||||||||
} | ||||||||||||||
glogger := sloggorm.NewWithConfig(cfg) | ||||||||||||||
database, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{ | ||||||||||||||
Logger: slogGorm.New(), | ||||||||||||||
Logger: glogger, | ||||||||||||||
}) | ||||||||||||||
if err != nil { | ||||||||||||||
slog.Error("Failed to connect to database", "error", err) | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -378,7 +378,7 @@ func (db *Database) GetRepo(orgIdKey any, repoName string) (*Repo, error) { | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (db *Database) GetRepoByFullName(orgIdKey any, repoFullName string) (*Repo, error) { | ||||||||||||||||||||||||
slog.Info("getting repo by name", | ||||||||||||||||||||||||
slog.Info("getting repo by full name", | ||||||||||||||||||||||||
"orgId", orgIdKey, | ||||||||||||||||||||||||
"repoName", repoFullName) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -1402,9 +1402,10 @@ func (db *Database) CreateOrganisation(name string, externalSource string, tenan | |||||||||||||||||||||||
return org, nil | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (db *Database) CreateProject(name string, org *Organisation, repoFullName string, isGenerated bool, isInMainBranch bool) (*Project, error) { | ||||||||||||||||||||||||
func (db *Database) CreateProject(name string, directory string, org *Organisation, repoFullName string, isGenerated bool, isInMainBranch bool) (*Project, error) { | ||||||||||||||||||||||||
project := &Project{ | ||||||||||||||||||||||||
Name: name, | ||||||||||||||||||||||||
Directory: directory, | ||||||||||||||||||||||||
Organisation: org, | ||||||||||||||||||||||||
RepoFullName: repoFullName, | ||||||||||||||||||||||||
Status: ProjectActive, | ||||||||||||||||||||||||
|
@@ -1642,15 +1643,19 @@ func (db *Database) RefreshProjectsFromRepo(orgId string, config configuration.D | |||||||||||||||||||||||
err = db.GormDB.Transaction(func(tx *gorm.DB) error { | ||||||||||||||||||||||||
for _, dc := range config.Projects { | ||||||||||||||||||||||||
projectName := dc.Name | ||||||||||||||||||||||||
projectDirectory := dc.Dir | ||||||||||||||||||||||||
p, err := db.GetProjectByName(orgId, repoFullName, projectName) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return fmt.Errorf("error retrieving project by name: %v", err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
if p == nil { | ||||||||||||||||||||||||
_, err := db.CreateProject(projectName, org, repoFullName, false, true) | ||||||||||||||||||||||||
_, err := db.CreateProject(projectName, projectDirectory, org, repoFullName, false, true) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return fmt.Errorf("could not create project: %v", err) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
p.Directory = projectDirectory | ||||||||||||||||||||||||
db.GormDB.Save(p) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+1656
to
1659
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for the database save operation. The direct save operation on line 1658 lacks error handling, which could lead to silent failures when updating the project directory. } else {
p.Directory = projectDirectory
- db.GormDB.Save(p)
+ err := db.GormDB.Save(p).Error
+ if err != nil {
+ return fmt.Errorf("error updating project directory: %v", err)
+ }
} 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,15 +4,13 @@ import ( | |||||
"fmt" | ||||||
"github.com/diggerhq/digger/backend/models" | ||||||
utils3 "github.com/diggerhq/digger/backend/utils" | ||||||
"github.com/diggerhq/digger/ee/drift/utils" | ||||||
dg_configuration "github.com/diggerhq/digger/libs/digger_config" | ||||||
utils2 "github.com/diggerhq/digger/next/utils" | ||||||
"log/slog" | ||||||
"strconv" | ||||||
"strings" | ||||||
) | ||||||
|
||||||
func LoadProjectsFromGithubRepo(gh utils2.GithubClientProvider, installationId string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) error { | ||||||
func LoadProjectsFromGithubRepo(gh utils3.GithubClientProvider, installationId string, repoFullName string, repoOwner string, repoName string, cloneUrl string, branch string) error { | ||||||
installationId64, err := strconv.ParseInt(installationId, 10, 64) | ||||||
if err != nil { | ||||||
slog.Error("failed to convert installation id %v to int64", "insallationId", installationId) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: Typo in error log parameter name 'insallationId'
Suggested change
|
||||||
|
@@ -36,7 +34,8 @@ func LoadProjectsFromGithubRepo(gh utils2.GithubClientProvider, installationId s | |||||
return fmt.Errorf("repo not found: Org: %v | repo: %v", orgId, diggerRepoName) | ||||||
} | ||||||
|
||||||
_, token, err := utils.GetGithubService(gh, installationId64, repoFullName, repoOwner, repoName) | ||||||
slog.Debug("getting github service", "installationId", installationId, "repoFullName", repoFullName, "repoOwner", repoOwner, "repoName", repoName) | ||||||
_, token, err := utils3.GetGithubService(gh, installationId64, repoFullName, repoOwner, repoName) | ||||||
if err != nil { | ||||||
slog.Error("getting github service", "error", err) | ||||||
return fmt.Errorf("error getting github service") | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -74,7 +74,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) { | |||||||
|
||||||||
// create test project | ||||||||
projectName := "test project" | ||||||||
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false) | ||||||||
_, err = database.CreateProject(projectName, "", org, repo.RepoFullName, false, false) | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
} | ||||||||
|
@@ -140,7 +140,7 @@ func TestThatRunQueueItemMovesFromQueuedToPlanningAfterPickup(t *testing.T) { | |||||||
for i, testParam := range testParameters { | ||||||||
ciService := github2.MockCiService{} | ||||||||
batch, _ := models.DB.CreateDiggerBatch(models.DiggerVCSGithub, 123, "", "", "", 22, "", "", "", nil, 0, "", false, true, nil) | ||||||||
project, _ := models.DB.CreateProject(fmt.Sprintf("test%v", i), nil, "", false, false) | ||||||||
project, _ := models.DB.CreateProject(fmt.Sprintf("test%v", i), "", nil, "", false, false) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Missing error handling for CreateProject - ignoring error could mask test failures
Suggested change
|
||||||||
planStage, _ := models.DB.CreateDiggerRunStage(batch.ID.String()) | ||||||||
applyStage, _ := models.DB.CreateDiggerRunStage(batch.ID.String()) | ||||||||
diggerRun, _ := models.DB.CreateDiggerRun("", 1, testParam.InitialStatus, "sha", "", 123, 1, project.Name, "apply", &planStage.ID, &applyStage.ID) | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,13 +13,17 @@ import ( | |||||
"github.com/dominikbraun/graph" | ||||||
"github.com/google/go-github/v61/github" | ||||||
"log" | ||||||
"log/slog" | ||||||
Comment on lines
15
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Using both log and slog packages - consider standardizing on slog throughout |
||||||
net "net/http" | ||||||
"os" | ||||||
"path" | ||||||
"strconv" | ||||||
) | ||||||
|
||||||
func GetGithubClient(gh utils.GithubClientProvider, installationId int64, repoFullName string) (*github.Client, *string, error) { | ||||||
slog.Debug("Getting GitHub client", | ||||||
"installationId", installationId, | ||||||
"repoFullName", repoFullName) | ||||||
repo, err := dbmodels.DB.GetRepoByInstllationIdAndRepoFullName(strconv.FormatInt(installationId, 10), repoFullName) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Method name contains typo: 'Instllation' is missing an 'a'
Suggested change
|
||||||
if err != nil { | ||||||
log.Printf("Error getting repo: %v", err) | ||||||
|
@@ -31,6 +35,7 @@ func GetGithubClient(gh utils.GithubClientProvider, installationId int64, repoFu | |||||
} | ||||||
|
||||||
func GetGithubService(gh utils.GithubClientProvider, installationId int64, repoFullName string, repoOwner string, repoName string) (*github2.GithubService, *string, error) { | ||||||
slog.Debug("getting github client", "installationId", installationId, "repoFullName", repoFullName) | ||||||
ghClient, token, err := GetGithubClient(gh, installationId, repoFullName) | ||||||
if err != nil { | ||||||
log.Printf("Error creating github app client: %v", err) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Log debug context when handling projects API requests to improve traceability