1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

Fix tracked time issues (#11349)

* Fix nil exeption: #11313

* fix 500

* activate test 😆

* move logic
This commit is contained in:
6543 2020-05-09 16:18:44 +02:00 committed by GitHub
parent b9df5da1f4
commit cd4f7ba5bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -72,17 +72,17 @@ func TestAPIDeleteTrackedTime(t *testing.T) {
//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)
session.MakeRequest(t, req, http.StatusForbidden)
/* Delete own time <-- ToDo: timout without reason
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)
session.MakeRequest(t, req, http.StatusNoContent)
//Delete non existing time
session.MakeRequest(t, req, http.StatusInternalServerError) */
session.MakeRequest(t, req, http.StatusNotFound)
//Reset time of user 2 on issue 2
trackedSeconds, err := models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
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)
session.MakeRequest(t, req, http.StatusNoContent)

View File

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

View File

@ -324,6 +324,10 @@ func DeleteTime(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err)
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 {
//Only Admin and User itself can delete their time