|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/references" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestXRef_AddCrossReferences(t *testing.T) { |
| 17 | + assert.NoError(t, PrepareTestDatabase()) |
| 18 | + |
| 19 | + // Issue #1 to test against |
| 20 | + itarget := testCreateIssue(t, 1, 2, "title1", "content1", false) |
| 21 | + |
| 22 | + // PR to close issue #1 |
| 23 | + content := fmt.Sprintf("content2, closes #%d", itarget.Index) |
| 24 | + pr := testCreateIssue(t, 1, 2, "title2", content, true) |
| 25 | + ref := AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: 0}).(*Comment) |
| 26 | + assert.Equal(t, CommentTypePullRef, ref.Type) |
| 27 | + assert.Equal(t, pr.RepoID, ref.RefRepoID) |
| 28 | + assert.Equal(t, true, ref.RefIsPull) |
| 29 | + assert.Equal(t, references.XRefActionCloses, ref.RefAction) |
| 30 | + |
| 31 | + // Comment on PR to reopen issue #1 |
| 32 | + content = fmt.Sprintf("content2, reopens #%d", itarget.Index) |
| 33 | + c := testCreateComment(t, 1, 2, pr.ID, content) |
| 34 | + ref = AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: pr.ID, RefCommentID: c.ID}).(*Comment) |
| 35 | + assert.Equal(t, CommentTypeCommentRef, ref.Type) |
| 36 | + assert.Equal(t, pr.RepoID, ref.RefRepoID) |
| 37 | + assert.Equal(t, true, ref.RefIsPull) |
| 38 | + assert.Equal(t, references.XRefActionReopens, ref.RefAction) |
| 39 | + |
| 40 | + // Issue mentioning issue #1 |
| 41 | + content = fmt.Sprintf("content3, mentions #%d", itarget.Index) |
| 42 | + i := testCreateIssue(t, 1, 2, "title3", content, false) |
| 43 | + ref = AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) |
| 44 | + assert.Equal(t, CommentTypeIssueRef, ref.Type) |
| 45 | + assert.Equal(t, pr.RepoID, ref.RefRepoID) |
| 46 | + assert.Equal(t, false, ref.RefIsPull) |
| 47 | + assert.Equal(t, references.XRefActionNone, ref.RefAction) |
| 48 | + |
| 49 | + // Issue #4 to test against |
| 50 | + itarget = testCreateIssue(t, 3, 3, "title4", "content4", false) |
| 51 | + |
| 52 | + // Cross-reference to issue #4 by admin |
| 53 | + content = fmt.Sprintf("content5, mentions user3/repo3#%d", itarget.Index) |
| 54 | + i = testCreateIssue(t, 2, 1, "title5", content, false) |
| 55 | + ref = AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) |
| 56 | + assert.Equal(t, CommentTypeIssueRef, ref.Type) |
| 57 | + assert.Equal(t, i.RepoID, ref.RefRepoID) |
| 58 | + assert.Equal(t, false, ref.RefIsPull) |
| 59 | + assert.Equal(t, references.XRefActionNone, ref.RefAction) |
| 60 | + |
| 61 | + // Cross-reference to issue #4 with no permission |
| 62 | + content = fmt.Sprintf("content6, mentions user3/repo3#%d", itarget.Index) |
| 63 | + i = testCreateIssue(t, 4, 5, "title6", content, false) |
| 64 | + AssertNotExistsBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}) |
| 65 | +} |
| 66 | + |
| 67 | +func TestXRef_NeuterCrossReferences(t *testing.T) { |
| 68 | + assert.NoError(t, PrepareTestDatabase()) |
| 69 | + |
| 70 | + // Issue #1 to test against |
| 71 | + itarget := testCreateIssue(t, 1, 2, "title1", "content1", false) |
| 72 | + |
| 73 | + // Issue mentioning issue #1 |
| 74 | + title := fmt.Sprintf("title2, mentions #%d", itarget.Index) |
| 75 | + i := testCreateIssue(t, 1, 2, title, "content2", false) |
| 76 | + ref := AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) |
| 77 | + assert.Equal(t, CommentTypeIssueRef, ref.Type) |
| 78 | + assert.Equal(t, references.XRefActionNone, ref.RefAction) |
| 79 | + |
| 80 | + d := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
| 81 | + i.Title = "title2, no mentions" |
| 82 | + assert.NoError(t, i.ChangeTitle(d, title)) |
| 83 | + |
| 84 | + ref = AssertExistsAndLoadBean(t, &Comment{IssueID: itarget.ID, RefIssueID: i.ID, RefCommentID: 0}).(*Comment) |
| 85 | + assert.Equal(t, CommentTypeIssueRef, ref.Type) |
| 86 | + assert.Equal(t, references.XRefActionNeutered, ref.RefAction) |
| 87 | +} |
| 88 | + |
| 89 | +func TestXRef_ResolveCrossReferences(t *testing.T) { |
| 90 | + assert.NoError(t, PrepareTestDatabase()) |
| 91 | + |
| 92 | + d := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) |
| 93 | + |
| 94 | + i1 := testCreateIssue(t, 1, 2, "title1", "content1", false) |
| 95 | + i2 := testCreateIssue(t, 1, 2, "title2", "content2", false) |
| 96 | + i3 := testCreateIssue(t, 1, 2, "title3", "content3", false) |
| 97 | + assert.NoError(t, i3.ChangeStatus(d, true)) |
| 98 | + |
| 99 | + pr := testCreatePR(t, 1, 2, "titlepr", fmt.Sprintf("closes #%d", i1.Index)) |
| 100 | + rp := AssertExistsAndLoadBean(t, &Comment{IssueID: i1.ID, RefIssueID: pr.Issue.ID, RefCommentID: 0}).(*Comment) |
| 101 | + |
| 102 | + c1 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i2.Index)) |
| 103 | + r1 := AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c1.ID}).(*Comment) |
| 104 | + |
| 105 | + // Must be ignored |
| 106 | + c2 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("mentions #%d", i2.Index)) |
| 107 | + AssertExistsAndLoadBean(t, &Comment{IssueID: i2.ID, RefIssueID: pr.Issue.ID, RefCommentID: c2.ID}) |
| 108 | + |
| 109 | + // Must be superseded by c4/r4 |
| 110 | + c3 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("reopens #%d", i3.Index)) |
| 111 | + AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c3.ID}) |
| 112 | + |
| 113 | + c4 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i3.Index)) |
| 114 | + r4 := AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment) |
| 115 | + |
| 116 | + refs, err := pr.ResolveCrossReferences() |
| 117 | + assert.NoError(t, err) |
| 118 | + assert.Len(t, refs, 3) |
| 119 | + assert.Equal(t, rp.ID, refs[0].ID, "bad ref rp: %+v", refs[0]) |
| 120 | + assert.Equal(t, r1.ID, refs[1].ID, "bad ref r1: %+v", refs[1]) |
| 121 | + assert.Equal(t, r4.ID, refs[2].ID, "bad ref r4: %+v", refs[2]) |
| 122 | +} |
| 123 | + |
| 124 | +func testCreateIssue(t *testing.T, repo, doer int64, title, content string, ispull bool) *Issue { |
| 125 | + r := AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) |
| 126 | + d := AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) |
| 127 | + i := &Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: ispull} |
| 128 | + |
| 129 | + sess := x.NewSession() |
| 130 | + defer sess.Close() |
| 131 | + assert.NoError(t, sess.Begin()) |
| 132 | + _, err := sess.SetExpr("`index`", "coalesce(MAX(`index`),0)+1").Where("repo_id=?", repo).Insert(i) |
| 133 | + assert.NoError(t, err) |
| 134 | + i, err = getIssueByID(sess, i.ID) |
| 135 | + assert.NoError(t, err) |
| 136 | + assert.NoError(t, i.addCrossReferences(sess, d)) |
| 137 | + assert.NoError(t, sess.Commit()) |
| 138 | + return i |
| 139 | +} |
| 140 | + |
| 141 | +func testCreatePR(t *testing.T, repo, doer int64, title, content string) *PullRequest { |
| 142 | + r := AssertExistsAndLoadBean(t, &Repository{ID: repo}).(*Repository) |
| 143 | + d := AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) |
| 144 | + i := &Issue{RepoID: r.ID, PosterID: d.ID, Poster: d, Title: title, Content: content, IsPull: true} |
| 145 | + pr := &PullRequest{HeadRepoID: repo, BaseRepoID: repo, HeadBranch: "head", BaseBranch: "base"} |
| 146 | + assert.NoError(t, NewPullRequest(r, i, nil, nil, pr, nil)) |
| 147 | + pr.Issue = i |
| 148 | + return pr |
| 149 | +} |
| 150 | + |
| 151 | +func testCreateComment(t *testing.T, repo, doer, issue int64, content string) *Comment { |
| 152 | + d := AssertExistsAndLoadBean(t, &User{ID: doer}).(*User) |
| 153 | + i := AssertExistsAndLoadBean(t, &Issue{ID: issue}).(*Issue) |
| 154 | + c := &Comment{Type: CommentTypeComment, PosterID: doer, Poster: d, IssueID: issue, Issue: i, Content: content} |
| 155 | + |
| 156 | + sess := x.NewSession() |
| 157 | + defer sess.Close() |
| 158 | + assert.NoError(t, sess.Begin()) |
| 159 | + _, err := sess.Insert(c) |
| 160 | + assert.NoError(t, err) |
| 161 | + assert.NoError(t, c.addCrossReferences(sess, d)) |
| 162 | + assert.NoError(t, sess.Commit()) |
| 163 | + return c |
| 164 | +} |
0 commit comments