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

add flag for updating on change #548

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type PageInfo struct {

Version struct {
Number int64 `json:"number"`
Message string `json:"message"`
} `json:"version"`

Ancestors []struct {
Expand Down
63 changes: 60 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package main

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -190,6 +193,12 @@ var flags = []cli.Flag{
TakesFile: true,
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() {
Expand Down Expand Up @@ -515,9 +524,51 @@ func processFile(
html = buffer.String()
}

err = api.UpdatePage(target, html, cCtx.Bool("minor-edit"), cCtx.String("version-message"), meta.Labels, meta.ContentAppearance)
if err != nil {
log.Fatal(err)
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 {
log.Fatal(err)
}
}

updateLabels(api, target, meta)
Expand Down Expand Up @@ -630,3 +681,9 @@ func setLogLevel(cCtx *cli.Context) error {

return nil
}

func getSHA1Hash(input string) string {
hash := sha1.New()
hash.Write([]byte(input))
return hex.EncodeToString(hash.Sum(nil))
}
Loading