Skip to content

Commit f6b2901

Browse files
bhalbrightzeripath
authored andcommitted
Add /milestones endpoint (#8733)
Create a /milestones endpoint which basically serves as a dashboard view for milestones, very similar to the /issues or /pulls page. Closes #8232
1 parent 7cc1674 commit f6b2901

File tree

14 files changed

+568
-7
lines changed

14 files changed

+568
-7
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ DEFAULT_ALLOW_ONLY_CONTRIBUTORS_TO_TRACK_TIME = true
511511
NO_REPLY_ADDRESS = noreply.%(DOMAIN)s
512512
; Show Registration button
513513
SHOW_REGISTRATION_BUTTON = true
514+
; Show milestones dashboard page - a view of all the user's milestones
515+
SHOW_MILESTONES_DASHBOARD_PAGE = true
514516
; Default value for AutoWatchNewRepos
515517
; When adding a repo to a team or creating a new repo all team members will watch the
516518
; repo automatically if enabled

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ relation to port exhaustion.
310310
- `EMAIL_DOMAIN_WHITELIST`: **\<empty\>**: If non-empty, list of domain names that can only be used to register
311311
on this instance.
312312
- `SHOW_REGISTRATION_BUTTON`: **! DISABLE\_REGISTRATION**: Show Registration Button
313+
- `SHOW_MILESTONES_DASHBOARD_PAGE`: **true** Enable this to show the milestones dashboard page - a view of all the user's milestones
313314
- `AUTO_WATCH_NEW_REPOS`: **true**: Enable this to let all organisation users watch new repos when they are created
314315
- `AUTO_WATCH_ON_CHANGES`: **false**: Enable this to make users watch a repository after their first commit to it
315316
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".

integrations/links_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ func testLinksAsUser(userName string, t *testing.T) {
8686
"/pulls?type=your_repositories&repos=[0]&sort=&state=closed",
8787
"/pulls?type=assigned&repos=[0]&sort=&state=closed",
8888
"/pulls?type=created_by&repos=[0]&sort=&state=closed",
89+
"/milestones",
90+
"/milestones?sort=mostcomplete&state=closed",
91+
"/milestones?type=your_repositories&sort=mostcomplete&state=closed",
92+
"/milestones?sort=&repos=[1]&state=closed",
93+
"/milestones?sort=&repos=[1]&state=open",
94+
"/milestones?repos=[0]&sort=mostissues&state=open",
8995
"/notifications",
9096
"/repo/create",
9197
"/repo/migrate",

models/issue_milestone.go

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717

1818
// Milestone represents a milestone of repository.
1919
type Milestone struct {
20-
ID int64 `xorm:"pk autoincr"`
21-
RepoID int64 `xorm:"INDEX"`
20+
ID int64 `xorm:"pk autoincr"`
21+
RepoID int64 `xorm:"INDEX"`
22+
Repo *Repository `xorm:"-"`
2223
Name string
2324
Content string `xorm:"TEXT"`
2425
RenderedContent string `xorm:"-"`
@@ -177,11 +178,38 @@ func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
177178
return nil
178179
}
179180

181+
func (m *Milestone) loadTotalTrackedTime(e Engine) error {
182+
type totalTimesByMilestone struct {
183+
MilestoneID int64
184+
Time int64
185+
}
186+
totalTime := &totalTimesByMilestone{MilestoneID: m.ID}
187+
has, err := e.Table("issue").
188+
Join("INNER", "milestone", "issue.milestone_id = milestone.id").
189+
Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
190+
Select("milestone_id, sum(time) as time").
191+
Where("milestone_id = ?", m.ID).
192+
GroupBy("milestone_id").
193+
Get(totalTime)
194+
if err != nil {
195+
return err
196+
} else if !has {
197+
return nil
198+
}
199+
m.TotalTrackedTime = totalTime.Time
200+
return nil
201+
}
202+
180203
// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
181204
func (milestones MilestoneList) LoadTotalTrackedTimes() error {
182205
return milestones.loadTotalTrackedTimes(x)
183206
}
184207

208+
// LoadTotalTrackedTime loads the tracked time for the milestone
209+
func (m *Milestone) LoadTotalTrackedTime() error {
210+
return m.loadTotalTrackedTime(x)
211+
}
212+
185213
func (milestones MilestoneList) getMilestoneIDs() []int64 {
186214
var ids = make([]int64, 0, len(milestones))
187215
for _, ms := range milestones {
@@ -465,3 +493,78 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
465493
}
466494
return sess.Commit()
467495
}
496+
497+
// CountMilestonesByRepoIDs map from repoIDs to number of milestones matching the options`
498+
func CountMilestonesByRepoIDs(repoIDs []int64, isClosed bool) (map[int64]int64, error) {
499+
sess := x.Where("is_closed = ?", isClosed)
500+
sess.In("repo_id", repoIDs)
501+
502+
countsSlice := make([]*struct {
503+
RepoID int64
504+
Count int64
505+
}, 0, 10)
506+
if err := sess.GroupBy("repo_id").
507+
Select("repo_id AS repo_id, COUNT(*) AS count").
508+
Table("milestone").
509+
Find(&countsSlice); err != nil {
510+
return nil, err
511+
}
512+
513+
countMap := make(map[int64]int64, len(countsSlice))
514+
for _, c := range countsSlice {
515+
countMap[c.RepoID] = c.Count
516+
}
517+
return countMap, nil
518+
}
519+
520+
// GetMilestonesByRepoIDs returns a list of milestones of given repositories and status.
521+
func GetMilestonesByRepoIDs(repoIDs []int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
522+
miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
523+
sess := x.Where("is_closed = ?", isClosed)
524+
sess.In("repo_id", repoIDs)
525+
if page > 0 {
526+
sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
527+
}
528+
529+
switch sortType {
530+
case "furthestduedate":
531+
sess.Desc("deadline_unix")
532+
case "leastcomplete":
533+
sess.Asc("completeness")
534+
case "mostcomplete":
535+
sess.Desc("completeness")
536+
case "leastissues":
537+
sess.Asc("num_issues")
538+
case "mostissues":
539+
sess.Desc("num_issues")
540+
default:
541+
sess.Asc("deadline_unix")
542+
}
543+
return miles, sess.Find(&miles)
544+
}
545+
546+
// MilestonesStats represents milestone statistic information.
547+
type MilestonesStats struct {
548+
OpenCount, ClosedCount int64
549+
}
550+
551+
// GetMilestonesStats returns milestone statistic information for dashboard by given conditions.
552+
func GetMilestonesStats(userRepoIDs []int64) (*MilestonesStats, error) {
553+
var err error
554+
stats := &MilestonesStats{}
555+
556+
stats.OpenCount, err = x.Where("is_closed = ?", false).
557+
And(builder.In("repo_id", userRepoIDs)).
558+
Count(new(Milestone))
559+
if err != nil {
560+
return nil, err
561+
}
562+
stats.ClosedCount, err = x.Where("is_closed = ?", true).
563+
And(builder.In("repo_id", userRepoIDs)).
564+
Count(new(Milestone))
565+
if err != nil {
566+
return nil, err
567+
}
568+
569+
return stats, nil
570+
}

models/issue_milestone_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,88 @@ func TestMilestoneList_LoadTotalTrackedTimes(t *testing.T) {
289289

290290
assert.Equal(t, miles[0].TotalTrackedTime, int64(3662))
291291
}
292+
293+
func TestCountMilestonesByRepoIDs(t *testing.T) {
294+
assert.NoError(t, PrepareTestDatabase())
295+
milestonesCount := func(repoID int64) (int, int) {
296+
repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
297+
return repo.NumOpenMilestones, repo.NumClosedMilestones
298+
}
299+
repo1OpenCount, repo1ClosedCount := milestonesCount(1)
300+
repo2OpenCount, repo2ClosedCount := milestonesCount(2)
301+
302+
openCounts, err := CountMilestonesByRepoIDs([]int64{1, 2}, false)
303+
assert.NoError(t, err)
304+
assert.EqualValues(t, repo1OpenCount, openCounts[1])
305+
assert.EqualValues(t, repo2OpenCount, openCounts[2])
306+
307+
closedCounts, err := CountMilestonesByRepoIDs([]int64{1, 2}, true)
308+
assert.NoError(t, err)
309+
assert.EqualValues(t, repo1ClosedCount, closedCounts[1])
310+
assert.EqualValues(t, repo2ClosedCount, closedCounts[2])
311+
}
312+
313+
func TestGetMilestonesByRepoIDs(t *testing.T) {
314+
assert.NoError(t, PrepareTestDatabase())
315+
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
316+
repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
317+
test := func(sortType string, sortCond func(*Milestone) int) {
318+
for _, page := range []int{0, 1} {
319+
openMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, false, sortType)
320+
assert.NoError(t, err)
321+
assert.Len(t, openMilestones, repo1.NumOpenMilestones+repo2.NumOpenMilestones)
322+
values := make([]int, len(openMilestones))
323+
for i, milestone := range openMilestones {
324+
values[i] = sortCond(milestone)
325+
}
326+
assert.True(t, sort.IntsAreSorted(values))
327+
328+
closedMilestones, err := GetMilestonesByRepoIDs([]int64{repo1.ID, repo2.ID}, page, true, sortType)
329+
assert.NoError(t, err)
330+
assert.Len(t, closedMilestones, repo1.NumClosedMilestones+repo2.NumClosedMilestones)
331+
values = make([]int, len(closedMilestones))
332+
for i, milestone := range closedMilestones {
333+
values[i] = sortCond(milestone)
334+
}
335+
assert.True(t, sort.IntsAreSorted(values))
336+
}
337+
}
338+
test("furthestduedate", func(milestone *Milestone) int {
339+
return -int(milestone.DeadlineUnix)
340+
})
341+
test("leastcomplete", func(milestone *Milestone) int {
342+
return milestone.Completeness
343+
})
344+
test("mostcomplete", func(milestone *Milestone) int {
345+
return -milestone.Completeness
346+
})
347+
test("leastissues", func(milestone *Milestone) int {
348+
return milestone.NumIssues
349+
})
350+
test("mostissues", func(milestone *Milestone) int {
351+
return -milestone.NumIssues
352+
})
353+
test("soonestduedate", func(milestone *Milestone) int {
354+
return int(milestone.DeadlineUnix)
355+
})
356+
}
357+
358+
func TestLoadTotalTrackedTime(t *testing.T) {
359+
assert.NoError(t, PrepareTestDatabase())
360+
milestone := AssertExistsAndLoadBean(t, &Milestone{ID: 1}).(*Milestone)
361+
362+
assert.NoError(t, milestone.LoadTotalTrackedTime())
363+
364+
assert.Equal(t, milestone.TotalTrackedTime, int64(3662))
365+
}
366+
367+
func TestGetMilestonesStats(t *testing.T) {
368+
assert.NoError(t, PrepareTestDatabase())
369+
repo1 := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
370+
repo2 := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
371+
372+
milestoneStats, err := GetMilestonesStats([]int64{repo1.ID, repo2.ID})
373+
assert.NoError(t, err)
374+
assert.EqualValues(t, repo1.NumOpenMilestones+repo2.NumOpenMilestones, milestoneStats.OpenCount)
375+
assert.EqualValues(t, repo1.NumClosedMilestones+repo2.NumClosedMilestones, milestoneStats.ClosedCount)
376+
}

modules/context/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ func Contexter() macaron.Handler {
334334
ctx.Data["IsLandingPageOrganizations"] = setting.LandingPageURL == setting.LandingPageOrganizations
335335

336336
ctx.Data["ShowRegistrationButton"] = setting.Service.ShowRegistrationButton
337+
ctx.Data["ShowMilestonesDashboardPage"] = setting.Service.ShowMilestonesDashboardPage
337338
ctx.Data["ShowFooterBranding"] = setting.ShowFooterBranding
338339
ctx.Data["ShowFooterVersion"] = setting.ShowFooterVersion
339340

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var Service struct {
2121
DisableRegistration bool
2222
AllowOnlyExternalRegistration bool
2323
ShowRegistrationButton bool
24+
ShowMilestonesDashboardPage bool
2425
RequireSignInView bool
2526
EnableNotifyMail bool
2627
EnableBasicAuth bool
@@ -62,6 +63,7 @@ func newService() {
6263
Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
6364
Service.EmailDomainWhitelist = sec.Key("EMAIL_DOMAIN_WHITELIST").Strings(",")
6465
Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!(Service.DisableRegistration || Service.AllowOnlyExternalRegistration))
66+
Service.ShowMilestonesDashboardPage = sec.Key("SHOW_MILESTONES_DASHBOARD_PAGE").MustBool(true)
6567
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
6668
Service.EnableBasicAuth = sec.Key("ENABLE_BASIC_AUTHENTICATION").MustBool(true)
6769
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ forks = Forks
6666
activities = Activities
6767
pull_requests = Pull Requests
6868
issues = Issues
69+
milestones = Milestones
6970

7071
cancel = Cancel
7172
add = Add

routers/routes/routes.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ func RegisterRoutes(m *macaron.Macaron) {
254254
}
255255
}
256256

257+
reqMilestonesDashboardPageEnabled := func(ctx *context.Context) {
258+
if !setting.Service.ShowMilestonesDashboardPage {
259+
ctx.Error(403)
260+
return
261+
}
262+
}
263+
257264
m.Use(user.GetNotificationCount)
258265

259266
// FIXME: not all routes need go through same middlewares.
@@ -276,6 +283,7 @@ func RegisterRoutes(m *macaron.Macaron) {
276283
m.Combo("/install", routers.InstallInit).Get(routers.Install).
277284
Post(bindIgnErr(auth.InstallForm{}), routers.InstallPost)
278285
m.Get("/^:type(issues|pulls)$", reqSignIn, user.Issues)
286+
m.Get("/milestones", reqSignIn, reqMilestonesDashboardPageEnabled, user.Milestones)
279287

280288
// ***** START: User *****
281289
m.Group("/user", func() {
@@ -556,6 +564,7 @@ func RegisterRoutes(m *macaron.Macaron) {
556564
m.Group("/:org", func() {
557565
m.Get("/dashboard", user.Dashboard)
558566
m.Get("/^:type(issues|pulls)$", user.Issues)
567+
m.Get("/milestones", reqMilestonesDashboardPageEnabled, user.Milestones)
559568
m.Get("/members", org.Members)
560569
m.Get("/members/action/:action", org.MembersAction)
561570

0 commit comments

Comments
 (0)