5
5
package repo
6
6
7
7
import (
8
+ "fmt"
8
9
"net/http"
10
+ "strings"
9
11
"time"
10
12
11
13
"code.gitea.io/gitea/models"
12
14
"code.gitea.io/gitea/modules/context"
13
15
api "code.gitea.io/gitea/modules/structs"
16
+ "code.gitea.io/gitea/routers/api/v1/utils"
14
17
)
15
18
16
19
// ListTrackedTimes list all the tracked times of an issue
@@ -37,6 +40,16 @@ func ListTrackedTimes(ctx *context.APIContext) {
37
40
// type: integer
38
41
// format: int64
39
42
// required: true
43
+ // - name: since
44
+ // in: query
45
+ // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
46
+ // type: string
47
+ // format: date-time
48
+ // - name: before
49
+ // in: query
50
+ // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
51
+ // type: string
52
+ // format: date-time
40
53
// responses:
41
54
// "200":
42
55
// "$ref": "#/responses/TrackedTimeList"
@@ -62,6 +75,11 @@ func ListTrackedTimes(ctx *context.APIContext) {
62
75
IssueID : issue .ID ,
63
76
}
64
77
78
+ if opts .CreatedBeforeUnix , opts .CreatedAfterUnix , err = utils .GetQueryBeforeSince (ctx ); err != nil {
79
+ ctx .InternalServerError (err )
80
+ return
81
+ }
82
+
65
83
if ! ctx .IsUserRepoAdmin () && ! ctx .User .IsAdmin {
66
84
opts .UserID = ctx .User .ID
67
85
}
@@ -141,7 +159,7 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
141
159
//allow only RepoAdmin, Admin and User to add time
142
160
user , err = models .GetUserByName (form .User )
143
161
if err != nil {
144
- ctx .Error (500 , "GetUserByName" , err )
162
+ ctx .Error (http . StatusInternalServerError , "GetUserByName" , err )
145
163
}
146
164
}
147
165
}
@@ -195,33 +213,33 @@ func ResetIssueTime(ctx *context.APIContext) {
195
213
// "400":
196
214
// "$ref": "#/responses/error"
197
215
// "403":
198
- // "$ref": "#/responses/error "
216
+ // "$ref": "#/responses/forbidden "
199
217
200
218
issue , err := models .GetIssueByIndex (ctx .Repo .Repository .ID , ctx .ParamsInt64 (":index" ))
201
219
if err != nil {
202
220
if models .IsErrIssueNotExist (err ) {
203
221
ctx .NotFound (err )
204
222
} else {
205
- ctx .Error (500 , "GetIssueByIndex" , err )
223
+ ctx .Error (http . StatusInternalServerError , "GetIssueByIndex" , err )
206
224
}
207
225
return
208
226
}
209
227
210
228
if ! ctx .Repo .CanUseTimetracker (issue , ctx .User ) {
211
229
if ! ctx .Repo .Repository .IsTimetrackerEnabled () {
212
- ctx .JSON (400 , struct { Message string }{Message : "time tracking disabled" })
230
+ ctx .JSON (http . StatusBadRequest , struct { Message string }{Message : "time tracking disabled" })
213
231
return
214
232
}
215
- ctx .Status (403 )
233
+ ctx .Status (http . StatusForbidden )
216
234
return
217
235
}
218
236
219
237
err = models .DeleteIssueUserTimes (issue , ctx .User )
220
238
if err != nil {
221
239
if models .IsErrNotExist (err ) {
222
- ctx .Error (404 , "DeleteIssueUserTimes" , err )
240
+ ctx .Error (http . StatusNotFound , "DeleteIssueUserTimes" , err )
223
241
} else {
224
- ctx .Error (500 , "DeleteIssueUserTimes" , err )
242
+ ctx .Error (http . StatusInternalServerError , "DeleteIssueUserTimes" , err )
225
243
}
226
244
return
227
245
}
@@ -266,52 +284,53 @@ func DeleteTime(ctx *context.APIContext) {
266
284
// "400":
267
285
// "$ref": "#/responses/error"
268
286
// "403":
269
- // "$ref": "#/responses/error "
287
+ // "$ref": "#/responses/forbidden "
270
288
271
289
issue , err := models .GetIssueByIndex (ctx .Repo .Repository .ID , ctx .ParamsInt64 (":index" ))
272
290
if err != nil {
273
291
if models .IsErrIssueNotExist (err ) {
274
292
ctx .NotFound (err )
275
293
} else {
276
- ctx .Error (500 , "GetIssueByIndex" , err )
294
+ ctx .Error (http . StatusInternalServerError , "GetIssueByIndex" , err )
277
295
}
278
296
return
279
297
}
280
298
281
299
if ! ctx .Repo .CanUseTimetracker (issue , ctx .User ) {
282
300
if ! ctx .Repo .Repository .IsTimetrackerEnabled () {
283
- ctx .JSON (400 , struct { Message string }{Message : "time tracking disabled" })
301
+ ctx .JSON (http . StatusBadRequest , struct { Message string }{Message : "time tracking disabled" })
284
302
return
285
303
}
286
- ctx .Status (403 )
304
+ ctx .Status (http . StatusForbidden )
287
305
return
288
306
}
289
307
290
308
time , err := models .GetTrackedTimeByID (ctx .ParamsInt64 (":id" ))
291
309
if err != nil {
292
- ctx .Error (500 , "GetTrackedTimeByID" , err )
310
+ ctx .Error (http . StatusInternalServerError , "GetTrackedTimeByID" , err )
293
311
return
294
312
}
295
313
296
314
if ! ctx .User .IsAdmin && time .UserID != ctx .User .ID {
297
315
//Only Admin and User itself can delete their time
298
- ctx .Status (403 )
316
+ ctx .Status (http . StatusForbidden )
299
317
return
300
318
}
301
319
302
320
err = models .DeleteTime (time )
303
321
if err != nil {
304
- ctx .Error (500 , "DeleteTime" , err )
322
+ ctx .Error (http . StatusInternalServerError , "DeleteTime" , err )
305
323
return
306
324
}
307
- ctx .Status (204 )
325
+ ctx .Status (http . StatusNoContent )
308
326
}
309
327
310
328
// ListTrackedTimesByUser lists all tracked times of the user
311
329
func ListTrackedTimesByUser (ctx * context.APIContext ) {
312
- // swagger:operation GET /repos/{owner}/{repo}/times/{user} user userTrackedTimes
330
+ // swagger:operation GET /repos/{owner}/{repo}/times/{user} repository userTrackedTimes
313
331
// ---
314
332
// summary: List a user's tracked times in a repo
333
+ // deprecated: true
315
334
// produces:
316
335
// - application/json
317
336
// parameters:
@@ -335,6 +354,8 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
335
354
// "$ref": "#/responses/TrackedTimeList"
336
355
// "400":
337
356
// "$ref": "#/responses/error"
357
+ // "403":
358
+ // "$ref": "#/responses/forbidden"
338
359
339
360
if ! ctx .Repo .Repository .IsTimetrackerEnabled () {
340
361
ctx .Error (http .StatusBadRequest , "" , "time tracking disabled" )
@@ -353,9 +374,23 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
353
374
ctx .NotFound ()
354
375
return
355
376
}
356
- trackedTimes , err := models .GetTrackedTimes (models.FindTrackedTimesOptions {
377
+
378
+ if ! ctx .IsUserRepoAdmin () && ! ctx .User .IsAdmin && ctx .User .ID != user .ID {
379
+ ctx .Error (http .StatusForbidden , "" , fmt .Errorf ("query user not allowed not enouth rights" ))
380
+ return
381
+ }
382
+
383
+ if ! ctx .IsUserRepoAdmin () && ! ctx .User .IsAdmin && ctx .User .ID != user .ID {
384
+ ctx .Error (http .StatusForbidden , "" , fmt .Errorf ("query user not allowed not enouth rights" ))
385
+ return
386
+ }
387
+
388
+ opts := models.FindTrackedTimesOptions {
357
389
UserID : user .ID ,
358
- RepositoryID : ctx .Repo .Repository .ID })
390
+ RepositoryID : ctx .Repo .Repository .ID ,
391
+ }
392
+
393
+ trackedTimes , err := models .GetTrackedTimes (opts )
359
394
if err != nil {
360
395
ctx .Error (http .StatusInternalServerError , "GetTrackedTimes" , err )
361
396
return
@@ -385,11 +420,27 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
385
420
// description: name of the repo
386
421
// type: string
387
422
// required: true
423
+ // - name: user
424
+ // in: query
425
+ // description: optional filter by user
426
+ // type: string
427
+ // - name: since
428
+ // in: query
429
+ // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
430
+ // type: string
431
+ // format: date-time
432
+ // - name: before
433
+ // in: query
434
+ // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
435
+ // type: string
436
+ // format: date-time
388
437
// responses:
389
438
// "200":
390
439
// "$ref": "#/responses/TrackedTimeList"
391
440
// "400":
392
441
// "$ref": "#/responses/error"
442
+ // "403":
443
+ // "$ref": "#/responses/forbidden"
393
444
394
445
if ! ctx .Repo .Repository .IsTimetrackerEnabled () {
395
446
ctx .Error (http .StatusBadRequest , "" , "time tracking disabled" )
@@ -400,8 +451,30 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
400
451
RepositoryID : ctx .Repo .Repository .ID ,
401
452
}
402
453
454
+ // Filters
455
+ qUser := strings .Trim (ctx .Query ("user" ), " " )
456
+ if qUser != "" {
457
+ user , err := models .GetUserByName (qUser )
458
+ if err != nil {
459
+ ctx .Error (http .StatusInternalServerError , "GetUserByName" , err )
460
+ return
461
+ }
462
+ opts .UserID = user .ID
463
+ }
464
+
465
+ var err error
466
+ if opts .CreatedBeforeUnix , opts .CreatedAfterUnix , err = utils .GetQueryBeforeSince (ctx ); err != nil {
467
+ ctx .InternalServerError (err )
468
+ return
469
+ }
470
+
403
471
if ! ctx .IsUserRepoAdmin () && ! ctx .User .IsAdmin {
404
- opts .UserID = ctx .User .ID
472
+ if opts .UserID == 0 {
473
+ opts .UserID = ctx .User .ID
474
+ } else {
475
+ ctx .Error (http .StatusForbidden , "" , fmt .Errorf ("query user not allowed not enouth rights" ))
476
+ return
477
+ }
405
478
}
406
479
407
480
trackedTimes , err := models .GetTrackedTimes (opts )
@@ -423,18 +496,39 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
423
496
// summary: List the current user's tracked times
424
497
// produces:
425
498
// - application/json
499
+ // parameters:
500
+ // - name: since
501
+ // in: query
502
+ // description: Only show times updated after the given time. This is a timestamp in RFC 3339 format
503
+ // type: string
504
+ // format: date-time
505
+ // - name: before
506
+ // in: query
507
+ // description: Only show times updated before the given time. This is a timestamp in RFC 3339 format
508
+ // type: string
509
+ // format: date-time
426
510
// responses:
427
511
// "200":
428
512
// "$ref": "#/responses/TrackedTimeList"
429
513
430
- trackedTimes , err := models .GetTrackedTimes (models.FindTrackedTimesOptions {UserID : ctx .User .ID })
514
+ opts := models.FindTrackedTimesOptions {UserID : ctx .User .ID }
515
+
516
+ var err error
517
+ if opts .CreatedBeforeUnix , opts .CreatedAfterUnix , err = utils .GetQueryBeforeSince (ctx ); err != nil {
518
+ ctx .InternalServerError (err )
519
+ return
520
+ }
521
+
522
+ trackedTimes , err := models .GetTrackedTimes (opts )
431
523
if err != nil {
432
524
ctx .Error (http .StatusInternalServerError , "GetTrackedTimesByUser" , err )
433
525
return
434
526
}
527
+
435
528
if err = trackedTimes .LoadAttributes (); err != nil {
436
529
ctx .Error (http .StatusInternalServerError , "LoadAttributes" , err )
437
530
return
438
531
}
532
+
439
533
ctx .JSON (http .StatusOK , trackedTimes .APIFormat ())
440
534
}
0 commit comments