Skip to content

Commit

Permalink
Merge pull request #110 from ctreminiom/feature/long-task-archive_
Browse files Browse the repository at this point in the history
Feature/long task archive
  • Loading branch information
ctreminiom authored Mar 8, 2022
2 parents c94c19e + a729452 commit 1266240
Show file tree
Hide file tree
Showing 9 changed files with 621 additions and 5 deletions.
12 changes: 7 additions & 5 deletions confluence/confluence.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ type Client struct {
HTTP *http.Client
Site *url.URL

Auth *AuthenticationService
Content *ContentService
Space *SpaceService
Label *LabelService
Search *SearchService
Auth *AuthenticationService
Content *ContentService
Space *SpaceService
Label *LabelService
Search *SearchService
LongTask *LongTaskService
}

func New(httpClient *http.Client, site string) (client *Client, err error) {
Expand Down Expand Up @@ -70,6 +71,7 @@ func New(httpClient *http.Client, site string) (client *Client, err error) {

client.Label = &LabelService{client: client}
client.Search = &SearchService{client: client}
client.LongTask = &LongTaskService{client: client}
return
}

Expand Down
28 changes: 28 additions & 0 deletions confluence/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,31 @@ func (c *ContentService) History(ctx context.Context, contentID string, expand [

return
}

// Archive archives a list of pages. The pages to be archived are specified as a list of content IDs.
// This API accepts the archival request and returns a task ID. The archival process happens asynchronously.
// Use the /longtask/ REST API to get the copy task status.
// Docs: https://docs.go-atlassian.io/confluence-cloud/content#archive-pages
func (c *ContentService) Archive(ctx context.Context, payload *model.ContentArchivePayloadScheme) (result *model.ContentArchiveResultScheme, response *ResponseScheme, err error) {

payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}

endpoint := "/wiki/rest/api/content/archive"

request, err := c.client.newRequest(ctx, http.MethodPost, endpoint, payloadAsReader)
if err != nil {
return nil, nil, err
}

request.Header.Add("Content-Type", "application/json")

response, err = c.client.Call(request, &result)
if err != nil {
return nil, response, err
}

return
}
180 changes: 180 additions & 0 deletions confluence/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,186 @@ import (
"time"
)

func TestContentService_Archive(t *testing.T) {

testCases := []struct {
name string
payload *model.ContentArchivePayloadScheme
mockFile string
wantHTTPMethod string
endpoint string
context context.Context
wantHTTPCodeReturn int
wantErr bool
expectedError string
}{
{
name: "when the parameters are correct",
payload: &model.ContentArchivePayloadScheme{
Pages: []*model.ContentArchiveIDPayloadScheme{
{
ID: 1001,
},
{
ID: 1001,
},
{
ID: 1001,
},
}},
wantHTTPMethod: http.MethodPost,
mockFile: "./mocks/archive-content.json",
endpoint: "/wiki/rest/api/content/archive",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: false,
},

{
name: "when the response body is empty",
payload: &model.ContentArchivePayloadScheme{
Pages: []*model.ContentArchiveIDPayloadScheme{
{
ID: 1001,
},
{
ID: 1001,
},
{
ID: 1001,
},
}},
wantHTTPMethod: http.MethodPost,
mockFile: "./mocks/empty-json.json",
endpoint: "/wiki/rest/api/content/archive",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
expectedError: "unexpected end of JSON input",
},

{
name: "when the payload is not provided",
payload: nil,
wantHTTPMethod: http.MethodPost,
mockFile: "./mocks/archive-content.json",
endpoint: "/wiki/rest/api/content/archive",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
expectedError: "failed to parse the interface pointer, please provide a valid one",
},

{
name: "when the response status is not valid",
payload: &model.ContentArchivePayloadScheme{
Pages: []*model.ContentArchiveIDPayloadScheme{
{
ID: 1001,
},
{
ID: 1001,
},
{
ID: 1001,
},
}},
wantHTTPMethod: http.MethodPost,
endpoint: "/wiki/rest/api/content/archive",
context: context.Background(),
wantHTTPCodeReturn: http.StatusBadRequest,
wantErr: true,
expectedError: "unexpected end of JSON input",
},

{
name: "when the context is not provided",
payload: &model.ContentArchivePayloadScheme{
Pages: []*model.ContentArchiveIDPayloadScheme{
{
ID: 1001,
},
{
ID: 1001,
},
{
ID: 1001,
},
}},
wantHTTPMethod: http.MethodPost,
endpoint: "/wiki/rest/api/content/archive",
context: nil,
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
expectedError: "request creation failed: net/http: nil Context",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {

//Init a new HTTP mock server
mockOptions := mockServerOptions{
Endpoint: testCase.endpoint,
MockFilePath: testCase.mockFile,
MethodAccepted: testCase.wantHTTPMethod,
ResponseCodeWanted: testCase.wantHTTPCodeReturn,
}

mockServer, err := startMockServer(&mockOptions)
if err != nil {
t.Fatal(err)
}

defer mockServer.Close()

//Init the library instance
mockClient, err := startMockClient(mockServer.URL)
if err != nil {
t.Fatal(err)
}

implementation := &ContentService{client: mockClient}

gotResult, gotResponse, err := implementation.Archive(testCase.context, testCase.payload)

if testCase.wantErr {

if err != nil {
t.Logf("error returned: %v", err.Error())
}

assert.EqualError(t, err, testCase.expectedError)

if gotResponse != nil {
t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
}

} else {

assert.NoError(t, err)
assert.NotEqual(t, gotResponse, nil)
assert.NotEqual(t, gotResult, nil)

apiEndpoint, err := url.Parse(gotResponse.Endpoint)
if err != nil {
t.Fatal(err)
}

var endpointToAssert string

if apiEndpoint.Query().Encode() != "" {
endpointToAssert = fmt.Sprintf("%v?%v", apiEndpoint.Path, apiEndpoint.Query().Encode())
} else {
endpointToAssert = apiEndpoint.Path
}

t.Logf("HTTP Endpoint Wanted: %v, HTTP Endpoint Returned: %v", testCase.endpoint, endpointToAssert)
assert.Equal(t, testCase.endpoint, endpointToAssert)
}
})
}
}

func TestContentService_Create(t *testing.T) {

testCases := []struct {
Expand Down
62 changes: 62 additions & 0 deletions confluence/longTask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package confluence

import (
"context"
"fmt"
"github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
"net/url"
"strconv"
)

type LongTaskService struct{ client *Client }

// Gets returns information about all active long-running tasks (e.g. space export),
// such as how long each task has been running and the percentage of each task that has completed.
// Docs: https://docs.go-atlassian.io/confluence-cloud/long-task#get-long-running-tasks
func (l *LongTaskService) Gets(ctx context.Context, start, limit int) (result *models.LongTaskPageScheme,
response *ResponseScheme, err error) {

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

var endpoint = fmt.Sprintf("/wiki/rest/api/longtask?%v", query.Encode())

request, err := l.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, nil, err
}

request.Header.Set("Accept", "application/json")

response, err = l.client.Call(request, &result)
if err != nil {
return nil, response, err
}

return
}

// Get returns information about an active long-running task (e.g. space export), such as how long it has been running
//and the percentage of the task that has completed.
// Docs: https://docs.go-atlassian.io/confluence-cloud/long-task#get-long-running-task
func (l *LongTaskService) Get(ctx context.Context, taskID string) (result *models.LongTaskScheme, response *ResponseScheme,
err error) {

var endpoint = fmt.Sprintf("/wiki/rest/api/longtask/%v", taskID)

request, err := l.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, nil, err
}

request.Header.Set("Accept", "application/json")

response, err = l.client.Call(request, &result)
if err != nil {
return nil, response, err
}

return
}
Loading

0 comments on commit 1266240

Please sign in to comment.