From c8d92fad305f78f0207203b3f1ea955e0ef0309d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 10 Sep 2015 07:53:40 -0400 Subject: [PATCH] #1595 pushing new branch will rereference issues in previous branch --- gogs.go | 2 +- models/action.go | 2 +- models/issue.go | 48 ++++++++++++++++++++++++++++++++++---------- modules/base/tool.go | 1 - templates/.VERSION | 2 +- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/gogs.go b/gogs.go index 2601c10593..80691329ea 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.6.9.0909 Beta" +const APP_VER = "0.6.9.0910 Beta" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/action.go b/models/action.go index 07f5b17a79..2e158cbf16 100644 --- a/models/action.go +++ b/models/action.go @@ -220,7 +220,7 @@ func updateIssuesCommit(u *User, repo *Repository, repoUserName, repoName string url := fmt.Sprintf("%s/%s/%s/commit/%s", setting.AppSubUrl, repoUserName, repoName, c.Sha1) message := fmt.Sprintf(`%s`, url, c.Message) - if _, err = CreateComment(u, repo, issue, 0, 0, COMMENT_TYPE_COMMIT_REF, message, nil); err != nil { + if err = CreateRefComment(u, repo, issue, message, c.Sha1); err != nil { return err } } diff --git a/models/issue.go b/models/issue.go index 00db990d8f..7ba5cd9c9a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1685,6 +1685,9 @@ type Comment struct { RenderedContent string `xorm:"-"` Created time.Time `xorm:"CREATED"` + // Reference issue in commit message + CommitSHA string `xorm:"VARCHAR(40)"` + Attachments []*Attachment `xorm:"-"` // For view issue page. @@ -1733,14 +1736,15 @@ func (c *Comment) EventTag() string { return "event-" + com.ToStr(c.ID) } -func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content string, uuids []string) (_ *Comment, err error) { +func createComment(e *xorm.Session, u *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content, commitSHA string, uuids []string) (_ *Comment, err error) { comment := &Comment{ - PosterID: u.Id, - Type: cmtType, - IssueID: issue.ID, - CommitID: commitID, - Line: line, - Content: content, + PosterID: u.Id, + Type: cmtType, + IssueID: issue.ID, + CommitID: commitID, + Line: line, + Content: content, + CommitSHA: commitSHA, } if _, err = e.Insert(comment); err != nil { return nil, err @@ -1819,18 +1823,18 @@ func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *I if !issue.IsClosed { cmtType = COMMENT_TYPE_REOPEN } - return createComment(e, doer, repo, issue, 0, 0, cmtType, "", nil) + return createComment(e, doer, repo, issue, 0, 0, cmtType, "", "", nil) } // CreateComment creates comment of issue or commit. -func CreateComment(doer *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content string, attachments []string) (comment *Comment, err error) { +func CreateComment(doer *User, repo *Repository, issue *Issue, commitID, line int64, cmtType CommentType, content, commitSHA string, attachments []string) (comment *Comment, err error) { sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return nil, err } - comment, err = createComment(sess, doer, repo, issue, commitID, line, cmtType, content, attachments) + comment, err = createComment(sess, doer, repo, issue, commitID, line, cmtType, content, commitSHA, attachments) if err != nil { return nil, err } @@ -1840,7 +1844,29 @@ func CreateComment(doer *User, repo *Repository, issue *Issue, commitID, line in // CreateIssueComment creates a plain issue comment. func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) { - return CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMENT, content, attachments) + return CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMENT, content, "", attachments) +} + +// CreateRefComment creates a commit reference comment to issue. +func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error { + if len(commitSHA) == 0 { + return fmt.Errorf("cannot create reference with empty commit SHA") + } + + // Check if same reference from same commit has already existed. + has, err := x.Get(&Comment{ + Type: COMMENT_TYPE_COMMIT_REF, + IssueID: issue.ID, + CommitSHA: commitSHA, + }) + if err != nil { + return fmt.Errorf("check reference comment: %v", err) + } else if has { + return nil + } + + _, err = CreateComment(doer, repo, issue, 0, 0, COMMENT_TYPE_COMMIT_REF, content, commitSHA, nil) + return err } // GetCommentByID returns the comment by given ID. diff --git a/modules/base/tool.go b/modules/base/tool.go index fa5202366a..b9a97c9c3d 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -29,7 +29,6 @@ import ( var Sanitizer = bluemonday.UGCPolicy().AllowAttrs("class").Matching(regexp.MustCompile(`[\p{L}\p{N}\s\-_',:\[\]!\./\\\(\)&]*`)).OnElements("code") - // Encode string to md5 hex value. func EncodeMd5(str string) string { m := md5.New() diff --git a/templates/.VERSION b/templates/.VERSION index 105769c650..36227852e3 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.6.9.0909 Beta \ No newline at end of file +0.6.9.0910 Beta \ No newline at end of file