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

✨ Added the Page.Gets() method on the Confluence V2 module #235

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
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
121 changes: 67 additions & 54 deletions confluence/internal/page_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,28 @@ 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.
//
// # The number of results is limited by the limit parameter and additional results
//
// (if available) will be available through the next cursor
// Gets returns all pages.
//
// 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
//
// # The number of results is limited by the limit parameter and additional results
// Bulk returns all pages.
//
// (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.
//
// # 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/labels/{id}/pages
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages-for-label
Expand Down Expand Up @@ -117,85 +107,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
Loading
Loading