Skip to content

Commit 5cf11a5

Browse files
authored
include projects api (#2006)
1 parent f6cd6a7 commit 5cf11a5

File tree

15 files changed

+105
-37
lines changed

15 files changed

+105
-37
lines changed

backend/bootstrap/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
229229
reposApiGroup.GET("/", controllers.ListReposApi)
230230
reposApiGroup.GET("/:repo_id/jobs", controllers.GetJobsForRepoApi)
231231

232+
projectsApiGroup := apiGroup.Group("/projects")
233+
projectsApiGroup.GET("/", controllers.ListProjectsApi)
234+
232235
githubApiGroup := apiGroup.Group("/github")
233236
githubApiGroup.POST("/link", controllers.LinkGithubInstallationToOrgApi)
234237

backend/controllers/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
614614

615615
// create test project
616616
projectName := "test project"
617-
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false)
617+
_, err = database.CreateProject(projectName, "", org, repo.RepoFullName, false, false)
618618
if err != nil {
619619
panic(err)
620620
}

backend/controllers/projects.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,50 @@ import (
2525
"gorm.io/gorm"
2626
)
2727

28-
func ListProjects(c *gin.Context) {
28+
func ListProjectsApi(c *gin.Context) {
29+
// assume all exists as validated in middleware
30+
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
31+
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)
2932

33+
var org models.Organisation
34+
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
35+
if err != nil {
36+
if errors.Is(err, gorm.ErrRecordNotFound) {
37+
slog.Info("Organisation not found", "organisationId", organisationId, "source", organisationSource)
38+
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId)
39+
} else {
40+
slog.Error("Error fetching organisation", "organisationId", organisationId, "source", organisationSource, "error", err)
41+
c.String(http.StatusInternalServerError, "Error fetching organisation")
42+
}
43+
return
44+
}
45+
46+
var projects []models.Project
47+
48+
err = models.DB.GormDB.Preload("Organisation").
49+
Where("projects.organisation_id = ?", org.ID).
50+
Order("name").
51+
Find(&projects).Error
52+
53+
if err != nil {
54+
slog.Error("Error fetching projects", "organisationId", organisationId, "orgId", org.ID, "error", err)
55+
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database")
56+
return
57+
}
58+
59+
marshalledRepos := make([]interface{}, 0)
60+
61+
for _, p := range projects {
62+
marshalled := p.MapToJsonStruct()
63+
marshalledRepos = append(marshalledRepos, marshalled)
64+
}
65+
66+
slog.Info("Successfully fetched projects", "organisationId", organisationId, "orgId", org.ID, "projectCount", len(projects))
67+
68+
response := make(map[string]interface{})
69+
response["result"] = marshalledRepos
70+
71+
c.JSON(http.StatusOK, response)
3072
}
3173

3274
func FindProjectsForRepo(c *gin.Context) {
@@ -353,11 +395,10 @@ func ReportProjectsForRepo(c *gin.Context) {
353395
)
354396

355397
project := models.Project{
356-
Name: request.Name,
357-
ConfigurationYaml: request.ConfigurationYaml,
358-
OrganisationID: org.ID,
359-
RepoFullName: repo.RepoFullName,
360-
Organisation: org,
398+
Name: request.Name,
399+
OrganisationID: org.ID,
400+
RepoFullName: repo.RepoFullName,
401+
Organisation: org,
361402
}
362403

363404
err = models.DB.GormDB.Create(&project).Error

backend/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ require (
2020
github.com/golang-jwt/jwt v3.2.2+incompatible
2121
github.com/google/go-github/v61 v61.0.0
2222
github.com/google/uuid v1.6.0
23+
github.com/imdatngo/slog-gorm/v2 v2.0.0
2324
github.com/ktrysmt/go-bitbucket v0.9.81
2425
github.com/migueleliasweb/go-github-mock v0.0.23
25-
github.com/orandin/slog-gorm v1.4.0
2626
github.com/robfig/cron v1.2.0
2727
github.com/samber/lo v1.39.0
2828
github.com/samber/slog-gin v1.15.0

backend/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:
14211421
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
14221422
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
14231423
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
1424+
github.com/imdatngo/slog-gorm/v2 v2.0.0 h1:SrMVqpExd3USDZjiFlJoFg3FJvZOreJX/d8N8G6PQ4w=
1425+
github.com/imdatngo/slog-gorm/v2 v2.0.0/go.mod h1:hp2V0UOoVXwMT9YUbB/EbaXCN7g/7txxekKaalO/6fU=
14241426
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
14251427
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
14261428
github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
@@ -1683,8 +1685,6 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ
16831685
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
16841686
github.com/opencontainers/runc v1.1.9 h1:XR0VIHTGce5eWPkaPesqTBrhW2yAcaraWfsEalNwQLM=
16851687
github.com/opencontainers/runc v1.1.9/go.mod h1:CbUumNnWCuTGFukNXahoo/RFBZvDAgRh/smNYNOhA50=
1686-
github.com/orandin/slog-gorm v1.4.0 h1:FgA8hJufF9/jeNSYoEXmHPPBwET2gwlF3B85JdpsTUU=
1687-
github.com/orandin/slog-gorm v1.4.0/go.mod h1:MoZ51+b7xE9lwGNPYEhxcUtRNrYzjdcKvA8QXQQGEPA=
16881688
github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA=
16891689
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
16901690
github.com/owenrumney/go-sarif v1.1.1 h1:QNObu6YX1igyFKhdzd7vgzmw7XsWN3/6NMGuDzBgXmE=

backend/migrations/20250704024948.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "projects" table
2+
ALTER TABLE "public"."projects" ADD COLUMN "directory" text NULL;

backend/migrations/20250704025208.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "projects" table
2+
ALTER TABLE "public"."projects" DROP COLUMN "configuration_yaml";

backend/migrations/atlas.sum

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:IeQ6uHSz71Ox7tBqG6sy05KmaRMpYqhr0SeipkVSEbM=
1+
h1:E1MYgdCTYDdHVINjSBVtGufzvYspZC8Wur7nvPK7fN0=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -50,3 +50,5 @@ h1:IeQ6uHSz71Ox7tBqG6sy05KmaRMpYqhr0SeipkVSEbM=
5050
20250512172515.sql h1:iIZIhFq3TTyjTzsxNWv3k4FcIkXfOCdHtmHNmYqjBMM=
5151
20250512213729.sql h1:n8bYIXWko9xOUs+FPcG7lS3GXMVQBSygXX7Hpj20eVs=
5252
20250530015921.sql h1:FidRW+0ur3mCDBPgP7V/sqr2jjfmi/CFJPRpNxTmqGs=
53+
20250704024948.sql h1:ki8bOrfd8y7Mfq40Dz5lH6MUuQqPVah+oimeRDVHOOQ=
54+
20250704025208.sql h1:XRY66dphINSj53mbxl5ALmyWqebuB1swPvUuUPvNR88=

backend/models/orgs.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ const (
7373

7474
type Project struct {
7575
gorm.Model
76-
Name string `gorm:"uniqueIndex:idx_project_org"`
77-
OrganisationID uint `gorm:"uniqueIndex:idx_project_org"`
78-
Organisation *Organisation
79-
RepoFullName string `gorm:"uniqueIndex:idx_project_org"`
80-
ConfigurationYaml string // TODO: probably needs to be deleted
81-
Status ProjectStatus
82-
IsGenerated bool
83-
IsInMainBranch bool
76+
Name string `gorm:"uniqueIndex:idx_project_org"`
77+
Directory string
78+
OrganisationID uint `gorm:"uniqueIndex:idx_project_org"`
79+
Organisation *Organisation
80+
RepoFullName string `gorm:"uniqueIndex:idx_project_org"`
81+
Status ProjectStatus
82+
IsGenerated bool
83+
IsInMainBranch bool
8484
}
8585

8686
func (p *Project) MapToJsonStruct() interface{} {
@@ -97,9 +97,6 @@ func (p *Project) MapToJsonStruct() interface{} {
9797
OrganisationName string `json:"organisation_name"`
9898
RepoID uint `json:"repo_id"`
9999
RepoFullName string `json:"repo_full_name"`
100-
RepoName string `json:"repo_name"`
101-
RepoOrg string `json:"repo_org"`
102-
RepoUrl string `json:"repo_url"`
103100
IsInMainBranch bool `json:"is_in_main_branch"`
104101
IsGenerated bool `json:"is_generated"`
105102
LastActivityTimestamp string `json:"last_activity_timestamp"`
@@ -108,6 +105,7 @@ func (p *Project) MapToJsonStruct() interface{} {
108105
}{
109106
Id: p.ID,
110107
Name: p.Name,
108+
Directory: p.Directory,
111109
OrganisationID: p.OrganisationID,
112110
OrganisationName: p.Organisation.Name,
113111
RepoFullName: p.RepoFullName,

backend/models/scheduler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func setupSuiteScheduler(tb testing.TB) (func(tb testing.TB), *Database) {
5454
}
5555

5656
projectName := "test project"
57-
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false)
57+
_, err = database.CreateProject(projectName, "", org, repo.RepoFullName, false, false)
5858
if err != nil {
5959
panic(err)
6060
}

0 commit comments

Comments
 (0)