diff --git a/pkg/confluence/api.go b/pkg/confluence/api.go index b99802d..8386d70 100644 --- a/pkg/confluence/api.go +++ b/pkg/confluence/api.go @@ -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"` @@ -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"` @@ -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}, diff --git a/pkg/mark/ancestry.go b/pkg/mark/ancestry.go index 6873ee8..d436778 100644 --- a/pkg/mark/ancestry.go +++ b/pkg/mark/ancestry.go @@ -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) diff --git a/pkg/mark/mark.go b/pkg/mark/mark.go index d0aa075..ba4497a 100644 --- a/pkg/mark/mark.go +++ b/pkg/mark/mark.go @@ -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) }