Skip to content

Commit

Permalink
Change payload for service desk request attachemnt create to provide … (
Browse files Browse the repository at this point in the history
#271)

* Change payload for service desk request attachemnt create to provide additional comment

* Fixed tests

* Fixed tests

---------

Co-authored-by: Florian Kinder <florian.kinder@enthus.de>
  • Loading branch information
Fank and Fank authored Jun 30, 2024
1 parent c8bc21c commit 06e675c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
20 changes: 8 additions & 12 deletions jira/sm/internal/attachment_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ 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"

model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/sm"
)

func NewAttachmentService(client service.Connector, version string) *AttachmentService {
Expand Down Expand Up @@ -38,8 +39,8 @@ func (s *AttachmentService) Gets(ctx context.Context, issueKeyOrID string, start
// POST /rest/servicedeskapi/request/{issueIdOrKey}/attachment
//
// https://docs.go-atlassian.io/jira-service-management-cloud/request/attachment#create-attachment
func (s *AttachmentService) Create(ctx context.Context, issueKeyOrID string, temporaryAttachmentIDs []string, public bool) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error) {
return s.internalClient.Create(ctx, issueKeyOrID, temporaryAttachmentIDs, public)
func (s *AttachmentService) Create(ctx context.Context, issueKeyOrID string, payload *model.RequestAttachmentCreationPayloadScheme) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error) {
return s.internalClient.Create(ctx, issueKeyOrID, payload)
}

type internalServiceRequestAttachmentImpl struct {
Expand Down Expand Up @@ -73,21 +74,16 @@ func (i *internalServiceRequestAttachmentImpl) Gets(ctx context.Context, issueKe
return page, res, nil
}

func (i *internalServiceRequestAttachmentImpl) Create(ctx context.Context, issueKeyOrID string, temporaryAttachmentIDs []string, public bool) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error) {
func (i *internalServiceRequestAttachmentImpl) Create(ctx context.Context, issueKeyOrID string, payload *model.RequestAttachmentCreationPayloadScheme) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error) {

if issueKeyOrID == "" {
return nil, nil, model.ErrNoIssueKeyOrIDError
}

if len(temporaryAttachmentIDs) == 0 {
if len(payload.TemporaryAttachmentIDs) == 0 {
return nil, nil, model.ErrNoAttachmentIDError
}

payload := map[string]interface{}{
"temporaryAttachmentIds": temporaryAttachmentIDs,
"public": public,
}

url := fmt.Sprintf("rest/servicedeskapi/request/%v/attachment", issueKeyOrID)

req, err := i.c.NewRequest(ctx, http.MethodPost, url, "", payload)
Expand Down
62 changes: 39 additions & 23 deletions jira/sm/internal/attachment_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_internalServiceRequestAttachmentImpl_Gets(t *testing.T) {
Expand Down Expand Up @@ -167,10 +169,9 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
}

type args struct {
ctx context.Context
issueKeyOrID string
temporaryAttachmentIDs []string
public bool
ctx context.Context
issueKeyOrID string
payload *model.RequestAttachmentCreationPayloadScheme
}

testCases := []struct {
Expand All @@ -184,10 +185,12 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
{
name: "when the parameters are correct",
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
temporaryAttachmentIDs: []string{"10001"},
public: true,
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
payload: &model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
},
},
on: func(fields *fields) {

Expand All @@ -198,7 +201,10 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
http.MethodPost,
"rest/servicedeskapi/request/DUMMY-2/attachment",
"",
map[string]interface{}{"public": true, "temporaryAttachmentIds": []string{"10001"}}).
&model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
}).
Return(&http.Request{}, nil)

client.On("Call",
Expand All @@ -213,10 +219,12 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
{
name: "when the http call cannot be executed",
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
temporaryAttachmentIDs: []string{"10001"},
public: true,
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
payload: &model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
},
},
on: func(fields *fields) {

Expand All @@ -227,7 +235,10 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
http.MethodPost,
"rest/servicedeskapi/request/DUMMY-2/attachment",
"",
map[string]interface{}{"public": true, "temporaryAttachmentIds": []string{"10001"}}).
&model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
}).
Return(&http.Request{}, nil)

client.On("Call",
Expand All @@ -244,10 +255,12 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
{
name: "when the request cannot be created",
args: args{
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
temporaryAttachmentIDs: []string{"10001"},
public: true,
ctx: context.Background(),
issueKeyOrID: "DUMMY-2",
payload: &model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
},
},
on: func(fields *fields) {

Expand All @@ -258,7 +271,10 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
http.MethodPost,
"rest/servicedeskapi/request/DUMMY-2/attachment",
"",
map[string]interface{}{"public": true, "temporaryAttachmentIds": []string{"10001"}}).
&model.RequestAttachmentCreationPayloadScheme{
TemporaryAttachmentIDs: []string{"10001"},
Public: true,
}).
Return(&http.Request{}, errors.New("client: no http request created"))

fields.c = client
Expand Down Expand Up @@ -290,7 +306,7 @@ func Test_internalServiceRequestAttachmentImpl_Create(t *testing.T) {
attachmentService := NewAttachmentService(testCase.fields.c, "latest")

gotResult, gotResponse, err := attachmentService.Create(testCase.args.ctx, testCase.args.issueKeyOrID,
testCase.args.temporaryAttachmentIDs, testCase.args.public)
testCase.args.payload)

if testCase.wantErr {

Expand Down
12 changes: 12 additions & 0 deletions pkg/infra/models/sm_request_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ type RequestAttachmentCreationCommentScheme struct {
} `json:"_links,omitempty"` // Links related to the comment.
}

// RequestAttachmentCreationPayloadScheme represents the payload for creating a request attachment.
type RequestAttachmentCreationPayloadScheme struct {
TemporaryAttachmentIDs []string `json:"temporaryAttachmentIds,omitempty"`
Public bool `json:"public,omitempty"`
AdditionalComment *RequestAttachmentCreationAdditionalCommentPayloadScheme `json:"additionalComment,omitempty"`
}

// RequestAttachmentCreationAdditionalCommentPayloadScheme represents the additional comment for creating a request attachment.
type RequestAttachmentCreationAdditionalCommentPayloadScheme struct {
Body string `json:"body,omitempty"`
}

// RequestAttachmentCreationScheme represents the creation of a request attachment.
type RequestAttachmentCreationScheme struct {
Comment *RequestAttachmentCreationCommentScheme `json:"comment,omitempty"` // The comment during the creation of the request attachment.
Expand Down
3 changes: 2 additions & 1 deletion service/sm/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sm

import (
"context"

model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
)

Expand All @@ -21,5 +22,5 @@ type AttachmentConnector interface {
// POST /rest/servicedeskapi/request/{issueIdOrKey}/attachment
//
// https://docs.go-atlassian.io/jira-service-management-cloud/request/attachment#create-attachment
Create(ctx context.Context, issueKeyOrID string, temporaryAttachmentIDs []string, public bool) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error)
Create(ctx context.Context, issueKeyOrID string, payload *model.RequestAttachmentCreationPayloadScheme) (*model.RequestAttachmentCreationScheme, *model.ResponseScheme, error)
}

0 comments on commit 06e675c

Please sign in to comment.