mark/page/ancestry.go

187 lines
3.4 KiB
Go
Raw Permalink Normal View History

2024-09-26 15:24:39 +02:00
package page
2019-04-08 22:44:27 +03:00
import (
"fmt"
"strings"
2024-09-26 15:24:39 +02:00
"github.com/kovetskiy/mark/confluence"
2019-04-08 22:44:27 +03:00
"github.com/reconquest/karma-go"
2020-11-03 17:12:51 +03:00
"github.com/reconquest/pkg/log"
2019-04-08 22:44:27 +03:00
)
func EnsureAncestry(
dryRun bool,
2019-04-08 22:44:27 +03:00
api *confluence.API,
space string,
ancestry []string,
) (*confluence.PageInfo, error) {
var parent *confluence.PageInfo
rest := ancestry
for i, title := range ancestry {
page, err := api.FindPage(space, title, "page")
2019-04-08 22:44:27 +03:00
if err != nil {
return nil, karma.Format(
err,
2019-04-19 10:31:41 +03:00
`error during finding parent page with title %q`,
2019-04-08 22:44:27 +03:00
title,
)
}
if page == nil {
break
}
2019-04-20 10:24:30 +03:00
log.Debugf(nil, "parent page %q exists: %s", title, page.Links.Full)
2019-04-08 22:44:27 +03:00
rest = ancestry[i:]
parent = page
}
if parent != nil {
rest = rest[1:]
} else {
page, err := api.FindRootPage(space)
if err != nil {
return nil, karma.Format(
err,
2019-04-19 10:31:41 +03:00
"can't find root page for space %q",
space,
2019-04-08 22:44:27 +03:00
)
}
parent = page
}
if len(rest) == 0 {
return parent, nil
}
2019-08-02 22:58:08 +03:00
log.Debugf(
nil,
2019-04-19 10:31:41 +03:00
"empty pages under %q to be created: %s",
2019-04-08 22:44:27 +03:00
parent.Title,
strings.Join(rest, ` > `),
)
if !dryRun {
for _, title := range rest {
page, err := api.CreatePage(space, "page", parent, title, ``)
if err != nil {
return nil, karma.Format(
err,
`error during creating parent page with title %q`,
title,
)
}
parent = page
2019-04-08 22:44:27 +03:00
}
} else {
log.Infof(
nil,
"skipping page creation due to enabled dry-run mode, "+
"need to create %d pages: %v",
len(rest),
rest,
)
2019-04-08 22:44:27 +03:00
}
return parent, nil
}
func ValidateAncestry(
api *confluence.API,
space string,
ancestry []string,
) (*confluence.PageInfo, error) {
page, err := api.FindPage(space, ancestry[len(ancestry)-1], "page")
2019-04-08 22:44:27 +03:00
if err != nil {
return nil, err
}
if page == nil {
return nil, nil
}
isHomepage := false
2019-04-08 22:44:27 +03:00
if len(page.Ancestors) < 1 {
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)
}
2019-04-08 22:44:27 +03:00
}
if !isHomepage && len(page.Ancestors) < len(ancestry) {
2021-02-02 08:05:23 +03:00
actual := []string{}
for _, ancestor := range page.Ancestors {
actual = append(actual, ancestor.Title)
}
valid := false
if len(actual) == len(ancestry)-1 {
broken := false
for i := 0; i < len(actual); i++ {
if actual[i] != ancestry[i] {
broken = true
break
}
}
if !broken {
if ancestry[len(ancestry)-1] == page.Title {
valid = true
}
}
}
if !valid {
return nil, karma.Describe("title", page.Title).
Describe("actual", strings.Join(actual, " > ")).
Describe("expected", strings.Join(ancestry, " > ")).
Format(nil, "the page has fewer parents than expected")
}
2019-04-08 22:44:27 +03:00
}
for _, parent := range ancestry[:len(ancestry)-1] {
found := false
// skipping root article title
for _, ancestor := range page.Ancestors {
if ancestor.Title == parent {
found = true
break
}
}
if !found {
list := []string{}
for _, ancestor := range page.Ancestors {
list = append(list, ancestor.Title)
}
return nil, karma.Describe("expected parent", parent).
Describe("list", strings.Join(list, "; ")).
Format(
nil,
"unexpected ancestry tree, did not find expected parent page in the tree",
)
2019-04-08 22:44:27 +03:00
}
}
return page, nil
}