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

allow space home update and its children pages #101

Merged
merged 5 commits into from
Aug 23, 2021
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
41 changes: 33 additions & 8 deletions pkg/confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ type API struct {
BaseURL string
}

type SpaceInfo struct {
ID int `json:"id"`
Key string `json:"key"`
Name string `json:"name"`

Homepage PageInfo `json:"homepage"`

Links struct {
Full string `json:"webui"`
} `json:"_links"`
}

type PageInfo struct {
ID string `json:"id"`
Title string `json:"title"`
Expand Down Expand Up @@ -122,6 +134,26 @@ func (api *API) FindRootPage(space string) (*PageInfo, error) {
}, nil
}

func (api *API) FindHomePage(space string) (*PageInfo, error) {
payload := map[string]string{
"expand": "homepage",
}

request, err := api.rest.Res(
"space/"+space, &SpaceInfo{},
).Get(payload)

if err != nil {
return nil, err
}

if request.Raw.StatusCode == 404 || request.Raw.StatusCode != 200 {
return nil, newErrorStatusNotOK(request)
}

return &request.Response.(*SpaceInfo).Homepage, nil
}

func (api *API) FindPage(space string, title string, pageType string) (*PageInfo, error) {
result := struct {
Results []PageInfo `json:"results"`
Expand Down Expand Up @@ -442,14 +474,7 @@ func (api *API) UpdatePage(
nextPageVersion := page.Version.Number + 1
oldAncestors := []map[string]interface{}{}

if page.Type != "blogpost" {
if len(page.Ancestors) == 0 {
return fmt.Errorf(
"page %q info does not contain any information about parents",
page.ID,
)
}

if page.Type != "blogpost" && len(page.Ancestors) > 0 {
// picking only the last one, which is required by confluence
oldAncestors = []map[string]interface{}{
{"id": page.Ancestors[len(page.Ancestors)-1].Id},
Expand Down
19 changes: 17 additions & 2 deletions pkg/mark/ancestry.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,26 @@ func ValidateAncestry(
return nil, nil
}

isHomepage := false
if len(page.Ancestors) < 1 {
return nil, fmt.Errorf(`page %q has no parents`, page.Title)
homepage, err := api.FindHomePage(space)
if err != nil {
return nil, karma.Format(
err,
"can't obtain home page from space %q",
space,
)
}

if page.ID == homepage.ID {
log.Debugf(nil, "page is homepage for space %q", space)
isHomepage = true
} else {
return nil, fmt.Errorf(`page %q has no parents`, page.Title)
}
}

if len(page.Ancestors) < len(ancestry) {
if !isHomepage && len(page.Ancestors) < len(ancestry) {
actual := []string{}
for _, ancestor := range page.Ancestors {
actual = append(actual, ancestor.Title)
Expand Down
19 changes: 18 additions & 1 deletion pkg/mark/mark.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,25 @@ func ResolvePage(
return nil, page, nil
}

// check to see if home page is in Parents
homepage, err := api.FindHomePage(meta.Space)
if err != nil {
return nil, nil, karma.Format(
err,
"can't obtain home page from space %q",
meta.Space,
)
}

skipHomeAncestry := false
if len(meta.Parents) > 0 {
if homepage.Title == meta.Parents[0] {
skipHomeAncestry = true
}
}

ancestry := meta.Parents
if page != nil {
if page != nil && !skipHomeAncestry {
ancestry = append(ancestry, page.Title)
}

Expand Down