Skip to content

Commit

Permalink
Add Support for Filtering Confluence V2 Bulk By ID
Browse files Browse the repository at this point in the history
The Confluence V2 API Bulk get supports filtering by IDs. This adds
support for that parameter optionally and is completely backwards
compatible.
  • Loading branch information
squatched committed Jun 13, 2023
1 parent cedda2e commit 8fd3c61
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
14 changes: 11 additions & 3 deletions confluence/internal/page_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
)

// NewPageService returns a new Confluence V2 Page service
Expand Down Expand Up @@ -40,8 +41,8 @@ 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) Bulk(ctx context.Context, cursor string, limit int, pageIDs ...int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.Bulk(ctx, cursor, limit, pageIDs...)
}

// GetsByLabel returns the pages of specified label.
Expand Down Expand Up @@ -141,10 +142,17 @@ func (i *internalPageImpl) Get(ctx context.Context, pageID int, format string, d
return page, response, nil
}

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

query := url.Values{}
query.Add("limit", strconv.Itoa(limit))
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 cursor != "" {
query.Add("cursor", cursor)
Expand Down
37 changes: 33 additions & 4 deletions confluence/internal/page_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
}

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

testCases := []struct {
Expand Down Expand Up @@ -179,6 +180,34 @@ func Test_internalPageImpl_Bulk(t *testing.T) {
},
},

{
name: "when the page ids are included",
args: args{
ctx: context.TODO(),
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?cursor=cursor-sample&id=1%2C2%2C3%2C4%2C5%2C6&limit=200",
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 @@ -214,7 +243,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.Bulk(testCase.args.ctx, testCase.args.cursor, testCase.args.limit, testCase.args.pageIDs...)

if testCase.wantErr {

Expand Down
2 changes: 1 addition & 1 deletion service/confluence/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type PageConnector interface {
// 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)
Bulk(ctx context.Context, cursor string, limit int, pageIDs... int) (*models.PageChunkScheme, *models.ResponseScheme, error)

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

0 comments on commit 8fd3c61

Please sign in to comment.