Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Filtering Confluence V2 Bulk #217

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions confluence/internal/page_impl.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
)

// NewPageService returns a new Confluence V2 Page service
@@ -44,6 +45,19 @@ func (p *PageService) Bulk(ctx context.Context, cursor string, limit int) (*mode
return p.internalClient.Bulk(ctx, cursor, limit)
}

// BulkFiltered returns all pages that fit the filtering criteria
//
// # The number of results is limited by the limit parameter and additional results
//
// (if available) will be available through the next cursor
//
// 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...)
}

// GetsByLabel returns the pages of specified label.
//
// # The number of results is limited by the limit parameter and additional results
@@ -142,14 +156,34 @@ func (i *internalPageImpl) Get(ctx context.Context, pageID int, format string, d
}

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) BulkFiltered(ctx context.Context, status, format, cursor string, limit int, pageIDs ...int) (*model.PageChunkScheme, *model.ResponseScheme, error) {

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 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, ","))
}

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

request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil)
142 changes: 142 additions & 0 deletions confluence/internal/page_impl_test.go
Original file line number Diff line number Diff line change
@@ -234,6 +234,148 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
}
}

func Test_internalPageImpl_BulkFiltered(t *testing.T) {

type fields struct {
c service.Client
}

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

testCases := []struct {
name string
fields fields
args args
on func(*fields)
wantErr bool
Err error
}{
{
name: "when the parameters are minimally correct",
args: args{
ctx: context.TODO(),
cursor: "cursor-sample",
limit: 200,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages?cursor=cursor-sample&limit=200",
nil).
Return(&http.Request{}, nil)

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

fields.c = client
},
},

{
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.NewClient(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{
ctx: context.TODO(),
cursor: "cursor-sample",
limit: 200,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages?cursor=cursor-sample&limit=200",
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)
}

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...,
)

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)
assert.NotEqual(t, gotResult, nil)
}

})
}
}

func Test_internalPageImpl_GetsByLabel(t *testing.T) {

type fields struct {
11 changes: 11 additions & 0 deletions service/confluence/page.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,17 @@ type PageConnector interface {
// 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.
//
// The number of results is limited by the limit parameter and additional results
//
// (if available) will be available through the next cursor
//
// 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)

// GetsByLabel returns the pages of specified label.
//
// The number of results is limited by the limit parameter and additional results