Skip to content

Commit 081485e

Browse files
authored
add milestone changed traker on issue view (#804)
1 parent 10644d6 commit 081485e

File tree

10 files changed

+134
-40
lines changed

10 files changed

+134
-40
lines changed

models/issue.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ type NewIssueOptions struct {
739739
IsPull bool
740740
}
741741

742-
func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
742+
func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
743743
opts.Issue.Title = strings.TrimSpace(opts.Issue.Title)
744744
opts.Issue.Index = opts.Repo.NextIssueIndex()
745745

@@ -754,9 +754,6 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
754754
if milestone != nil {
755755
opts.Issue.MilestoneID = milestone.ID
756756
opts.Issue.Milestone = milestone
757-
if err = changeMilestoneAssign(e, opts.Issue, -1); err != nil {
758-
return err
759-
}
760757
}
761758
}
762759

@@ -785,6 +782,12 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
785782
return err
786783
}
787784

785+
if opts.Issue.MilestoneID > 0 {
786+
if err = changeMilestoneAssign(e, doer, opts.Issue, -1); err != nil {
787+
return err
788+
}
789+
}
790+
788791
if opts.IsPull {
789792
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
790793
} else {
@@ -849,7 +852,7 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string)
849852
return err
850853
}
851854

852-
if err = newIssue(sess, NewIssueOptions{
855+
if err = newIssue(sess, issue.Poster, NewIssueOptions{
853856
Repo: repo,
854857
Issue: issue,
855858
LableIDs: labelIDs,
@@ -1773,7 +1776,7 @@ func ChangeMilestoneIssueStats(issue *Issue) (err error) {
17731776
return sess.Commit()
17741777
}
17751778

1776-
func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64) error {
1779+
func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
17771780
if oldMilestoneID > 0 {
17781781
m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
17791782
if err != nil {
@@ -1810,18 +1813,28 @@ func changeMilestoneAssign(e *xorm.Session, issue *Issue, oldMilestoneID int64)
18101813
}
18111814
}
18121815

1816+
if err := issue.loadRepo(e); err != nil {
1817+
return err
1818+
}
1819+
1820+
if oldMilestoneID > 0 || issue.MilestoneID > 0 {
1821+
if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil {
1822+
return err
1823+
}
1824+
}
1825+
18131826
return updateIssue(e, issue)
18141827
}
18151828

18161829
// ChangeMilestoneAssign changes assignment of milestone for issue.
1817-
func ChangeMilestoneAssign(issue *Issue, oldMilestoneID int64) (err error) {
1830+
func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
18181831
sess := x.NewSession()
18191832
defer sess.Close()
18201833
if err = sess.Begin(); err != nil {
18211834
return err
18221835
}
18231836

1824-
if err = changeMilestoneAssign(sess, issue, oldMilestoneID); err != nil {
1837+
if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
18251838
return err
18261839
}
18271840
return sess.Commit()

models/issue_comment.go

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const (
3838
CommentTypePullRef
3939
// Labels changed
4040
CommentTypeLabel
41+
// Milestone changed
42+
CommentTypeMilestone
4143
)
4244

4345
// CommentTag defines comment tag type
@@ -58,9 +60,13 @@ type Comment struct {
5860
PosterID int64 `xorm:"INDEX"`
5961
Poster *User `xorm:"-"`
6062
IssueID int64 `xorm:"INDEX"`
61-
CommitID int64
6263
LabelID int64
6364
Label *Label `xorm:"-"`
65+
OldMilestoneID int64
66+
MilestoneID int64
67+
OldMilestone *Milestone `xorm:"-"`
68+
Milestone *Milestone `xorm:"-"`
69+
CommitID int64
6470
Line int64
6571
Content string `xorm:"TEXT"`
6672
RenderedContent string `xorm:"-"`
@@ -204,6 +210,36 @@ func (c *Comment) LoadLabel() error {
204210
return nil
205211
}
206212

213+
// LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
214+
func (c *Comment) LoadMilestone() error {
215+
if c.OldMilestoneID > 0 {
216+
var oldMilestone Milestone
217+
has, err := x.ID(c.OldMilestoneID).Get(&oldMilestone)
218+
if err != nil {
219+
return err
220+
} else if !has {
221+
return ErrMilestoneNotExist{
222+
ID: c.OldMilestoneID,
223+
}
224+
}
225+
c.OldMilestone = &oldMilestone
226+
}
227+
228+
if c.MilestoneID > 0 {
229+
var milestone Milestone
230+
has, err := x.ID(c.MilestoneID).Get(&milestone)
231+
if err != nil {
232+
return err
233+
} else if !has {
234+
return ErrMilestoneNotExist{
235+
ID: c.MilestoneID,
236+
}
237+
}
238+
c.Milestone = &milestone
239+
}
240+
return nil
241+
}
242+
207243
// MailParticipants sends new comment emails to repository watchers
208244
// and mentioned people.
209245
func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
@@ -233,15 +269,17 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
233269
LabelID = opts.Label.ID
234270
}
235271
comment := &Comment{
236-
Type: opts.Type,
237-
PosterID: opts.Doer.ID,
238-
Poster: opts.Doer,
239-
IssueID: opts.Issue.ID,
240-
LabelID: LabelID,
241-
CommitID: opts.CommitID,
242-
CommitSHA: opts.CommitSHA,
243-
Line: opts.LineNum,
244-
Content: opts.Content,
272+
Type: opts.Type,
273+
PosterID: opts.Doer.ID,
274+
Poster: opts.Doer,
275+
IssueID: opts.Issue.ID,
276+
LabelID: LabelID,
277+
OldMilestoneID: opts.OldMilestoneID,
278+
MilestoneID: opts.MilestoneID,
279+
CommitID: opts.CommitID,
280+
CommitSHA: opts.CommitSHA,
281+
Line: opts.LineNum,
282+
Content: opts.Content,
245283
}
246284
if _, err = e.Insert(comment); err != nil {
247285
return nil, err
@@ -367,6 +405,17 @@ func createLabelComment(e *xorm.Session, doer *User, repo *Repository, issue *Is
367405
})
368406
}
369407

408+
func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldMilestoneID, milestoneID int64) (*Comment, error) {
409+
return createComment(e, &CreateCommentOptions{
410+
Type: CommentTypeMilestone,
411+
Doer: doer,
412+
Repo: repo,
413+
Issue: issue,
414+
OldMilestoneID: oldMilestoneID,
415+
MilestoneID: milestoneID,
416+
})
417+
}
418+
370419
// CreateCommentOptions defines options for creating comment
371420
type CreateCommentOptions struct {
372421
Type CommentType
@@ -375,11 +424,13 @@ type CreateCommentOptions struct {
375424
Issue *Issue
376425
Label *Label
377426

378-
CommitID int64
379-
CommitSHA string
380-
LineNum int64
381-
Content string
382-
Attachments []string // UUIDs of attachments
427+
OldMilestoneID int64
428+
MilestoneID int64
429+
CommitID int64
430+
CommitSHA string
431+
LineNum int64
432+
Content string
433+
Attachments []string // UUIDs of attachments
383434
}
384435

385436
// CreateComment creates comment of issue or commit.

models/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
470470
return err
471471
}
472472

473-
if err = newIssue(sess, NewIssueOptions{
473+
if err = newIssue(sess, pull.Poster, NewIssueOptions{
474474
Repo: repo,
475475
Issue: pull,
476476
LableIDs: labelIDs,

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ issues.label_templates.use = Use this label set
543543
issues.label_templates.fail_to_load_file = Failed to load label template file '%s': %v
544544
issues.add_label_at = `added the <div class="ui label" style="color: %s; background-color: %s">%s</div> label %s`
545545
issues.remove_label_at = `removed the <div class="ui label" style="color: %s; background-color: %s">%s</div> label %s`
546+
issues.add_milestone_at = `added this to the <b>%s</b> milestone %s`
547+
issues.change_milestone_at = `modified the milestone from <b>%s</b> to <b>%s</b> %s`
548+
issues.remove_milestone_at = `removed this from the <b>%s</b> milestone %s`
546549
issues.open_tab = %d Open
547550
issues.close_tab = %d Closed
548551
issues.filter_label = Label

options/locale/locale_zh-CN.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ issues.label_templates.use=加载标签模板
503503
issues.label_templates.fail_to_load_file=加载标签模板文件 '%s' 时发生错误:%v
504504
issues.add_label_at = ` %[4]s 添加了标签 <div class="ui label" style="color: %[1]s; background-color: %[2]s">%[3]s</div>`
505505
issues.remove_label_at = ` %[4]s 删除了标签 <div class="ui label" style="color: %[1]s; background-color: %[2]s">%[3]s</div>`
506+
issues.add_milestone_at = ` %[2]s 添加了里程碑 <b>%[1]s</b>`
507+
issues.change_milestone_at = `%[3]s 修改了里程碑从 <b>%[1]s</b> 到 <b>%[2]s</b>`
508+
issues.remove_milestone_at = `%[2]s 删除了里程碑 <b>%[1]s</b>`
506509
issues.open_tab=%d 个开启中
507510
issues.close_tab=%d 个已关闭
508511
issues.filter_label=标签筛选

public/js/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ function initCommentForm() {
168168
var $list = $('.ui' + select_id + '.list');
169169
var hasUpdateAction = $menu.data('action') == 'update';
170170

171+
$(select_id).dropdown('setting', 'onHide', function(){
172+
if (hasUpdateAction) {
173+
location.reload();
174+
}
175+
});
176+
171177
$menu.find('.item:not(.no-select)').click(function () {
172178
$(this).parent().find('.item').each(function () {
173179
$(this).removeClass('selected active')

routers/api/v1/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
168168
issue.MilestoneID != *form.Milestone {
169169
oldMilestoneID := issue.MilestoneID
170170
issue.MilestoneID = *form.Milestone
171-
if err = models.ChangeMilestoneAssign(issue, oldMilestoneID); err != nil {
171+
if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
172172
ctx.Error(500, "ChangeMilestoneAssign", err)
173173
return
174174
}

routers/api/v1/repo/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
241241
issue.MilestoneID != form.Milestone {
242242
oldMilestoneID := issue.MilestoneID
243243
issue.MilestoneID = form.Milestone
244-
if err = models.ChangeMilestoneAssign(issue, oldMilestoneID); err != nil {
244+
if err = models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
245245
ctx.Error(500, "ChangeMilestoneAssign", err)
246246
return
247247
}

routers/repo/issue.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,22 +384,27 @@ func ValidateRepoMetas(ctx *context.Context, form auth.CreateIssueForm) ([]int64
384384
return nil, 0, 0
385385
}
386386

387-
// Check labels.
388-
labelIDs, err := base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
389-
if err != nil {
390-
return nil, 0, 0
391-
}
392-
labelIDMark := base.Int64sToMap(labelIDs)
387+
var labelIDs []int64
393388
hasSelected := false
394-
for i := range labels {
395-
if labelIDMark[labels[i].ID] {
396-
labels[i].IsChecked = true
397-
hasSelected = true
389+
// Check labels.
390+
if len(form.LabelIDs) > 0 {
391+
labelIDs, err = base.StringsToInt64s(strings.Split(form.LabelIDs, ","))
392+
if err != nil {
393+
return nil, 0, 0
394+
}
395+
labelIDMark := base.Int64sToMap(labelIDs)
396+
397+
for i := range labels {
398+
if labelIDMark[labels[i].ID] {
399+
labels[i].IsChecked = true
400+
hasSelected = true
401+
}
398402
}
399403
}
404+
405+
ctx.Data["Labels"] = labels
400406
ctx.Data["HasSelectedLabel"] = hasSelected
401407
ctx.Data["label_ids"] = form.LabelIDs
402-
ctx.Data["Labels"] = labels
403408

404409
// Check milestone.
405410
milestoneID := form.MilestoneID
@@ -617,6 +622,11 @@ func ViewIssue(ctx *context.Context) {
617622
ctx.Handle(500, "LoadLabel", err)
618623
return
619624
}
625+
} else if comment.Type == models.CommentTypeMilestone {
626+
if err = comment.LoadMilestone(); err != nil {
627+
ctx.Handle(500, "LoadMilestone", err)
628+
return
629+
}
620630
}
621631
}
622632

@@ -625,7 +635,6 @@ func ViewIssue(ctx *context.Context) {
625635
canDelete := false
626636

627637
if ctx.IsSigned && pull.HeadBranch != "master" {
628-
629638
if err := pull.GetHeadRepo(); err != nil {
630639
log.Error(4, "GetHeadRepo: %v", err)
631640
} else if ctx.User.IsWriterOfRepo(pull.HeadRepo) {
@@ -729,7 +738,7 @@ func UpdateIssueMilestone(ctx *context.Context) {
729738

730739
// Not check for invalid milestone id and give responsibility to owners.
731740
issue.MilestoneID = milestoneID
732-
if err := models.ChangeMilestoneAssign(issue, oldMilestoneID); err != nil {
741+
if err := models.ChangeMilestoneAssign(issue, ctx.User, oldMilestoneID); err != nil {
733742
ctx.Handle(500, "ChangeMilestoneAssign", err)
734743
return
735744
}

templates/repo/issue/view_content.tmpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,16 @@
151151
<img src="{{.Poster.RelAvatarLink}}">
152152
</a>
153153
<span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.Name}}</a>
154-
{{if .Content}}{{$.i18n.Tr "repo.issues.add_label_at" .Label.ForegroundColor .Label.Color .Label.Name $createdStr | Safe}}{{else}}{{$.i18n.Tr "repo.issues.remove_label_at" .Label.ForegroundColor .Label.Color .Label.Name $createdStr | Safe}}{{end}}</span>
154+
{{if .Content}}{{$.i18n.Tr "repo.issues.add_label_at" .Label.ForegroundColor .Label.Color .Label.Name $createdStr | Safe}}{{else}}{{$.i18n.Tr "repo.issues.remove_label_at" .Label.ForegroundColor .Label.Color .Label.Name $createdStr | Safe}}{{end}}</span>
155+
</div>
156+
{{else if eq .Type 8}}
157+
<div class="event">
158+
<span class="octicon octicon-primitive-dot"></span>
159+
<a class="ui avatar image" href="{{.Poster.HomeLink}}">
160+
<img src="{{.Poster.RelAvatarLink}}">
161+
</a>
162+
<span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.Name}}</a>
163+
{{if gt .OldMilestoneID 0}}{{if gt .MilestoneID 0}}{{$.i18n.Tr "repo.issues.change_milestone_at" .OldMilestone.Name .Milestone.Name $createdStr | Safe}}{{else}}{{$.i18n.Tr "repo.issues.remove_milestone_at" .OldMilestone.Name $createdStr | Safe}}{{end}}{{else if gt .MilestoneID 0}}{{$.i18n.Tr "repo.issues.add_milestone_at" .Milestone.Name $createdStr | Safe}}{{end}}</span>
155164
</div>
156165
{{end}}
157166

0 commit comments

Comments
 (0)