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

Change label management to rely on independent rest calls #454

Merged
merged 3 commits into from
Jul 19, 2024
Merged
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 .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(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.EqualFold(metaLabel, 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.EqualFold(label.Name, 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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there was a typo here anyway.

Is the change backwards compatible with this one in mind?

"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