2017-01-01 13:15:09 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
package issues_test
|
2017-01-01 13:15:09 -05:00
|
|
|
|
|
|
|
import (
|
2024-03-31 09:27:59 -04:00
|
|
|
"fmt"
|
2017-01-01 13:15:09 -05:00
|
|
|
"testing"
|
2024-03-31 09:27:59 -04:00
|
|
|
"time"
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2023-09-08 17:09:23 -04:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-05-24 22:06:27 -04:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-06-09 10:34:49 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-03-31 09:27:59 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-01-01 13:15:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_LoadAttributes(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadAttributes(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NotNil(t, pr.Merger)
|
|
|
|
assert.Equal(t, pr.MergerID, pr.Merger.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_LoadIssue(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadIssue(db.DefaultContext))
|
2017-01-08 22:15:07 -05:00
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:31:55 -05:00
|
|
|
func TestPullRequest_LoadBaseRepo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NotNil(t, pr.BaseRepo)
|
|
|
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadBaseRepo(db.DefaultContext))
|
2017-01-08 22:15:07 -05:00
|
|
|
assert.NotNil(t, pr.BaseRepo)
|
|
|
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:31:55 -05:00
|
|
|
func TestPullRequest_LoadHeadRepo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.LoadHeadRepo(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NotNil(t, pr.HeadRepo)
|
|
|
|
assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestMerge
|
|
|
|
|
|
|
|
// TODO TestNewPullRequest
|
|
|
|
|
|
|
|
func TestPullRequestsNewest(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-15 02:13:19 -04:00
|
|
|
prs, count, err := issues_model.PullRequests(db.DefaultContext, 1, &issues_model.PullRequestsOptions{
|
2021-09-24 07:32:56 -04:00
|
|
|
ListOptions: db.ListOptions{
|
2020-01-24 14:00:29 -05:00
|
|
|
Page: 1,
|
|
|
|
},
|
2017-01-01 13:15:09 -05:00
|
|
|
State: "open",
|
|
|
|
SortType: "newest",
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2020-01-17 01:03:40 -05:00
|
|
|
assert.EqualValues(t, 3, count)
|
|
|
|
if assert.Len(t, prs, 3) {
|
|
|
|
assert.EqualValues(t, 5, prs[0].ID)
|
|
|
|
assert.EqualValues(t, 2, prs[1].ID)
|
|
|
|
assert.EqualValues(t, 1, prs[2].ID)
|
2017-08-28 05:17:45 -04:00
|
|
|
}
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 22:06:27 -04:00
|
|
|
func TestLoadRequestedReviewers(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-05-24 22:06:27 -04:00
|
|
|
|
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pull.LoadIssue(db.DefaultContext))
|
2023-05-24 22:06:27 -04:00
|
|
|
issue := pull.Issue
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issue.LoadRepo(db.DefaultContext))
|
|
|
|
assert.Empty(t, pull.RequestedReviewers)
|
2023-05-24 22:06:27 -04:00
|
|
|
|
|
|
|
user1, err := user_model.GetUserByID(db.DefaultContext, 1)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-05-24 22:06:27 -04:00
|
|
|
|
2023-08-09 22:39:21 -04:00
|
|
|
comment, err := issues_model.AddReviewRequest(db.DefaultContext, issue, user1, &user_model.User{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-05-24 22:06:27 -04:00
|
|
|
assert.NotNil(t, comment)
|
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext))
|
2023-05-24 22:06:27 -04:00
|
|
|
assert.Len(t, pull.RequestedReviewers, 1)
|
|
|
|
|
2023-08-09 22:39:21 -04:00
|
|
|
comment, err = issues_model.RemoveReviewRequest(db.DefaultContext, issue, user1, &user_model.User{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-05-24 22:06:27 -04:00
|
|
|
assert.NotNil(t, comment)
|
|
|
|
|
|
|
|
pull.RequestedReviewers = nil
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext))
|
2023-05-24 22:06:27 -04:00
|
|
|
assert.Empty(t, pull.RequestedReviewers)
|
|
|
|
}
|
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
func TestPullRequestsOldest(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-15 02:13:19 -04:00
|
|
|
prs, count, err := issues_model.PullRequests(db.DefaultContext, 1, &issues_model.PullRequestsOptions{
|
2021-09-24 07:32:56 -04:00
|
|
|
ListOptions: db.ListOptions{
|
2020-01-24 14:00:29 -05:00
|
|
|
Page: 1,
|
|
|
|
},
|
2017-01-01 13:15:09 -05:00
|
|
|
State: "open",
|
|
|
|
SortType: "oldest",
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2020-01-17 01:03:40 -05:00
|
|
|
assert.EqualValues(t, 3, count)
|
|
|
|
if assert.Len(t, prs, 3) {
|
|
|
|
assert.EqualValues(t, 1, prs[0].ID)
|
|
|
|
assert.EqualValues(t, 2, prs[1].ID)
|
|
|
|
assert.EqualValues(t, 5, prs[2].ID)
|
2017-08-28 05:17:45 -04:00
|
|
|
}
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmergedPullRequest(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-11-19 03:12:33 -05:00
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(db.DefaultContext, 1, 1, "branch2", "master", issues_model.PullRequestFlowGithub)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, int64(2), pr.ID)
|
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
_, err = issues_model.GetUnmergedPullRequest(db.DefaultContext, 1, 9223372036854775807, "branch1", "master", issues_model.PullRequestFlowGithub)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
2022-01-03 14:45:58 -05:00
|
|
|
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-01-03 14:45:58 -05:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.True(t, exist)
|
2022-01-03 14:45:58 -05:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
exist, err = issues_model.HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "not_exist_branch")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.False(t, exist)
|
2022-01-03 14:45:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-07-22 10:14:27 -04:00
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
for _, pr := range prs {
|
|
|
|
assert.Equal(t, int64(1), pr.HeadRepoID)
|
|
|
|
assert.Equal(t, "branch2", pr.HeadBranch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 09:27:59 -04:00
|
|
|
func TestGetUnmergedPullRequestsByHeadInfoMax(t *testing.T) {
|
|
|
|
defer tests.AddFixtures("models/fixtures/TestGetUnmergedPullRequestsByHeadInfoMax/")()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2024-03-31 09:27:59 -04:00
|
|
|
|
|
|
|
repoID := int64(1)
|
|
|
|
olderThan := int64(0)
|
|
|
|
|
|
|
|
// for NULL created field the olderThan condition is ignored
|
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, "branch2")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
assert.Equal(t, int64(1), prs[0].HeadRepoID)
|
|
|
|
|
|
|
|
// test for when the created field is set
|
|
|
|
branch := "branchmax"
|
|
|
|
prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, prs)
|
2024-03-31 09:27:59 -04:00
|
|
|
olderThan = time.Now().UnixNano()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
for _, pr := range prs {
|
|
|
|
assert.Equal(t, int64(1), pr.HeadRepoID)
|
|
|
|
assert.Equal(t, branch, pr.HeadBranch)
|
|
|
|
}
|
|
|
|
pr := prs[0]
|
|
|
|
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
table string
|
|
|
|
field string
|
|
|
|
id int64
|
|
|
|
match any
|
|
|
|
nomatch any
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
table: "issue",
|
|
|
|
field: "is_closed",
|
|
|
|
id: pr.IssueID,
|
|
|
|
match: false,
|
|
|
|
nomatch: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
table: "pull_request",
|
|
|
|
field: "flow",
|
|
|
|
id: pr.ID,
|
|
|
|
match: issues_model.PullRequestFlowGithub,
|
|
|
|
nomatch: issues_model.PullRequestFlowAGit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
table: "pull_request",
|
|
|
|
field: "head_repo_id",
|
|
|
|
id: pr.ID,
|
|
|
|
match: pr.HeadRepoID,
|
|
|
|
nomatch: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
table: "pull_request",
|
|
|
|
field: "head_branch",
|
|
|
|
id: pr.ID,
|
|
|
|
match: pr.HeadBranch,
|
|
|
|
nomatch: "something else",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
table: "pull_request",
|
|
|
|
field: "has_merged",
|
|
|
|
id: pr.ID,
|
|
|
|
match: false,
|
|
|
|
nomatch: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testCase.field, func(t *testing.T) {
|
|
|
|
update := fmt.Sprintf("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", testCase.table, testCase.field)
|
|
|
|
|
|
|
|
// expect no match
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.nomatch, testCase.id)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, prs)
|
2024-03-31 09:27:59 -04:00
|
|
|
|
|
|
|
// expect one match
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.match, testCase.id)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-03-31 09:27:59 -04:00
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
|
|
|
|
// identical to the known PR
|
|
|
|
assert.Equal(t, pr.ID, prs[0].ID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-07-22 10:14:27 -04:00
|
|
|
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(db.DefaultContext, 1, "master")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
pr := prs[0]
|
|
|
|
assert.Equal(t, int64(2), pr.ID)
|
|
|
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
|
|
|
assert.Equal(t, "master", pr.BaseBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByIndex(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 05:37:59 -04:00
|
|
|
pr, err := issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 2)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
|
|
|
assert.Equal(t, int64(2), pr.Index)
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 9223372036854775807, 9223372036854775807)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2021-10-05 10:41:48 -04:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
_, err = issues_model.GetPullRequestByIndex(db.DefaultContext, 1, 0)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByID(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 05:37:59 -04:00
|
|
|
pr, err := issues_model.GetPullRequestByID(db.DefaultContext, 1)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, int64(1), pr.ID)
|
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
_, err = issues_model.GetPullRequestByID(db.DefaultContext, 9223372036854775807)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByIssueID(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 05:37:59 -04:00
|
|
|
pr, err := issues_model.GetPullRequestByIssueID(db.DefaultContext, 2)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
_, err = issues_model.GetPullRequestByIssueID(db.DefaultContext, 9223372036854775807)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_Update(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2017-02-07 06:47:55 -05:00
|
|
|
pr.BaseBranch = "baseBranch"
|
|
|
|
pr.HeadBranch = "headBranch"
|
2023-10-11 00:24:07 -04:00
|
|
|
pr.Update(db.DefaultContext)
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, "baseBranch", pr.BaseBranch)
|
|
|
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.CheckConsistencyFor(t, pr)
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_UpdateCols(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 05:37:59 -04:00
|
|
|
pr := &issues_model.PullRequest{
|
2017-01-01 13:15:09 -05:00
|
|
|
ID: 1,
|
|
|
|
BaseBranch: "baseBranch",
|
|
|
|
HeadBranch: "headBranch",
|
|
|
|
}
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, pr.UpdateCols(db.DefaultContext, "head_branch"))
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Equal(t, "master", pr.BaseBranch)
|
|
|
|
assert.Equal(t, "headBranch", pr.HeadBranch)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.CheckConsistencyFor(t, pr)
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequestList_LoadAttributes(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
prs := []*issues_model.PullRequest{
|
2022-08-15 22:22:25 -04:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issues_model.PullRequestList(prs).LoadAttributes(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
for _, pr := range prs {
|
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, pr.IssueID, pr.Issue.ID)
|
|
|
|
}
|
2017-01-08 22:15:07 -05:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes(db.DefaultContext))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestAddTestPullRequestTask
|
|
|
|
|
2018-08-13 15:04:39 -04:00
|
|
|
func TestPullRequest_IsWorkInProgress(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-11-19 03:12:33 -05:00
|
|
|
pr.LoadIssue(db.DefaultContext)
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2023-10-11 00:24:07 -04:00
|
|
|
assert.False(t, pr.IsWorkInProgress(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
|
|
|
|
pr.Issue.Title = "WIP: " + pr.Issue.Title
|
2023-10-11 00:24:07 -04:00
|
|
|
assert.True(t, pr.IsWorkInProgress(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
|
|
|
|
pr.Issue.Title = "[wip]: " + pr.Issue.Title
|
2023-10-11 00:24:07 -04:00
|
|
|
assert.True(t, pr.IsWorkInProgress(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
2022-11-19 03:12:33 -05:00
|
|
|
pr.LoadIssue(db.DefaultContext)
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
assert.Empty(t, pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
|
|
|
|
original := pr.Issue.Title
|
|
|
|
pr.Issue.Title = "WIP: " + original
|
2022-11-19 03:12:33 -05:00
|
|
|
assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
|
|
|
|
pr.Issue.Title = "[wip] " + original
|
2022-11-19 03:12:33 -05:00
|
|
|
assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix(db.DefaultContext))
|
2018-08-13 15:04:39 -04:00
|
|
|
}
|
2022-06-13 05:37:59 -04:00
|
|
|
|
|
|
|
func TestDeleteOrphanedObjects(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 05:37:59 -04:00
|
|
|
|
|
|
|
countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.EqualValues(t, 3, orphaned)
|
|
|
|
|
2022-11-19 03:12:33 -05:00
|
|
|
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
|
|
|
|
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2022-06-13 05:37:59 -04:00
|
|
|
assert.EqualValues(t, countBefore, countAfter)
|
|
|
|
}
|
2023-06-08 04:56:05 -04:00
|
|
|
|
|
|
|
func TestParseCodeOwnersLine(t *testing.T) {
|
|
|
|
type CodeOwnerTest struct {
|
|
|
|
Line string
|
|
|
|
Tokens []string
|
|
|
|
}
|
|
|
|
|
|
|
|
given := []CodeOwnerTest{
|
|
|
|
{Line: "", Tokens: nil},
|
|
|
|
{Line: "# comment", Tokens: []string{}},
|
|
|
|
{Line: "!.* @user1 @org1/team1", Tokens: []string{"!.*", "@user1", "@org1/team1"}},
|
|
|
|
{Line: `.*\\.js @user2 #comment`, Tokens: []string{`.*\.js`, "@user2"}},
|
2023-09-13 22:59:53 -04:00
|
|
|
{Line: `docs/(aws|google|azure)/[^/]*\\.(md|txt) @org3 @org2/team2`, Tokens: []string{`docs/(aws|google|azure)/[^/]*\.(md|txt)`, "@org3", "@org2/team2"}},
|
|
|
|
{Line: `\#path @org3`, Tokens: []string{`#path`, "@org3"}},
|
|
|
|
{Line: `path\ with\ spaces/ @org3`, Tokens: []string{`path with spaces/`, "@org3"}},
|
2023-06-08 04:56:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range given {
|
|
|
|
tokens := issues_model.TokenizeCodeOwnersLine(g.Line)
|
|
|
|
assert.Equal(t, g.Tokens, tokens, "Codeowners tokenizer failed")
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 10:34:49 -04:00
|
|
|
|
|
|
|
func TestGetApprovers(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-06-09 10:34:49 -04:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 5})
|
|
|
|
// Official reviews are already deduplicated. Allow unofficial reviews
|
|
|
|
// to assert that there are no duplicated approvers.
|
|
|
|
setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly = false
|
2023-10-11 00:24:07 -04:00
|
|
|
approvers := pr.GetApprovers(db.DefaultContext)
|
2023-09-13 22:59:53 -04:00
|
|
|
expected := "Reviewed-by: User Five <user5@example.com>\nReviewed-by: Org Six <org6@example.com>\n"
|
2023-06-09 10:34:49 -04:00
|
|
|
assert.EqualValues(t, expected, approvers)
|
|
|
|
}
|
2023-09-08 17:09:23 -04:00
|
|
|
|
2024-02-24 03:18:39 -05:00
|
|
|
func TestGetPullRequestByMergedCommit(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2024-02-24 03:18:39 -05:00
|
|
|
pr, err := issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 1, "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2024-02-24 03:18:39 -05:00
|
|
|
assert.EqualValues(t, 1, pr.ID)
|
|
|
|
|
|
|
|
_, err = issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 0, "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.ErrorAs(t, err, &issues_model.ErrPullRequestNotExist{})
|
2024-02-24 03:18:39 -05:00
|
|
|
_, err = issues_model.GetPullRequestByMergedCommit(db.DefaultContext, 1, "")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.ErrorAs(t, err, &issues_model.ErrPullRequestNotExist{})
|
2024-02-24 03:18:39 -05:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:09:23 -04:00
|
|
|
func TestMigrate_InsertPullRequests(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-09-08 17:09:23 -04:00
|
|
|
reponame := "repo1"
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
|
|
|
|
i := &issues_model.Issue{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
Title: "title1",
|
|
|
|
Content: "issuecontent1",
|
|
|
|
IsPull: true,
|
|
|
|
PosterID: owner.ID,
|
|
|
|
Poster: owner,
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &issues_model.PullRequest{
|
|
|
|
Issue: i,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := issues_model.InsertPullRequests(db.DefaultContext, p)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-09-08 17:09:23 -04:00
|
|
|
|
|
|
|
_ = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{IssueID: i.ID})
|
|
|
|
|
|
|
|
unittest.CheckConsistencyFor(t, &issues_model.Issue{}, &issues_model.PullRequest{})
|
|
|
|
}
|