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 <6543@obermui.de>
Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com>
This commit is contained in:
guillep2k 2020-05-09 14:08:41 -03:00 committed by GitHub
parent dd6e604f8f
commit 0b216f40fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 7 deletions

View file

@ -60,17 +60,17 @@ func TestAPIDeleteTrackedTime(t *testing.T) {
//Deletion not allowed //Deletion not allowed
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) 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)
session.MakeRequest(t, req, http.StatusForbidden) session.MakeRequest(t, req, http.StatusForbidden)
/* Delete own time <-- ToDo: timout without reason
time3 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime) time3 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 3}).(*models.TrackedTime)
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) 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)
session.MakeRequest(t, req, http.StatusNoContent) session.MakeRequest(t, req, http.StatusNoContent)
//Delete non existing time //Delete non existing time
session.MakeRequest(t, req, http.StatusInternalServerError) */ session.MakeRequest(t, req, http.StatusNotFound)
//Reset time of user 2 on issue 2 //Reset time of user 2 on issue 2
trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2}) trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, int64(3662), trackedSeconds) assert.Equal(t, int64(3661), trackedSeconds)
req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token) req = NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
session.MakeRequest(t, req, http.StatusNoContent) session.MakeRequest(t, req, http.StatusNoContent)

View file

@ -273,6 +273,10 @@ func DeleteTime(t *TrackedTime) error {
return err return err
} }
if err := t.loadAttributes(sess); err != nil {
return err
}
if err := deleteTime(sess, t); err != nil { if err := deleteTime(sess, t); err != nil {
return err return err
} }
@ -312,10 +316,8 @@ func deleteTime(e Engine, t *TrackedTime) error {
// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id // GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
func GetTrackedTimeByID(id int64) (*TrackedTime, error) { func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
time := &TrackedTime{ time := new(TrackedTime)
ID: id, has, err := x.ID(id).Get(time)
}
has, err := x.Get(time)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

View file

@ -5,6 +5,7 @@
package repo package repo
import ( import (
"fmt"
"net/http" "net/http"
"time" "time"
@ -296,6 +297,10 @@ func DeleteTime(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err) ctx.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err)
return return
} }
if time.Deleted {
ctx.NotFound(fmt.Errorf("tracked time [%d] already deleted", time.ID))
return
}
if !ctx.User.IsAdmin && time.UserID != ctx.User.ID { if !ctx.User.IsAdmin && time.UserID != ctx.User.ID {
//Only Admin and User itself can delete their time //Only Admin and User itself can delete their time