2020-01-16 03:52:26 +00:00
|
|
|
// Copyright 2020 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 gitea
|
|
|
|
|
|
|
|
import (
|
2020-01-26 02:46:38 +00:00
|
|
|
"log"
|
2020-01-16 03:52:26 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPull(t *testing.T) {
|
2020-01-26 02:46:38 +00:00
|
|
|
log.Println("== TestPull ==")
|
2020-01-16 03:52:26 +00:00
|
|
|
c := newTestClient()
|
2020-09-14 03:37:09 +01:00
|
|
|
user, _, err := c.GetMyUserInfo()
|
2020-01-16 03:52:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-04-22 08:52:45 +01:00
|
|
|
repoName := "repo_pull_test"
|
|
|
|
forkOrg := "ForkOrg"
|
2020-04-23 13:51:36 +01:00
|
|
|
if !preparePullTest(t, c, repoName, forkOrg) {
|
2020-01-26 02:46:38 +00:00
|
|
|
return
|
2020-01-16 03:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoPullRequests list PRs of one repository
|
2020-09-14 03:37:09 +01:00
|
|
|
pulls, _, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
2020-01-16 03:52:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, pulls, 0)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pullUpdateFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
2022-04-22 08:52:45 +01:00
|
|
|
Base: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
Head: forkOrg + ":overwrite_licence",
|
|
|
|
Title: "overwrite a file",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, pullUpdateFile)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pullNewFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
2022-04-22 08:52:45 +01:00
|
|
|
Base: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
Head: forkOrg + ":new_file",
|
|
|
|
Title: "create a file",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, pullNewFile)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pullConflict, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
2022-04-22 08:52:45 +01:00
|
|
|
Base: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
Head: forkOrg + ":will_conflict",
|
|
|
|
Title: "this pull will conflict",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, pullConflict)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, pulls, 3)
|
|
|
|
|
2022-01-13 17:30:39 +00:00
|
|
|
diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index, PullRequestDiffOptions{
|
|
|
|
Binary: true,
|
|
|
|
})
|
2020-09-07 04:17:54 +01:00
|
|
|
assert.NoError(t, err)
|
2021-03-08 21:15:42 +00:00
|
|
|
assert.True(t, len(diff) > 1100 && len(diff) < 1300)
|
2020-09-14 03:37:09 +01:00
|
|
|
patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
|
2020-09-07 04:17:54 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, len(patch) > len(diff))
|
|
|
|
|
2021-07-03 13:29:50 +01:00
|
|
|
commits, _, err := c.ListPullRequestCommits(c.username, repoName, pullUpdateFile.Index, ListPullRequestCommitsOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, commits, 1) && assert.Len(t, commits[0].Files, 1) {
|
|
|
|
assert.EqualValues(t, "LICENSE", commits[0].Files[0].Filename)
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:51:36 +01:00
|
|
|
// test Update pull
|
2020-09-14 03:37:09 +01:00
|
|
|
pr, _, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, pullUpdateFile.HasMerged)
|
|
|
|
assert.True(t, pullUpdateFile.Mergeable)
|
2020-09-14 03:37:09 +01:00
|
|
|
merged, _, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
|
2020-05-20 23:41:21 +01:00
|
|
|
Style: MergeStyleSquash,
|
|
|
|
Title: pullUpdateFile.Title,
|
|
|
|
Message: "squash: " + pullUpdateFile.Title,
|
2020-04-23 13:51:36 +01:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, merged)
|
2020-09-14 03:37:09 +01:00
|
|
|
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullUpdateFile.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, merged)
|
2020-09-14 03:37:09 +01:00
|
|
|
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, pullUpdateFile.Head.Name, pr.Head.Name)
|
|
|
|
assert.EqualValues(t, pullUpdateFile.Base.Name, pr.Base.Name)
|
|
|
|
assert.NotEqual(t, pullUpdateFile.Base.Sha, pr.Base.Sha)
|
|
|
|
assert.Len(t, *pr.MergedCommitID, 40)
|
|
|
|
assert.True(t, pr.HasMerged)
|
|
|
|
|
|
|
|
// test conflict pull
|
2020-09-14 03:37:09 +01:00
|
|
|
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
2022-04-22 08:52:45 +01:00
|
|
|
assert.False(t, pr.HasMerged)
|
|
|
|
assert.False(t, pr.Mergeable)
|
2020-09-14 03:37:09 +01:00
|
|
|
merged, _, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
|
2020-05-20 23:41:21 +01:00
|
|
|
Style: MergeStyleMerge,
|
|
|
|
Title: "pullConflict",
|
|
|
|
Message: "pullConflict Msg",
|
2020-04-23 13:51:36 +01:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, merged)
|
2020-09-14 03:37:09 +01:00
|
|
|
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullConflict.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, merged)
|
2020-09-14 03:37:09 +01:00
|
|
|
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, pr.MergedCommitID)
|
|
|
|
assert.False(t, pr.HasMerged)
|
|
|
|
|
|
|
|
state := StateClosed
|
2020-09-14 03:37:09 +01:00
|
|
|
pr, _, err = c.EditPullRequest(user.UserName, repoName, pullConflict.Index, EditPullRequestOption{
|
2020-04-23 13:51:36 +01:00
|
|
|
Title: "confl",
|
|
|
|
State: &state,
|
|
|
|
})
|
2020-05-27 14:04:51 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, state, pr.State)
|
2020-04-23 13:51:36 +01:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{
|
2020-04-23 13:51:36 +01:00
|
|
|
State: StateClosed,
|
|
|
|
Sort: "leastupdate",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, pulls, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
|
2020-09-14 03:37:09 +01:00
|
|
|
_, _ = c.DeleteRepo(forkOrg, repoName)
|
|
|
|
_, _ = c.DeleteRepo(c.username, repoName)
|
|
|
|
_, _ = c.DeleteOrg(forkOrg)
|
2020-04-23 13:51:36 +01:00
|
|
|
|
|
|
|
origRepo, err := createTestRepo(t, repoName, c)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
org, _, err := c.CreateOrg(CreateOrgOption{Name: forkOrg})
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
forkRepo, _, err := c.CreateFork(origRepo.Owner.UserName, origRepo.Name, CreateForkOption{Organization: &org.UserName})
|
2020-04-23 13:51:36 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, forkRepo)
|
|
|
|
|
2022-04-22 08:52:45 +01:00
|
|
|
mainLicense, _, err := c.GetContents(forkRepo.Owner.UserName, forkRepo.Name, "main", "LICENSE")
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, mainLicense) {
|
2020-04-23 13:51:36 +01:00
|
|
|
return false
|
|
|
|
}
|
2020-01-16 03:52:26 +00:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
updatedFile, _, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
|
2020-05-21 11:13:32 +01:00
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "Overwrite",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2020-05-21 11:13:32 +01:00
|
|
|
NewBranchName: "overwrite_licence",
|
2020-04-23 13:51:36 +01:00
|
|
|
},
|
2022-04-22 08:52:45 +01:00
|
|
|
SHA: mainLicense.SHA,
|
2020-04-23 13:51:36 +01:00
|
|
|
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
|
|
|
|
})
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, updatedFile) {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-16 03:52:26 +00:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
newFile, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
|
2020-04-23 13:51:36 +01:00
|
|
|
Content: "QSBuZXcgRmlsZQo=",
|
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "creat a new file",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
NewBranchName: "new_file",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-16 03:52:26 +00:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
conflictFile1, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
|
2020-04-23 13:51:36 +01:00
|
|
|
Content: "U3RhcnQgQ29uZmxpY3QK",
|
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "Start Conflict",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile1) {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-16 03:52:26 +00:00
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
conflictFile2, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
|
2020-04-23 13:51:36 +01:00
|
|
|
Content: "V2lsbEhhdmUgQ29uZmxpY3QK",
|
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "creat a new file witch will conflict",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2020-04-23 13:51:36 +01:00
|
|
|
NewBranchName: "will_conflict",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile2) {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-16 03:52:26 +00:00
|
|
|
|
2020-04-23 13:51:36 +01:00
|
|
|
return true
|
2020-01-16 03:52:26 +00:00
|
|
|
}
|