Repository: ctreminiom/go-atlassian
Pull request #129 (2022-06-30 22:09:18)

:sparkles: Mapped the Jira.Fields.Delete endpoint


Patch:
diff --git a/jira/v2/issueField.go b/jira/v2/issueField.go
index db13943..3502fe6 100644
--- a/jira/v2/issueField.go
+++ b/jira/v2/issueField.go
@@ -117,3 +117,28 @@ func (f *FieldService) Search(ctx context.Context, options *models.FieldSearchOp
 
 	return
 }
+
+// Delete deletes a custom field. The custom field is deleted whether it is in the trash or not.
+// See Edit or delete a custom field for more information on trashing and deleting custom fields.
+// Docs: TODO: The documentation needs to be created
+func (f *FieldService) Delete(ctx context.Context, fieldId string) (result *models.TaskScheme, response *ResponseScheme,
+	err error) {
+
+	if fieldId == "" {
+		return nil, nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/2/field/%v", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	response, err = f.client.call(request, &result)
+	if err != nil {
+		return
+	}
+
+	return
+}
diff --git a/jira/v3/issueField.go b/jira/v3/issueField.go
index 92a01c7..ff6ab14 100644
--- a/jira/v3/issueField.go
+++ b/jira/v3/issueField.go
@@ -117,3 +117,28 @@ func (f *FieldService) Search(ctx context.Context, options *models.FieldSearchOp
 
 	return
 }
+
+// Delete deletes a custom field. The custom field is deleted whether it is in the trash or not.
+// See Edit or delete a custom field for more information on trashing and deleting custom fields.
+// Docs: TODO: The documentation needs to be created
+func (f *FieldService) Delete(ctx context.Context, fieldId string) (result *models.TaskScheme, response *ResponseScheme,
+	err error) {
+
+	if fieldId == "" {
+		return nil, nil, models.ErrNoFieldIDError
+	}
+
+	endpoint := fmt.Sprintf("rest/api/3/field/%v", fieldId)
+
+	request, err := f.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	response, err = f.client.call(request, &result)
+	if err != nil {
+		return
+	}
+
+	return
+}


Test patch:
diff --git a/jira/v2/issueField_test.go b/jira/v2/issueField_test.go
index 5647e14..21d8481 100644
--- a/jira/v2/issueField_test.go
+++ b/jira/v2/issueField_test.go
@@ -10,6 +10,132 @@ import (
 	"testing"
 )
 
+func TestFieldService_Delete(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",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/2/field/10001",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/2/field/10001",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/2/field/10001",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/2/field/10001",
+			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}
+
+			getResult, gotResponse, err := service.Delete(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)
+				assert.NotEqual(t, getResult, 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_Create(t *testing.T) {
 
 	testCases := []struct {
diff --git a/jira/v3/issueField_test.go b/jira/v3/issueField_test.go
index 2ddf266..87b8fd8 100644
--- a/jira/v3/issueField_test.go
+++ b/jira/v3/issueField_test.go
@@ -10,6 +10,132 @@ import (
 	"testing"
 )
 
+func TestFieldService_Delete(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",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/3/field/10001",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "when the status code is incorrect",
+			fieldId:            "10001",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/3/field/10001",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the context is not provided",
+			fieldId:            "10001",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/3/field/10001",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "when the field id is not provided",
+			fieldId:            "",
+			mockFile:           "../v3/mocks/task.json",
+			wantHTTPMethod:     http.MethodDelete,
+			endpoint:           "/rest/api/3/field/10001",
+			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}
+
+			getResult, gotResponse, err := service.Delete(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)
+				assert.NotEqual(t, getResult, 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_Create(t *testing.T) {
 
 	testCases := []struct {