Skip to content

Commit f6cd6a7

Browse files
authored
load projects on push (#1964)
* load projects on push
1 parent ea47217 commit f6cd6a7

File tree

14 files changed

+187
-188
lines changed

14 files changed

+187
-188
lines changed

backend/controllers/github.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
101101
return
102102
}
103103
}
104+
case *github.PushEvent:
105+
slog.Info("Processing PushEvent",
106+
"repo", *event.Repo.FullName,
107+
)
108+
109+
go handlePushEvent(gh, event, appId64)
104110

105111
case *github.IssueCommentEvent:
106112
slog.Info("Processing IssueCommentEvent",
@@ -384,11 +390,40 @@ func handleInstallationDeletedEvent(installation *github.InstallationEvent, appI
384390
return nil
385391
}
386392

393+
func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent, appId int64) error {
394+
slog.Debug("Handling push event", "appId", appId, "payload", payload)
395+
defer func() {
396+
if r := recover(); r != nil {
397+
stack := string(debug.Stack())
398+
slog.Error("Recovered from panic in handlePushEvent", "error", r)
399+
fmt.Printf("Stack trace:\n%s\n", stack)
400+
}
401+
}()
402+
403+
installationId := *payload.Installation.ID
404+
repoName := *payload.Repo.Name
405+
repoOwner := *payload.Repo.Owner.Login
406+
repoFullName := *payload.Repo.FullName
407+
cloneURL := *payload.Repo.CloneURL
408+
ref := *payload.Ref
409+
defaultBranch := *payload.Repo.DefaultBranch
410+
411+
if strings.HasSuffix(ref, defaultBranch) {
412+
err := services.LoadProjectsFromGithubRepo(gh, strconv.FormatInt(installationId, 10), repoFullName, repoOwner, repoName, cloneURL, defaultBranch)
413+
if err != nil {
414+
slog.Error("Failed to load projects from GitHub repo", "error", err)
415+
}
416+
}
417+
418+
return nil
419+
}
420+
387421
func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullRequestEvent, ciBackendProvider ci_backends.CiBackendProvider, appId int64) error {
388422
defer func() {
389423
if r := recover(); r != nil {
390424
stack := string(debug.Stack())
391-
slog.Error("Recovered from panic in handlePullRequestEvent", "error", r, slog.Group("stack", slog.String("trace", stack)))
425+
slog.Error("Recovered from panic in handlePullRequestEvent", "error", r, slog.Group("stack"))
426+
fmt.Printf("Stack trace:\n%s\n", stack)
392427
}
393428
}()
394429

@@ -1151,7 +1186,8 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
11511186
defer func() {
11521187
if r := recover(); r != nil {
11531188
stack := string(debug.Stack())
1154-
slog.Error("Recovered from panic in handleIssueCommentEvent", "error", r, slog.Group("stack", slog.String("trace", stack)))
1189+
slog.Error("Recovered from panic in handleIssueCommentEvent", "error", r, slog.Group("stack"))
1190+
fmt.Printf("Stack trace:\n%s\n", stack)
11551191
}
11561192
}()
11571193

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, false, false)
617+
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false)
618618
if err != nil {
619619
panic(err)
620620
}

backend/controllers/policies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func upsertPolicyForRepoAndProject(c *gin.Context, policyType string) {
249249
if projectResult.RowsAffected == 0 {
250250
projectModel = models.Project{
251251
OrganisationID: orgID.(uint),
252-
RepoID: repoModel.ID,
252+
RepoFullName: repoModel.RepoFullName,
253253
Name: projectName,
254254
}
255255
err := models.DB.GormDB.Create(&projectModel).Error

backend/controllers/projects.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func ProjectDetails(c *gin.Context) {
251251
slog.Info("Successfully retrieved project details",
252252
"projectId", projectId,
253253
"projectName", project.Name,
254-
"repoName", project.Repo.Name,
254+
"repoFullName", project.RepoFullName,
255255
)
256256

257257
c.JSON(http.StatusOK, project.MapToJsonStruct())
@@ -355,9 +355,8 @@ func ReportProjectsForRepo(c *gin.Context) {
355355
project := models.Project{
356356
Name: request.Name,
357357
ConfigurationYaml: request.ConfigurationYaml,
358-
RepoID: repo.ID,
359358
OrganisationID: org.ID,
360-
Repo: &repo,
359+
RepoFullName: repo.RepoFullName,
361360
Organisation: org,
362361
}
363362

backend/controllers/runs.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ func RunsForProject(c *gin.Context) {
5757
return
5858
}
5959

60+
repo, err := models.DB.GetRepoByFullName(org.ID, project.RepoFullName)
61+
if err != nil {
62+
slog.Error("Could not fetch repo", "projectId", projectId, "repoFullName", project.RepoFullName, "error", err)
63+
c.String(http.StatusInternalServerError, "Could not fetch repo")
64+
return
65+
}
66+
6067
if project.OrganisationID != org.ID {
6168
slog.Warn("Forbidden access: not allowed to access project",
6269
"projectOrgId", project.OrganisationID,
@@ -66,9 +73,9 @@ func RunsForProject(c *gin.Context) {
6673
return
6774
}
6875

69-
runs, err := models.DB.ListDiggerRunsForProject(project.Name, project.Repo.ID)
76+
runs, err := models.DB.ListDiggerRunsForProject(project.Name, repo.ID)
7077
if err != nil {
71-
slog.Error("Could not fetch runs", "projectId", projectId, "repoId", project.Repo.ID, "error", err)
78+
slog.Error("Could not fetch runs", "projectId", projectId, "repoId", repo.ID, "error", err)
7279
c.String(http.StatusInternalServerError, "Could not fetch runs")
7380
return
7481
}
@@ -87,7 +94,7 @@ func RunsForProject(c *gin.Context) {
8794
slog.Info("Successfully fetched runs for project",
8895
"projectId", projectId,
8996
"projectName", project.Name,
90-
"repoId", project.Repo.ID,
97+
"repoId", repo.ID,
9198
"runCount", len(runs))
9299

93100
response := make(map[string]interface{})

backend/migrations/20250530015921.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Modify "projects" table
2+
ALTER TABLE "public"."projects" DROP COLUMN "repo_id", ADD COLUMN "repo_full_name" text NULL;
3+
-- Create index "idx_project_org" to table: "projects"
4+
DROP INDEX IF EXISTS "idx_project";
5+
CREATE UNIQUE INDEX "idx_project_org" ON "public"."projects" ("name", "organisation_id", "repo_full_name");

backend/migrations/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:7qYia+JfrMJUdNvsjR8hZosttT36aVdOje5TP3ekX4w=
1+
h1:IeQ6uHSz71Ox7tBqG6sy05KmaRMpYqhr0SeipkVSEbM=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -49,3 +49,4 @@ h1:7qYia+JfrMJUdNvsjR8hZosttT36aVdOje5TP3ekX4w=
4949
20250416152705.sql h1:yeszz4c/BlyVgUUIk7aTUz/DUNMC40+9fe1Q8Ya4TVI=
5050
20250512172515.sql h1:iIZIhFq3TTyjTzsxNWv3k4FcIkXfOCdHtmHNmYqjBMM=
5151
20250512213729.sql h1:n8bYIXWko9xOUs+FPcG7lS3GXMVQBSygXX7Hpj20eVs=
52+
20250530015921.sql h1:FidRW+0ur3mCDBPgP7V/sqr2jjfmi/CFJPRpNxTmqGs=

backend/models/orgs.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ func (p *ProjectRun) MapToJsonStruct() interface{} {
5555
Id: p.ID,
5656
ProjectID: p.ProjectID,
5757
ProjectName: p.Project.Name,
58-
RepoUrl: p.Project.Repo.RepoUrl,
59-
RepoFullName: p.Project.Repo.RepoFullName,
6058
StartedAt: time.UnixMilli(p.StartedAt),
6159
EndedAt: time.UnixMilli(p.EndedAt),
6260
Status: p.Status,
@@ -75,11 +73,10 @@ const (
7573

7674
type Project struct {
7775
gorm.Model
78-
Name string `gorm:"uniqueIndex:idx_project"`
79-
OrganisationID uint `gorm:"uniqueIndex:idx_project"`
76+
Name string `gorm:"uniqueIndex:idx_project_org"`
77+
OrganisationID uint `gorm:"uniqueIndex:idx_project_org"`
8078
Organisation *Organisation
81-
RepoID uint `gorm:"uniqueIndex:idx_project"`
82-
Repo *Repo
79+
RepoFullName string `gorm:"uniqueIndex:idx_project_org"`
8380
ConfigurationYaml string // TODO: probably needs to be deleted
8481
Status ProjectStatus
8582
IsGenerated bool
@@ -112,12 +109,8 @@ func (p *Project) MapToJsonStruct() interface{} {
112109
Id: p.ID,
113110
Name: p.Name,
114111
OrganisationID: p.OrganisationID,
115-
RepoID: p.RepoID,
116112
OrganisationName: p.Organisation.Name,
117-
RepoFullName: p.Repo.RepoFullName,
118-
RepoName: p.Repo.RepoName,
119-
RepoOrg: p.Repo.RepoOrganisation,
120-
RepoUrl: p.Repo.RepoUrl,
113+
RepoFullName: p.RepoFullName,
121114
LastActivityTimestamp: p.UpdatedAt.String(),
122115
LastActivityAuthor: "unknown",
123116
LastActivityStatus: string(status),

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, 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)