Skip to content

Commit

Permalink
Merge pull request #142 from ctreminiom/feature/attachment-field
Browse files Browse the repository at this point in the history
Feature/attachment field
  • Loading branch information
ctreminiom authored Sep 15, 2022
2 parents a715b4c + 1663de2 commit a0ad3c5
Show file tree
Hide file tree
Showing 10 changed files with 875 additions and 9 deletions.
38 changes: 38 additions & 0 deletions jira/internal/attachment_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
)

func NewIssueAttachmentService(client service.Client, version string) (*IssueAttachmentService, error) {
Expand Down Expand Up @@ -76,11 +78,47 @@ func (i *IssueAttachmentService) Add(ctx context.Context, issueKeyOrId, fileName
return i.internalClient.Add(ctx, issueKeyOrId, fileName, file)
}

// Download returns the contents of an attachment. A Range header can be set to define a range of bytes within the attachment to download.
//
// See the HTTP Range header standard for details.
//
// GET /rest/api/{2-3}/attachment/content/{id}
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/attachments#download-attachment
func (i *IssueAttachmentService) Download(ctx context.Context, attachmentID string, redirect bool) (*model.ResponseScheme, error) {
return i.internalClient.Download(ctx, attachmentID, redirect)
}

type internalIssueAttachmentServiceImpl struct {
c service.Client
version string
}

func (i *internalIssueAttachmentServiceImpl) Download(ctx context.Context, attachmentID string, redirect bool) (*model.ResponseScheme, error) {

if attachmentID == "" {
return nil, model.ErrNoAttachmentIDError
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("rest/api/%v/attachment/content/%v", i.version, attachmentID))

if !redirect {

params := url.Values{}
params.Add("redirect", "false") //default: true

endpoint.WriteString(fmt.Sprintf("?%v", params.Encode()))
}

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return nil, err
}

return i.c.Call(request, nil)
}

func (i *internalIssueAttachmentServiceImpl) Settings(ctx context.Context) (*model.AttachmentSettingScheme, *model.ResponseScheme, error) {

endpoint := fmt.Sprintf("rest/api/%v/attachment/meta", i.version)
Expand Down
149 changes: 149 additions & 0 deletions jira/internal/attachment_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,3 +770,152 @@ func Test_internalIssueAttachmentServiceImpl_Add(t *testing.T) {
})
}
}

func Test_internalIssueAttachmentServiceImpl_Download(t *testing.T) {

type fields struct {
c service.Client
version string
}

type args struct {
ctx context.Context
attachmentId string
redirect bool
}

testCases := []struct {
name string
fields fields
args args
on func(*fields)
wantErr bool
Err error
}{
{
name: "when the api version is v2",
fields: fields{version: "2"},
args: args{
ctx: context.TODO(),
attachmentId: "1110",
redirect: false,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"rest/api/2/attachment/content/1110?redirect=false",
nil).
Return(&http.Request{}, nil)

client.On("Call",
&http.Request{},
nil).
Return(&model.ResponseScheme{}, nil)

fields.c = client
},
},

{
name: "when the api version is v3",
fields: fields{version: "3"},
args: args{
ctx: context.TODO(),
attachmentId: "1110",
redirect: true,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"rest/api/3/attachment/content/1110",
nil).
Return(&http.Request{}, nil)

client.On("Call",
&http.Request{},
nil).
Return(&model.ResponseScheme{}, nil)

fields.c = client

},
},

{
name: "when the attachment id is not provided",
fields: fields{version: "2"},
args: args{
ctx: context.TODO(),
attachmentId: "",
},
on: func(fields *fields) {
fields.c = mocks.NewClient(t)
},
wantErr: true,
Err: model.ErrNoAttachmentIDError,
},

{
name: "when the http request cannot be created",
fields: fields{version: "2"},
args: args{
ctx: context.TODO(),
attachmentId: "1110",
redirect: true,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"rest/api/2/attachment/content/1110",
nil).
Return(&http.Request{}, errors.New("error, unable to create the http request"))

fields.c = client

},
wantErr: true,
Err: errors.New("error, unable to create the http request"),
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {

if testCase.on != nil {
testCase.on(&testCase.fields)
}

attachmentService, err := NewIssueAttachmentService(testCase.fields.c, testCase.fields.version)
assert.NoError(t, err)

gotResponse, err := attachmentService.Download(testCase.args.ctx, testCase.args.attachmentId, testCase.args.redirect)

if testCase.wantErr {

if err != nil {
t.Logf("error returned: %v", err.Error())
}

assert.EqualError(t, err, testCase.Err.Error())

} else {

assert.NoError(t, err)
assert.NotEqual(t, gotResponse, nil)
}

})
}
}
7 changes: 5 additions & 2 deletions jira/internal/field_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"strings"
)

func NewIssueFieldService(client service.Client, version string, configuration *IssueFieldConfigService, context *IssueFieldContextService) (*IssueFieldService, error) {
func NewIssueFieldService(client service.Client, version string, configuration *IssueFieldConfigService, context *IssueFieldContextService,
trash *IssueFieldTrashService) (*IssueFieldService, error) {

if version == "" {
return nil, model.ErrNoVersionProvided
Expand All @@ -22,13 +23,15 @@ func NewIssueFieldService(client service.Client, version string, configuration *
internalClient: &internalIssueFieldServiceImpl{c: client, version: version},
Configuration: configuration,
Context: context,
Trash: trash,
}, nil
}

type IssueFieldService struct {
internalClient jira.FieldConnector
Configuration *IssueFieldConfigService
Context *IssueFieldContextService
Trash *IssueFieldTrashService
}

// Gets returns system and custom issue fields according to the following rules:
Expand Down Expand Up @@ -73,7 +76,7 @@ func (i *IssueFieldService) Search(ctx context.Context, options *model.FieldSear
//
// DELETE /rest/api/{2-3}/field/{id}
//
// TODO: The documentation for the method Field.Delete() needs to be created!
// https://docs.go-atlassian.io/jira-software-cloud/issues/fields#delete-field
func (i *IssueFieldService) Delete(ctx context.Context, fieldId string) (*model.TaskScheme, *model.ResponseScheme, error) {
return i.internalClient.Delete(ctx, fieldId)
}
Expand Down
8 changes: 4 additions & 4 deletions jira/internal/field_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func Test_internalIssueFieldServiceImpl_Gets(t *testing.T) {
testCase.on(&testCase.fields)
}

fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil)
fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil, nil)
assert.NoError(t, err)

gotResult, gotResponse, err := fieldService.Gets(testCase.args.ctx)
Expand Down Expand Up @@ -322,7 +322,7 @@ func Test_internalIssueFieldServiceImpl_Create(t *testing.T) {
testCase.on(&testCase.fields)
}

fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil)
fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil, nil)
assert.NoError(t, err)

gotResult, gotResponse, err := fieldService.Create(testCase.args.ctx, testCase.args.payload)
Expand Down Expand Up @@ -508,7 +508,7 @@ func Test_internalIssueFieldServiceImpl_Search(t *testing.T) {
testCase.on(&testCase.fields)
}

fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil)
fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil, nil)
assert.NoError(t, err)

gotResult, gotResponse, err := fieldService.Search(testCase.args.ctx, testCase.args.options,
Expand Down Expand Up @@ -657,7 +657,7 @@ func Test_internalIssueFieldServiceImpl_Delete(t *testing.T) {
testCase.on(&testCase.fields)
}

fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil)
fieldService, err := NewIssueFieldService(testCase.fields.c, testCase.fields.version, nil, nil, nil)
assert.NoError(t, err)

gotResult, gotResponse, err := fieldService.Delete(testCase.args.ctx, testCase.args.fieldId)
Expand Down
Loading

0 comments on commit a0ad3c5

Please sign in to comment.