Fix MergePullRequest & extend Tests (#278)

rm MergePullRequestResponse

Fix MergePullRequest

Extend Tests

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/278
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
6543 2020-04-23 12:51:36 +00:00
parent 0cf676d9f9
commit 79665cae15
2 changed files with 161 additions and 27 deletions

View file

@ -153,19 +153,17 @@ type MergePullRequestOption struct {
MergeMessageField string `json:"MergeMessageField"` MergeMessageField string `json:"MergeMessageField"`
} }
// MergePullRequestResponse response when merging a pull request
type MergePullRequestResponse struct {
}
// MergePullRequest merge a PR to repository by PR id // MergePullRequest merge a PR to repository by PR id
func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (*MergePullRequestResponse, error) { func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (bool, error) {
body, err := json.Marshal(&opt) body, err := json.Marshal(&opt)
if err != nil { if err != nil {
return nil, err return false, err
} }
response := new(MergePullRequestResponse) status, err := c.getStatusCode("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), jsonHeader, bytes.NewReader(body))
return response, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index), if err != nil {
jsonHeader, bytes.NewReader(body), response) return false, err
}
return status == 200, nil
} }
// IsPullRequestMerged test if one PR is merged to one repository // IsPullRequestMerged test if one PR is merged to one repository

View file

@ -18,34 +18,170 @@ func TestPull(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
var repoName = "repo_pull_test" var repoName = "repo_pull_test"
_, err = createTestRepo(t, repoName, c) var forkOrg = "ForkOrg"
if err != nil { if !preparePullTest(t, c, repoName, forkOrg) {
return return
} }
// ListRepoPullRequests list PRs of one repository // ListRepoPullRequests list PRs of one repository
pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{ pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
ListOptions: ListOptions{Page: 1},
State: StateAll,
Sort: "leastupdate",
})
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, pulls, 0) assert.Len(t, pulls, 0)
//ToDo add git stuff to have different branches witch can be used to create PRs and test merge etc ... pullUpdateFile, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
Base: "master",
Head: forkOrg + ":overwrite_licence",
Title: "overwrite a file",
})
assert.NoError(t, err)
assert.NotNil(t, pullUpdateFile)
// GetPullRequest get information of one PR pullNewFile, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
//func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) Base: "master",
Head: forkOrg + ":new_file",
Title: "create a file",
})
assert.NoError(t, err)
assert.NotNil(t, pullNewFile)
// CreatePullRequest create pull request with options pullConflict, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
//func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) Base: "master",
Head: forkOrg + ":will_conflict",
Title: "this pull will conflict",
})
assert.NoError(t, err)
assert.NotNil(t, pullConflict)
// EditPullRequest modify pull request with PR id and options pulls, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
//func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) assert.NoError(t, err)
assert.Len(t, pulls, 3)
// MergePullRequest merge a PR to repository by PR id // test Update pull
//func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (*MergePullRequestResponse, error) pr, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.False(t, pullUpdateFile.HasMerged)
assert.True(t, pullUpdateFile.Mergeable)
merged, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
Do: "squash",
MergeTitleField: pullUpdateFile.Title,
MergeMessageField: "squash: " + pullUpdateFile.Title,
})
assert.NoError(t, err)
assert.True(t, merged)
merged, err = c.IsPullRequestMerged(user.UserName, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.True(t, merged)
pr, err = c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
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)
// IsPullRequestMerged test if one PR is merged to one repository // test conflict pull
//func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) pr, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.False(t, pullConflict.HasMerged)
assert.False(t, pullConflict.Mergeable)
merged, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
Do: "merge",
MergeTitleField: "pullConflict",
MergeMessageField: "pullConflict Msg",
})
assert.NoError(t, err)
assert.False(t, merged)
merged, err = c.IsPullRequestMerged(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.False(t, merged)
pr, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.Nil(t, pr.MergedCommitID)
assert.False(t, pr.HasMerged)
state := StateClosed
pr, err = c.EditPullRequest(user.UserName, repoName, pullConflict.Index, EditPullRequestOption{
Title: "confl",
State: &state,
})
pulls, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{
State: StateClosed,
Sort: "leastupdate",
})
assert.NoError(t, err)
assert.Len(t, pulls, 2)
}
func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
_ = c.DeleteRepo(forkOrg, repoName)
_ = c.DeleteRepo(c.username, repoName)
_ = c.DeleteOrg(forkOrg)
origRepo, err := createTestRepo(t, repoName, c)
if !assert.NoError(t, err) {
return false
}
org, err := c.CreateOrg(CreateOrgOption{UserName: forkOrg})
assert.NoError(t, err)
forkRepo, err := c.CreateFork(origRepo.Owner.UserName, origRepo.Name, CreateForkOption{Organization: &org.UserName})
assert.NoError(t, err)
assert.NotNil(t, forkRepo)
masterLicence, err := c.GetContents(forkRepo.Owner.UserName, forkRepo.Name, "master", "LICENSE")
if !assert.NoError(t, err) || !assert.NotNil(t, masterLicence) {
return false
}
updatedFile, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
DeleteFileOptions: DeleteFileOptions{
FileOptions: FileOptions{
Message: "Overwrite",
BranchName: "master",
NewBranchName: "overwrite_licence",
},
SHA: masterLicence.SHA,
},
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
})
if !assert.NoError(t, err) || !assert.NotNil(t, updatedFile) {
return false
}
newFile, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
Content: "QSBuZXcgRmlsZQo=",
FileOptions: FileOptions{
Message: "creat a new file",
BranchName: "master",
NewBranchName: "new_file",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
return false
}
conflictFile1, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
Content: "U3RhcnQgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "Start Conflict",
BranchName: "master",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile1) {
return false
}
conflictFile2, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
Content: "V2lsbEhhdmUgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "creat a new file witch will conflict",
BranchName: "master",
NewBranchName: "will_conflict",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile2) {
return false
}
return true
} }