add flag for updating on change

This commit is contained in:
Kassem Sandarusi 2025-01-09 18:39:18 -06:00 committed by Manuel Rüger
parent c63201159d
commit 5accce3b17
2 changed files with 61 additions and 3 deletions

View File

@ -49,6 +49,7 @@ type PageInfo struct {
Version struct { Version struct {
Number int64 `json:"number"` Number int64 `json:"number"`
Message string `json:"message"`
} `json:"version"` } `json:"version"`
Ancestors []struct { Ancestors []struct {

59
main.go
View File

@ -2,9 +2,12 @@ package main
import ( import (
"bytes" "bytes"
"crypto/sha1"
"encoding/hex"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"slices" "slices"
"strings" "strings"
"time" "time"
@ -190,6 +193,12 @@ var flags = []cli.Flag{
TakesFile: true, TakesFile: true,
EnvVars: []string{"MARK_INCLUDE_PATH"}, EnvVars: []string{"MARK_INCLUDE_PATH"},
}), }),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: "changes-only",
Value: false,
Usage: "Avoids re-uploading pages that haven't changed since the last run.",
EnvVars: []string{"CHANGES_ONLY"},
}),
} }
func main() { func main() {
@ -515,10 +524,52 @@ func processFile(
html = buffer.String() html = buffer.String()
} }
err = api.UpdatePage(target, html, cCtx.Bool("minor-edit"), cCtx.String("version-message"), meta.Labels, meta.ContentAppearance) var finalVersionMessage string
var shouldUpdatePage bool = true
if cCtx.Bool("changes-only") {
contentHash := getSHA1Hash(html)
log.Debugf(
nil,
"content hash: %s",
contentHash,
)
versionPattern := `\[v([a-f0-9]{40})]$`
re := regexp.MustCompile(versionPattern)
matches := re.FindStringSubmatch(target.Version.Message)
if len(matches) > 1 {
log.Debugf(
nil,
"previous content hash: %s",
matches[1],
)
if matches[1] == contentHash {
log.Infof(
nil,
"page %q is already up to date",
target.Title,
)
shouldUpdatePage = false
}
}
finalVersionMessage = fmt.Sprintf("%s [v%s]", cCtx.String("version-message"), contentHash)
} else {
finalVersionMessage = cCtx.String("version-message")
}
if shouldUpdatePage {
err = api.UpdatePage(target, html, cCtx.Bool("minor-edit"), finalVersionMessage, meta.Labels, meta.ContentAppearance)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
updateLabels(api, target, meta) updateLabels(api, target, meta)
@ -630,3 +681,9 @@ func setLogLevel(cCtx *cli.Context) error {
return nil return nil
} }
func getSHA1Hash(input string) string {
hash := sha1.New()
hash.Write([]byte(input))
return hex.EncodeToString(hash.Sum(nil))
}