Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Extend times API #9200

Merged
merged 114 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
114 commits
Select commit Hold shift + click to select a range
15cba15
git ignore fuse tmp files
6543 Dec 2, 2019
81e5ea6
Deprecate old "times" API
6543 Nov 28, 2019
e14d62b
add new times api struct
6543 Nov 28, 2019
7946829
update "GET /user/times"
6543 Dec 2, 2019
d8c0c4e
update "GET /repos/{owner}/{repo}/times"
6543 Dec 2, 2019
7601790
corect swagger description (id -> index)
6543 Dec 2, 2019
7e5b5b0
update time POST
6543 Dec 2, 2019
c06805d
add reset func
6543 Dec 2, 2019
261222d
AddTimeOption -> EditTimeOption
6543 Dec 2, 2019
e5ece6b
make delete work + forbidden to remove more time on issue than you have
6543 Dec 2, 2019
88c6147
return full issue instead of issue_index
6543 Dec 6, 2019
a64cc39
dont Deprecate TimesByUser
6543 Dec 8, 2019
9d71a37
new Delete func & optimize
6543 Dec 8, 2019
b6239c0
add Created to EditTimeOption
6543 Dec 8, 2019
5bb0b2c
add CommentTypeDeleteTimeManual
6543 Dec 8, 2019
8dfcd5e
use CommentTypeDeleteTimeManual
6543 Dec 9, 2019
9dda7db
add DELETE .../times/{id} endpoint
6543 Dec 9, 2019
855002d
cleanup & co
6543 Dec 9, 2019
979a13c
do ToDo
6543 Dec 9, 2019
e0facee
allow RepoAdmin, Admin and User to add tracked time for User
6543 Dec 9, 2019
8720395
Merge branch 'master' into api-times-refactor
6543 Dec 9, 2019
754b813
fix lint
6543 Dec 9, 2019
3892d0b
Merge branch 'master' into api-times-refactor
6543 Dec 16, 2019
ffa9ea3
fix thing after merge
6543 Dec 16, 2019
cd77248
correct comments
6543 Dec 16, 2019
4a37fb8
code format
6543 Dec 16, 2019
aa4162e
TEST related things
6543 Dec 16, 2019
140946a
add TestAPIGetTrackedTimes
6543 Dec 16, 2019
7ca2a6a
Merge branch 'master' into api-times-refactor
6543 Dec 16, 2019
59f8a0e
DELETE send 204 on succes
6543 Dec 16, 2019
c4b20ef
extend unit test + fixtures
6543 Dec 16, 2019
6726e3c
extend unit test + fixtures
6543 Dec 17, 2019
3fa89a5
add TESTs for addition and deletion
6543 Dec 17, 2019
fbfbf73
fix TEST: time compare with Unix()
6543 Dec 17, 2019
e83f743
fix TestAPIDeleteTrackedTime
6543 Dec 17, 2019
4a597db
check if tracked time exist on issue befor reset & more
6543 Dec 17, 2019
76eb17e
deactivate test for DEL specific TrackedTime:
6543 Dec 17, 2019
16cef4a
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
64f6a10
Print PASS for "test" like "test-sqlite" does
6543 Dec 17, 2019
18d65bf
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
16b5689
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
68474d1
Revert "git ignore fuse tmp files"
6543 Dec 17, 2019
33030cc
add comment (only for backwards compatibility)
6543 Dec 17, 2019
e1b9a4f
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
81d039e
swagger got it
6543 Dec 17, 2019
6a62537
add suggestions @lunny
6543 Dec 17, 2019
0f666cb
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
a8bf5c7
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
f9b4114
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
cf9b9d4
Merge branch 'master' into api-times-refactor
6543 Dec 17, 2019
f187d02
Merge branch 'master' into api-times-refactor
6543 Dec 18, 2019
f0008d7
Merge branch 'master' into api-times-refactor
6543 Dec 18, 2019
627f9a8
Merge branch 'master' into api-times-refactor
6543 Dec 18, 2019
220879e
Merge branch 'master' into api-times-refactor
6543 Dec 19, 2019
de4e79d
Merge branch 'master' into api-times-refactor
6543 Dec 19, 2019
898f515
Merge branch 'master' into api-times-refactor
6543 Dec 19, 2019
a6d1176
Merge branch 'master' into api-times-refactor
6543 Dec 20, 2019
df56c56
add suggestion
6543 Dec 20, 2019
c805fff
Merge branch 'master' into api-times-refactor
6543 Dec 20, 2019
f2c6efe
use Engine opt for deleteTimes to make it reusable
6543 Dec 21, 2019
e7daebf
Merge branch 'master' into api-times-refactor
6543 Dec 21, 2019
dc626b6
format code
6543 Dec 21, 2019
59ac977
handle error
6543 Dec 21, 2019
21b7754
Merge branch 'master' into api-times-refactor
6543 Dec 22, 2019
0771fff
Merge branch 'master' into api-times-refactor
6543 Dec 22, 2019
2153fbe
Merge branch 'master' into api-times-refactor
6543 Dec 23, 2019
8a0d8a0
spell corect
6543 Dec 23, 2019
46ab8e9
only one AddTime()
6543 Dec 23, 2019
318e238
add attribues
6543 Dec 23, 2019
25474e7
add functions for a single session
6543 Dec 23, 2019
9661f85
use same session
6543 Dec 23, 2019
8a541b1
spell corect
6543 Dec 23, 2019
398707c
Merge branch 'master' into api-times-refactor
6543 Dec 23, 2019
4562fcf
dont delete times - mark as deleted
6543 Dec 23, 2019
b540212
old functions ignor deleted
6543 Dec 23, 2019
cfd79fb
make sure no nil pointer exeption
6543 Dec 23, 2019
a02d80f
handle deleted flag correct
6543 Dec 23, 2019
18f5aa6
fmt
6543 Dec 23, 2019
6145a0f
bool false is 0
6543 Dec 23, 2019
71677da
use xorm builder to support all DBs
6543 Dec 24, 2019
1a93cb4
Merge branch 'master' into api-times-refactor
6543 Dec 24, 2019
5d707d8
dont forget sess.Begin()
6543 Dec 24, 2019
887df7d
Merge branch 'master' into api-times-refactor
6543 Dec 24, 2019
ed57d8e
use createComment
6543 Dec 24, 2019
fb192bf
more specific
6543 Dec 24, 2019
c409f9e
Merge branch 'master' into api-times-refactor
6543 Dec 25, 2019
9a0517c
Update models/issue_tracked_time.go
6543 Dec 25, 2019
c0244ab
code format
6543 Dec 25, 2019
8f1dc9a
Merge branch 'master' into api-times-refactor
6543 Dec 25, 2019
bca58d4
move to right place
6543 Dec 25, 2019
cbe5075
Merge branch 'master' into api-times-refactor
6543 Dec 25, 2019
92186f2
Merge branch 'master' into api-times-refactor
6543 Dec 25, 2019
43dbc59
Merge branch 'master' into api-times-refactor
6543 Dec 26, 2019
60ab62c
Apply suggestions from code review
6543 Dec 26, 2019
69f2fa5
Update models/issue_list.go
6543 Dec 26, 2019
2b38708
Merge branch 'master' into api-times-refactor
6543 Dec 26, 2019
7f2876a
optimize deleteTimes()
6543 Dec 26, 2019
34a90d8
not working jet
6543 Dec 26, 2019
f6d8e6d
make it work
6543 Dec 26, 2019
ed1f495
update gitignore after code refactor
6543 Dec 26, 2019
d6c35bb
workaround by @zeripath
6543 Dec 26, 2019
16b5ca6
add Comment to do this better in future
6543 Dec 26, 2019
8f137db
revert "add PASS to -make test-"
6543 Dec 26, 2019
050ae6f
working solution ... for now
6543 Dec 26, 2019
7f9d174
Merge branch 'master' into api-times-refactor
6543 Dec 27, 2019
1b14ac9
revert suggestion
6543 Dec 27, 2019
f07a761
give it a try
6543 Dec 27, 2019
0366115
Merge branch 'master' into api-times-refactor
lunny Dec 27, 2019
a12aac9
Merge branch 'master' into api-times-refactor
zeripath Dec 27, 2019
4438401
Merge branch 'master' into api-times-refactor
6543 Dec 27, 2019
0419d99
Apply suggestions from code review
6543 Dec 27, 2019
c4bc46e
prepare for conflict resolve
6543 Dec 27, 2019
753b1fc
Merge branch 'master' into api-times-refactor
6543 Dec 27, 2019
36cc104
Merge branch 'master' into api-times-refactor
zeripath Dec 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions integrations/api_issue_tracked_time_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
41 changes: 39 additions & 2 deletions models/fixtures/tracked_time.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,68 @@
issue_id: 1
time: 400
created_unix: 946684800
deleted: false

-
id: 2
user_id: 2
issue_id: 2
time: 3661
created_unix: 946684801
deleted: false

-
id: 3
user_id: 2
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
4 changes: 3 additions & 1 deletion models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions models/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
2 changes: 1 addition & 1 deletion models/issue_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
4 changes: 3 additions & 1 deletion models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6543 marked this conversation as resolved.
Show resolved Hide resolved
"xorm.io/xorm"
)

Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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").
Expand Down
4 changes: 2 additions & 2 deletions models/issue_milestone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion models/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading