Repository: ctreminiom/go-atlassian
Pull request #139 (2022-08-25 18:25:01)

:sparkles: Edited issueField.go
1. Added the possibility to move customfield to the trash
2. Added the possibility to restore a customfield from the trash


Patch:
diff --git a/jira/v2/issueField.go b/jira/v2/issueField.go
index 3502fe6..6c3b573 100644
--- a/jira/v2/issueField.go
+++ b/jira/v2/issueField.go
@@ -142,3 +142,53 @@ func (f *FieldService) Delete(ctx context.Context, fieldId string) (result *mode
 
 	return
 }
+
+// Trash moves a custom field to trash
+func (f *FieldService) Trash(ctx context.Context, fieldId string) (response *ResponseScheme, err error) {
+
+	if fieldId == "" {
+		return nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/2/field/%v/trash", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+	request.Header.Set("Content-Type", "application/json")
+
+	response, err = f.client.call(request, nil)
+	if err != nil {
+		return
+	}
+
+	return
+}
+
+// Restore restores a custom field from trash.
+func (f *FieldService) Restore(ctx context.Context, fieldId string) (response *ResponseScheme, err error) {
+
+	if fieldId == "" {
+		return nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/2/field/%v/restore", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+	request.Header.Set("Content-Type", "application/json")
+
+	response, err = f.client.call(request, nil)
+	if err != nil {
+		return
+	}
+
+	return
+}
diff --git a/jira/v3/issueField.go b/jira/v3/issueField.go
index ff6ab14..e8b792f 100644
--- a/jira/v3/issueField.go
+++ b/jira/v3/issueField.go
@@ -142,3 +142,53 @@ func (f *FieldService) Delete(ctx context.Context, fieldId string) (result *mode
 
 	return
 }
+
+// Trash moves a custom field to trash
+func (f *FieldService) Trash(ctx context.Context, fieldId string) (response *ResponseScheme, err error) {
+
+	if fieldId == "" {
+		return nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/3/field/%v/trash", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+	request.Header.Set("Content-Type", "application/json")
+
+	response, err = f.client.call(request, nil)
+	if err != nil {
+		return
+	}
+
+	return
+}
+
+// Restore restores a custom field from trash.
+func (f *FieldService) Restore(ctx context.Context, fieldId string) (response *ResponseScheme, err error) {
+
+	if fieldId == "" {
+		return nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/3/field/%v/restore", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+	request.Header.Set("Content-Type", "application/json")
+
+	response, err = f.client.call(request, nil)
+	if err != nil {
+		return
+	}
+
+	return
+}


Test patch:
diff --git a/jira/v2/issueField_test.go b/jira/v2/issueField_test.go
index 21d8481..676ad31 100644
--- a/jira/v2/issueField_test.go
+++ b/jira/v2/issueField_test.go
@@ -10,6 +10,248 @@ import (
 	"testing"
 )
 
+func TestFieldService_Restore(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		mockFile           string
+		fieldId            string
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "when the parameters are correct",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/restore",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			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,
+				MockFilePath:       testCase.mockFile,
+				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 := &FieldService{client: mockClient}
+
+			gotResponse, err := service.Restore(testCase.context, testCase.fieldId)
+
+			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.Code)
+				}
+			} 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)
+
+				t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
+				assert.Equal(t, gotResponse.Code, testCase.wantHTTPCodeReturn)
+			}
+
+		})
+	}
+}
+
+func TestFieldService_Trash(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		mockFile           string
+		fieldId            string
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "when the parameters are correct",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/trash",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/2/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			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,
+				MockFilePath:       testCase.mockFile,
+				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 := &FieldService{client: mockClient}
+
+			gotResponse, err := service.Trash(testCase.context, testCase.fieldId)
+
+			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.Code)
+				}
+			} 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)
+
+				t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
+				assert.Equal(t, gotResponse.Code, testCase.wantHTTPCodeReturn)
+			}
+
+		})
+	}
+}
+
 func TestFieldService_Delete(t *testing.T) {
 
 	testCases := []struct {
diff --git a/jira/v3/issueField_test.go b/jira/v3/issueField_test.go
index 87b8fd8..8a9d245 100644
--- a/jira/v3/issueField_test.go
+++ b/jira/v3/issueField_test.go
@@ -10,6 +10,248 @@ import (
 	"testing"
 )
 
+func TestFieldService_Restore(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		mockFile           string
+		fieldId            string
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "when the parameters are correct",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/restore",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/restore",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			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,
+				MockFilePath:       testCase.mockFile,
+				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 := &FieldService{client: mockClient}
+
+			gotResponse, err := service.Restore(testCase.context, testCase.fieldId)
+
+			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.Code)
+				}
+			} 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)
+
+				t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
+				assert.Equal(t, gotResponse.Code, testCase.wantHTTPCodeReturn)
+			}
+
+		})
+	}
+}
+
+func TestFieldService_Trash(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		mockFile           string
+		fieldId            string
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "when the parameters are correct",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/trash",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			wantHTTPMethod:     http.MethodPost,
+			endpoint:           "/rest/api/3/field/10001/trash",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			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,
+				MockFilePath:       testCase.mockFile,
+				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 := &FieldService{client: mockClient}
+
+			gotResponse, err := service.Trash(testCase.context, testCase.fieldId)
+
+			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.Code)
+				}
+			} 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)
+
+				t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
+				assert.Equal(t, gotResponse.Code, testCase.wantHTTPCodeReturn)
+			}
+
+		})
+	}
+}
+
 func TestFieldService_Delete(t *testing.T) {
 
 	testCases := []struct {