Repository: ctreminiom/go-atlassian
Pull request #287 (2024-07-02 04:32:33)

Replaced parameters with options and added internal
Because of the strange handling that `public` & `internal` are `true` by default, I need to use a pointer to be able to set the value to `false` if needed.
<img width="609" alt="image" src="https://github.com/ctreminiom/go-atlassian/assets/1900106/4496e497-d707-465a-86ca-43829aa73751">

Currently its not possible to hide internal comments, and adding more parameters i something agains the Go styleguide.
So i replaced the parameters with an options struct, which parses the properties and create query parameters out of it.
This would be a good attempt to use for other structs as well.


Patch:
diff --git a/go.mod b/go.mod
index b751078..9ab3fb0 100644
--- a/go.mod
+++ b/go.mod
@@ -4,6 +4,7 @@ go 1.14
 
 require (
 	dario.cat/mergo v1.0.0
+	github.com/google/go-querystring v1.1.0
 	github.com/google/uuid v1.6.0
 	github.com/stretchr/testify v1.9.0
 	github.com/tidwall/gjson v1.17.1
diff --git a/go.sum b/go.sum
index 7855961..632583f 100644
--- a/go.sum
+++ b/go.sum
@@ -3,6 +3,10 @@ dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
+github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
+github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
 github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
 github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -23,6 +27,8 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
 github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
 github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
 github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/jira/sm/internal/comment_impl.go b/jira/sm/internal/comment_impl.go
index a4c8cee..e52e600 100644
--- a/jira/sm/internal/comment_impl.go
+++ b/jira/sm/internal/comment_impl.go
@@ -3,13 +3,16 @@ package internal
 import (
 	"context"
 	"fmt"
-	model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
-	"github.com/ctreminiom/go-atlassian/service"
-	"github.com/ctreminiom/go-atlassian/service/sm"
 	"net/http"
 	"net/url"
 	"strconv"
 	"strings"
+
+	"github.com/google/go-querystring/query"
+
+	model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
+	"github.com/ctreminiom/go-atlassian/service"
+	"github.com/ctreminiom/go-atlassian/service/sm"
 )
 
 func NewCommentService(client service.Connector, version string) *CommentService {
@@ -32,8 +35,8 @@ type CommentService struct {
 // GET /rest/servicedeskapi/request/{issueIdOrKey}/comment
 //
 // https://docs.go-atlassian.io/jira-service-management-cloud/request/comments#get-request-comments
-func (s *CommentService) Gets(ctx context.Context, issueKeyOrID string, public bool, expand []string, start, limit int) (*model.RequestCommentPageScheme, *model.ResponseScheme, error) {
-	return s.internalClient.Gets(ctx, issueKeyOrID, public, expand, start, limit)
+func (s *CommentService) Gets(ctx context.Context, issueKeyOrID string, options *model.RequestCommentOptionsScheme) (*model.RequestCommentPageScheme, *model.ResponseScheme, error) {
+	return s.internalClient.Gets(ctx, issueKeyOrID, options)
 }
 
 // Get returns details of a customer request's comment.
@@ -68,26 +71,23 @@ type internalServiceRequestCommentImpl struct {
 	version string
 }
 
-func (i *internalServiceRequestCommentImpl) Gets(ctx context.Context, issueKeyOrID string, public bool, expand []string, start, limit int) (*model.RequestCommentPageScheme, *model.ResponseScheme, error) {
+func (i *internalServiceRequestCommentImpl) Gets(ctx context.Context, issueKeyOrID string, options *model.RequestCommentOptionsScheme) (*model.RequestCommentPageScheme, *model.ResponseScheme, error) {
 
 	if issueKeyOrID == "" {
 		return nil, nil, model.ErrNoIssueKeyOrIDError
 	}
 
-	params := url.Values{}
-	params.Add("start", strconv.Itoa(start))
-	params.Add("limit", strconv.Itoa(limit))
+	endpoint := fmt.Sprintf("rest/servicedeskapi/request/%v/comment", issueKeyOrID)
 
-	if !public {
-		params.Add("public", "false")
-	}
+	if options != nil {
+		q, err := query.Values(options)
+		if err != nil {
+			return nil, nil, err
+		}
 
-	if len(expand) != 0 {
-		params.Add("expand", strings.Join(expand, ","))
+		endpoint += "?" + q.Encode()
 	}
 
-	endpoint := fmt.Sprintf("rest/servicedeskapi/request/%v/comment?%v", issueKeyOrID, params.Encode())
-
 	req, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
 	if err != nil {
 		return nil, nil, err
diff --git a/pkg/infra/models/sm_request_comment.go b/pkg/infra/models/sm_request_comment.go
index 7e6b483..f198839 100644
--- a/pkg/infra/models/sm_request_comment.go
+++ b/pkg/infra/models/sm_request_comment.go
@@ -1,5 +1,13 @@
 package models
 
+type RequestCommentOptionsScheme struct {
+	Public   *bool    `url:"public,omitempty"`
+	Internal *bool    `url:"internal,omitempty"`
+	Expand   []string `url:"expand,omitempty"`
+	Start    int      `url:"start,omitempty"`
+	Limit    int      `url:"limit,omitempty"`
+}
+
 // RequestCommentPageScheme represents a page of request comments in a system.
 type RequestCommentPageScheme struct {
 	Size       int                           `json:"size"`       // The number of request comments on the page.
diff --git a/service/sm/comment.go b/service/sm/comment.go
index eb3472a..d160315 100644
--- a/service/sm/comment.go
+++ b/service/sm/comment.go
@@ -2,6 +2,7 @@ package sm
 
 import (
 	"context"
+
 	model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
 )
 
@@ -16,7 +17,7 @@ type CommentConnector interface {
 	// GET /rest/servicedeskapi/request/{issueIdOrKey}/comment
 	//
 	// https://docs.go-atlassian.io/jira-service-management-cloud/request/comments#get-request-comments
-	Gets(ctx context.Context, issueKeyOrID string, public bool, expand []string, start, limit int) (*model.RequestCommentPageScheme, *model.ResponseScheme, error)
+	Gets(ctx context.Context, issueKeyOrID string, options *model.RequestCommentOptionsScheme) (*model.RequestCommentPageScheme, *model.ResponseScheme, error)
 
 	// Get returns details of a customer request's comment.
 	//


Test patch:
diff --git a/jira/sm/internal/comment_impl_test.go b/jira/sm/internal/comment_impl_test.go
index 2c80a21..2c36bab 100644
--- a/jira/sm/internal/comment_impl_test.go
+++ b/jira/sm/internal/comment_impl_test.go
@@ -3,12 +3,14 @@ package internal
 import (
 	"context"
 	"errors"
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+
 	model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
 	"github.com/ctreminiom/go-atlassian/service"
 	"github.com/ctreminiom/go-atlassian/service/mocks"
-	"github.com/stretchr/testify/assert"
-	"net/http"
-	"testing"
 )
 
 func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
@@ -20,11 +22,11 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 	type args struct {
 		ctx          context.Context
 		issueKeyOrID string
-		public       bool
-		expand       []string
-		start, limit int
+		options      *model.RequestCommentOptionsScheme
 	}
 
+	truePtr := true
+
 	testCases := []struct {
 		name    string
 		fields  fields
@@ -38,10 +40,12 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 			args: args{
 				ctx:          context.Background(),
 				issueKeyOrID: "DUMMY-2",
-				public:       true,
-				expand:       []string{"attachment"},
-				start:        100,
-				limit:        50,
+				options: &model.RequestCommentOptionsScheme{
+					Public: &truePtr,
+					Expand: []string{"attachment"},
+					Start:  100,
+					Limit:  50,
+				},
 			},
 			on: func(fields *fields) {
 
@@ -50,7 +54,7 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 				client.On("NewRequest",
 					context.Background(),
 					http.MethodGet,
-					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&start=100",
+					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&public=true&start=100",
 					"",
 					nil).
 					Return(&http.Request{}, nil)
@@ -69,10 +73,12 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 			args: args{
 				ctx:          context.Background(),
 				issueKeyOrID: "DUMMY-2",
-				public:       true,
-				expand:       []string{"attachment"},
-				start:        100,
-				limit:        50,
+				options: &model.RequestCommentOptionsScheme{
+					Public: &truePtr,
+					Expand: []string{"attachment"},
+					Start:  100,
+					Limit:  50,
+				},
 			},
 			on: func(fields *fields) {
 
@@ -81,7 +87,7 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 				client.On("NewRequest",
 					context.Background(),
 					http.MethodGet,
-					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&start=100",
+					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&public=true&start=100",
 					"",
 					nil).
 					Return(&http.Request{}, nil)
@@ -102,10 +108,12 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 			args: args{
 				ctx:          context.Background(),
 				issueKeyOrID: "DUMMY-2",
-				public:       true,
-				expand:       []string{"attachment"},
-				start:        100,
-				limit:        50,
+				options: &model.RequestCommentOptionsScheme{
+					Public: &truePtr,
+					Expand: []string{"attachment"},
+					Start:  100,
+					Limit:  50,
+				},
 			},
 			on: func(fields *fields) {
 
@@ -114,7 +122,7 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 				client.On("NewRequest",
 					context.Background(),
 					http.MethodGet,
-					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&start=100",
+					"rest/servicedeskapi/request/DUMMY-2/comment?expand=attachment&limit=50&public=true&start=100",
 					"",
 					nil).
 					Return(&http.Request{}, errors.New("client: no http request created"))
@@ -147,9 +155,7 @@ func Test_internalServiceRequestCommentImpl_Gets(t *testing.T) {
 
 			commentService := NewCommentService(testCase.fields.c, "latest")
 
-			gotResult, gotResponse, err := commentService.Gets(testCase.args.ctx, testCase.args.issueKeyOrID, testCase.args.public,
-				testCase.args.expand, testCase.args.start,
-				testCase.args.limit)
+			gotResult, gotResponse, err := commentService.Gets(testCase.args.ctx, testCase.args.issueKeyOrID, testCase.args.options)
 
 			if testCase.wantErr {