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 confluence v2 get children #208

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ to .

> If you want to ask a question, we assume that you have read the available [Documentation](https://docs.go-atlassian.io/).

Before you ask a question, it is best to search for existing [Issues](https://github.com/ctreminiom/go-atlassianissues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
Before you ask a question, it is best to search for existing [Issues](https://github.com/ctreminiom/go-atlassian/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly related of course but was reading through these guidelines and noticed this.


If you then still feel the need to ask a question and need clarification, we recommend the following:

Expand Down
42 changes: 42 additions & 0 deletions confluence/internal/page_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func (p *PageService) GetsBySpace(ctx context.Context, spaceID int, cursor strin
return p.internalClient.GetsBySpace(ctx, spaceID, cursor, limit)
}

// GetsByParent returns all children of a page.
//
// 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/{id}/children
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages-by-parent
func (p *PageService) GetsByParent(ctx context.Context, pageID int, cursor string, limit int) (*model.PageChunkScheme, *model.ResponseScheme, error) {
return p.internalClient.GetsByParent(ctx, pageID, cursor, limit)
}

// Create creates a page in the space.
//
// Pages are created as published by default unless specified as a draft in the status field.
Expand Down Expand Up @@ -228,6 +241,35 @@ func (i *internalPageImpl) GetsBySpace(ctx context.Context, spaceID int, cursor
return chunk, response, nil
}

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

if parentID == 0 {
return nil, nil, model.ErrNoPageIDError
}

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

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

endpoint := fmt.Sprintf("wiki/api/v2/pages/%v/children?%v", parentID, 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)
if err != nil {
return nil, response, err
}

return chunk, response, nil
}

func (i *internalPageImpl) Create(ctx context.Context, payload *model.PageCreatePayloadScheme) (*model.PageScheme, *model.ResponseScheme, error) {

reader, err := i.c.TransformStructToReader(payload)
Expand Down
146 changes: 146 additions & 0 deletions confluence/internal/page_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,152 @@ func Test_internalPageImpl_GetsBySpace(t *testing.T) {
}
}

func Test_internalPageImpl_GetsByParent(t *testing.T) {

type fields struct {
c service.Client
}

type args struct {
ctx context.Context
parentID int
cursor string
limit int
}

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

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages/20001/children?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 parent id is not provided",
args: args{
ctx: context.TODO(),
},
wantErr: true,
Err: model.ErrNoPageIDError,
},

{
name: "when the http request cannot be created",
args: args{
ctx: context.TODO(),
parentID: 20001,
cursor: "cursor-sample",
limit: 200,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

client.On("NewRequest",
context.Background(),
http.MethodGet,
"wiki/api/v2/pages/20001/children?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"),
},

{
name: "when the call fails",
args: args{
ctx: context.TODO(),
parentID: 20001,
cursor: "cursor-sample",
limit: 200,
},
on: func(fields *fields) {

client := mocks.NewClient(t)

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

client.On("Call",
&http.Request{},
&model.PageChunkScheme{}).
Return(&model.ResponseScheme{}, errors.New("error, request failed"))

fields.c = client
},

wantErr: true,
Err: errors.New("error, request failed"),
},
}

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.GetsByParent(testCase.args.ctx, testCase.args.parentID,
testCase.args.cursor, testCase.args.limit)

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_Delete(t *testing.T) {

type fields struct {
Expand Down
9 changes: 9 additions & 0 deletions go-atlassian.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
11 changes: 11 additions & 0 deletions service/confluence/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ type PageConnector interface {
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages-in-space
GetsBySpace(ctx context.Context, spaceID int, cursor string, limit int) (*models.PageChunkScheme, *models.ResponseScheme, error)

// GetsByParent returns all children of a page.
//
// 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/{id}/children
//
// https://docs.go-atlassian.io/confluence-cloud/v2/page#get-pages-by-parent
GetsByParent(ctx context.Context, spaceID int, cursor string, limit int) (*models.PageChunkScheme, *models.ResponseScheme, error)
squatched marked this conversation as resolved.
Show resolved Hide resolved

// Create creates a page in the space.
//
// Pages are created as published by default unless specified as a draft in the status field.
Expand Down