[API] Extend times API (#9200)
Extensively extend the times API. close #8833; close #8513; close #8559
This commit is contained in:
		
							parent
							
								
									0bcf644da4
								
							
						
					
					
						commit
						f2d03cda96
					
				
					 19 changed files with 916 additions and 194 deletions
				
			
		
							
								
								
									
										109
									
								
								integrations/api_issue_tracked_time_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								integrations/api_issue_tracked_time_test.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -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()) | ||||
| } | ||||
|  | @ -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 | ||||
|  | @ -18,17 +20,52 @@ | |||
|   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 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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"). | ||||
|  |  | |||
|  | @ -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) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -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"). | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -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) { | ||||
|  |  | |||
|  | @ -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) | ||||
| } | ||||
| 
 | ||||
| // 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, | ||||
| // 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() (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, | ||||
| 	} | ||||
| 	if _, err := x.Insert(tt); err != nil { | ||||
| 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 := issue.loadRepo(x); err != nil { | ||||
| 
 | ||||
| 	t, err := addTime(sess, user, issue, amount, created) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if _, err := CreateComment(&CreateCommentOptions{ | ||||
| 
 | ||||
| 	if err := issue.loadRepo(sess); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 
 | ||||
| 	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 | ||||
| } | ||||
|  |  | |||
|  | @ -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) | ||||
| } | ||||
|  |  | |||
|  | @ -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
 | ||||
|  |  | |||
							
								
								
									
										30
									
								
								models/migrations/v116.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								models/migrations/v116.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -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() | ||||
| } | ||||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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. | ||||
|  |  | |||
|  | @ -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() { | ||||
|  |  | |||
|  | @ -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 | ||||
| 	} | ||||
| 	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()) | ||||
| } | ||||
| 
 | ||||
| // 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 a 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()) | ||||
| } | ||||
|  |  | |||
|  | @ -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 | ||||
| 	} | ||||
|  |  | |||
|  | @ -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 }} | ||||
|  | @ -421,5 +422,17 @@ | |||
| 			{{$.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}} | ||||
|  |  | |||
|  | @ -3242,105 +3242,6 @@ | |||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/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": [ | ||||
|  | @ -4425,6 +4326,211 @@ | |||
|         } | ||||
|       } | ||||
|     }, | ||||
|     "/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": [ | ||||
|  | @ -8047,11 +8153,21 @@ | |||
|         "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" | ||||
|  | @ -11283,7 +11399,11 @@ | |||
|           "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" | ||||
|  | @ -11295,9 +11415,14 @@ | |||
|           "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" | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue