2017-01-01 13:15:09 -05:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-09 14:57:58 -05:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullRequest_LoadAttributes(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NoError(t, pr.LoadAttributes())
|
|
|
|
assert.NotNil(t, pr.Merger)
|
|
|
|
assert.Equal(t, pr.MergerID, pr.Merger.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_LoadIssue(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NoError(t, pr.LoadIssue())
|
|
|
|
assert.NotNil(t, pr.Issue)
|
|
|
|
assert.Equal(t, int64(2), pr.Issue.ID)
|
2017-01-08 22:15:07 -05:00
|
|
|
assert.NoError(t, pr.LoadIssue())
|
|
|
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
2020-03-02 17:31:55 -05:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo())
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NotNil(t, pr.BaseRepo)
|
|
|
|
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
|
2020-03-02 17:31:55 -05:00
|
|
|
assert.NoError(t, pr.LoadBaseRepo())
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
2020-03-02 17:31:55 -05:00
|
|
|
assert.NoError(t, pr.LoadHeadRepo())
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
prs, count, err := PullRequests(1, &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",
|
|
|
|
Labels: []string{},
|
|
|
|
})
|
|
|
|
assert.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
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequestsOldest(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
prs, count, err := PullRequests(1, &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",
|
|
|
|
Labels: []string{},
|
|
|
|
})
|
|
|
|
assert.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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-07-28 05:42:56 -04:00
|
|
|
pr, err := GetUnmergedPullRequest(1, 1, "branch2", "master", PullRequestFlowGithub)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), pr.ID)
|
|
|
|
|
2021-07-28 05:42:56 -04:00
|
|
|
_, err = GetUnmergedPullRequest(1, 9223372036854775807, "branch1", "master", PullRequestFlowGithub)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrPullRequestNotExist(err))
|
|
|
|
}
|
|
|
|
|
2022-01-03 14:45:58 -05:00
|
|
|
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
2022-05-03 15:46:28 -04:00
|
|
|
exist, err := HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
|
2022-01-03 14:45:58 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, exist)
|
|
|
|
|
2022-05-03 15:46:28 -04:00
|
|
|
exist, err = HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "not_exist_branch")
|
2022-01-03 14:45:58 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exist)
|
|
|
|
}
|
|
|
|
|
2017-01-01 13:15:09 -05:00
|
|
|
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, prs, 1)
|
|
|
|
for _, pr := range prs {
|
|
|
|
assert.Equal(t, int64(1), pr.HeadRepoID)
|
|
|
|
assert.Equal(t, "branch2", pr.HeadBranch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
prs, err := GetUnmergedPullRequestsByBaseInfo(1, "master")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
pr, err := GetPullRequestByIndex(1, 2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), pr.BaseRepoID)
|
|
|
|
assert.Equal(t, int64(2), pr.Index)
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
_, err = GetPullRequestByIndex(9223372036854775807, 9223372036854775807)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrPullRequestNotExist(err))
|
2021-10-05 10:41:48 -04:00
|
|
|
|
|
|
|
_, err = GetPullRequestByIndex(1, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrPullRequestNotExist(err))
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-04-28 07:48:48 -04:00
|
|
|
pr, err := GetPullRequestByID(db.DefaultContext, 1)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(1), pr.ID)
|
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2022-04-28 07:48:48 -04:00
|
|
|
_, err = GetPullRequestByID(db.DefaultContext, 9223372036854775807)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrPullRequestNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetPullRequestByIssueID(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
pr, err := GetPullRequestByIssueID(2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2), pr.IssueID)
|
|
|
|
|
2019-06-12 15:41:28 -04:00
|
|
|
_, err = GetPullRequestByIssueID(9223372036854775807)
|
2017-01-01 13:15:09 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, IsErrPullRequestNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_Update(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
2017-02-07 06:47:55 -05:00
|
|
|
pr.BaseBranch = "baseBranch"
|
|
|
|
pr.HeadBranch = "headBranch"
|
2017-01-01 13:15:09 -05:00
|
|
|
pr.Update()
|
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: pr.ID}).(*PullRequest)
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
pr := &PullRequest{
|
|
|
|
ID: 1,
|
|
|
|
BaseBranch: "baseBranch",
|
|
|
|
HeadBranch: "headBranch",
|
|
|
|
}
|
2020-01-10 00:50:23 -05:00
|
|
|
assert.NoError(t, pr.UpdateCols("head_branch"))
|
2017-01-01 13:15:09 -05:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
pr = unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
|
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) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-01-01 13:15:09 -05:00
|
|
|
|
|
|
|
prs := []*PullRequest{
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest),
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest),
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
assert.NoError(t, PullRequestList(prs).LoadAttributes())
|
|
|
|
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
|
|
|
|
|
|
|
assert.NoError(t, PullRequestList([]*PullRequest{}).LoadAttributes())
|
2017-01-01 13:15:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestAddTestPullRequestTask
|
|
|
|
|
2018-08-13 15:04:39 -04:00
|
|
|
func TestPullRequest_IsWorkInProgress(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
|
2018-08-13 15:04:39 -04:00
|
|
|
pr.LoadIssue()
|
|
|
|
|
|
|
|
assert.False(t, pr.IsWorkInProgress())
|
|
|
|
|
|
|
|
pr.Issue.Title = "WIP: " + pr.Issue.Title
|
|
|
|
assert.True(t, pr.IsWorkInProgress())
|
|
|
|
|
|
|
|
pr.Issue.Title = "[wip]: " + pr.Issue.Title
|
|
|
|
assert.True(t, pr.IsWorkInProgress())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_GetWorkInProgressPrefixWorkInProgress(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2018-08-13 15:04:39 -04:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
|
2018-08-13 15:04:39 -04:00
|
|
|
pr.LoadIssue()
|
|
|
|
|
|
|
|
assert.Empty(t, pr.GetWorkInProgressPrefix())
|
|
|
|
|
|
|
|
original := pr.Issue.Title
|
|
|
|
pr.Issue.Title = "WIP: " + original
|
|
|
|
assert.Equal(t, "WIP:", pr.GetWorkInProgressPrefix())
|
|
|
|
|
|
|
|
pr.Issue.Title = "[wip] " + original
|
|
|
|
assert.Equal(t, "[wip]", pr.GetWorkInProgressPrefix())
|
|
|
|
}
|
2021-05-08 15:19:42 -04:00
|
|
|
|
|
|
|
func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
|
2021-05-08 15:19:42 -04:00
|
|
|
|
2022-05-03 15:46:28 -04:00
|
|
|
msg, err := pr.GetDefaultMergeMessage(db.DefaultContext)
|
2022-03-31 10:53:08 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", msg)
|
2021-05-08 15:19:42 -04:00
|
|
|
|
|
|
|
pr.BaseRepoID = 1
|
|
|
|
pr.HeadRepoID = 2
|
2022-05-03 15:46:28 -04:00
|
|
|
msg, err = pr.GetDefaultMergeMessage(db.DefaultContext)
|
2022-03-31 10:53:08 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Merge pull request 'issue3' (#3) from user2/repo1:branch2 into master", msg)
|
2021-05-08 15:19:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-05-08 15:19:42 -04:00
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
externalTracker := repo_model.RepoUnit{
|
2021-11-09 14:57:58 -05:00
|
|
|
Type: unit.TypeExternalTracker,
|
2021-12-09 20:27:50 -05:00
|
|
|
Config: &repo_model.ExternalTrackerConfig{
|
2021-05-08 15:19:42 -04:00
|
|
|
ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
|
|
|
|
},
|
|
|
|
}
|
2021-12-09 20:27:50 -05:00
|
|
|
baseRepo := &repo_model.Repository{Name: "testRepo", ID: 1}
|
2021-11-24 04:49:20 -05:00
|
|
|
baseRepo.Owner = &user_model.User{Name: "testOwner"}
|
2021-12-09 20:27:50 -05:00
|
|
|
baseRepo.Units = []*repo_model.RepoUnit{&externalTracker}
|
2021-05-08 15:19:42 -04:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest)
|
2021-05-08 15:19:42 -04:00
|
|
|
|
2022-05-03 15:46:28 -04:00
|
|
|
msg, err := pr.GetDefaultMergeMessage(db.DefaultContext)
|
2022-03-31 10:53:08 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Merge pull request 'issue3' (!3) from branch2 into master", msg)
|
2021-05-08 15:19:42 -04:00
|
|
|
|
|
|
|
pr.BaseRepoID = 1
|
|
|
|
pr.HeadRepoID = 2
|
2022-05-03 15:46:28 -04:00
|
|
|
msg, err = pr.GetDefaultMergeMessage(db.DefaultContext)
|
2022-03-31 10:53:08 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "Merge pull request 'issue3' (!3) from user2/repo1:branch2 into master", msg)
|
2021-05-08 15:19:42 -04:00
|
|
|
}
|
2022-05-03 15:46:28 -04:00
|
|
|
|
|
|
|
func TestPullRequest_GetDefaultSquashMessage(t *testing.T) {
|
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
|
|
|
|
|
|
|
|
msg, err := pr.GetDefaultSquashMessage(db.DefaultContext)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "issue3 (#3)", msg)
|
|
|
|
}
|