Skip to content

Commit

Permalink
Change label management to rely on independent rest calls
Browse files Browse the repository at this point in the history
  • Loading branch information
camielwnzl authored and mrueg committed Jul 19, 2024
1 parent d168ec8 commit 4a3a69b
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.idea/
/mark.test
/profile.cov
.vscode
65 changes: 65 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -522,6 +523,8 @@ func processFile(
log.Fatal(err)
}

updateLabels(err, api, target, meta)

if cCtx.Bool("edit-lock") {
log.Infof(
nil,
Expand All @@ -539,6 +542,68 @@ func processFile(
return target
}

func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *mark.Meta) {

labelInfo, err := api.GetPageLabels(target, "global")
if err != nil {
log.Fatal(err)
}

log.Debug("Page Labels:")
log.Debug(labelInfo.Labels)

log.Debug("Meta Labels:")
log.Debug(meta.Labels)

delLabels := determineLabelsToRemove(labelInfo, meta)
log.Debug("Del Labels:")
log.Debug(delLabels)

addLabels := determineLabelsToAdd(meta, labelInfo)
log.Debug("Add Labels:")
log.Debug(addLabels)

if len(addLabels) > 0 {
_, err = api.AddPageLabels(target, addLabels)
if err != nil {
log.Fatal(err)
}
}

for _, label := range delLabels {
_, err = api.DeletePageLabel(target, label)
if err != nil {
log.Fatal(err)
}
}
}

// Page has label but label not in Metadata
func determineLabelsToRemove(labelInfo *confluence.LabelInfo, meta *mark.Meta) []string {
var labels []string
for _, label := range labelInfo.Labels {
if !slices.ContainsFunc(meta.Labels, func(metaLabel string) bool {
return strings.ToLower(metaLabel) == strings.ToLower(label.Name)
}) {
labels = append(labels, label.Name)
}
}
return labels
}

// Metadata has label but Page does not have it
func determineLabelsToAdd(meta *mark.Meta, labelInfo *confluence.LabelInfo) []string {
var labels []string
for _, metaLabel := range meta.Labels {
if !slices.ContainsFunc(labelInfo.Labels, func(label confluence.Label) bool {
return strings.ToLower(label.Name) == strings.ToLower(metaLabel)
}) {
labels = append(labels, metaLabel)
}
}
return labels
}

func configFilePath() string {
fp, err := os.UserConfigDir()
if err != nil {
Expand Down
81 changes: 69 additions & 12 deletions pkg/confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ type AttachmentInfo struct {
} `json:"_links"`
}

type Label struct {
ID string `json:"id"`
Prefix string `json:"prefix"`
Name string `json:"name"`
}
type LabelInfo struct {
Labels []Label `json:"results"`
Size int `json:"number"`
}
type form struct {
buffer io.Reader
writer *multipart.Writer
Expand Down Expand Up @@ -514,17 +523,6 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
}
}

labels := []map[string]interface{}{}
for _, label := range newLabels {
if label != "" {
item := map[string]interface{}{
"prexix": "global",
"name": label,
}
labels = append(labels, item)
}
}

payload := map[string]interface{}{
"id": page.ID,
"type": page.Type,
Expand All @@ -542,7 +540,6 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
},
},
"metadata": map[string]interface{}{
"labels": labels,
// Fix to set full-width as has changed on Confluence APIs again.
// https://jira.atlassian.com/browse/CONFCLOUD-65447
//
Expand Down Expand Up @@ -570,6 +567,66 @@ func (api *API) UpdatePage(page *PageInfo, newContent string, minorEdit bool, ve
return nil
}

func (api *API) AddPageLabels(page *PageInfo, newLabels []string) (*LabelInfo, error) {

labels := []map[string]interface{}{}
for _, label := range newLabels {
if label != "" {
item := map[string]interface{}{
"prefix": "global",
"name": label,
}
labels = append(labels, item)
}
}

payload := labels

request, err := api.rest.Res(
"content/"+page.ID+"/label", &LabelInfo{},
).Post(payload)
if err != nil {
return nil, err
}

if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}

return request.Response.(*LabelInfo), nil
}

func (api *API) DeletePageLabel(page *PageInfo, label string) (*LabelInfo, error) {

request, err := api.rest.Res(
"content/"+page.ID+"/label/"+label, &LabelInfo{},
).Delete()
if err != nil {
return nil, err
}

if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}

return request.Response.(*LabelInfo), nil
}

func (api *API) GetPageLabels(page *PageInfo, prefix string) (*LabelInfo, error) {

request, err := api.rest.Res(
"content/"+page.ID+"/label", &LabelInfo{},
).Get(map[string]string{"prefix": prefix})
if err != nil {
return nil, err
}

if request.Raw.StatusCode != http.StatusOK {
return nil, newErrorStatusNotOK(request)
}
return request.Response.(*LabelInfo), nil
}

func (api *API) GetUserByName(name string) (*User, error) {
var response struct {
Results []struct {
Expand Down

0 comments on commit 4a3a69b

Please sign in to comment.