mirror of
https://github.com/gfleury/go-bitbucket-v1.git
synced 2025-04-18 06:38:06 -05:00
Merge pull request #51 from gfleury/CreatePullRequest
Adding new wrapper for CreatePullRequest and struct as parameter
This commit is contained in:
commit
ab43fa296d
@ -42,7 +42,7 @@ type CloneLink struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Links struct {
|
type Links struct {
|
||||||
Self []SelfLink `json:"self"`
|
Self []SelfLink `json:"self,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Project struct {
|
type Project struct {
|
||||||
@ -78,14 +78,14 @@ type UserWithNameEmail struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserWithLinks struct {
|
type UserWithLinks struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name,omitempty"`
|
||||||
Email string `json:"emailAddress"`
|
Email string `json:"emailAddress,omitempty"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id,omitempty"`
|
||||||
DisplayName string `json:"displayName"`
|
DisplayName string `json:"displayName,omitempty"`
|
||||||
Active bool `json:"active"`
|
Active bool `json:"active,omitempty"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug,omitempty"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type,omitempty"`
|
||||||
Links Links `json:"links"`
|
Links Links `json:"links,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
@ -99,10 +99,10 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UserWithMetadata struct {
|
type UserWithMetadata struct {
|
||||||
User UserWithLinks `json:"user"`
|
User UserWithLinks `json:"user,omitempty"`
|
||||||
Role string `json:"role"`
|
Role string `json:"role,omitempty"`
|
||||||
Approved bool `json:"approved"`
|
Approved bool `json:"approved,omitempty"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PermissionGlobal are global permissions
|
// PermissionGlobal are global permissions
|
||||||
@ -192,9 +192,9 @@ type PullRequest struct {
|
|||||||
FromRef PullRequestRef `json:"fromRef"`
|
FromRef PullRequestRef `json:"fromRef"`
|
||||||
ToRef PullRequestRef `json:"toRef"`
|
ToRef PullRequestRef `json:"toRef"`
|
||||||
Locked bool `json:"locked"`
|
Locked bool `json:"locked"`
|
||||||
Author UserWithMetadata `json:"author"`
|
Author *UserWithMetadata `json:"author,omitempty"`
|
||||||
Reviewers []UserWithMetadata `json:"reviewers"`
|
Reviewers []UserWithMetadata `json:"reviewers"`
|
||||||
Participants []UserWithMetadata `json:"participants"`
|
Participants []UserWithMetadata `json:"participants,omitempty"`
|
||||||
Properties struct {
|
Properties struct {
|
||||||
MergeResult MergeResult `json:"mergeResult"`
|
MergeResult MergeResult `json:"mergeResult"`
|
||||||
ResolvedTaskCount int `json:"resolvedTaskCount"`
|
ResolvedTaskCount int `json:"resolvedTaskCount"`
|
||||||
|
@ -10,6 +10,8 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mitchellh/mapstructure"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultAPIService default service
|
// DefaultAPIService default service
|
||||||
@ -583,6 +585,17 @@ Create a new pull request between two branches. The branches may be in the same
|
|||||||
|
|
||||||
@return */
|
@return */
|
||||||
func (a *DefaultApiService) Create(projectKey, repositorySlug string, localVarOptionals map[string]interface{}) (*APIResponse, error) {
|
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 (
|
var (
|
||||||
localVarHTTPMethod = strings.ToUpper("Post")
|
localVarHTTPMethod = strings.ToUpper("Post")
|
||||||
localVarPostBody interface{}
|
localVarPostBody interface{}
|
||||||
@ -599,7 +612,7 @@ func (a *DefaultApiService) Create(projectKey, repositorySlug string, localVarOp
|
|||||||
localVarQueryParams := url.Values{}
|
localVarQueryParams := url.Values{}
|
||||||
localVarFormParams := url.Values{}
|
localVarFormParams := url.Values{}
|
||||||
|
|
||||||
localVarPostBody, err := json.Marshal(localVarOptionals)
|
localVarPostBody, err := json.Marshal(pullRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -431,6 +431,46 @@ func TestDefaultApiService_Create(t *testing.T) {
|
|||||||
wantErr, integrationTest bool
|
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},
|
{"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 {
|
for _, tt := range tests {
|
||||||
if tt.integrationTest != runIntegrationTests {
|
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)
|
t.Errorf("DefaultApiService.Create() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
got.Response = nil
|
||||||
if !reflect.DeepEqual(got, tt.want) {
|
if !reflect.DeepEqual(got, tt.want) {
|
||||||
t.Errorf("DefaultApiService.Create() = %v, want %v", 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) {
|
func TestDefaultApiService_CreateBranch(t *testing.T) {
|
||||||
type fields struct {
|
type fields struct {
|
||||||
client *APIClient
|
client *APIClient
|
||||||
|
Loading…
x
Reference in New Issue
Block a user