@@ -50,6 +50,13 @@ func RunsForProject(c *gin.Context) {
50
50
return
51
51
}
52
52
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
+
53
60
runs , err := models .DB .ListDiggerRunsForProject (project .Name , project .Repo .ID )
54
61
if err != nil {
55
62
log .Printf ("could not fetch runs: %v" , err )
@@ -67,5 +74,56 @@ func RunsForProject(c *gin.Context) {
67
74
}
68
75
serializedRuns = append (serializedRuns , serializedRun )
69
76
}
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 )
71
129
}
0 commit comments