Skip to content

Commit

Permalink
✨ Added the Page.Gets() method on the Confluence V2 module
Browse files Browse the repository at this point in the history
1. Created a method called Gets() under the PageConnector interface

2. Deprecated the Bulk() method because the Gets() method enables the possibility to set filter criteria.

3. Removed the BulkFiltered() method

4. Added the Unit Test Cases with a 95% of coverage.
  • Loading branch information
ctreminiom committed Aug 14, 2023
1 parent 1ea650d commit d67977a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 101 deletions.
111 changes: 68 additions & 43 deletions confluence/internal/page_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (p *PageService) Get(ctx context.Context, pageID int, format string, draft
return p.internalClient.Get(ctx, pageID, format, draft, version)
}

// Bulk returns all pages.
// Gets returns all pages.
//
// # The number of results is limited by the limit parameter and additional results
//
Expand All @@ -39,21 +39,23 @@ func (p *PageService) Get(ctx context.Context, pageID int, format string, draft
// GET /wiki/api/v2/pages
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages
func (p *PageService) Bulk(ctx context.Context, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.Bulk(ctx, cursor, limit)
func (p *PageService) Gets(ctx context.Context, options *model.PageOptionsScheme, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.Gets(ctx, options, cursor, limit)
}

// BulkFiltered returns all pages that fit the filtering criteria
// Bulk returns all pages.
//
// # The number of results is limited by the limit parameter and additional results
//
// (if available) will be available through the next cursor
//
// Deprecated. Please use Page.Gets() instead.
//
// GET /wiki/api/v2/pages
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages
func (p *PageService) BulkFiltered(ctx context.Context, status, format, cursor string, limit int, pageIDs ...int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.BulkFiltered(ctx, status, format, cursor, limit, pageIDs...)
func (p *PageService) Bulk(ctx context.Context, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.Bulk(ctx, cursor, limit)
}

// GetsByLabel returns the pages of specified label.
Expand Down Expand Up @@ -117,85 +119,108 @@ type internalPageImpl struct {
c service.Connector
}

func (i *internalPageImpl) Get(ctx context.Context, pageID int, format string, draft bool, version int) (*model.PageScheme, *model.ResponseScheme, error) {

if pageID == 0 {
return nil, nil, model.ErrNoPageIDError
}
func (i *internalPageImpl) Gets(ctx context.Context, options *model.PageOptionsScheme, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {

query := url.Values{}
query.Add("limit", strconv.Itoa(limit))

if format != "" {
query.Add("body-format", format)
if cursor != "" {
query.Add("cursor", cursor)
}

if draft {
query.Add("get-draft", "true")
}
if options != nil {

if version != 0 {
query.Add("version", strconv.Itoa(version))
if options.Title != "" {
query.Add("title", options.Title)
}

if options.Sort != "" {
query.Add("sort", options.Sort)
}

if options.BodyFormat != "" {
query.Add("body-format", options.BodyFormat)
}

if options.Status != nil {
query.Add("status", strings.Join(options.Status, ","))
}

if len(options.PageIDs) > 0 {

var pageIDs = make([]string, 0, len(options.PageIDs))
for _, pageIDAsInt := range options.PageIDs {
pageIDs = append(pageIDs, strconv.Itoa(pageIDAsInt))
}

query.Add("id", strings.Join(pageIDs, ","))
}

if len(options.SpaceIDs) > 0 {

var spaceIDs = make([]string, 0, len(options.SpaceIDs))
for _, spaceIDAsInt := range options.SpaceIDs {
spaceIDs = append(spaceIDs, strconv.Itoa(spaceIDAsInt))
}

query.Add("space-id", strings.Join(spaceIDs, ","))
}
}

endpoint := fmt.Sprintf("wiki/api/v2/pages/%v?%v", pageID, query.Encode())
endpoint := fmt.Sprintf("wiki/api/v2/pages?%v", query.Encode())

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

page := new(model.PageScheme)
response, err := i.c.Call(request, page)
chunk := new(model.PageChunkScheme)
response, err := i.c.Call(request, chunk)
if err != nil {
return nil, response, err
}

return page, response, nil
return chunk, response, nil
}

func (i *internalPageImpl) Bulk(ctx context.Context, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return i.BulkFiltered(ctx, "", "", cursor, limit)
}
func (i *internalPageImpl) Get(ctx context.Context, pageID int, format string, draft bool, version int) (*model.PageScheme, *model.ResponseScheme, error) {

func (i *internalPageImpl) BulkFiltered(ctx context.Context, status, format, cursor string, limit int, pageIDs ...int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
if pageID == 0 {
return nil, nil, model.ErrNoPageIDError
}

query := url.Values{}
query.Add("limit", strconv.Itoa(limit))

if status != "" {
query.Add("status", status)
}

if format != "" {
query.Add("body-format", format)
}

if cursor != "" {
query.Add("cursor", cursor)
if draft {
query.Add("get-draft", "true")
}

if len(pageIDs) > 0 {
ids := make([]string, 0, len(pageIDs))
for _, id := range pageIDs {
ids = append(ids, strconv.Itoa(id))
}
query.Add("id", strings.Join(ids, ","))
if version != 0 {
query.Add("version", strconv.Itoa(version))
}

endpoint := fmt.Sprintf("wiki/api/v2/pages?%v", query.Encode())
endpoint := fmt.Sprintf("wiki/api/v2/pages/%v?%v", pageID, query.Encode())

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

chunk := new(model.PageChunkScheme)
response, err := i.c.Call(request, chunk)
page := new(model.PageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}

return chunk, response, nil
return page, response, nil
}

func (i *internalPageImpl) Bulk(ctx context.Context, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return i.Gets(ctx, nil, cursor, limit)
}

func (i *internalPageImpl) GetsByLabel(ctx context.Context, labelID int, sort, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
Expand Down
87 changes: 32 additions & 55 deletions confluence/internal/page_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,17 @@ func Test_internalPageImpl_Get(t *testing.T) {
}
}

func Test_internalPageImpl_Bulk(t *testing.T) {
func Test_internalPageImpl_Gets(t *testing.T) {

type fields struct {
c service.Connector
}

type args struct {
ctx context.Context
cursor string
limit int
ctx context.Context
options *model.PageOptionsScheme
cursor string
limit int
}

testCases := []struct {
Expand All @@ -154,7 +155,15 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
{
name: "when the parameters are correct",
args: args{
ctx: context.TODO(),
ctx: context.TODO(),
options: &model.PageOptionsScheme{
PageIDs: []int{112, 1223},
SpaceIDs: []int{3040, 3040},
Sort: "-created-date",
Status: []string{"current", "trashed"},
Title: "Title sample!!",
BodyFormat: "atlas_doc_format",
},
cursor: "cursor-sample",
limit: 200,
},
Expand All @@ -165,7 +174,7 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages?cursor=cursor-sample&limit=200",
"wiki/api/v2/pages?body-format=atlas_doc_format&cursor=cursor-sample&id=112%2C1223&limit=200&sort=-created-date&space-id=3040%2C3040&status=current%2Ctrashed&title=Title+sample%21%21",
"", nil).
Return(&http.Request{}, nil)

Expand All @@ -183,7 +192,15 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
args: args{
ctx: context.TODO(),
cursor: "cursor-sample",
limit: 200,
options: &model.PageOptionsScheme{
PageIDs: []int{112, 1223},
SpaceIDs: []int{3040, 3040},
Sort: "-created-date",
Status: []string{"current", "trashed"},
Title: "Title sample!!",
BodyFormat: "atlas_doc_format",
},
limit: 200,
},
on: func(fields *fields) {

Expand All @@ -192,7 +209,7 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages?cursor=cursor-sample&limit=200",
"wiki/api/v2/pages?body-format=atlas_doc_format&cursor=cursor-sample&id=112%2C1223&limit=200&sort=-created-date&space-id=3040%2C3040&status=current%2Ctrashed&title=Title+sample%21%21",
"", nil).
Return(&http.Request{}, errors.New("error, unable to create the http request"))

Expand All @@ -213,7 +230,7 @@ func Test_internalPageImpl_Bulk(t *testing.T) {

newService := NewPageService(testCase.fields.c)

gotResult, gotResponse, err := newService.Bulk(testCase.args.ctx, testCase.args.cursor, testCase.args.limit)
gotResult, gotResponse, err := newService.Gets(testCase.args.ctx, testCase.args.options, testCase.args.cursor, testCase.args.limit)

if testCase.wantErr {

Expand All @@ -233,19 +250,16 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
}
}

func Test_internalPageImpl_BulkFiltered(t *testing.T) {
func Test_internalPageImpl_Bulk(t *testing.T) {

type fields struct {
c service.Connector
}

type args struct {
ctx context.Context
status string
format string
cursor string
limit int
pageIDs []int
ctx context.Context
cursor string
limit int
}

testCases := []struct {
Expand All @@ -257,7 +271,7 @@ func Test_internalPageImpl_BulkFiltered(t *testing.T) {
Err error
}{
{
name: "when the parameters are minimally correct",
name: "when the parameters are correct",
args: args{
ctx: context.TODO(),
cursor: "cursor-sample",
Expand All @@ -283,36 +297,6 @@ func Test_internalPageImpl_BulkFiltered(t *testing.T) {
},
},

{
name: "when the parameters are maximally correct",
args: args{
ctx: context.TODO(),
status: "status-sample",
format: "format-sample",
cursor: "cursor-sample",
limit: 200,
pageIDs: []int{1, 2, 3, 4, 5, 6},
},
on: func(fields *fields) {

client := mocks.NewConnector(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages?body-format=format-sample&cursor=cursor-sample&id=1%2C2%2C3%2C4%2C5%2C6&limit=200&status=status-sample",
"", nil).
Return(&http.Request{}, nil)

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

fields.c = client
},
},

{
name: "when the http request cannot be created",
args: args{
Expand Down Expand Up @@ -348,14 +332,7 @@ func Test_internalPageImpl_BulkFiltered(t *testing.T) {

newService := NewPageService(testCase.fields.c)

gotResult, gotResponse, err := newService.BulkFiltered(
testCase.args.ctx,
testCase.args.status,
testCase.args.format,
testCase.args.cursor,
testCase.args.limit,
testCase.args.pageIDs...,
)
gotResult, gotResponse, err := newService.Bulk(testCase.args.ctx, testCase.args.cursor, testCase.args.limit)

if testCase.wantErr {

Expand Down
9 changes: 9 additions & 0 deletions pkg/infra/models/confluence_page.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package models

type PageOptionsScheme struct {
PageIDs []int
SpaceIDs []int
Sort string
Status []string
Title string
BodyFormat string
}

type PageChunkScheme struct {
Results []*PageScheme `json:"results,omitempty"`
Links *PageChunkLinksScheme `json:"_links,omitempty"`
Expand Down
7 changes: 4 additions & 3 deletions service/confluence/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ type PageConnector interface {
//
// (if available) will be available through the next cursor
//
// Deprecated. Please use Page.Gets() instead.
//
// GET /wiki/api/v2/pages
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages
Bulk(ctx context.Context, cursor string, limit int) (*models.PageChunkScheme, *models.ResponseScheme, error)

// BulkFiltered returns all pages that fit the filtering criteria.
// Gets returns all pages that fit the filtering criteria.
//
// The number of results is limited by the limit parameter and additional results
//
Expand All @@ -36,7 +37,7 @@ type PageConnector interface {
// GET /wiki/api/v2/pages
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages
BulkFiltered(ctx context.Context, status, format, cursor string, limit int, pageIDs ...int) (*models.PageChunkScheme, *models.ResponseScheme, error)
Gets(ctx context.Context, options *models.PageOptionsScheme, cursor string, limit int) (*models.PageChunkScheme, *models.ResponseScheme, error)

// GetsByLabel returns the pages of specified label.
//
Expand Down

0 comments on commit d67977a

Please sign in to comment.