Repository: nukosuke/go-zendesk
Pull request #247 (2022-12-08 01:50:49)

Add 'make comment private' API
Converts a public comment to an internal note


Patch:
diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go
index fb16b06..f6f4473 100644
--- a/zendesk/mock/client.go
+++ b/zendesk/mock/client.go
@@ -1254,6 +1254,20 @@ func (mr *ClientMockRecorder) ListTicketComments(arg0, arg1 interface{}) *gomock
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListTicketComments", reflect.TypeOf((*Client)(nil).ListTicketComments), arg0, arg1)
 }
 
+// MakeCommentPrivate mocks base method.
+func (m *Client) MakeCommentPrivate(arg0 context.Context, arg1, arg2 int64) error {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "MakeCommentPrivate", arg0, arg1, arg2)
+	ret0, _ := ret[0].(error)
+	return ret0
+}
+
+// MakeCommentPrivate indicates an expected call of MakeCommentPrivate.
+func (mr *ClientMockRecorder) MakeCommentPrivate(arg0, arg1, arg2 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MakeCommentPrivate", reflect.TypeOf((*Client)(nil).MakeCommentPrivate), arg0, arg1, arg2)
+}
+
 // Post mocks base method.
 func (m *Client) Post(arg0 context.Context, arg1 string, arg2 interface{}) ([]byte, error) {
 	m.ctrl.T.Helper()
diff --git a/zendesk/ticket_comment.go b/zendesk/ticket_comment.go
index d9606f3..e1c14b2 100644
--- a/zendesk/ticket_comment.go
+++ b/zendesk/ticket_comment.go
@@ -11,6 +11,7 @@ import (
 type TicketCommentAPI interface {
 	CreateTicketComment(ctx context.Context, ticketID int64, ticketComment TicketComment) (TicketComment, error)
 	ListTicketComments(ctx context.Context, ticketID int64) ([]TicketComment, error)
+	MakeCommentPrivate(ctx context.Context, ticketID int64, ticketCommentID int64) error
 }
 
 // TicketComment is a struct for ticket comment payload
@@ -101,3 +102,12 @@ func (z *Client) ListTicketComments(ctx context.Context, ticketID int64) ([]Tick
 
 	return result.TicketComments, err
 }
+
+// MakeCommentPrivate converts an existing ticket comment to an internal note that is not publicly viewable.
+//
+// ref: https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_comments/#make-comment-private
+func (z *Client) MakeCommentPrivate(ctx context.Context, ticketID int64, ticketCommentID int64) error {
+	path := fmt.Sprintf("/tickets/%d/comments/%d/make_private", ticketID, ticketCommentID)
+	_, err := z.put(ctx, path, nil)
+	return err
+}


Test patch:
diff --git a/zendesk/ticket_comment_test.go b/zendesk/ticket_comment_test.go
index 6c8a897..731cef0 100644
--- a/zendesk/ticket_comment_test.go
+++ b/zendesk/ticket_comment_test.go
@@ -1,7 +1,9 @@
 package zendesk
 
 import (
+	"errors"
 	"net/http"
+	"net/http/httptest"
 	"testing"
 )
 
@@ -51,3 +53,50 @@ func TestListTicketComments(t *testing.T) {
 		t.Fatalf("Returned ticket comments does not have the expected length %d. Ticket comments length is %d", expectedLength, len(ticketComments))
 	}
 }
+
+func TestMakeCommentPrivate(t *testing.T) {
+	t.Parallel()
+	tests := []struct {
+		name           string
+		apiReturn      error
+		expectedErrStr string
+	}{
+		{
+			name:           "successfully made private",
+			apiReturn:      nil,
+			expectedErrStr: "",
+		},
+		{
+			name:           "error making private",
+			apiReturn:      errors.New(`{"error":"Couldn't authenticate you"}`),
+			expectedErrStr: `401: {"error":"Couldn't authenticate you"}`,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+				if test.apiReturn != nil {
+					w.WriteHeader(http.StatusUnauthorized)
+					_, _ = w.Write([]byte(test.apiReturn.Error()))
+				} else {
+					w.WriteHeader(http.StatusOK)
+					_, _ = w.Write(nil)
+				}
+			}))
+			defer mockAPI.Close()
+
+			client := newTestClient(mockAPI)
+			err := client.MakeCommentPrivate(ctx, 2, 12841284)
+			if err == nil {
+				if test.expectedErrStr != "" {
+					t.Fatalf("Expected error %s, did not get one", test.expectedErrStr)
+				}
+			} else {
+				if test.expectedErrStr != err.Error() {
+					t.Fatalf("Got %s, wanted %s", err.Error(), test.expectedErrStr)
+				}
+			}
+		})
+	}
+}