Skip to content

[WIP] [Feature] Implement issue reference cross comment #8147

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,16 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
go HookQueue.Add(issue.RepoID)
}

err = FindAndCreateIssueRef(issue.Poster, repo, issue, issue.Content)
if err != nil {
return err
}

err = FindAndCreateIssueRef(issue.Poster, repo, issue, issue.Title)
if err != nil {
return err
}

return nil
}

Expand Down
75 changes: 75 additions & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package models

import (
"fmt"
"html"
"strings"

"code.gitea.io/gitea/modules/git"
Expand Down Expand Up @@ -830,6 +831,11 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri
return nil, fmt.Errorf("CreateComment: %v", err)
}

err = FindAndCreateIssueRef(doer, repo, issue, content)
if err != nil {
return nil, err
}

mode, _ := AccessLevel(doer, repo)
if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentCreated,
Expand All @@ -845,6 +851,51 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri
return comment, nil
}

// FindAndCreateIssueRef Search message for issue ref and create cross ref comment
func FindAndCreateIssueRef(doer *User, repo *Repository, issue *Issue, message string) error {
refMarked := make(map[int64]bool)
var refRepo *Repository
var err error
for _, m := range issueReferenceKeywordsPat.FindAllStringSubmatch(message, -1) {
if len(m[3]) == 0 {
continue
}
ref := m[3]

// issue is from another repo
if len(m[1]) > 0 && len(m[2]) > 0 {
refRepo, err = GetRepositoryFromMatch(m[1], m[2])
if err != nil {
continue
}
} else {
refRepo = repo
}
refIssue, err := getIssueFromRef(refRepo, ref)
if err != nil {
return err
}

if refIssue == nil || refMarked[refIssue.ID] {
continue
}
refMarked[refIssue.ID] = true

issueType := "issues"

if issue.IsPull {
issueType = "pull"
}

message := fmt.Sprintf(`<a href="%s/%s/%d">%s</a>`, repo.Link(), issueType, issue.ID, html.EscapeString(issue.Title))
if err = CreateIssueRefComment(doer, refRepo, refIssue, message); err != nil {
return err
}
}

return nil
}

// CreateRefComment creates a commit reference comment to issue.
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
if len(commitSHA) == 0 {
Expand Down Expand Up @@ -874,6 +925,30 @@ func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commi
return err
}

// CreateIssueRefComment creates a issue reference comment to issue.
func CreateIssueRefComment(doer *User, repo *Repository, issue *Issue, content string) error {
// Check if same reference from same issue has already existed.
has, err := x.Get(&Comment{
Type: CommentTypeIssueRef,
IssueID: issue.ID,
Content: content,
})
if err != nil {
return fmt.Errorf("CreateIssueRefComment (check has): %v", err)
} else if has {
return nil
}

_, err = CreateComment(&CreateCommentOptions{
Type: CommentTypeIssueRef,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
})
return err
}

// GetCommentByID returns the comment by given ID.
func GetCommentByID(id int64) (*Comment, error) {
c := new(Comment)
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ issues.reopen_comment_issue = Comment and Reopen
issues.create_comment = Comment
issues.closed_at = `closed <a id="%[1]s" href="#%[1]s">%[2]s</a>`
issues.reopened_at = `reopened <a id="%[1]s" href="#%[1]s">%[2]s</a>`
issues.issue_ref_at = `referenced this issue from <b>%s</b>`
issues.commit_ref_at = `referenced this issue from a commit <a id="%[1]s" href="#%[1]s">%[2]s</a>`
issues.poster = Poster
issues.collaborator = Collaborator
Expand Down
15 changes: 15 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,11 @@ func UpdateIssueTitle(ctx *context.Context) {
return
}

if err := models.FindAndCreateIssueRef(ctx.User, ctx.Repo.Repository, issue, title); err != nil {
ctx.ServerError("FindAndCreateIssueRef", err)
return
}

notification.NotifyIssueChangeTitle(ctx.User, issue, oldTitle)

ctx.JSON(200, map[string]interface{}{
Expand All @@ -1067,6 +1072,11 @@ func UpdateIssueContent(ctx *context.Context) {
return
}

if err := models.FindAndCreateIssueRef(ctx.User, ctx.Repo.Repository, issue, content); err != nil {
ctx.ServerError("FindAndCreateIssueRef", err)
return
}

ctx.JSON(200, map[string]interface{}{
"content": string(markdown.Render([]byte(issue.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
})
Expand Down Expand Up @@ -1339,6 +1349,11 @@ func UpdateCommentContent(ctx *context.Context) {
return
}

if err = models.FindAndCreateIssueRef(ctx.User, ctx.Repo.Repository, comment.Issue, comment.Content); err != nil {
ctx.ServerError("FindAndCreateIssueRef", err)
return
}

notification.NotifyUpdateComment(ctx.User, comment, oldContent)

ctx.JSON(200, map[string]interface{}{
Expand Down
11 changes: 11 additions & 0 deletions templates/repo/issue/view_content/comments.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@
</a>
<span class="text grey"><a href="{{.Poster.HomeLink}}">{{.Poster.GetDisplayName}}</a> {{$.i18n.Tr "repo.issues.closed_at" .EventTag $createdStr | Safe}}</span>
</div>
{{else if eq .Type 3}}
<div class="event">
<span class="octicon octicon-bookmark issue-symbol"></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.issue_ref_at" .Content | Safe}}
</span>
</div>
{{else if eq .Type 4}}
<div class="event">
<span class="octicon octicon-bookmark"></span>
Expand Down