]> source.dussan.org Git - gitea.git/commitdiff
[API] Extend times API (#9200)
author6543 <6543@obermui.de>
Fri, 27 Dec 2019 20:30:58 +0000 (21:30 +0100)
committerzeripath <art27@cantab.net>
Fri, 27 Dec 2019 20:30:58 +0000 (20:30 +0000)
Extensively extend the times API.

close #8833; close #8513; close #8559

19 files changed:
integrations/api_issue_tracked_time_test.go [new file with mode: 0644]
models/fixtures/tracked_time.yml
models/issue_comment.go
models/issue_list.go
models/issue_list_test.go
models/issue_milestone.go
models/issue_milestone_test.go
models/issue_test.go
models/issue_tracked_time.go
models/issue_tracked_time_test.go
models/migrations/migrations.go
models/migrations/v116.go [new file with mode: 0644]
modules/structs/issue_tracked_time.go
options/locale/locale_en-US.ini
routers/api/v1/api.go
routers/api/v1/repo/issue_tracked_time.go
routers/repo/issue_timetrack.go
templates/repo/issue/view_content/comments.tmpl
templates/swagger/v1_json.tmpl

diff --git a/integrations/api_issue_tracked_time_test.go b/integrations/api_issue_tracked_time_test.go
new file mode 100644 (file)
index 0000000..ed6c036
--- /dev/null
@@ -0,0 +1,109 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package integrations
+
+import (
+       "fmt"
+       "net/http"
+       "testing"
+       "time"
+
+       "code.gitea.io/gitea/models"
+       api "code.gitea.io/gitea/modules/structs"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestAPIGetTrackedTimes(t *testing.T) {
+       defer prepareTestEnv(t)()
+
+       user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+       issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
+       assert.NoError(t, issue2.LoadRepo())
+
+       session := loginUser(t, user2.Name)
+       token := getTokenForLoggedInUser(t, session)
+
+       req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
+       resp := session.MakeRequest(t, req, http.StatusOK)
+       var apiTimes api.TrackedTimeList
+       DecodeJSON(t, resp, &apiTimes)
+       expect, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue2.ID})
+       assert.NoError(t, err)
+       assert.Len(t, apiTimes, 3)
+
+       for i, time := range expect {
+               assert.Equal(t, time.ID, apiTimes[i].ID)
+               assert.EqualValues(t, issue2.Title, apiTimes[i].Issue.Title)
+               assert.EqualValues(t, issue2.ID, apiTimes[i].IssueID)
+               assert.Equal(t, time.Created.Unix(), apiTimes[i].Created.Unix())
+               assert.Equal(t, time.Time, apiTimes[i].Time)
+               user, err := models.GetUserByID(time.UserID)
+               assert.NoError(t, err)
+               assert.Equal(t, user.Name, apiTimes[i].UserName)
+       }
+}
+
+func TestAPIDeleteTrackedTime(t *testing.T) {
+       defer prepareTestEnv(t)()
+
+       time6 := models.AssertExistsAndLoadBean(t, &models.TrackedTime{ID: 6}).(*models.TrackedTime)
+       issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
+       assert.NoError(t, issue2.LoadRepo())
+       user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+
+       session := loginUser(t, user2.Name)
+       token := getTokenForLoggedInUser(t, session)
+
+       //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) */
+
+       //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)
+
+       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.StatusNotFound)
+
+       trackedSeconds, err = models.GetTrackedSeconds(models.FindTrackedTimesOptions{IssueID: 2, UserID: 2})
+       assert.NoError(t, err)
+       assert.Equal(t, int64(0), trackedSeconds)
+}
+
+func TestAPIAddTrackedTimes(t *testing.T) {
+       defer prepareTestEnv(t)()
+
+       issue2 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
+       assert.NoError(t, issue2.LoadRepo())
+       user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
+       admin := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
+
+       session := loginUser(t, admin.Name)
+       token := getTokenForLoggedInUser(t, session)
+
+       urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/times?token=%s", user2.Name, issue2.Repo.Name, issue2.Index, token)
+
+       req := NewRequestWithJSON(t, "POST", urlStr, &api.AddTimeOption{
+               Time:    33,
+               User:    user2.Name,
+               Created: time.Unix(947688818, 0),
+       })
+       resp := session.MakeRequest(t, req, http.StatusOK)
+       var apiNewTime api.TrackedTime
+       DecodeJSON(t, resp, &apiNewTime)
+
+       assert.EqualValues(t, 33, apiNewTime.Time)
+       assert.EqualValues(t, user2.ID, apiNewTime.UserID)
+       assert.EqualValues(t, 947688818, apiNewTime.Created.Unix())
+}
index 06a71c5ad99dd764dbc4533ec86b15e819bdb3d4..768af38d9e20f28c5e780bb695e59908eecb2319 100644 (file)
@@ -4,6 +4,7 @@
   issue_id: 1
   time: 400
   created_unix: 946684800
+  deleted: false
 
 -
   id: 2
@@ -11,6 +12,7 @@
   issue_id: 2
   time: 3661
   created_unix: 946684801
+  deleted: false
 
 -
   id: 3
   issue_id: 2
   time: 1
   created_unix: 946684802
+  deleted: false
 
 -
   id: 4
   user_id: -1
   issue_id: 4
   time: 1
-  created_unix: 946684802
+  created_unix: 946684803
+  deleted: false
 
 -
   id: 5
   user_id: 2
   issue_id: 5
   time: 1
-  created_unix: 946684802
+  created_unix: 946684804
+  deleted: false
+
+-
+  id: 6
+  user_id: 1
+  issue_id: 2
+  time: 20
+  created_unix: 946684812
+  deleted: false
+
+-
+  id: 7
+  user_id: 2
+  issue_id: 4
+  time: 3
+  created_unix: 946684813
+  deleted: false
+
+-
+  id: 8
+  user_id: 1
+  issue_id: 4
+  time: 71
+  created_unix: 947688814
+  deleted: false
+
+-
+  id: 9
+  user_id: 2
+  issue_id: 2
+  time: 100000
+  created_unix: 947688815
+  deleted: true
index aeaee68003775379c11bd6ab9161fa5defbd84dd..3ba6790216bd5365ba9952c15b208e51b81cef6d 100644 (file)
@@ -84,6 +84,8 @@ const (
        CommentTypeUnlock
        // Change pull request's target branch
        CommentTypeChangeTargetBranch
+       // Delete time manual for time tracking
+       CommentTypeDeleteTimeManual
 )
 
 // CommentTag defines comment tag type
@@ -100,7 +102,7 @@ const (
 // Comment represents a comment in commit and issue page.
 type Comment struct {
        ID               int64       `xorm:"pk autoincr"`
-       Type             CommentType `xorm:"index"`
+       Type             CommentType `xorm:"INDEX"`
        PosterID         int64       `xorm:"INDEX"`
        Poster           *User       `xorm:"-"`
        OriginalAuthor   string
index e3516b55b948e58cf0e303db872c0e1d250911bb..4554f906c48c317c6dc167b6fb525d33a5dbaa3e 100644 (file)
@@ -429,6 +429,7 @@ func (issues IssueList) loadTotalTrackedTimes(e Engine) (err error) {
 
                // select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
                rows, err := e.Table("tracked_time").
+                       Where("deleted = ?", false).
                        Select("issue_id, sum(time) as time").
                        In("issue_id", ids[:limit]).
                        GroupBy("issue_id").
index 9197e0615aa4b90de7d598260527d8e1ffb4a9d7..f5a91702f25dd2124ed5446bfe26f9009cdd5768 100644 (file)
@@ -66,7 +66,7 @@ func TestIssueList_LoadAttributes(t *testing.T) {
                if issue.ID == int64(1) {
                        assert.Equal(t, int64(400), issue.TotalTrackedTime)
                } else if issue.ID == int64(2) {
-                       assert.Equal(t, int64(3662), issue.TotalTrackedTime)
+                       assert.Equal(t, int64(3682), issue.TotalTrackedTime)
                }
        }
 }
index b7191f66ffa2cc3b5d58e2000589d2bb9e6d5240..f33ad19ada496b4648956af2a32c3a7da2aac306 100644 (file)
@@ -10,8 +10,8 @@ import (
        "code.gitea.io/gitea/modules/setting"
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/timeutil"
-       "xorm.io/builder"
 
+       "xorm.io/builder"
        "xorm.io/xorm"
 )
 
@@ -153,6 +153,7 @@ func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
        rows, err := e.Table("issue").
                Join("INNER", "milestone", "issue.milestone_id = milestone.id").
                Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
+               Where("tracked_time.deleted = ?", false).
                Select("milestone_id, sum(time) as time").
                In("milestone_id", milestones.getMilestoneIDs()).
                GroupBy("milestone_id").
@@ -187,6 +188,7 @@ func (m *Milestone) loadTotalTrackedTime(e Engine) error {
        has, err := e.Table("issue").
                Join("INNER", "milestone", "issue.milestone_id = milestone.id").
                Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
+               Where("tracked_time.deleted = ?", false).
                Select("milestone_id, sum(time) as time").
                Where("milestone_id = ?", m.ID).
                GroupBy("milestone_id").
index 787b849cce3d5e25b22e9380deadee5069b630b6..5b7d19aa4dbb96e2e5ac9458238bd10dc25fd50d 100644 (file)
@@ -287,7 +287,7 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
 
        assert.NoError(t, miles.LoadTotalTrackedTimes())
 
-       assert.Equal(t, miles[0].TotalTrackedTime, int64(3662))
+       assert.Equal(t, int64(3682), miles[0].TotalTrackedTime)
 }
 
 func TestCountMilestonesByRepoIDs(t *testing.T) {
@@ -361,7 +361,7 @@ func TestLoadTotalTrackedTime(t *testing.T) {
 
        assert.NoError(t, milestone.LoadTotalTrackedTime())
 
-       assert.Equal(t, milestone.TotalTrackedTime, int64(3662))
+       assert.Equal(t, int64(3682), milestone.TotalTrackedTime)
 }
 
 func TestGetMilestonesStats(t *testing.T) {
index d369b0acf5c3bb805a779ddbd11a259440ec00ba..ec4867d075f7e28b12e21303d3dd0c0e247fd627 100644 (file)
@@ -259,7 +259,7 @@ func TestIssue_loadTotalTimes(t *testing.T) {
        ms, err := GetIssueByID(2)
        assert.NoError(t, err)
        assert.NoError(t, ms.loadTotalTimes(x))
-       assert.Equal(t, int64(3662), ms.TotalTrackedTime)
+       assert.Equal(t, int64(3682), ms.TotalTrackedTime)
 }
 
 func TestIssue_SearchIssueIDsByKeyword(t *testing.T) {
index f616836c85e176494cd4598aacbed30a9141638a..bcb163f3c5af415c598d46299f4d41bffdbdada6 100644 (file)
@@ -16,28 +16,86 @@ import (
 
 // TrackedTime represents a time that was spent for a specific issue.
 type TrackedTime struct {
-       ID          int64     `xorm:"pk autoincr" json:"id"`
-       IssueID     int64     `xorm:"INDEX" json:"issue_id"`
-       UserID      int64     `xorm:"INDEX" json:"user_id"`
-       Created     time.Time `xorm:"-" json:"created"`
-       CreatedUnix int64     `xorm:"created" json:"-"`
-       Time        int64     `json:"time"`
+       ID          int64     `xorm:"pk autoincr"`
+       IssueID     int64     `xorm:"INDEX"`
+       Issue       *Issue    `xorm:"-"`
+       UserID      int64     `xorm:"INDEX"`
+       User        *User     `xorm:"-"`
+       Created     time.Time `xorm:"-"`
+       CreatedUnix int64     `xorm:"created"`
+       Time        int64     `xorm:"NOT NULL"`
+       Deleted     bool      `xorm:"NOT NULL DEFAULT false"`
 }
 
+// TrackedTimeList is a List of TrackedTime's
+type TrackedTimeList []*TrackedTime
+
 // AfterLoad is invoked from XORM after setting the values of all fields of this object.
 func (t *TrackedTime) AfterLoad() {
        t.Created = time.Unix(t.CreatedUnix, 0).In(setting.DefaultUILocation)
 }
 
+// LoadAttributes load Issue, User
+func (t *TrackedTime) LoadAttributes() (err error) {
+       return t.loadAttributes(x)
+}
+
+func (t *TrackedTime) loadAttributes(e Engine) (err error) {
+       if t.Issue == nil {
+               t.Issue, err = getIssueByID(e, t.IssueID)
+               if err != nil {
+                       return
+               }
+               err = t.Issue.loadRepo(e)
+               if err != nil {
+                       return
+               }
+       }
+       if t.User == nil {
+               t.User, err = getUserByID(e, t.UserID)
+               if err != nil {
+                       return
+               }
+       }
+       return
+}
+
 // APIFormat converts TrackedTime to API format
-func (t *TrackedTime) APIFormat() *api.TrackedTime {
-       return &api.TrackedTime{
-               ID:      t.ID,
-               IssueID: t.IssueID,
-               UserID:  t.UserID,
-               Time:    t.Time,
-               Created: t.Created,
+func (t *TrackedTime) APIFormat() (apiT *api.TrackedTime) {
+       apiT = &api.TrackedTime{
+               ID:       t.ID,
+               IssueID:  t.IssueID,
+               UserID:   t.UserID,
+               UserName: t.User.Name,
+               Time:     t.Time,
+               Created:  t.Created,
+       }
+       if t.Issue != nil {
+               apiT.Issue = t.Issue.APIFormat()
+       }
+       if t.User != nil {
+               apiT.UserName = t.User.Name
+       }
+       return
+}
+
+// LoadAttributes load Issue, User
+func (tl TrackedTimeList) LoadAttributes() (err error) {
+       for _, t := range tl {
+               if err = t.LoadAttributes(); err != nil {
+                       return err
+               }
+       }
+       return
+}
+
+// APIFormat converts TrackedTimeList to API format
+func (tl TrackedTimeList) APIFormat() api.TrackedTimeList {
+       result := make([]*api.TrackedTime, 0, len(tl))
+       for _, t := range tl {
+               result = append(result, t.APIFormat())
        }
+       return result
 }
 
 // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
@@ -50,7 +108,7 @@ type FindTrackedTimesOptions struct {
 
 // ToCond will convert each condition into a xorm-Cond
 func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
-       cond := builder.NewCond()
+       cond := builder.NewCond().And(builder.Eq{"tracked_time.deleted": false})
        if opts.IssueID != 0 {
                cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
        }
@@ -71,37 +129,73 @@ func (opts *FindTrackedTimesOptions) ToSession(e Engine) *xorm.Session {
        if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
                return e.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(opts.ToCond())
        }
-       return x.Where(opts.ToCond())
+       return e.Where(opts.ToCond())
 }
 
-// GetTrackedTimes returns all tracked times that fit to the given options.
-func GetTrackedTimes(options FindTrackedTimesOptions) (trackedTimes []*TrackedTime, err error) {
-       err = options.ToSession(x).Find(&trackedTimes)
+func getTrackedTimes(e Engine, options FindTrackedTimesOptions) (trackedTimes TrackedTimeList, err error) {
+       err = options.ToSession(e).Find(&trackedTimes)
        return
 }
 
+// GetTrackedTimes returns all tracked times that fit to the given options.
+func GetTrackedTimes(opts FindTrackedTimesOptions) (TrackedTimeList, error) {
+       return getTrackedTimes(x, opts)
+}
+
+func getTrackedSeconds(e Engine, opts FindTrackedTimesOptions) (trackedSeconds int64, err error) {
+       return opts.ToSession(e).SumInt(&TrackedTime{}, "time")
+}
+
+// GetTrackedSeconds return sum of seconds
+func GetTrackedSeconds(opts FindTrackedTimesOptions) (int64, error) {
+       return getTrackedSeconds(x, opts)
+}
+
 // AddTime will add the given time (in seconds) to the issue
-func AddTime(user *User, issue *Issue, time int64) (*TrackedTime, error) {
-       tt := &TrackedTime{
-               IssueID: issue.ID,
-               UserID:  user.ID,
-               Time:    time,
+func AddTime(user *User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) {
+       sess := x.NewSession()
+       defer sess.Close()
+
+       if err := sess.Begin(); err != nil {
+               return nil, err
        }
-       if _, err := x.Insert(tt); err != nil {
+
+       t, err := addTime(sess, user, issue, amount, created)
+       if err != nil {
                return nil, err
        }
-       if err := issue.loadRepo(x); err != nil {
+
+       if err := issue.loadRepo(sess); err != nil {
                return nil, err
        }
-       if _, err := CreateComment(&CreateCommentOptions{
+
+       if _, err := createComment(sess, &CreateCommentOptions{
                Issue:   issue,
                Repo:    issue.Repo,
                Doer:    user,
-               Content: SecToTime(time),
+               Content: SecToTime(amount),
                Type:    CommentTypeAddTimeManual,
        }); err != nil {
                return nil, err
        }
+
+       return t, sess.Commit()
+}
+
+func addTime(e Engine, user *User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) {
+       if created.IsZero() {
+               created = time.Now()
+       }
+       tt := &TrackedTime{
+               IssueID: issue.ID,
+               UserID:  user.ID,
+               Time:    amount,
+               Created: created,
+       }
+       if _, err := e.Insert(tt); err != nil {
+               return nil, err
+       }
+
        return tt, nil
 }
 
@@ -131,3 +225,101 @@ func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
        }
        return totalTimes, nil
 }
+
+// DeleteIssueUserTimes deletes times for issue
+func DeleteIssueUserTimes(issue *Issue, user *User) error {
+       sess := x.NewSession()
+       defer sess.Close()
+
+       if err := sess.Begin(); err != nil {
+               return err
+       }
+
+       opts := FindTrackedTimesOptions{
+               IssueID: issue.ID,
+               UserID:  user.ID,
+       }
+
+       removedTime, err := deleteTimes(sess, opts)
+       if err != nil {
+               return err
+       }
+       if removedTime == 0 {
+               return ErrNotExist{}
+       }
+
+       if err := issue.loadRepo(sess); err != nil {
+               return err
+       }
+       if _, err := createComment(sess, &CreateCommentOptions{
+               Issue:   issue,
+               Repo:    issue.Repo,
+               Doer:    user,
+               Content: "- " + SecToTime(removedTime),
+               Type:    CommentTypeDeleteTimeManual,
+       }); err != nil {
+               return err
+       }
+
+       return sess.Commit()
+}
+
+// DeleteTime delete a specific Time
+func DeleteTime(t *TrackedTime) error {
+       sess := x.NewSession()
+       defer sess.Close()
+
+       if err := sess.Begin(); err != nil {
+               return err
+       }
+
+       if err := deleteTime(sess, t); err != nil {
+               return err
+       }
+
+       if _, err := createComment(sess, &CreateCommentOptions{
+               Issue:   t.Issue,
+               Repo:    t.Issue.Repo,
+               Doer:    t.User,
+               Content: "- " + SecToTime(t.Time),
+               Type:    CommentTypeDeleteTimeManual,
+       }); err != nil {
+               return err
+       }
+
+       return sess.Commit()
+}
+
+func deleteTimes(e Engine, opts FindTrackedTimesOptions) (removedTime int64, err error) {
+
+       removedTime, err = getTrackedSeconds(e, opts)
+       if err != nil || removedTime == 0 {
+               return
+       }
+
+       _, err = opts.ToSession(e).Table("tracked_time").Cols("deleted").Update(&TrackedTime{Deleted: true})
+       return
+}
+
+func deleteTime(e Engine, t *TrackedTime) error {
+       if t.Deleted {
+               return ErrNotExist{ID: t.ID}
+       }
+       t.Deleted = true
+       _, err := e.ID(t.ID).Cols("deleted").Update(t)
+       return err
+}
+
+// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
+func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
+       time := &TrackedTime{
+               ID: id,
+       }
+       has, err := x.Get(time)
+       if err != nil {
+               return nil, err
+       } else if !has {
+               return nil, ErrNotExist{ID: id}
+       }
+       return time, nil
+}
index 130e8f33e244b95cb86267303944e2ce24d193f7..551c918d734dadd310e007c4faf2923d529ca73a 100644 (file)
@@ -1,7 +1,12 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
 package models
 
 import (
        "testing"
+       "time"
 
        "github.com/stretchr/testify/assert"
 )
@@ -16,14 +21,14 @@ func TestAddTime(t *testing.T) {
        assert.NoError(t, err)
 
        //3661 = 1h 1min 1s
-       trackedTime, err := AddTime(user3, issue1, 3661)
+       trackedTime, err := AddTime(user3, issue1, 3661, time.Now())
        assert.NoError(t, err)
        assert.Equal(t, int64(3), trackedTime.UserID)
        assert.Equal(t, int64(1), trackedTime.IssueID)
        assert.Equal(t, int64(3661), trackedTime.Time)
 
        tt := AssertExistsAndLoadBean(t, &TrackedTime{UserID: 3, IssueID: 1}).(*TrackedTime)
-       assert.Equal(t, tt.Time, int64(3661))
+       assert.Equal(t, int64(3661), tt.Time)
 
        comment := AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddTimeManual, PosterID: 3, IssueID: 1}).(*Comment)
        assert.Equal(t, comment.Content, "1h 1min 1s")
@@ -36,7 +41,7 @@ func TestGetTrackedTimes(t *testing.T) {
        times, err := GetTrackedTimes(FindTrackedTimesOptions{IssueID: 1})
        assert.NoError(t, err)
        assert.Len(t, times, 1)
-       assert.Equal(t, times[0].Time, int64(400))
+       assert.Equal(t, int64(400), times[0].Time)
 
        times, err = GetTrackedTimes(FindTrackedTimesOptions{IssueID: -1})
        assert.NoError(t, err)
@@ -45,8 +50,8 @@ func TestGetTrackedTimes(t *testing.T) {
        // by User
        times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 1})
        assert.NoError(t, err)
-       assert.Len(t, times, 1)
-       assert.Equal(t, times[0].Time, int64(400))
+       assert.Len(t, times, 3)
+       assert.Equal(t, int64(400), times[0].Time)
 
        times, err = GetTrackedTimes(FindTrackedTimesOptions{UserID: 3})
        assert.NoError(t, err)
@@ -55,15 +60,15 @@ func TestGetTrackedTimes(t *testing.T) {
        // by Repo
        times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 2})
        assert.NoError(t, err)
-       assert.Len(t, times, 1)
-       assert.Equal(t, times[0].Time, int64(1))
+       assert.Len(t, times, 3)
+       assert.Equal(t, int64(1), times[0].Time)
        issue, err := GetIssueByID(times[0].IssueID)
        assert.NoError(t, err)
        assert.Equal(t, issue.RepoID, int64(2))
 
        times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 1})
        assert.NoError(t, err)
-       assert.Len(t, times, 4)
+       assert.Len(t, times, 5)
 
        times, err = GetTrackedTimes(FindTrackedTimesOptions{RepositoryID: 10})
        assert.NoError(t, err)
@@ -83,10 +88,15 @@ func TestTotalTimes(t *testing.T) {
 
        total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 2})
        assert.NoError(t, err)
-       assert.Len(t, total, 1)
+       assert.Len(t, total, 2)
        for user, time := range total {
-               assert.Equal(t, int64(2), user.ID)
-               assert.Equal(t, "1h 1min 2s", time)
+               if user.ID == 2 {
+                       assert.Equal(t, "1h 1min 2s", time)
+               } else if user.ID == 1 {
+                       assert.Equal(t, "20s", time)
+               } else {
+                       assert.Error(t, assert.AnError)
+               }
        }
 
        total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 5})
@@ -99,5 +109,5 @@ func TestTotalTimes(t *testing.T) {
 
        total, err = TotalTimes(FindTrackedTimesOptions{IssueID: 4})
        assert.NoError(t, err)
-       assert.Len(t, total, 0)
+       assert.Len(t, total, 2)
 }
index 00d836695f190e32f97968002d2dae88b94c8cff..e8bb3f16d460f7376dfd4bf967b722c3285f82ef 100644 (file)
@@ -286,6 +286,8 @@ var migrations = []Migration{
        NewMigration("Remove authentication credentials from stored URL", sanitizeOriginalURL),
        // v115 -> v116
        NewMigration("add user_id prefix to existing user avatar name", renameExistingUserAvatarName),
+       // v116 -> v117
+       NewMigration("Extend TrackedTimes", extendTrackedTimes),
 }
 
 // Migrate database to current version
diff --git a/models/migrations/v116.go b/models/migrations/v116.go
new file mode 100644 (file)
index 0000000..6587d02
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+       "code.gitea.io/gitea/models"
+
+       "xorm.io/xorm"
+)
+
+func extendTrackedTimes(x *xorm.Engine) error {
+       sess := x.NewSession()
+       defer sess.Close()
+
+       if err := sess.Begin(); err != nil {
+               return err
+       }
+
+       if _, err := sess.Exec("DELETE FROM tracked_time WHERE time IS NULL"); err != nil {
+               return err
+       }
+
+       if err := sess.Sync2(new(models.TrackedTime)); err != nil {
+               return err
+       }
+
+       return sess.Commit()
+}
index be90b36267bf30f83116a48a72360f8550d21fce..7e150687ef03eb20dafbaf7c3ec39a22a159ebd3 100644 (file)
@@ -8,23 +8,31 @@ import (
        "time"
 )
 
+// AddTimeOption options for adding time to an issue
+type AddTimeOption struct {
+       // time in seconds
+       // required: true
+       Time int64 `json:"time" binding:"Required"`
+       // swagger:strfmt date-time
+       Created time.Time `json:"created"`
+       // User who spent the time (optional)
+       User string `json:"user_name"`
+}
+
 // TrackedTime worked time for an issue / pr
 type TrackedTime struct {
        ID int64 `json:"id"`
        // swagger:strfmt date-time
        Created time.Time `json:"created"`
        // Time in seconds
-       Time    int64 `json:"time"`
-       UserID  int64 `json:"user_id"`
-       IssueID int64 `json:"issue_id"`
+       Time int64 `json:"time"`
+       // deprecated (only for backwards compatibility)
+       UserID   int64  `json:"user_id"`
+       UserName string `json:"user_name"`
+       // deprecated (only for backwards compatibility)
+       IssueID int64  `json:"issue_id"`
+       Issue   *Issue `json:"issue"`
 }
 
-// TrackedTimes represent a list of tracked times
-type TrackedTimes []*TrackedTime
-
-// AddTimeOption options for adding time to an issue
-type AddTimeOption struct {
-       // time in seconds
-       // required: true
-       Time int64 `json:"time" binding:"Required"`
-}
+// TrackedTimeList represents a list of tracked times
+type TrackedTimeList []*TrackedTime
index c9416e727a2b86ee3529cd92b039edde8b3941ca..691190427148d78a1b34c50cf4a811b339343041 100644 (file)
@@ -960,6 +960,7 @@ issues.add_time = Manually Add Time
 issues.add_time_short = Add Time
 issues.add_time_cancel = Cancel
 issues.add_time_history = `added spent time %s`
+issues.del_time_history= `deleted spent time %s`
 issues.add_time_hours = Hours
 issues.add_time_minutes = Minutes
 issues.add_time_sum_to_small = No time was entered.
index c2f019eb41e528a048332b2ad5a12ae7b830dd22..0bb5320b1602171883eb4967431d60c8bb56c170 100644 (file)
@@ -687,8 +687,11 @@ func RegisterRoutes(m *macaron.Macaron) {
                                                        m.Delete("/:id", reqToken(), repo.DeleteIssueLabel)
                                                })
                                                m.Group("/times", func() {
-                                                       m.Combo("").Get(repo.ListTrackedTimes).
-                                                               Post(reqToken(), bind(api.AddTimeOption{}), repo.AddTime)
+                                                       m.Combo("", reqToken()).
+                                                               Get(repo.ListTrackedTimes).
+                                                               Post(bind(api.AddTimeOption{}), repo.AddTime).
+                                                               Delete(repo.ResetIssueTime)
+                                                       m.Delete("/:id", reqToken(), repo.DeleteTime)
                                                })
                                                m.Combo("/deadline").Post(reqToken(), bind(api.EditDeadlineOption{}), repo.UpdateIssueDeadline)
                                                m.Group("/stopwatch", func() {
index 0d3ca5c177dabf53235973f89634f2f2558b7e5c..80830e2fe6f71d9054241607115502f2241da504 100644 (file)
@@ -6,23 +6,16 @@ package repo
 
 import (
        "net/http"
+       "time"
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/context"
        api "code.gitea.io/gitea/modules/structs"
 )
 
-func trackedTimesToAPIFormat(trackedTimes []*models.TrackedTime) []*api.TrackedTime {
-       apiTrackedTimes := make([]*api.TrackedTime, len(trackedTimes))
-       for i, trackedTime := range trackedTimes {
-               apiTrackedTimes[i] = trackedTime.APIFormat()
-       }
-       return apiTrackedTimes
-}
-
 // ListTrackedTimes list all the tracked times of an issue
 func ListTrackedTimes(ctx *context.APIContext) {
-       // swagger:operation GET /repos/{owner}/{repo}/issues/{id}/times issue issueTrackedTimes
+       // swagger:operation GET /repos/{owner}/{repo}/issues/{index}/times issue issueTrackedTimes
        // ---
        // summary: List an issue's tracked times
        // produces:
@@ -38,7 +31,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
        //   description: name of the repo
        //   type: string
        //   required: true
-       // - name: id
+       // - name: index
        //   in: path
        //   description: index of the issue
        //   type: integer
@@ -64,20 +57,32 @@ func ListTrackedTimes(ctx *context.APIContext) {
                return
        }
 
-       trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{IssueID: issue.ID})
+       opts := models.FindTrackedTimesOptions{
+               RepositoryID: ctx.Repo.Repository.ID,
+               IssueID:      issue.ID,
+       }
+
+       if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin {
+               opts.UserID = ctx.User.ID
+       }
+
+       trackedTimes, err := models.GetTrackedTimes(opts)
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByIssue", err)
+               ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
+               return
+       }
+       if err = trackedTimes.LoadAttributes(); err != nil {
+               ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
                return
        }
-       apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
-       ctx.JSON(http.StatusOK, &apiTrackedTimes)
+       ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
 }
 
-// AddTime adds time manual to the given issue
+// AddTime add time manual to the given issue
 func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
-       // swagger:operation Post /repos/{owner}/{repo}/issues/{id}/times issue issueAddTime
+       // swagger:operation Post /repos/{owner}/{repo}/issues/{index}/times issue issueAddTime
        // ---
-       // summary: Add tracked time to a issue
+       // summary: Add tracked time to a issue
        // consumes:
        // - application/json
        // produces:
@@ -93,9 +98,9 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
        //   description: name of the repo
        //   type: string
        //   required: true
-       // - name: id
+       // - name: index
        //   in: path
-       //   description: index of the issue to add tracked time to
+       //   description: index of the issue
        //   type: integer
        //   format: int64
        //   required: true
@@ -129,14 +134,179 @@ func AddTime(ctx *context.APIContext, form api.AddTimeOption) {
                ctx.Status(http.StatusForbidden)
                return
        }
-       trackedTime, err := models.AddTime(ctx.User, issue, form.Time)
+
+       user := ctx.User
+       if form.User != "" {
+               if (ctx.IsUserRepoAdmin() && ctx.User.Name != form.User) || ctx.User.IsAdmin {
+                       //allow only RepoAdmin, Admin and User to add time
+                       user, err = models.GetUserByName(form.User)
+                       if err != nil {
+                               ctx.Error(500, "GetUserByName", err)
+                       }
+               }
+       }
+
+       created := time.Time{}
+       if !form.Created.IsZero() {
+               created = form.Created
+       }
+
+       trackedTime, err := models.AddTime(user, issue, form.Time, created)
        if err != nil {
                ctx.Error(http.StatusInternalServerError, "AddTime", err)
                return
        }
+       if err = trackedTime.LoadAttributes(); err != nil {
+               ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
+               return
+       }
        ctx.JSON(http.StatusOK, trackedTime.APIFormat())
 }
 
+// ResetIssueTime reset time manual to the given issue
+func ResetIssueTime(ctx *context.APIContext) {
+       // swagger:operation Delete /repos/{owner}/{repo}/issues/{index}/times issue issueResetTime
+       // ---
+       // summary: Reset a tracked time of an issue
+       // consumes:
+       // - application/json
+       // produces:
+       // - application/json
+       // parameters:
+       // - name: owner
+       //   in: path
+       //   description: owner of the repo
+       //   type: string
+       //   required: true
+       // - name: repo
+       //   in: path
+       //   description: name of the repo
+       //   type: string
+       //   required: true
+       // - name: index
+       //   in: path
+       //   description: index of the issue to add tracked time to
+       //   type: integer
+       //   format: int64
+       //   required: true
+       // responses:
+       //   "204":
+       //     "$ref": "#/responses/empty"
+       //   "400":
+       //     "$ref": "#/responses/error"
+       //   "403":
+       //     "$ref": "#/responses/error"
+
+       issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
+       if err != nil {
+               if models.IsErrIssueNotExist(err) {
+                       ctx.NotFound(err)
+               } else {
+                       ctx.Error(500, "GetIssueByIndex", err)
+               }
+               return
+       }
+
+       if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
+               if !ctx.Repo.Repository.IsTimetrackerEnabled() {
+                       ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
+                       return
+               }
+               ctx.Status(403)
+               return
+       }
+
+       err = models.DeleteIssueUserTimes(issue, ctx.User)
+       if err != nil {
+               if models.IsErrNotExist(err) {
+                       ctx.Error(404, "DeleteIssueUserTimes", err)
+               } else {
+                       ctx.Error(500, "DeleteIssueUserTimes", err)
+               }
+               return
+       }
+       ctx.Status(204)
+}
+
+// DeleteTime delete a specific time by id
+func DeleteTime(ctx *context.APIContext) {
+       // swagger:operation Delete /repos/{owner}/{repo}/issues/{index}/times/{id} issue issueDeleteTime
+       // ---
+       // summary: Delete specific tracked time
+       // consumes:
+       // - application/json
+       // produces:
+       // - application/json
+       // parameters:
+       // - name: owner
+       //   in: path
+       //   description: owner of the repo
+       //   type: string
+       //   required: true
+       // - name: repo
+       //   in: path
+       //   description: name of the repo
+       //   type: string
+       //   required: true
+       // - name: index
+       //   in: path
+       //   description: index of the issue
+       //   type: integer
+       //   format: int64
+       //   required: true
+       // - name: id
+       //   in: path
+       //   description: id of time to delete
+       //   type: integer
+       //   format: int64
+       //   required: true
+       // responses:
+       //   "204":
+       //     "$ref": "#/responses/empty"
+       //   "400":
+       //     "$ref": "#/responses/error"
+       //   "403":
+       //     "$ref": "#/responses/error"
+
+       issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
+       if err != nil {
+               if models.IsErrIssueNotExist(err) {
+                       ctx.NotFound(err)
+               } else {
+                       ctx.Error(500, "GetIssueByIndex", err)
+               }
+               return
+       }
+
+       if !ctx.Repo.CanUseTimetracker(issue, ctx.User) {
+               if !ctx.Repo.Repository.IsTimetrackerEnabled() {
+                       ctx.JSON(400, struct{ Message string }{Message: "time tracking disabled"})
+                       return
+               }
+               ctx.Status(403)
+               return
+       }
+
+       time, err := models.GetTrackedTimeByID(ctx.ParamsInt64(":id"))
+       if err != nil {
+               ctx.Error(500, "GetTrackedTimeByID", err)
+               return
+       }
+
+       if !ctx.User.IsAdmin && time.UserID != ctx.User.ID {
+               //Only Admin and User itself can delete their time
+               ctx.Status(403)
+               return
+       }
+
+       err = models.DeleteTime(time)
+       if err != nil {
+               ctx.Error(500, "DeleteTime", err)
+               return
+       }
+       ctx.Status(204)
+}
+
 // ListTrackedTimesByUser  lists all tracked times of the user
 func ListTrackedTimesByUser(ctx *context.APIContext) {
        // swagger:operation GET /repos/{owner}/{repo}/times/{user} user userTrackedTimes
@@ -187,11 +357,14 @@ func ListTrackedTimesByUser(ctx *context.APIContext) {
                UserID:       user.ID,
                RepositoryID: ctx.Repo.Repository.ID})
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByUser", err)
+               ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
                return
        }
-       apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
-       ctx.JSON(http.StatusOK, &apiTrackedTimes)
+       if err = trackedTimes.LoadAttributes(); err != nil {
+               ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
+               return
+       }
+       ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
 }
 
 // ListTrackedTimesByRepository lists all tracked times of the repository
@@ -222,14 +395,25 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
                ctx.Error(http.StatusBadRequest, "", "time tracking disabled")
                return
        }
-       trackedTimes, err := models.GetTrackedTimes(models.FindTrackedTimesOptions{
-               RepositoryID: ctx.Repo.Repository.ID})
+
+       opts := models.FindTrackedTimesOptions{
+               RepositoryID: ctx.Repo.Repository.ID,
+       }
+
+       if !ctx.IsUserRepoAdmin() && !ctx.User.IsAdmin {
+               opts.UserID = ctx.User.ID
+       }
+
+       trackedTimes, err := models.GetTrackedTimes(opts)
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByUser", err)
+               ctx.Error(http.StatusInternalServerError, "GetTrackedTimes", err)
                return
        }
-       apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
-       ctx.JSON(http.StatusOK, &apiTrackedTimes)
+       if err = trackedTimes.LoadAttributes(); err != nil {
+               ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
+               return
+       }
+       ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
 }
 
 // ListMyTrackedTimes lists all tracked times of the current user
@@ -248,6 +432,9 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
                ctx.Error(http.StatusInternalServerError, "GetTrackedTimesByUser", err)
                return
        }
-       apiTrackedTimes := trackedTimesToAPIFormat(trackedTimes)
-       ctx.JSON(http.StatusOK, &apiTrackedTimes)
+       if err = trackedTimes.LoadAttributes(); err != nil {
+               ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
+               return
+       }
+       ctx.JSON(http.StatusOK, trackedTimes.APIFormat())
 }
index 05cf13793112d99db7e916d0da752aa17c2fe6f8..0f711bc7344c2d87ac7c589579ce8451590dd8f8 100644 (file)
@@ -39,7 +39,7 @@ func AddTimeManually(c *context.Context, form auth.AddTimeManuallyForm) {
                return
        }
 
-       if _, err := models.AddTime(c.User, issue, int64(total.Seconds())); err != nil {
+       if _, err := models.AddTime(c.User, issue, int64(total.Seconds()), time.Now()); err != nil {
                c.ServerError("AddTime", err)
                return
        }
index d8a90caa858ec42a9671f012d4ba450f07bb15b8..170379bb7f8afe164a4dbfc442451bccc6cd7191 100644 (file)
@@ -6,7 +6,8 @@
         5 = COMMENT_REF, 6 = PULL_REF, 7 = COMMENT_LABEL, 12 = START_TRACKING,
         13 = STOP_TRACKING, 14 = ADD_TIME_MANUAL, 16 = ADDED_DEADLINE, 17 = MODIFIED_DEADLINE,
         18 = REMOVED_DEADLINE, 19 = ADD_DEPENDENCY, 20 = REMOVE_DEPENDENCY, 21 = CODE,
-        22 = REVIEW, 23 = ISSUE_LOCKED, 24 = ISSUE_UNLOCKED, 25 = TARGET_BRANCH_CHANGED -->
+        22 = REVIEW, 23 = ISSUE_LOCKED, 24 = ISSUE_UNLOCKED, 25 = TARGET_BRANCH_CHANGED,
+        26 = DELETE_TIME_MANUAL -->
        {{if eq .Type 0}}
                <div class="comment" id="{{.HashTag}}">
                {{if .OriginalAuthor }}
                        {{$.i18n.Tr "repo.pulls.change_target_branch_at" (.OldRef|Escape) (.NewRef|Escape) $createdStr | Safe}}
                        </span>
                </div>
+       {{else if eq .Type 26}}
+               <div class="event" id="{{.HashTag}}">
+                       <span class="octicon octicon-primitive-dot"></span>
+                       <a class="ui avatar image" href="{{.Poster.HomeLink}}">
+                               <img src="{{.Poster.RelAvatarLink}}">
+                       </a>
+                       <span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.GetDisplayName}}</a> {{$.i18n.Tr "repo.issues.del_time_history"  $createdStr | Safe}}</span>
+                       <div class="detail">
+                               <span class="octicon octicon-clock"></span>
+                               <span class="text grey">{{.Content}}</span>
+                       </div>
+               </div>
        {{end}}
 {{end}}
index c4fa1f3112a615c540d8b78658155423b88c7cfc..7f08b939cc96324d21c8b4c040497684a18bae82 100644 (file)
         }
       }
     },
-    "/repos/{owner}/{repo}/issues/{id}/times": {
-      "get": {
-        "produces": [
-          "application/json"
-        ],
-        "tags": [
-          "issue"
-        ],
-        "summary": "List an issue's tracked times",
-        "operationId": "issueTrackedTimes",
-        "parameters": [
-          {
-            "type": "string",
-            "description": "owner of the repo",
-            "name": "owner",
-            "in": "path",
-            "required": true
-          },
-          {
-            "type": "string",
-            "description": "name of the repo",
-            "name": "repo",
-            "in": "path",
-            "required": true
-          },
-          {
-            "type": "integer",
-            "format": "int64",
-            "description": "index of the issue",
-            "name": "id",
-            "in": "path",
-            "required": true
-          }
-        ],
-        "responses": {
-          "200": {
-            "$ref": "#/responses/TrackedTimeList"
-          },
-          "404": {
-            "$ref": "#/responses/notFound"
-          }
-        }
-      },
-      "post": {
-        "consumes": [
-          "application/json"
-        ],
-        "produces": [
-          "application/json"
-        ],
-        "tags": [
-          "issue"
-        ],
-        "summary": "Add a tracked time to a issue",
-        "operationId": "issueAddTime",
-        "parameters": [
-          {
-            "type": "string",
-            "description": "owner of the repo",
-            "name": "owner",
-            "in": "path",
-            "required": true
-          },
-          {
-            "type": "string",
-            "description": "name of the repo",
-            "name": "repo",
-            "in": "path",
-            "required": true
-          },
-          {
-            "type": "integer",
-            "format": "int64",
-            "description": "index of the issue to add tracked time to",
-            "name": "id",
-            "in": "path",
-            "required": true
-          },
-          {
-            "name": "body",
-            "in": "body",
-            "schema": {
-              "$ref": "#/definitions/AddTimeOption"
-            }
-          }
-        ],
-        "responses": {
-          "200": {
-            "$ref": "#/responses/TrackedTime"
-          },
-          "400": {
-            "$ref": "#/responses/error"
-          },
-          "403": {
-            "$ref": "#/responses/forbidden"
-          }
-        }
-      }
-    },
     "/repos/{owner}/{repo}/issues/{index}": {
       "get": {
         "produces": [
         }
       }
     },
+    "/repos/{owner}/{repo}/issues/{index}/times": {
+      "get": {
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "issue"
+        ],
+        "summary": "List an issue's tracked times",
+        "operationId": "issueTrackedTimes",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the issue",
+            "name": "index",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "200": {
+            "$ref": "#/responses/TrackedTimeList"
+          },
+          "404": {
+            "$ref": "#/responses/notFound"
+          }
+        }
+      },
+      "post": {
+        "consumes": [
+          "application/json"
+        ],
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "issue"
+        ],
+        "summary": "Add tracked time to a issue",
+        "operationId": "issueAddTime",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the issue",
+            "name": "index",
+            "in": "path",
+            "required": true
+          },
+          {
+            "name": "body",
+            "in": "body",
+            "schema": {
+              "$ref": "#/definitions/AddTimeOption"
+            }
+          }
+        ],
+        "responses": {
+          "200": {
+            "$ref": "#/responses/TrackedTime"
+          },
+          "400": {
+            "$ref": "#/responses/error"
+          },
+          "403": {
+            "$ref": "#/responses/forbidden"
+          }
+        }
+      },
+      "delete": {
+        "consumes": [
+          "application/json"
+        ],
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "issue"
+        ],
+        "summary": "Reset a tracked time of an issue",
+        "operationId": "issueResetTime",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the issue to add tracked time to",
+            "name": "index",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "204": {
+            "$ref": "#/responses/empty"
+          },
+          "400": {
+            "$ref": "#/responses/error"
+          },
+          "403": {
+            "$ref": "#/responses/error"
+          }
+        }
+      }
+    },
+    "/repos/{owner}/{repo}/issues/{index}/times/{id}": {
+      "delete": {
+        "consumes": [
+          "application/json"
+        ],
+        "produces": [
+          "application/json"
+        ],
+        "tags": [
+          "issue"
+        ],
+        "summary": "Delete specific tracked time",
+        "operationId": "issueDeleteTime",
+        "parameters": [
+          {
+            "type": "string",
+            "description": "owner of the repo",
+            "name": "owner",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "string",
+            "description": "name of the repo",
+            "name": "repo",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "index of the issue",
+            "name": "index",
+            "in": "path",
+            "required": true
+          },
+          {
+            "type": "integer",
+            "format": "int64",
+            "description": "id of time to delete",
+            "name": "id",
+            "in": "path",
+            "required": true
+          }
+        ],
+        "responses": {
+          "204": {
+            "$ref": "#/responses/empty"
+          },
+          "400": {
+            "$ref": "#/responses/error"
+          },
+          "403": {
+            "$ref": "#/responses/error"
+          }
+        }
+      }
+    },
     "/repos/{owner}/{repo}/keys": {
       "get": {
         "produces": [
         "time"
       ],
       "properties": {
+        "created": {
+          "type": "string",
+          "format": "date-time",
+          "x-go-name": "Created"
+        },
         "time": {
           "description": "time in seconds",
           "type": "integer",
           "format": "int64",
           "x-go-name": "Time"
+        },
+        "user_name": {
+          "description": "User who spent the time (optional)",
+          "type": "string",
+          "x-go-name": "User"
         }
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"
           "format": "int64",
           "x-go-name": "ID"
         },
+        "issue": {
+          "$ref": "#/definitions/Issue"
+        },
         "issue_id": {
+          "description": "deprecated (only for backwards compatibility)",
           "type": "integer",
           "format": "int64",
           "x-go-name": "IssueID"
           "x-go-name": "Time"
         },
         "user_id": {
+          "description": "deprecated (only for backwards compatibility)",
           "type": "integer",
           "format": "int64",
           "x-go-name": "UserID"
+        },
+        "user_name": {
+          "type": "string",
+          "x-go-name": "UserName"
         }
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"