Skip to content

Commit 3e0525b

Browse files
authored
Track assignee for issue (#808)
* track assignee for issue * fix lint * use getUserByID instead Get
1 parent 68bdaf0 commit 3e0525b

File tree

5 files changed

+94
-11
lines changed

5 files changed

+94
-11
lines changed

models/issue.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,11 +703,23 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
703703

704704
// ChangeAssignee changes the Asssignee field of this issue.
705705
func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
706+
var oldAssigneeID = issue.AssigneeID
706707
issue.AssigneeID = assigneeID
707708
if err = UpdateIssueUserByAssignee(issue); err != nil {
708709
return fmt.Errorf("UpdateIssueUserByAssignee: %v", err)
709710
}
710711

712+
sess := x.NewSession()
713+
defer sess.Close()
714+
715+
if err = issue.loadRepo(sess); err != nil {
716+
return fmt.Errorf("loadRepo: %v", err)
717+
}
718+
719+
if _, err = createAssigneeComment(sess, doer, issue.Repo, issue, oldAssigneeID, assigneeID); err != nil {
720+
return fmt.Errorf("createAssigneeComment: %v", err)
721+
}
722+
711723
issue.Assignee, err = GetUserByID(issue.AssigneeID)
712724
if err != nil && !IsErrUserNotExist(err) {
713725
log.Error(4, "GetUserByID [assignee_id: %v]: %v", issue.AssigneeID, err)
@@ -798,6 +810,15 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
798810
}
799811
}
800812

813+
if opts.Issue.AssigneeID > 0 {
814+
if err = opts.Issue.loadRepo(e); err != nil {
815+
return err
816+
}
817+
if _, err = createAssigneeComment(e, doer, opts.Issue.Repo, opts.Issue, -1, opts.Issue.AssigneeID); err != nil {
818+
return err
819+
}
820+
}
821+
801822
if opts.IsPull {
802823
_, err = e.Exec("UPDATE `repository` SET num_pulls = num_pulls + 1 WHERE id = ?", opts.Issue.RepoID)
803824
} else {

models/issue_comment.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const (
4040
CommentTypeLabel
4141
// Milestone changed
4242
CommentTypeMilestone
43+
// Assignees changed
44+
CommentTypeAssignees
4345
)
4446

4547
// CommentTag defines comment tag type
@@ -55,17 +57,22 @@ const (
5557

5658
// Comment represents a comment in commit and issue page.
5759
type Comment struct {
58-
ID int64 `xorm:"pk autoincr"`
59-
Type CommentType
60-
PosterID int64 `xorm:"INDEX"`
61-
Poster *User `xorm:"-"`
62-
IssueID int64 `xorm:"INDEX"`
63-
LabelID int64
64-
Label *Label `xorm:"-"`
65-
OldMilestoneID int64
66-
MilestoneID int64
67-
OldMilestone *Milestone `xorm:"-"`
68-
Milestone *Milestone `xorm:"-"`
60+
ID int64 `xorm:"pk autoincr"`
61+
Type CommentType
62+
PosterID int64 `xorm:"INDEX"`
63+
Poster *User `xorm:"-"`
64+
IssueID int64 `xorm:"INDEX"`
65+
LabelID int64
66+
Label *Label `xorm:"-"`
67+
OldMilestoneID int64
68+
MilestoneID int64
69+
OldMilestone *Milestone `xorm:"-"`
70+
Milestone *Milestone `xorm:"-"`
71+
OldAssigneeID int64
72+
AssigneeID int64
73+
Assignee *User `xorm:"-"`
74+
OldAssignee *User `xorm:"-"`
75+
6976
CommitID int64
7077
Line int64
7178
Content string `xorm:"TEXT"`
@@ -240,6 +247,25 @@ func (c *Comment) LoadMilestone() error {
240247
return nil
241248
}
242249

250+
// LoadAssignees if comment.Type is CommentTypeAssignees, then load assignees
251+
func (c *Comment) LoadAssignees() error {
252+
var err error
253+
if c.OldAssigneeID > 0 {
254+
c.OldAssignee, err = getUserByID(x, c.OldAssigneeID)
255+
if err != nil {
256+
return err
257+
}
258+
}
259+
260+
if c.AssigneeID > 0 {
261+
c.Assignee, err = getUserByID(x, c.AssigneeID)
262+
if err != nil {
263+
return err
264+
}
265+
}
266+
return nil
267+
}
268+
243269
// MailParticipants sends new comment emails to repository watchers
244270
// and mentioned people.
245271
func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (err error) {
@@ -276,6 +302,8 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
276302
LabelID: LabelID,
277303
OldMilestoneID: opts.OldMilestoneID,
278304
MilestoneID: opts.MilestoneID,
305+
OldAssigneeID: opts.OldAssigneeID,
306+
AssigneeID: opts.AssigneeID,
279307
CommitID: opts.CommitID,
280308
CommitSHA: opts.CommitSHA,
281309
Line: opts.LineNum,
@@ -416,6 +444,17 @@ func createMilestoneComment(e *xorm.Session, doer *User, repo *Repository, issue
416444
})
417445
}
418446

447+
func createAssigneeComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, oldAssigneeID, assigneeID int64) (*Comment, error) {
448+
return createComment(e, &CreateCommentOptions{
449+
Type: CommentTypeAssignees,
450+
Doer: doer,
451+
Repo: repo,
452+
Issue: issue,
453+
OldAssigneeID: oldAssigneeID,
454+
AssigneeID: assigneeID,
455+
})
456+
}
457+
419458
// CreateCommentOptions defines options for creating comment
420459
type CreateCommentOptions struct {
421460
Type CommentType
@@ -426,6 +465,8 @@ type CreateCommentOptions struct {
426465

427466
OldMilestoneID int64
428467
MilestoneID int64
468+
OldAssigneeID int64
469+
AssigneeID int64
429470
CommitID int64
430471
CommitSHA string
431472
LineNum int64

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ issues.remove_label_at = `removed the <div class="ui label" style="color: %s; ba
546546
issues.add_milestone_at = `added this to the <b>%s</b> milestone %s`
547547
issues.change_milestone_at = `modified the milestone from <b>%s</b> to <b>%s</b> %s`
548548
issues.remove_milestone_at = `removed this from the <b>%s</b> milestone %s`
549+
issues.self_assign_at = `self-assigned this %s`
550+
issues.add_assignee_at = `was assigned by <b>%s</b> %s`
551+
issues.remove_assignee_at = `removed their assignment %s`
549552
issues.open_tab = %d Open
550553
issues.close_tab = %d Closed
551554
issues.filter_label = Label

routers/repo/issue.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ func ViewIssue(ctx *context.Context) {
615615
ctx.Handle(500, "LoadMilestone", err)
616616
return
617617
}
618+
} else if comment.Type == models.CommentTypeAssignees {
619+
if err = comment.LoadAssignees(); err != nil {
620+
ctx.Handle(500, "LoadAssignees", err)
621+
return
622+
}
618623
}
619624
}
620625

templates/repo/issue/view_content.tmpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@
162162
<span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.Name}}</a>
163163
{{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>
164164
</div>
165+
{{else if eq .Type 9}}
166+
<div class="event">
167+
<span class="octicon octicon-primitive-dot"></span>
168+
{{if gt .AssigneeID 0}}{{if eq .Poster.ID .AssigneeID}}<a class="ui avatar image" href="{{.Poster.HomeLink}}">
169+
<img src="{{.Poster.RelAvatarLink}}">
170+
</a> <span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.Name}}</a> {{$.i18n.Tr "repo.issues.self_assign_at" $createdStr | Safe}} </span>
171+
{{else}}<a class="ui avatar image" href="{{.Assignee.HomeLink}}">
172+
<img src="{{.Assignee.RelAvatarLink}}">
173+
</a><span class="text grey"><a href="{{.Assignee.HomeLink}}">{{.Assignee.Name}}</a> {{$.i18n.Tr "repo.issues.add_assignee_at" .Poster.Name $createdStr | Safe}} </span>{{end}}{{else if gt .OldAssigneeID 0}}
174+
<a class="ui avatar image" href="{{.Poster.HomeLink}}">
175+
<img src="{{.Poster.RelAvatarLink}}">
176+
</a> <span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.Name}}</a> {{$.i18n.Tr "repo.issues.remove_assignee_at" $createdStr | Safe}} </span>{{end}}
177+
</div>
165178
{{end}}
166179

167180
{{end}}

0 commit comments

Comments
 (0)