Merge pull request #51 from gfleury/CreatePullRequest

Adding new wrapper for CreatePullRequest and struct as parameter
This commit is contained in:
George Fleury 2020-03-16 21:46:09 -03:00 committed by GitHub
commit ab43fa296d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 16 deletions

View File

@ -42,7 +42,7 @@ type CloneLink struct {
}
type Links struct {
Self []SelfLink `json:"self"`
Self []SelfLink `json:"self,omitempty"`
}
type Project struct {
@ -78,14 +78,14 @@ type UserWithNameEmail struct {
}
type UserWithLinks struct {
Name string `json:"name"`
Email string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
Links Links `json:"links"`
Name string `json:"name,omitempty"`
Email string `json:"emailAddress,omitempty"`
ID int `json:"id,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Active bool `json:"active,omitempty"`
Slug string `json:"slug,omitempty"`
Type string `json:"type,omitempty"`
Links Links `json:"links,omitempty"`
}
type User struct {
@ -99,10 +99,10 @@ type User struct {
}
type UserWithMetadata struct {
User UserWithLinks `json:"user"`
Role string `json:"role"`
Approved bool `json:"approved"`
Status string `json:"status"`
User UserWithLinks `json:"user,omitempty"`
Role string `json:"role,omitempty"`
Approved bool `json:"approved,omitempty"`
Status string `json:"status,omitempty"`
}
// PermissionGlobal are global permissions
@ -192,9 +192,9 @@ type PullRequest struct {
FromRef PullRequestRef `json:"fromRef"`
ToRef PullRequestRef `json:"toRef"`
Locked bool `json:"locked"`
Author UserWithMetadata `json:"author"`
Author *UserWithMetadata `json:"author,omitempty"`
Reviewers []UserWithMetadata `json:"reviewers"`
Participants []UserWithMetadata `json:"participants"`
Participants []UserWithMetadata `json:"participants,omitempty"`
Properties struct {
MergeResult MergeResult `json:"mergeResult"`
ResolvedTaskCount int `json:"resolvedTaskCount"`

View File

@ -10,6 +10,8 @@ import (
"io/ioutil"
"net/url"
"strings"
"github.com/mitchellh/mapstructure"
)
// DefaultAPIService default service
@ -583,6 +585,17 @@ Create a new pull request between two branches. The branches may be in the same
@return */
func (a *DefaultApiService) Create(projectKey, repositorySlug string, localVarOptionals map[string]interface{}) (*APIResponse, error) {
var pullRequest PullRequest
err := mapstructure.Decode(localVarOptionals, &pullRequest)
if err != nil {
return nil, err
}
return a.CreatePullRequest(projectKey, repositorySlug, pullRequest)
}
func (a *DefaultApiService) CreatePullRequest(projectKey, repositorySlug string, pullRequest PullRequest) (*APIResponse, error) {
var (
localVarHTTPMethod = strings.ToUpper("Post")
localVarPostBody interface{}
@ -599,7 +612,7 @@ func (a *DefaultApiService) Create(projectKey, repositorySlug string, localVarOp
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
localVarPostBody, err := json.Marshal(localVarOptionals)
localVarPostBody, err := json.Marshal(pullRequest)
if err != nil {
return nil, err
}

View File

@ -431,6 +431,46 @@ func TestDefaultApiService_Create(t *testing.T) {
wantErr, integrationTest bool
}{
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Post https://stash.domain.com/rest/api/1.0/projects//repos//pull-requests: context canceled"}, true, false},
{"InvalidRequest", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
localVarOptionals: map[string]interface{}{"values": "values"}},
&APIResponse{
Message: "Status: 400 , Body: {errors:[{context:null,message:title must be supplied for this request,exceptionName:null}]}"},
true, true},
{"ValidRequestNoBranch", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
localVarOptionals: map[string]interface{}{
"title": "test PR",
"description": "test Desc",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": map[string]interface{}{
"id": "refs/heads/feature",
"repository": map[string]interface{}{
"slug": "repo1",
"project": map[string]interface{}{
"key": "PROJ",
},
},
},
"toRef": map[string]interface{}{
"id": "refs/heads/master",
"repository": map[string]interface{}{
"slug": "repo1",
"project": map[string]interface{}{
"key": "PROJ",
},
},
},
"locked": false,
},
},
&APIResponse{
Message: `Status: 404 , Body: {errors:[{context:null,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException}]}`},
true, true},
}
for _, tt := range tests {
if tt.integrationTest != runIntegrationTests {
@ -445,6 +485,7 @@ func TestDefaultApiService_Create(t *testing.T) {
t.Errorf("DefaultApiService.Create() error = %v, wantErr %v", err, tt.wantErr)
return
}
got.Response = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DefaultApiService.Create() = %v, want %v", got, tt.want)
}
@ -452,6 +493,84 @@ func TestDefaultApiService_Create(t *testing.T) {
}
}
func TestDefaultApiService_CreatePullRequest(t *testing.T) {
type fields struct {
client *APIClient
}
type args struct {
projectKey string
repositorySlug string
localVarOptionals PullRequest
}
tests := []struct {
name string
fields fields
args args
want *APIResponse
wantErr, integrationTest bool
}{
{"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{}, &APIResponse{Message: "Post https://stash.domain.com/rest/api/1.0/projects//repos//pull-requests: context canceled"}, true, false},
{"InvalidRequest", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
localVarOptionals: PullRequest{},
},
&APIResponse{
Message: "Status: 400 , Body: {errors:[{context:null,message:title must be supplied for this request,exceptionName:null}]}"},
true, true},
{"ValidRequestNoBranch", fields{client: generateConfigRealLocalServer()},
args{projectKey: "PROJ",
repositorySlug: "repo1",
localVarOptionals: PullRequest{
Title: "test PR",
Description: "test Desc",
State: "OPEN",
Open: true,
Closed: false,
FromRef: PullRequestRef{
ID: "refs/heads/feature",
Repository: Repository{
Slug: "repo1",
Project: Project{
Key: "PROJ",
},
},
},
ToRef: PullRequestRef{
ID: "refs/heads/master",
Repository: Repository{
Slug: "repo1",
Project: Project{
Key: "PROJ",
},
},
},
Locked: false,
},
},
&APIResponse{Message: `Status: 404 , Body: {errors:[{context:null,message:Repository \repo1\ of project with key \PROJ\ has no branch \refs/heads/feature\,exceptionName:com.atlassian.bitbucket.commit.NoSuchCommitException}]}`},
true, true},
}
for _, tt := range tests {
if tt.integrationTest != runIntegrationTests {
continue
}
t.Run(tt.name, func(t *testing.T) {
a := &DefaultApiService{
client: tt.fields.client,
}
got, err := a.CreatePullRequest(tt.args.projectKey, tt.args.repositorySlug, tt.args.localVarOptionals)
if (err != nil) != tt.wantErr {
t.Errorf("DefaultApiService.Create() error = %v, wantErr %v", err, tt.wantErr)
return
}
got.Response = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DefaultApiService.Create() = %v, want %v", got, tt.want)
}
})
}
}
func TestDefaultApiService_CreateBranch(t *testing.T) {
type fields struct {
client *APIClient