Skip to content

load projects on push #1964

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
return
}
}
case *github.PushEvent:
slog.Info("Processing PushEvent",
"action", *event.Action,
"repo", *event.Repo.FullName,
)

go handlePushEvent(gh, event, appId64)

case *github.IssueCommentEvent:
slog.Info("Processing IssueCommentEvent",
Expand Down Expand Up @@ -383,6 +390,33 @@ func handleInstallationDeletedEvent(installation *github.InstallationEvent, appI
return nil
}

func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent, appId int64) error {
slog.Debug("Handling push event", "appId", appId, "payload", payload)
defer func() {
if r := recover(); r != nil {
stack := string(debug.Stack())
slog.Error("Recovered from panic in handlePushEvent", "error", r, slog.Group("stack", slog.String("trace", stack)))
}
}()

installationId := *payload.Installation.ID
repoName := *payload.Repo.Name
repoOwner := *payload.Repo.Owner.Login
repoFullName := *payload.Repo.FullName
cloneURL := *payload.Repo.CloneURL
ref := *payload.Ref
defaultBranch := *payload.Repo.DefaultBranch

if strings.HasSuffix(ref, defaultBranch) {
err := services.LoadProjectsFromGithubRepo(gh, strconv.FormatInt(installationId, 10), repoFullName, repoOwner, repoName, cloneURL, defaultBranch)
if err != nil {
slog.Error("Failed to load projects from GitHub repo", "error", err)
}
}

return nil
}

func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullRequestEvent, ciBackendProvider ci_backends.CiBackendProvider, appId int64) error {
defer func() {
if r := recover(); r != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {

// create test project
projectName := "test project"
_, err = database.CreateProject(projectName, org, repo, false, false)
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion backend/controllers/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func upsertPolicyForRepoAndProject(c *gin.Context, policyType string) {
if projectResult.RowsAffected == 0 {
projectModel = models.Project{
OrganisationID: orgID.(uint),
RepoID: repoModel.ID,
RepoFullName: repoModel.RepoFullName,
Name: projectName,
}
err := models.DB.GormDB.Create(&projectModel).Error
Expand Down
5 changes: 2 additions & 3 deletions backend/controllers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func ProjectDetails(c *gin.Context) {
slog.Info("Successfully retrieved project details",
"projectId", projectId,
"projectName", project.Name,
"repoName", project.Repo.Name,
"repoFullName", project.RepoFullName,
)

c.JSON(http.StatusOK, project.MapToJsonStruct())
Expand Down Expand Up @@ -355,9 +355,8 @@ func ReportProjectsForRepo(c *gin.Context) {
project := models.Project{
Name: request.Name,
ConfigurationYaml: request.ConfigurationYaml,
RepoID: repo.ID,
OrganisationID: org.ID,
Repo: &repo,
RepoFullName: repo.RepoFullName,
Organisation: org,
}

Expand Down
13 changes: 10 additions & 3 deletions backend/controllers/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ func RunsForProject(c *gin.Context) {
return
}

repo, err := models.DB.GetRepoByFullName(org.ID, project.RepoFullName)
if err != nil {
slog.Error("Could not fetch repo", "projectId", projectId, "repoFullName", project.RepoFullName, "error", err)
c.String(http.StatusInternalServerError, "Could not fetch repo")
return
}

if project.OrganisationID != org.ID {
slog.Warn("Forbidden access: not allowed to access project",
"projectOrgId", project.OrganisationID,
Expand All @@ -66,9 +73,9 @@ func RunsForProject(c *gin.Context) {
return
}

runs, err := models.DB.ListDiggerRunsForProject(project.Name, project.Repo.ID)
runs, err := models.DB.ListDiggerRunsForProject(project.Name, repo.ID)
if err != nil {
slog.Error("Could not fetch runs", "projectId", projectId, "repoId", project.Repo.ID, "error", err)
slog.Error("Could not fetch runs", "projectId", projectId, "repoId", repo.ID, "error", err)
c.String(http.StatusInternalServerError, "Could not fetch runs")
return
}
Expand All @@ -87,7 +94,7 @@ func RunsForProject(c *gin.Context) {
slog.Info("Successfully fetched runs for project",
"projectId", projectId,
"projectName", project.Name,
"repoId", project.Repo.ID,
"repoId", repo.ID,
"runCount", len(runs))

response := make(map[string]interface{})
Expand Down
5 changes: 5 additions & 0 deletions backend/migrations/20250530015921.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Modify "projects" table
ALTER TABLE "public"."projects" DROP COLUMN "repo_id", ADD COLUMN "repo_full_name" text NULL;
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Missing data migration and nullability considerations
Dropping repo_id will erase existing repo associations and adding a nullable repo_full_name leaves legacy rows with null. You need to backfill repo_full_name (e.g., via a JOIN on the repos table) before dropping repo_id, then enforce NOT NULL if your code assumes every project has a repo name.

🤖 Prompt for AI Agents
In backend/migrations/20250530015921.sql at lines 1 to 2, the migration drops
the repo_id column and adds a nullable repo_full_name column without preserving
existing data. To fix this, first add the repo_full_name column as nullable,
then backfill it by joining the projects table with the repos table to populate
repo_full_name based on repo_id. After ensuring all rows have repo_full_name
set, alter the column to be NOT NULL if required, and finally drop the repo_id
column.

-- Create index "idx_project_org" to table: "projects"
DROP INDEX IF EXISTS "idx_project";
CREATE UNIQUE INDEX "idx_project_org" ON "public"."projects" ("name", "organisation_id", "repo_full_name");
3 changes: 2 additions & 1 deletion backend/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
h1:7qYia+JfrMJUdNvsjR8hZosttT36aVdOje5TP3ekX4w=
h1:IeQ6uHSz71Ox7tBqG6sy05KmaRMpYqhr0SeipkVSEbM=
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
Expand Down Expand Up @@ -49,3 +49,4 @@ h1:7qYia+JfrMJUdNvsjR8hZosttT36aVdOje5TP3ekX4w=
20250416152705.sql h1:yeszz4c/BlyVgUUIk7aTUz/DUNMC40+9fe1Q8Ya4TVI=
20250512172515.sql h1:iIZIhFq3TTyjTzsxNWv3k4FcIkXfOCdHtmHNmYqjBMM=
20250512213729.sql h1:n8bYIXWko9xOUs+FPcG7lS3GXMVQBSygXX7Hpj20eVs=
20250530015921.sql h1:FidRW+0ur3mCDBPgP7V/sqr2jjfmi/CFJPRpNxTmqGs=
15 changes: 4 additions & 11 deletions backend/models/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ func (p *ProjectRun) MapToJsonStruct() interface{} {
Id: p.ID,
ProjectID: p.ProjectID,
ProjectName: p.Project.Name,
RepoUrl: p.Project.Repo.RepoUrl,
RepoFullName: p.Project.Repo.RepoFullName,
StartedAt: time.UnixMilli(p.StartedAt),
EndedAt: time.UnixMilli(p.EndedAt),
Status: p.Status,
Expand All @@ -75,11 +73,10 @@ const (

type Project struct {
gorm.Model
Name string `gorm:"uniqueIndex:idx_project"`
OrganisationID uint `gorm:"uniqueIndex:idx_project"`
Name string `gorm:"uniqueIndex:idx_project_org"`
OrganisationID uint `gorm:"uniqueIndex:idx_project_org"`
Organisation *Organisation
RepoID uint `gorm:"uniqueIndex:idx_project"`
Repo *Repo
RepoFullName string `gorm:"uniqueIndex:idx_project_org"`
ConfigurationYaml string // TODO: probably needs to be deleted
Status ProjectStatus
IsGenerated bool
Expand Down Expand Up @@ -112,12 +109,8 @@ func (p *Project) MapToJsonStruct() interface{} {
Id: p.ID,
Name: p.Name,
OrganisationID: p.OrganisationID,
RepoID: p.RepoID,
OrganisationName: p.Organisation.Name,
RepoFullName: p.Repo.RepoFullName,
RepoName: p.Repo.RepoName,
RepoOrg: p.Repo.RepoOrganisation,
RepoUrl: p.Repo.RepoUrl,
RepoFullName: p.RepoFullName,
LastActivityTimestamp: p.UpdatedAt.String(),
LastActivityAuthor: "unknown",
LastActivityStatus: string(status),
Expand Down
2 changes: 1 addition & 1 deletion backend/models/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func setupSuiteScheduler(tb testing.TB) (func(tb testing.TB), *Database) {
}

projectName := "test project"
_, err = database.CreateProject(projectName, org, repo, false, false)
_, err = database.CreateProject(projectName, org, repo.RepoFullName, false, false)
if err != nil {
panic(err)
}
Expand Down
Loading
Loading