Stale manifest recovery (#7)
Reviewed-on: https://gitea.westgate.pw/ShadowsOverWestgate/sow-tools/pulls/7 Co-authored-by: vickydotbat <vickydotbat@tutamail.com> Co-committed-by: vickydotbat <vickydotbat@tutamail.com>
This commit is contained in:
@@ -64,11 +64,12 @@ type deployResult struct {
|
||||
}
|
||||
|
||||
type wikiDeployPage struct {
|
||||
PageID string
|
||||
Title string
|
||||
Namespace string
|
||||
Content string
|
||||
Hash string
|
||||
PageID string
|
||||
Title string
|
||||
PublicSlug string
|
||||
Namespace string
|
||||
Content string
|
||||
Hash string
|
||||
}
|
||||
|
||||
type wikiDeployManifest struct {
|
||||
@@ -330,11 +331,12 @@ func collectLocalPages(sourceDir string, namespaces []string) (map[string]wikiDe
|
||||
}
|
||||
content := string(raw)
|
||||
pages[pageID] = wikiDeployPage{
|
||||
PageID: pageID,
|
||||
Title: extractPageTitle(content, pageID),
|
||||
Namespace: namespace,
|
||||
Content: content,
|
||||
Hash: computeManagedHash(content),
|
||||
PageID: pageID,
|
||||
Title: extractPageTitle(content, pageID),
|
||||
PublicSlug: extractWikiPagePublicSlug(content),
|
||||
Namespace: namespace,
|
||||
Content: content,
|
||||
Hash: computeManagedHash(content),
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
@@ -415,7 +417,39 @@ func planNodeBBDeploy(pages map[string]wikiDeployPage, manifest wikiDeployManife
|
||||
}
|
||||
remote, err := client.getPost(entry.PID)
|
||||
if err != nil {
|
||||
return nil, result, next, err
|
||||
if !isNodeBBMissingResource(err) {
|
||||
return nil, result, next, err
|
||||
}
|
||||
entry.TID = 0
|
||||
entry.PID = 0
|
||||
if entry.CID != 0 {
|
||||
adopted, ok, adoptErr := adoptExistingNodeBBPage(page, entry.CID, remotePagesByCID, client)
|
||||
if adoptErr != nil {
|
||||
return nil, result, next, adoptErr
|
||||
}
|
||||
if ok {
|
||||
entry.TID = adopted.TID
|
||||
entry.PID = adopted.PID
|
||||
entry.CID = adopted.CID
|
||||
}
|
||||
}
|
||||
next.Pages[pageID] = entry
|
||||
if entry.PID == 0 {
|
||||
if !opts.AllowCreates {
|
||||
return nil, result, next, fmt.Errorf("wiki page %q has no manifest post mapping; pass --create and --category %s=<cid> to create it", pageID, page.Namespace)
|
||||
}
|
||||
if entry.CID == 0 {
|
||||
return nil, result, next, fmt.Errorf("wiki page %q requires --category %s=<cid> for creation", pageID, page.Namespace)
|
||||
}
|
||||
result.Created++
|
||||
page.Title = entry.TopicTitle
|
||||
plans = append(plans, wikiDeployPlan{Page: page, Entry: entry, Action: "create", Content: page.Content})
|
||||
continue
|
||||
}
|
||||
remote, err = client.getPost(entry.PID)
|
||||
if err != nil {
|
||||
return nil, result, next, err
|
||||
}
|
||||
}
|
||||
if entry.TID == 0 && remote.TID != 0 {
|
||||
entry.TID = remote.TID
|
||||
@@ -602,6 +636,15 @@ func fetchNodeBBWikiPageMapping(page wikiDeployPage, cid int, remotePage nodeBBW
|
||||
}
|
||||
|
||||
func matchExistingNodeBBPage(page wikiDeployPage, remotePages []nodeBBWikiPage) (nodeBBWikiPage, bool) {
|
||||
if page.PublicSlug != "" {
|
||||
for _, candidate := range remotePages {
|
||||
for _, slug := range []string{nodeBBPathLeaf(candidate.Slug), nodeBBPathLeaf(candidate.WikiPath)} {
|
||||
if slugifyWikiTitle(slug) == page.PublicSlug {
|
||||
return candidate, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
desiredTitles := map[string]struct{}{}
|
||||
for _, title := range []string{page.Title, pageIDLeaf(page.PageID)} {
|
||||
if normalized := normalizeWikiTitle(title); normalized != "" {
|
||||
@@ -880,6 +923,32 @@ func renderArchivedWikiPage(pageID, title string) string {
|
||||
}, "\n"))
|
||||
}
|
||||
|
||||
func extractWikiPagePublicSlug(content string) string {
|
||||
const pageMarker = "sow-topdata-wiki:page="
|
||||
const slugField = "wiki_slug="
|
||||
start := strings.Index(content, pageMarker)
|
||||
if start < 0 {
|
||||
return ""
|
||||
}
|
||||
end := strings.Index(content[start:], "-->")
|
||||
if end < 0 {
|
||||
return ""
|
||||
}
|
||||
marker := content[start : start+end]
|
||||
fieldStart := strings.Index(marker, slugField)
|
||||
if fieldStart < 0 {
|
||||
return ""
|
||||
}
|
||||
value := marker[fieldStart+len(slugField):]
|
||||
if fields := strings.Fields(value); len(fields) > 0 {
|
||||
slug := strings.TrimSpace(fields[0])
|
||||
if slug != "" && slug == slugifyWikiTitle(slug) {
|
||||
return slug
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func extractPageTitle(content, fallback string) string {
|
||||
if title := extractMarkdownTitle(content); title != "" {
|
||||
return title
|
||||
@@ -979,6 +1048,11 @@ func (e nodeBBHTTPError) Error() string {
|
||||
return fmt.Sprintf("NodeBB %s %s failed: HTTP %d: %s", e.Method, e.Path, e.Status, e.Body)
|
||||
}
|
||||
|
||||
func isNodeBBMissingResource(err error) bool {
|
||||
var httpErr nodeBBHTTPError
|
||||
return errors.As(err, &httpErr) && (httpErr.Status == http.StatusNotFound || httpErr.Status == http.StatusGone)
|
||||
}
|
||||
|
||||
type nodeBBPost struct {
|
||||
PID int
|
||||
TID int
|
||||
|
||||
Reference in New Issue
Block a user