diff --git a/jira/v2/workflow.go b/jira/v2/workflow.go index 95246836..1c892968 100644 --- a/jira/v2/workflow.go +++ b/jira/v2/workflow.go @@ -43,19 +43,33 @@ func (w *WorkflowService) Create(ctx context.Context, payload *models.WorkflowPa // Gets returns a paginated list of published classic workflows. // When workflow names are specified, details of those workflows are returned. // Otherwise, all published classic workflows are returned. -func (w *WorkflowService) Gets(ctx context.Context, workflowNames, expand []string, startAt, maxResults int) (result *models.WorkflowPageScheme, +func (w *WorkflowService) Gets(ctx context.Context, options *models.WorkflowSearchOptions, startAt, maxResults int) (result *models.WorkflowPageScheme, response *ResponseScheme, err error) { params := url.Values{} params.Add("startAt", strconv.Itoa(startAt)) params.Add("maxResults", strconv.Itoa(maxResults)) - for _, workflowName := range workflowNames { - params.Add("workflowName", workflowName) - } + if options != nil { + + for _, name := range options.WorkflowNames { + params.Add("workflowName", name) + } + + if options.QueryString != "" { + params.Add("queryString", options.QueryString) + } + + if options.OrderBy != "" { + params.Add("orderBy", options.OrderBy) + } + + params.Add("isActive", fmt.Sprintf("%T", options.IsActive)) + + if len(options.Expand) != 0 { + params.Add("expand", strings.Join(options.Expand, ",")) + } - if len(expand) != 0 { - params.Add("expand", strings.Join(expand, ",")) } var endpoint = fmt.Sprintf("/rest/api/2/workflow/search?%v", params.Encode()) diff --git a/jira/v2/workflow_test.go b/jira/v2/workflow_test.go index 4e88b0c8..8d89ccc0 100644 --- a/jira/v2/workflow_test.go +++ b/jira/v2/workflow_test.go @@ -139,109 +139,124 @@ func TestWorkflowService_Delete(t *testing.T) { func TestWorkflowService_Gets(t *testing.T) { testCases := []struct { - name string - startAt, maxResults int - mockFile string - workflowNames, expand []string - wantHTTPMethod string - endpoint string - context context.Context - wantHTTPCodeReturn int - wantErr bool + name string + startAt, maxResults int + mockFile string + options *models.WorkflowSearchOptions + wantHTTPMethod string + endpoint string + context context.Context + wantHTTPCodeReturn int + wantErr bool }{ { - name: "GetsWorkflowsWheTheParametersAreCorrect", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheParametersAreCorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: false, }, { - name: "GetsWorkflowsWheTheContextIsNotSet", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheContextIsNotSet", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", - context: nil, + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", wantHTTPCodeReturn: http.StatusOK, wantErr: true, }, { - name: "GetsWorkflowsWheTheRequestMethodIsIncorrect", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheRequestMethodIsIncorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodHead, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: true, }, { - name: "GetsWorkflowsWheTheStatusCodeIsIncorrect", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheStatusCodeIsIncorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusBadRequest, wantErr: true, }, { - name: "GetsWorkflowsWheTheExpandsAreNotSet", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: nil, - wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", - context: context.Background(), - wantHTTPCodeReturn: http.StatusOK, - wantErr: false, - }, - - { - name: "GetsWorkflowsWheTheWorkflowNamesAreNotSet", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/get-workflows.json", - workflowNames: nil, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheExpandsAreNotSet", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0", + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: false, }, { - name: "GetsWorkflowsWheTheResponseBodyIsEmpty", - startAt: 0, - maxResults: 50, - mockFile: "../v3/mocks/empty_json.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheResponseBodyIsEmpty", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/empty_json.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/2/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: true, @@ -275,9 +290,7 @@ func TestWorkflowService_Gets(t *testing.T) { i := &WorkflowService{client: mockClient} - gotResult, gotResponse, err := i.Gets(testCase.context, testCase.workflowNames, testCase.expand, - testCase.startAt, - testCase.maxResults) + gotResult, gotResponse, err := i.Gets(testCase.context, testCase.options, testCase.startAt, testCase.maxResults) if testCase.wantErr { diff --git a/jira/v3/workflow.go b/jira/v3/workflow.go index 261a7b34..02564cdf 100644 --- a/jira/v3/workflow.go +++ b/jira/v3/workflow.go @@ -44,19 +44,33 @@ func (w *WorkflowService) Create(ctx context.Context, payload *models.WorkflowPa // When workflow names are specified, details of those workflows are returned. // Otherwise, all published classic workflows are returned. // Atlassian Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-workflows/#api-rest-api-3-workflow-search-get -func (w *WorkflowService) Gets(ctx context.Context, workflowNames, expand []string, startAt, maxResults int) (result *models.WorkflowPageScheme, +func (w *WorkflowService) Gets(ctx context.Context, options *models.WorkflowSearchOptions, startAt, maxResults int) (result *models.WorkflowPageScheme, response *ResponseScheme, err error) { params := url.Values{} params.Add("startAt", strconv.Itoa(startAt)) params.Add("maxResults", strconv.Itoa(maxResults)) - for _, workflowName := range workflowNames { - params.Add("workflowName", workflowName) - } + if options != nil { + + for _, name := range options.WorkflowNames { + params.Add("workflowName", name) + } + + if options.QueryString != "" { + params.Add("queryString", options.QueryString) + } + + if options.OrderBy != "" { + params.Add("orderBy", options.OrderBy) + } + + params.Add("isActive", fmt.Sprintf("%T", options.IsActive)) + + if len(options.Expand) != 0 { + params.Add("expand", strings.Join(options.Expand, ",")) + } - if len(expand) != 0 { - params.Add("expand", strings.Join(expand, ",")) } var endpoint = fmt.Sprintf("/rest/api/3/workflow/search?%v", params.Encode()) diff --git a/jira/v3/workflow_test.go b/jira/v3/workflow_test.go index 4ee8430e..e01e8ec7 100644 --- a/jira/v3/workflow_test.go +++ b/jira/v3/workflow_test.go @@ -139,109 +139,124 @@ func TestWorkflowService_Delete(t *testing.T) { func TestWorkflowService_Gets(t *testing.T) { testCases := []struct { - name string - startAt, maxResults int - mockFile string - workflowNames, expand []string - wantHTTPMethod string - endpoint string - context context.Context - wantHTTPCodeReturn int - wantErr bool + name string + startAt, maxResults int + mockFile string + options *models.WorkflowSearchOptions + wantHTTPMethod string + endpoint string + context context.Context + wantHTTPCodeReturn int + wantErr bool }{ { - name: "GetsWorkflowsWheTheParametersAreCorrect", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheParametersAreCorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: false, }, { - name: "GetsWorkflowsWheTheContextIsNotSet", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheContextIsNotSet", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", - context: nil, + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", wantHTTPCodeReturn: http.StatusOK, wantErr: true, }, { - name: "GetsWorkflowsWheTheRequestMethodIsIncorrect", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheRequestMethodIsIncorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodHead, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: true, }, { - name: "GetsWorkflowsWheTheStatusCodeIsIncorrect", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheStatusCodeIsIncorrect", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusBadRequest, wantErr: true, }, { - name: "GetsWorkflowsWheTheExpandsAreNotSet", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: nil, - wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", - context: context.Background(), - wantHTTPCodeReturn: http.StatusOK, - wantErr: false, - }, - - { - name: "GetsWorkflowsWheTheWorkflowNamesAreNotSet", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/get-workflows.json", - workflowNames: nil, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheExpandsAreNotSet", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/get-workflows.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0", + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: false, }, { - name: "GetsWorkflowsWheTheResponseBodyIsEmpty", - startAt: 0, - maxResults: 50, - mockFile: "./mocks/empty_json.json", - workflowNames: []string{"workflow name 1", "workflow name 2"}, - expand: []string{"transitions", "transitions.rules", "default"}, + name: "GetsWorkflowsWheTheResponseBodyIsEmpty", + startAt: 0, + maxResults: 50, + mockFile: "../v3/mocks/empty_json.json", + options: &models.WorkflowSearchOptions{ + QueryString: "name", + WorkflowNames: []string{"workflow name 1", "workflow name 2"}, + OrderBy: "name", + IsActive: true, + Expand: []string{"transitions", "transitions.rules", "default"}, + }, wantHTTPMethod: http.MethodGet, - endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&maxResults=50&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", + endpoint: "/rest/api/3/workflow/search?expand=transitions%2Ctransitions.rules%2Cdefault&isActive=bool&maxResults=50&orderBy=name&queryString=name&startAt=0&workflowName=workflow+name+1&workflowName=workflow+name+2", context: context.Background(), wantHTTPCodeReturn: http.StatusOK, wantErr: true, @@ -275,9 +290,7 @@ func TestWorkflowService_Gets(t *testing.T) { i := &WorkflowService{client: mockClient} - gotResult, gotResponse, err := i.Gets(testCase.context, testCase.workflowNames, testCase.expand, - testCase.startAt, - testCase.maxResults) + gotResult, gotResponse, err := i.Gets(testCase.context, testCase.options, testCase.startAt, testCase.maxResults) if testCase.wantErr { diff --git a/pkg/infra/models/jira_workflow.go b/pkg/infra/models/jira_workflow.go index 5d88a7e9..7bc0d9dd 100644 --- a/pkg/infra/models/jira_workflow.go +++ b/pkg/infra/models/jira_workflow.go @@ -1,5 +1,13 @@ package models +type WorkflowSearchOptions struct { + QueryString string + WorkflowNames []string + OrderBy string + IsActive bool + Expand []string +} + type WorkflowPageScheme struct { Self string `json:"self,omitempty"` NextPage string `json:"nextPage,omitempty"`