Skip to content

Commit d9bc8f9

Browse files
authored
Add run details endpoint (#1326)
* make runs list nested * run details endpoint
1 parent 63f509f commit d9bc8f9

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

backend/controllers/runs.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func RunsForProject(c *gin.Context) {
5050
return
5151
}
5252

53+
if project.OrganisationID != org.ID {
54+
log.Printf("Forbidden access: not allowed to access projectID: %v logged in org: %v", project.OrganisationID, org.ID)
55+
c.String(http.StatusForbidden, "No access to this project")
56+
return
57+
58+
}
59+
5360
runs, err := models.DB.ListDiggerRunsForProject(project.Name, project.Repo.ID)
5461
if err != nil {
5562
log.Printf("could not fetch runs: %v", err)
@@ -67,5 +74,56 @@ func RunsForProject(c *gin.Context) {
6774
}
6875
serializedRuns = append(serializedRuns, serializedRun)
6976
}
70-
c.JSON(http.StatusOK, serializedRuns)
77+
response := make(map[string]interface{})
78+
response["runs"] = serializedRuns
79+
c.JSON(http.StatusOK, response)
80+
}
81+
82+
func RunDetails(c *gin.Context) {
83+
currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
84+
runIdStr := c.Param("run_id")
85+
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+
}
128+
c.JSON(http.StatusOK, response)
71129
}

backend/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ func main() {
167167
projectsApiGroup.GET("/", controllers.FindProjectsForOrg)
168168
projectsApiGroup.GET("/:project_id/runs", controllers.RunsForProject)
169169

170+
runsApiGroup := apiGroup.Group("/runs")
171+
runsApiGroup.Use(middleware.GetWebMiddleware())
172+
runsApiGroup.GET("/:run_id", controllers.RunDetails)
173+
170174
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
171175

172176
r.Run(fmt.Sprintf(":%d", cfg.GetInt("port")))

backend/models/storage.go

Lines changed: 4 additions & 1 deletion
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)