Repository: ctreminiom/go-atlassian
Pull request #47 (2021-05-28 15:26:02)

Dev


Patch:
diff --git a/jira/examples/issue-field-options/order/order.go b/jira/examples/issue-field-options/order/order.go
new file mode 100644
index 0000000..03aa76a
--- /dev/null
+++ b/jira/examples/issue-field-options/order/order.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+	"context"
+	"github.com/ctreminiom/go-atlassian/jira"
+	"log"
+	"os"
+	"sort"
+)
+
+func main() {
+
+	var (
+		host  = os.Getenv("HOST")
+		mail  = os.Getenv("MAIL")
+		token = os.Getenv("TOKEN")
+	)
+
+	atlassian, err := jira.New(nil, host)
+	if err != nil {
+		return
+	}
+
+	atlassian.Auth.SetBasicAuth(mail, token)
+
+	var (
+		fieldID   = "customfield_10038"
+		contextID = 10180
+	)
+
+	log.Println("Getting the field context options")
+	options, response, err := atlassian.Issue.Field.Context.Option.Gets(context.Background(), fieldID, contextID, nil, 0, 50)
+	if err != nil {
+		if response != nil {
+			log.Println("Response HTTP Response", string(response.BodyAsBytes))
+		}
+		return
+	}
+
+	log.Println("Response HTTP Code", response.StatusCode)
+	log.Println("HTTP Endpoint Used", response.Endpoint)
+
+	var (
+		optionsAsMap = make(map[string]string)
+		optionsAsList []string
+	)
+
+	for _, option := range options.Values {
+		optionsAsList = append(optionsAsList, option.Value)
+		optionsAsMap[option.Value] = option.ID
+	}
+
+	log.Println("Sorting the fields")
+	sort.Strings(optionsAsList)
+
+	log.Println("Creating the new option ID's payload to order")
+	var optionsIDsAsList []string
+	for _, option := range optionsAsList {
+		optionsIDsAsList = append(optionsIDsAsList, optionsAsMap[option])
+	}
+
+	var payload = &jira.OrderFieldOptionPayloadScheme{
+		Position:             "First",
+		CustomFieldOptionIds: optionsIDsAsList,
+	}
+
+	log.Println("Ordering the options")
+	response, err = atlassian.Issue.Field.Context.Option.Order(context.Background(), fieldID, contextID, payload)
+	if err != nil {
+		if response != nil {
+			log.Println("Response HTTP Code", response.StatusCode)
+			log.Println("HTTP Endpoint Used", response.Endpoint)
+			log.Fatal(err)
+		}
+		return
+	}
+
+	log.Println("Response HTTP Code", response.StatusCode)
+	log.Println("HTTP Endpoint Used", response.Endpoint)
+}
\ No newline at end of file
diff --git a/jira/issueFieldOptionContext.go b/jira/issueFieldOptionContext.go
index 6e374ad..3f9d3ae 100644
--- a/jira/issueFieldOptionContext.go
+++ b/jira/issueFieldOptionContext.go
@@ -181,3 +181,43 @@ func (f *FieldOptionContextService) Delete(ctx context.Context, fieldID string,
 
 	return
 }
+
+type OrderFieldOptionPayloadScheme struct {
+	After string `json:"after,omitempty"`
+	Position             string   `json:"position,omitempty"`
+	CustomFieldOptionIds []string `json:"customFieldOptionIds,omitempty"`
+}
+
+// Order changes the order of custom field options or cascading options in a context.
+// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-custom-field-options/#api-rest-api-3-field-fieldid-context-contextid-option-move-put
+func (f *FieldOptionContextService) Order(ctx context.Context, fieldID string, contextID int, payload *OrderFieldOptionPayloadScheme) (response *Response, err error) {
+
+	if fieldID == "" {
+		return nil, fmt.Errorf("error, fieldID value is nil, please provide a valid fieldID value")
+	}
+
+	if contextID == 0 {
+		return nil, fmt.Errorf("error, fieldID value is nil, please provide a valid contextID value")
+	}
+
+	if payload == nil {
+		return nil, fmt.Errorf("error, payload value is nil, please provide a pointer of OrderFieldOptionPayloadScheme struct")
+	}
+
+	var endpoint = fmt.Sprintf("/rest/api/3/field/%v/context/%v/option/move", fieldID, contextID)
+
+	request, err := f.client.newRequest(ctx, http.MethodPut, endpoint, payload)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+	request.Header.Set("Content-Type", "application/json")
+
+	response, err = f.client.Do(request)
+	if err != nil {
+		return
+	}
+
+	return
+}
\ No newline at end of file
diff --git a/jira/permissionScheme.go b/jira/permissionScheme.go
index 2b6adab..61a44ce 100644
--- a/jira/permissionScheme.go
+++ b/jira/permissionScheme.go
@@ -66,10 +66,6 @@ type PermissionScopeItemScheme struct {
 // Docs: https://docs.go-atlassian.io/jira-software-cloud/permissions/scheme#get-permission-scheme
 func (p *PermissionSchemeService) Get(ctx context.Context, permissionSchemeID int) (result *PermissionSchemeScheme, response *Response, err error) {
 
-	if permissionSchemeID == 0 {
-		return nil, nil, fmt.Errorf("error!, please provide a valid permissionSchemeID value")
-	}
-
 	var endpoint = fmt.Sprintf("rest/api/3/permissionscheme/%v", permissionSchemeID)
 
 	request, err := p.client.newRequest(ctx, http.MethodGet, endpoint, nil)


Test patch:
diff --git a/jira/issueFieldOptionContext_test.go b/jira/issueFieldOptionContext_test.go
index b7e7059..d9961b0 100644
--- a/jira/issueFieldOptionContext_test.go
+++ b/jira/issueFieldOptionContext_test.go
@@ -1080,3 +1080,198 @@ func TestFieldOptionContextService_Update(t *testing.T) {
 	}
 
 }
+
+func TestFieldOptionContextService_Order(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		fieldID            string
+		contextID          int
+		payload *OrderFieldOptionPayloadScheme
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "OrderFieldContextsWhenTheParametersAreCorrect",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            false,
+		},
+		{
+			name:               "OrderFieldContextsWhenThePayloadIsNotProvided",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: nil,
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheContextIDIsNotProvided",
+			fieldID:            "0001",
+			contextID:          0,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheFieldIDIsEmpty",
+			fieldID:            "",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheContextIsNil",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheRequestMethodIsIncorrect",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheEndpointIsIncorrect",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/000",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusNoContent,
+			wantErr:            true,
+		},
+
+		{
+			name:               "OrderFieldContextsWhenTheStatusCodeIsIncorrect",
+			fieldID:            "0001",
+			contextID:          100,
+			payload: &OrderFieldOptionPayloadScheme{
+				Position:             "Last",
+				CustomFieldOptionIds: []string{"111"},
+			},
+			wantHTTPMethod:     http.MethodPut,
+			endpoint:           "/rest/api/3/field/0001/context/100/option/move",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+	}
+
+	for _, testCase := range testCases {
+		t.Run(testCase.name, func(t *testing.T) {
+
+			//Init a new HTTP mock server
+			mockOptions := mockServerOptions{
+				Endpoint:           testCase.endpoint,
+				MethodAccepted:     testCase.wantHTTPMethod,
+				ResponseCodeWanted: testCase.wantHTTPCodeReturn,
+			}
+
+			mockServer, err := startMockServer(&mockOptions)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			defer mockServer.Close()
+
+			//Init the library instance
+			mockClient, err := startMockClient(mockServer.URL)
+			if err != nil {
+				t.Fatal(err)
+			}
+
+			service := &FieldOptionContextService{client: mockClient}
+			gotResponse, err := service.Order(testCase.context, testCase.fieldID, testCase.contextID, testCase.payload)
+
+			if testCase.wantErr {
+
+				if err != nil {
+					t.Logf("error returned: %v", err.Error())
+				}
+
+				assert.Error(t, err)
+
+				if gotResponse != nil {
+					t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.StatusCode)
+				}
+			} else {
+
+				assert.NoError(t, err)
+				assert.NotEqual(t, gotResponse, nil)
+
+				apiEndpoint, err := url.Parse(gotResponse.Endpoint)
+				if err != nil {
+					t.Fatal(err)
+				}
+
+				var endpointToAssert string
+
+				if apiEndpoint.Query().Encode() != "" {
+					endpointToAssert = fmt.Sprintf("%v?%v", apiEndpoint.Path, apiEndpoint.Query().Encode())
+				} else {
+					endpointToAssert = apiEndpoint.Path
+				}
+
+				t.Logf("HTTP Endpoint Wanted: %v, HTTP Endpoint Returned: %v", testCase.endpoint, endpointToAssert)
+				assert.Equal(t, testCase.endpoint, endpointToAssert)
+			}
+
+		})
+	}
+
+}
+