From b3c7f99f7f5cd7ca31a96948e19b2ceaea5b0283 Mon Sep 17 00:00:00 2001 From: gfleury Date: Fri, 27 Mar 2020 15:51:23 -0300 Subject: [PATCH] Adding search endpoint SearchCode --- api_response.go | 64 +++++++++++++++++++++ api_response_test.go | 72 +++++++++++++++++++++++ default_api.go | 59 +++++++++++++++++++ default_api_test.go | 83 ++++++++++++++++++++++++--- test/bb-mock-server/go/api_default.go | 4 ++ test/bb-mock-server/go/routers.go | 7 +++ 6 files changed, 281 insertions(+), 8 deletions(-) diff --git a/api_response.go b/api_response.go index 79a9959..cdef3ca 100644 --- a/api_response.go +++ b/api_response.go @@ -71,6 +71,17 @@ type Repository struct { Clone []CloneLink `json:"clone,omitempty"` Self []SelfLink `json:"self,omitempty"` } `json:"links,omitempty"` + Owner *struct { + Name string `json:"name"` + EmailAddress string `json:"emailAddress"` + ID int `json:"id"` + DisplayName string `json:"displayName"` + Active bool `json:"active"` + Slug string `json:"slug"` + Type string `json:"type"` + AvatarURL string `json:"avatarUrl"` + } `json:"owner,omitempty"` + Origin *Repository `json:"origin,omitempty"` } type UserWithNameEmail struct { @@ -349,6 +360,52 @@ type Webhook struct { Configuration WebhookConfiguration `json:"configuration"` } +type Code struct { + Category string `json:"category,omitempty"` + IsLastPage bool `json:"isLastPage,omitempty"` + Count int `json:"count,omitempty"` + Start int `json:"start,omitempty"` + Limit int `json:"limit,omitempty"` + NextStart int `json:"nextStart,omitempty"` + Values []struct { + Repository *Repository `json:"repository,omitempty"` + File string `json:"file"` + HitContexts [][]struct { + Line int `json:"line"` + Text string `json:"text"` + } `json:"hitContexts"` + HitCount int `json:"hitCount"` + } `json:"values,omitempty"` +} + +type Scope struct { + Type string `json:"type"` +} + +type Query struct { + Substituted bool `json:"substituted"` +} + +type SearchResult struct { + Scope Scope `json:"scope"` + Code *Code `json:"code,omitempty"` + Repositories *Code `json:"repositories,omitempty"` + Query Query `json:"query"` +} + +type Limits struct { + Primary int `json:"primary"` + Secondary int `json:"secondary"` +} + +type SearchQuery struct { + Query string `json:"query"` + Entities struct { + Code Code `json:"code"` + } `json:"entities"` + Limits Limits `json:"limits"` +} + // String converts global permission to its string representation func (p PermissionGlobal) String() string { return string(p) @@ -482,6 +539,13 @@ func GetWebhooksResponse(r *APIResponse) ([]Webhook, error) { return h, err } +// GetSearchResultResponse cast Search results into structure +func GetSearchResultResponse(r *APIResponse) (SearchResult, error) { + var h SearchResult + err := mapstructure.Decode(r.Values, &h) + return h, err +} + // NewAPIResponse create new APIResponse from http.Response func NewAPIResponse(r *http.Response) *APIResponse { diff --git a/api_response_test.go b/api_response_test.go index dc506f6..9d1f198 100644 --- a/api_response_test.go +++ b/api_response_test.go @@ -1036,3 +1036,75 @@ func TestHasNextPage(t *testing.T) { }) } } + +func TestGetSearchResultResponse(t *testing.T) { + type args struct { + r *APIResponse + } + tests := []struct { + name string + args args + want SearchResult + wantErr bool + }{ + { + name: "Empty list", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{}, + }, + }, + want: SearchResult{}, + }, + { + name: "Bad response", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{"values": "not an array"}, + }, + }, + want: SearchResult{}, + }, + { + name: "Good response", + args: args{ + r: &APIResponse{ + Values: map[string]interface{}{ + "scope": map[string]interface{}{"type": "GLOBAL"}, + "code": map[string]interface{}{ + "category": "primary", + "isLastPage": false, + "count": 187, + "start": 0, + "nextStart": 25, + "values": []map[string]interface{}{}, + }, + }, + }, + }, + want: SearchResult{ + Scope: Scope{ + Type: "GLOBAL", + }, + Code: &Code{ + Category: "primary", + IsLastPage: false, + Count: 187, + NextStart: 25, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetSearchResultResponse(tt.args.r) + if (err != nil) != tt.wantErr { + t.Errorf("GetDiffResponse() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("GetDiffResponse() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/default_api.go b/default_api.go index 300a4d0..137f4df 100644 --- a/default_api.go +++ b/default_api.go @@ -13697,3 +13697,62 @@ func (a *DefaultApiService) SetCommitStatus(commitId string, buildStatus BuildSt return NewBitbucketAPIResponse(localVarHTTPResponse) } + +/* +SearchCode + + * @param searchString Search string + +*/ +func (a *DefaultApiService) SearchCode(query SearchQuery) (*APIResponse, error) { + var ( + localVarHttpMethod = strings.ToUpper("Post") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + ) + + // create path and map variables + localVarPath := a.client.cfg.BasePath + "/search/latest/search" + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + // to determine the Content-Type header + localVarHttpContentTypes := []string{} + + // set Content-Type header + localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes) + if localVarHttpContentType != "" { + localVarHeaderParams["Content-Type"] = localVarHttpContentType + } + + // to determine the Accept header + localVarHttpHeaderAccepts := []string{"application/json"} + + // set Accept header + localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts) + if localVarHttpHeaderAccept != "" { + localVarHeaderParams["Accept"] = localVarHttpHeaderAccept + } + + // body params + localVarPostBody = &query + r, err := a.client.prepareRequest(a.client.ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) + if err != nil { + return nil, err + } + + localVarHTTPResponse, err := a.client.callAPI(r) + if err != nil || localVarHTTPResponse == nil { + return NewAPIResponseWithError(localVarHTTPResponse, nil, err) + } + defer localVarHTTPResponse.Body.Close() + if localVarHTTPResponse.StatusCode >= 300 { + bodyBytes, _ := ioutil.ReadAll(localVarHTTPResponse.Body) + return NewAPIResponseWithError(localVarHTTPResponse, bodyBytes, reportError("Status: %v, Body: %s", localVarHTTPResponse.Status, bodyBytes)) + } + + return NewBitbucketAPIResponse(localVarHTTPResponse) +} diff --git a/default_api_test.go b/default_api_test.go index 002e0df..125b03f 100644 --- a/default_api_test.go +++ b/default_api_test.go @@ -7909,11 +7909,11 @@ func TestDefaultApiService_GetCommitStats(t *testing.T) { } got, err := a.GetCommitStats(tt.args.commitID) if (err != nil) != tt.wantErr { - t.Errorf("DefaultApiService.WithdrawApproval() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("DefaultApiService.GetCommitStats() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("DefaultApiService.WithdrawApproval() = %v, want %v", got, tt.want) + t.Errorf("DefaultApiService.GetCommitStats() = %v, want %v", got, tt.want) } }) } @@ -7945,11 +7945,11 @@ func TestDefaultApiService_GetCommitStatus(t *testing.T) { } got, err := a.GetCommitStatus(tt.args.commitID) if (err != nil) != tt.wantErr { - t.Errorf("DefaultApiService.WithdrawApproval() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("DefaultApiService.GetCommitStatus() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("DefaultApiService.WithdrawApproval() = %v, want %v", got, tt.want) + t.Errorf("DefaultApiService.GetCommitStatus() = %v, want %v", got, tt.want) } }) } @@ -7981,11 +7981,11 @@ func TestDefaultApiService_GetCommitsStats(t *testing.T) { } got, err := a.GetCommitsStats(tt.args.commitsID) if (err != nil) != tt.wantErr { - t.Errorf("DefaultApiService.WithdrawApproval() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("DefaultApiService.GetCommitsStats() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("DefaultApiService.WithdrawApproval() = %v, want %v", got, tt.want) + t.Errorf("DefaultApiService.GetCommitsStats() = %v, want %v", got, tt.want) } }) } @@ -8018,11 +8018,78 @@ func TestDefaultApiService_SetCommitStatus(t *testing.T) { } got, err := a.SetCommitStatus(tt.args.commitID, tt.args.buildStatus) if (err != nil) != tt.wantErr { - t.Errorf("DefaultApiService.WithdrawApproval() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("DefaultApiService.SetCommitStatus() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { - t.Errorf("DefaultApiService.WithdrawApproval() = %v, want %v", got, tt.want) + t.Errorf("DefaultApiService.SetCommitStatus() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestDefaultApiService_SearchCode(t *testing.T) { + type fields struct { + client *APIClient + } + type args struct { + query SearchQuery + } + tests := []struct { + name string + fields fields + args args + want *APIResponse + wantErr, integrationTest bool + }{ + {"networkErrorContextExceeded", fields{client: generateConfigFake()}, args{query: SearchQuery{}}, &APIResponse{Message: "Post https://stash.domain.com/rest/search/latest/search: context canceled"}, true, false}, + {"GoodEmptySearch", + fields{client: generateConfigRealLocalServer()}, + args{query: SearchQuery{ + Query: "git clone", + Limits: Limits{ + Primary: 10, + Secondary: 100, + }, + }}, + &APIResponse{ + Values: map[string]interface{}{ + "query": map[string]interface{}{ + "substituted": false, + }, + "code": map[string]interface{}{ + "category": "primary", + "count": float64(0), + "isLastPage": true, + "nextStart": float64(10), + "start": float64(0), + "values": []interface{}{}, + }, + "scope": map[string]interface{}{ + "type": "GLOBAL", + }, + }, + }, + false, + false, + }, + } + 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.SearchCode(tt.args.query) + if (err != nil) != tt.wantErr { + t.Errorf("DefaultApiService.SearchCode() error = %v, wantErr %v", err, tt.wantErr) + return + } + got.Response = nil + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("DefaultApiService.SearchCode() = %v, want %v", got, tt.want) } }) } diff --git a/test/bb-mock-server/go/api_default.go b/test/bb-mock-server/go/api_default.go index 2e89c6d..925c8b2 100644 --- a/test/bb-mock-server/go/api_default.go +++ b/test/bb-mock-server/go/api_default.go @@ -794,3 +794,7 @@ func GetCommitsStats(w http.ResponseWriter, r *http.Request) { func SetCommitStatus(w http.ResponseWriter, r *http.Request) { HandleRequest(w, r) } + +func SearchCode(w http.ResponseWriter, r *http.Request) { + HandleRequest(w, r) +} diff --git a/test/bb-mock-server/go/routers.go b/test/bb-mock-server/go/routers.go index ff34d9b..070aa54 100644 --- a/test/bb-mock-server/go/routers.go +++ b/test/bb-mock-server/go/routers.go @@ -1419,4 +1419,11 @@ var routes = Routes{ "/rest/build-status/1.0/commits/{commitId}", SetCommitStatus, }, + + Route{ + "SearchCode", + strings.ToUpper("Post"), + "/rest/search/latest/search", + SearchCode, + }, }