Skip to content

Commit 19a3cb4

Browse files
committed
add endpoint for project runs
1 parent d3c045a commit 19a3cb4

File tree

8 files changed

+187
-41
lines changed

8 files changed

+187
-41
lines changed

backend/controllers/github_after_merge.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,26 @@ func GithubAppWebHookAfterMerge(c *gin.Context) {
7474
}
7575
}
7676

77-
case *github.IssueCommentEvent:
78-
log.Printf("IssueCommentEvent, action: %v IN APPLY AFTER MERGE\n", *event.Action)
79-
if event.Sender.Type != nil && *event.Sender.Type == "Bot" {
80-
c.String(http.StatusOK, "OK")
81-
return
82-
}
83-
err := handleIssueCommentEvent(gh, event)
84-
if err != nil {
85-
log.Printf("handleIssueCommentEvent error: %v", err)
86-
c.String(http.StatusInternalServerError, err.Error())
87-
return
88-
}
89-
case *github.PullRequestEvent:
90-
log.Printf("Got pull request event for %d IN APPLY AFTER MERGE", *event.PullRequest.ID)
91-
err := handlePullRequestEvent(gh, event)
92-
if err != nil {
93-
log.Printf("handlePullRequestEvent error: %v", err)
94-
c.String(http.StatusInternalServerError, err.Error())
95-
return
96-
}
77+
//case *github.IssueCommentEvent:
78+
// log.Printf("IssueCommentEvent, action: %v IN APPLY AFTER MERGE\n", *event.Action)
79+
// if event.Sender.Type != nil && *event.Sender.Type == "Bot" {
80+
// c.String(http.StatusOK, "OK")
81+
// return
82+
// }
83+
// err := handleIssueCommentEvent(gh, event)
84+
// if err != nil {
85+
// log.Printf("handleIssueCommentEvent error: %v", err)
86+
// c.String(http.StatusInternalServerError, err.Error())
87+
// return
88+
// }
89+
//case *github.PullRequestEvent:
90+
// log.Printf("Got pull request event for %d IN APPLY AFTER MERGE", *event.PullRequest.ID)
91+
// err := handlePullRequestEvent(gh, event)
92+
// if err != nil {
93+
// log.Printf("handlePullRequestEvent error: %v", err)
94+
// c.String(http.StatusInternalServerError, err.Error())
95+
// return
96+
// }
9797
case *github.PushEvent:
9898
log.Printf("Got push event for %d", event.Repo.URL)
9999
err := handlePushEventApplyAfterMerge(gh, event)
@@ -110,6 +110,7 @@ func GithubAppWebHookAfterMerge(c *gin.Context) {
110110
}
111111

112112
func handlePushEventApplyAfterMerge(gh utils.GithubClientProvider, payload *github.PushEvent) error {
113+
print("*** HANDLING PUSH EVENT *****")
113114
installationId := *payload.Installation.ID
114115
repoName := *payload.Repo.Name
115116
repoFullName := *payload.Repo.FullName
@@ -155,7 +156,8 @@ func handlePushEventApplyAfterMerge(gh utils.GithubClientProvider, payload *gith
155156
})
156157

157158
// ==== starting apply after merge part =======
158-
diggerYmlStr, ghService, config, projectsGraph, err := getDiggerConfigForBranch(gh, installationId, repoFullName, repoOwner, repoName, cloneURL, commitId)
159+
// TODO: Replace branch with actual commitID
160+
diggerYmlStr, ghService, config, projectsGraph, err := getDiggerConfigForBranch(gh, installationId, repoFullName, repoOwner, repoName, cloneURL, defaultBranch)
159161
if err != nil {
160162
log.Printf("getDiggerConfigForPR error: %v", err)
161163
return fmt.Errorf("error getting digger config")
@@ -235,7 +237,7 @@ func handlePushEventApplyAfterMerge(gh utils.GithubClientProvider, payload *gith
235237
return fmt.Errorf("error creating digger job")
236238
}
237239

238-
_, err = models.DB.CreateDiggerJob(planBatch.ID, applyJobSpec, impactedProjects[i].WorkflowFile)
240+
_, err = models.DB.CreateDiggerJob(applyBatch.ID, applyJobSpec, impactedProjects[i].WorkflowFile)
239241
if err != nil {
240242
log.Printf("Error creating digger job: %v", err)
241243
return fmt.Errorf("error creating digger job")

backend/controllers/runs.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package controllers
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/diggerhq/digger/backend/middleware"
7+
"github.com/diggerhq/digger/backend/models"
8+
"github.com/gin-gonic/gin"
9+
"gorm.io/gorm"
10+
"log"
11+
"net/http"
12+
"strconv"
13+
)
14+
15+
func RunsForProject(c *gin.Context) {
16+
currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
17+
projectIdStr := c.Param("project_id")
18+
19+
if projectIdStr == "" {
20+
c.String(http.StatusBadRequest, "ProjectId not specified")
21+
return
22+
}
23+
24+
projectId, err := strconv.Atoi(projectIdStr)
25+
if err != nil {
26+
c.String(http.StatusBadRequest, "Invalid ProjectId")
27+
return
28+
}
29+
30+
if !exists {
31+
c.String(http.StatusForbidden, "Not allowed to access this resource")
32+
return
33+
}
34+
35+
var org models.Organisation
36+
err = models.DB.GormDB.Where("id = ?", currentOrg).First(&org).Error
37+
if err != nil {
38+
if errors.Is(err, gorm.ErrRecordNotFound) {
39+
c.String(http.StatusNotFound, fmt.Sprintf("Could not find organisation: %v", currentOrg))
40+
} else {
41+
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database")
42+
}
43+
return
44+
}
45+
46+
project, err := models.DB.GetProject(uint(projectId))
47+
if err != nil {
48+
log.Printf("could not fetch project: %v", err)
49+
c.String(http.StatusInternalServerError, "Could not fetch project")
50+
return
51+
}
52+
53+
runs, err := models.DB.ListDiggerRunsForProject(project.Name, project.Repo.ID)
54+
if err != nil {
55+
log.Printf("could not fetch runs: %v", err)
56+
c.String(http.StatusInternalServerError, "Could not fetch runs")
57+
return
58+
}
59+
60+
serializedRuns := make([]interface{}, 0)
61+
for _, run := range runs {
62+
serializedRun, err := run.MapToJsonStruct()
63+
if err != nil {
64+
log.Printf("could not unmarshal run: %v", err)
65+
c.String(http.StatusInternalServerError, "Could not unmarshal runs")
66+
return
67+
}
68+
serializedRuns = append(serializedRuns, serializedRun)
69+
}
70+
c.JSON(http.StatusOK, serializedRuns)
71+
}

backend/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func main() {
165165
projectsApiGroup := apiGroup.Group("/projects")
166166
projectsApiGroup.Use(middleware.GetWebMiddleware())
167167
projectsApiGroup.GET("/", controllers.FindProjectsForOrg)
168+
projectsApiGroup.GET("/:project_id/runs", controllers.RunsForProject)
168169

169170
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
170171

backend/migrations/20240404165910.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Modify "digger_runs" table
2+
ALTER TABLE "public"."digger_runs" DROP COLUMN "project_id", ADD COLUMN "project_name" text NULL;

backend/migrations/atlas.sum

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:wCerMVvOi8Co+XSFeE+18Vcex7+cmseQ9ZJp4sHZwaY=
1+
h1:xkkYzycO+Sb/rIvE5VwTvzTb6dA6kyuQQ1/ZT/Oy9no=
22
20231227132525.sql h1:43xn7XC0GoJsCnXIMczGXWis9d504FAWi4F1gViTIcw=
33
20240115170600.sql h1:IW8fF/8vc40+eWqP/xDK+R4K9jHJ9QBSGO6rN9LtfSA=
44
20240116123649.sql h1:R1JlUIgxxF6Cyob9HdtMqiKmx/BfnsctTl5rvOqssQw=
@@ -14,3 +14,4 @@ h1:wCerMVvOi8Co+XSFeE+18Vcex7+cmseQ9ZJp4sHZwaY=
1414
20240404160724.sql h1:cjEZiC7JC0dCJIRSIh1OBBhi2IBuRZEBj/ifoYqNzAw=
1515
20240404161121.sql h1:ZbMLfHRom6Tws+2M3BcnMu1lcjz/YFwAI8kGmC+I+H4=
1616
20240404161723.sql h1:z3bJcKs0ZJSyTJewqgE0GSHpn33sX7zgc2rmCMF99Qo=
17+
20240404165910.sql h1:ofwrBzkvnxFz7sOrtaF3vb2xHsenPmUTSSBHvO1NEdI=

backend/models/runs.go

+55-17
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ type DiggerRun struct {
4747
Repo *Repo
4848
ProjectName string
4949
RunType RunType
50-
PlanStage *DiggerRunStage
50+
PlanStage DiggerRunStage
5151
PlanStageId *uint
52-
ApplyStage *DiggerRunStage
52+
ApplyStage DiggerRunStage
5353
ApplyStageId *uint
5454
}
5555

@@ -60,29 +60,67 @@ type DiggerRunStage struct {
6060
}
6161

6262
type SerializedRunStage struct {
63-
DiggerJobId string `json:"digger_job_id"`
64-
Status orchestrator_scheduler.DiggerJobStatus `json:"status"`
65-
ProjectName string `json:"project_name"`
66-
WorkflowRunUrl *string `json:"workflow_run_url"`
67-
ResourcesCreated uint `json:"resources_created"`
68-
ResourcesDeleted uint `json:"resources_deleted"`
69-
ResourcesUpdated uint `json:"resources_updated"`
63+
//DiggerRunId uint `json:"digger_run_id"`
64+
DiggerJobId string `json:"digger_job_id"`
65+
Status orchestrator_scheduler.DiggerJobStatus `json:"status"`
66+
ProjectName string `json:"project_name"`
67+
WorkflowRunUrl *string `json:"workflow_run_url"`
68+
ResourcesCreated uint `json:"resources_created"`
69+
ResourcesDeleted uint `json:"resources_deleted"`
70+
ResourcesUpdated uint `json:"resources_updated"`
71+
LastActivityTimeStamp string `json:"last_activity_timestamp"`
7072
}
7173

72-
func (r *DiggerRunStage) MapToJsonStruct() (interface{}, error) {
73-
job, err := DB.GetDiggerJobFromRunStage(*r)
74+
func (r *DiggerRun) MapToJsonStruct() (interface{}, error) {
75+
planStage, err := r.PlanStage.MapToJsonStruct()
76+
if err != nil {
77+
log.Printf("error serializing run: %v", err)
78+
return nil, err
79+
}
80+
81+
applyStage, err := r.ApplyStage.MapToJsonStruct()
82+
if err != nil {
83+
log.Printf("error serializing run: %v", err)
84+
return nil, err
85+
}
86+
87+
x := struct {
88+
Id uint `json:"id"`
89+
Status string `json:"status"`
90+
Type string `json:"type"`
91+
ApprovalAuthor string `json:"approval_author"`
92+
ApprovalStatus string `json:"approval_status"`
93+
ApprovalDate string `json:"approval_date"`
94+
LastActivityTimeStamp string `json:"last_activity_time_stamp"`
95+
PlanStage SerializedRunStage `json:"plan_stage"`
96+
ApplyStage SerializedRunStage `json:"apply_stage"`
97+
}{
98+
Id: r.ID,
99+
Status: string(r.Status),
100+
Type: string(r.RunType),
101+
LastActivityTimeStamp: r.UpdatedAt.String(),
102+
PlanStage: *planStage,
103+
ApplyStage: *applyStage,
104+
}
105+
106+
return x, nil
107+
}
108+
109+
func (r DiggerRunStage) MapToJsonStruct() (*SerializedRunStage, error) {
110+
job, err := DB.GetDiggerJobFromRunStage(r)
74111
if err != nil {
75112
log.Printf("Could not retrive job from run")
76113
return nil, err
77114
}
78115

79-
return SerializedRunStage{
116+
return &SerializedRunStage{
80117
DiggerJobId: job.DiggerJobID,
81118
Status: job.Status,
82-
//ProjectName: r.Run.Project.Name,
83-
WorkflowRunUrl: job.WorkflowRunUrl,
84-
ResourcesCreated: job.DiggerJobSummary.ResourcesCreated,
85-
ResourcesUpdated: job.DiggerJobSummary.ResourcesUpdated,
86-
ResourcesDeleted: job.DiggerJobSummary.ResourcesDeleted,
119+
//ProjectName: r.Run.ProjectName,
120+
WorkflowRunUrl: job.WorkflowRunUrl,
121+
ResourcesCreated: job.DiggerJobSummary.ResourcesCreated,
122+
ResourcesUpdated: job.DiggerJobSummary.ResourcesUpdated,
123+
ResourcesDeleted: job.DiggerJobSummary.ResourcesDeleted,
124+
LastActivityTimeStamp: r.UpdatedAt.String(),
87125
}, nil
88126
}

backend/models/storage.go

+31
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ func (db *Database) GetProjectByProjectId(c *gin.Context, projectId uint, orgIdK
169169
return &project, true
170170
}
171171

172+
func (db *Database) GetProject(projectId uint) (*Project, error) {
173+
log.Printf("GetProject, project id: %v\n", projectId)
174+
var project Project
175+
176+
err := db.GormDB.Preload("Organisation").Preload("Repo").
177+
Where("id = ?", projectId).
178+
First(&project).Error
179+
180+
if err != nil {
181+
log.Printf("Unknown error occurred while fetching database, %v\n", err)
182+
return nil, err
183+
}
184+
185+
return &project, nil
186+
}
187+
172188
// GetProjectByName return project for specified org and repo
173189
// if record doesn't exist return nil
174190
func (db *Database) GetProjectByName(orgId any, repo *Repo, name string) (*Project, error) {
@@ -655,6 +671,21 @@ func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte, wor
655671
return job, nil
656672
}
657673

674+
func (db *Database) ListDiggerRunsForProject(projectName string, repoId uint) ([]DiggerRun, error) {
675+
var runs []DiggerRun
676+
677+
err := db.GormDB.Preload("PlanStage").Preload("ApplyStage").
678+
Where("project_name = ? AND repo_id= ?", projectName, repoId).Order("created_at desc").Find(&runs).Error
679+
680+
if err != nil {
681+
log.Printf("Unknown error occurred while fetching database, %v\n", err)
682+
return nil, err
683+
}
684+
685+
log.Printf("ListDiggerRunsForProject, number of runs:%d\n", len(runs))
686+
return runs, nil
687+
}
688+
658689
func (db *Database) CreateDiggerRun(Triggertype string, PrNumber int, Status DiggerRunStatus, CommitId string, DiggerConfig string, GithubInstallationId int64, RepoId uint, ProjectName string, RunType RunType, planStageId *uint, applyStageId *uint) (*DiggerRun, error) {
659690
dr := &DiggerRun{
660691
Triggertype: Triggertype,

libs/orchestrator/github/github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func ProcessGitHubPushEvent(payload *github.PushEvent, diggerConfig *digger_conf
575575
repo := *payload.Repo.Name
576576

577577
// TODO: Refactor to make generic interface
578-
changedFiles, err := ciService.(GithubService).GetChangedFilesForCommit(owner, repo, commitId)
578+
changedFiles, err := ciService.(*GithubService).GetChangedFilesForCommit(owner, repo, commitId)
579579
if err != nil {
580580
return nil, nil, 0, fmt.Errorf("could not get changed files")
581581
}

0 commit comments

Comments
 (0)