Skip to content

Commit 57fcb63

Browse files
committed
run details endpoint
1 parent 95f902d commit 57fcb63

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

backend/controllers/runs.go

+44-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,50 @@ func RunsForProject(c *gin.Context) {
8080
}
8181

8282
func RunDetails(c *gin.Context) {
83-
//currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
84-
//projectIdStr := c.Param("project_id")
83+
currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
84+
runIdStr := c.Param("run_id")
8585

86-
response := make(map[string]interface{})
86+
if runIdStr == "" {
87+
c.String(http.StatusBadRequest, "RunID not specified")
88+
return
89+
}
90+
91+
runId, err := strconv.Atoi(runIdStr)
92+
if err != nil {
93+
c.String(http.StatusBadRequest, "Invalid RunId")
94+
return
95+
}
96+
97+
if !exists {
98+
c.String(http.StatusForbidden, "Not allowed to access this resource")
99+
return
100+
}
101+
102+
var org models.Organisation
103+
err = models.DB.GormDB.Where("id = ?", currentOrg).First(&org).Error
104+
if err != nil {
105+
if errors.Is(err, gorm.ErrRecordNotFound) {
106+
c.String(http.StatusNotFound, fmt.Sprintf("Could not find organisation: %v", currentOrg))
107+
} else {
108+
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database")
109+
}
110+
return
111+
}
112+
113+
run, err := models.DB.GetDiggerRun(uint(runId))
114+
if err != nil {
115+
log.Printf("Could not fetch run: %v", err)
116+
c.String(http.StatusBadRequest, "Could not fetch run, please check that it exists")
117+
}
118+
if run.Repo.OrganisationID != org.ID {
119+
c.String(http.StatusForbidden, "Not allowed to access this resource")
120+
return
121+
}
122+
123+
response, err := run.MapToJsonStruct()
124+
if err != nil {
125+
c.String(http.StatusInternalServerError, "Could not unmarshall data")
126+
return
127+
}
87128
c.JSON(http.StatusOK, response)
88129
}

backend/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func main() {
169169

170170
runsApiGroup := apiGroup.Group("/runs")
171171
runsApiGroup.Use(middleware.GetWebMiddleware())
172-
projectsApiGroup.GET("/:project_id/runs/:id", controllers.RunDetails)
172+
runsApiGroup.GET("/:run_id", controllers.RunDetails)
173173

174174
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
175175

backend/models/storage.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,10 @@ func (db *Database) GetLastDiggerRunForProject(projectId uint) (*DiggerRun, erro
734734

735735
func (db *Database) GetDiggerRun(id uint) (*DiggerRun, error) {
736736
dr := &DiggerRun{}
737-
result := db.GormDB.Where("id=? ", id).Find(dr)
737+
result := db.GormDB.Preload("Repo").
738+
Preload("ApplyStage").
739+
Preload("PlanStage").
740+
Where("id=? ", id).Find(dr)
738741
if result.Error != nil {
739742
return nil, result.Error
740743
}

0 commit comments

Comments
 (0)