Skip to content

Commit 0b216f4

Browse files
guillep2k6543
andauthored
Fix tracked time issues (#11349) (#11354)
Backport #11349 * Fix tracked time issues (#11349) * Fix nil exeption: #11313 * fix 500 * activate test 😆 * move logic * Add missing import Co-authored-by: 6543 <[email protected]> Co-authored-by: Guillermo Prandi <[email protected]>
1 parent dd6e604 commit 0b216f4

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

integrations/api_issue_tracked_time_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ func TestAPIDeleteTrackedTime(t *testing.T) {
6060
//Deletion not allowed
6161
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time6.ID, token)
6262
session.MakeRequest(t, req, http.StatusForbidden)
63-
/* Delete own time <-- ToDo: timout without reason
63+
6464
time3 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime)
6565
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times/%d?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, time3.ID, token)
6666
session.MakeRequest(t, req, http.StatusNoContent)
6767
//Delete non existing time
68-
session.MakeRequest(t, req, http.StatusInternalServerError) */
68+
session.MakeRequest(t, req, http.StatusNotFound)
6969

7070
//Reset time of user 2 on issue 2
7171
trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
7272
assert.NoError(t, err)
73-
assert.Equal(t, int64(3662), trackedSeconds)
73+
assert.Equal(t, int64(3661), trackedSeconds)
7474

7575
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
7676
session.MakeRequest(t, req, http.StatusNoContent)

models/issue_tracked_time.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ func DeleteTime(t *TrackedTime) error {
273273
return err
274274
}
275275

276+
if err := t.loadAttributes(sess); err != nil {
277+
return err
278+
}
279+
276280
if err := deleteTime(sess, t); err != nil {
277281
return err
278282
}
@@ -312,10 +316,8 @@ func deleteTime(e Engine, t *TrackedTime) error {
312316

313317
// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
314318
func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
315-
time := &TrackedTime{
316-
ID: id,
317-
}
318-
has, err := x.Get(time)
319+
time := new(TrackedTime)
320+
has, err := x.ID(id).Get(time)
319321
if err != nil {
320322
return nil, err
321323
} else if !has {

routers/api/v1/repo/issue_tracked_time.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package repo
66

77
import (
8+
"fmt"
89
"net/http"
910
"time"
1011

@@ -296,6 +297,10 @@ func DeleteTime(ctx *context.APIContext) {
296297
ctx.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err)
297298
return
298299
}
300+
if time.Deleted {
301+
ctx.NotFound(fmt.Errorf("tracked time [%d] already deleted", time.ID))
302+
return
303+
}
299304

300305
if !ctx.User.IsAdmin && time.UserID != ctx.User.ID {
301306
//Only Admin and User itself can delete their time

0 commit comments

Comments
 (0)