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 inline comment support #145

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
56 changes: 41 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ import (
)

type Flags struct {
FileGlobPatten string `docopt:"-f"`
CompileOnly bool `docopt:"--compile-only"`
DryRun bool `docopt:"--dry-run"`
EditLock bool `docopt:"-k"`
DropH1 bool `docopt:"--drop-h1"`
MinorEdit bool `docopt:"--minor-edit"`
Color string `docopt:"--color"`
Debug bool `docopt:"--debug"`
Trace bool `docopt:"--trace"`
Username string `docopt:"-u"`
Password string `docopt:"-p"`
TargetURL string `docopt:"-l"`
BaseURL string `docopt:"--base-url"`
Config string `docopt:"--config"`
Ci bool `docopt:"--ci"`
FileGlobPatten string `docopt:"-f"`
CompileOnly bool `docopt:"--compile-only"`
DryRun bool `docopt:"--dry-run"`
EditLock bool `docopt:"-k"`
DropH1 bool `docopt:"--drop-h1"`
MinorEdit bool `docopt:"--minor-edit"`
Color string `docopt:"--color"`
Debug bool `docopt:"--debug"`
Trace bool `docopt:"--trace"`
Username string `docopt:"-u"`
Password string `docopt:"-p"`
TargetURL string `docopt:"-l"`
BaseURL string `docopt:"--base-url"`
Config string `docopt:"--config"`
Ci bool `docopt:"--ci"`
PreserveComments bool `docopt:"--preserve-comments"`
}

const (
Expand Down Expand Up @@ -72,6 +73,7 @@ Options:
-c --config <path> Use the specified configuration file.
[default: $HOME/.config/mark]
--ci Runs on CI mode. It won't fail if files are not found.
--preserve-comments Try to preserve the comment from the Confluence page.
-h --help Show this message.
-v --version Show version.
`
Expand Down Expand Up @@ -245,6 +247,7 @@ func processFile(
)
}

var pageCreated bool
var target *confluence.PageInfo

if meta != nil {
Expand Down Expand Up @@ -273,6 +276,7 @@ func processFile(
meta.Title,
)
}
pageCreated = true
}

target = page
Expand Down Expand Up @@ -328,6 +332,28 @@ func processFile(
html = buffer.String()
}

if flags.PreserveComments && !pageCreated {
comments, err := api.GetInlineComments(target.ID)
if err != nil {
log.Warningf(
err,
"can't fetch comments %s %q",
meta.Type,
meta.Title,
)
}

html, err = mark.MergeComments(html, comments)
if err != nil {
log.Warningf(
err,
"can't merge comments %s %q",
meta.Type,
meta.Title,
)
}
}

err = api.UpdatePage(target, html, flags.MinorEdit, meta.Labels)
if err != nil {
log.Fatal(err)
Expand Down
27 changes: 27 additions & 0 deletions pkg/confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ type PageInfo struct {
} `json:"_links"`
}

type InlineComments struct {
Results []struct {
Extensions struct {
Location string `json:"location"`
InlineProperties struct {
OriginalSelection string `json:"originalSelection"`
MarkerRef string `json:"markerRef"`
} `json:"inlineProperties"`
} `json:"extensions"`
} `json:"results"`
}

type AttachmentInfo struct {
Filename string `json:"title"`
ID string `json:"id"`
Expand Down Expand Up @@ -420,6 +432,21 @@ func (api *API) GetPageByID(pageID string) (*PageInfo, error) {
return request.Response.(*PageInfo), nil
}

func (api *API) GetInlineComments(pageID string) (*InlineComments, error) {
request, err := api.rest.Res(
"content/"+pageID+"/child/comment", &InlineComments{},
).Get(map[string]string{"expand": "extensions.inlineProperties", "location": "inline"})
if err != nil {
return nil, err
}

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

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

func (api *API) CreatePage(
space string,
pageType string,
Expand Down
18 changes: 18 additions & 0 deletions pkg/mark/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"html"
"io"
"net/url"
"os"
Expand Down Expand Up @@ -160,6 +162,22 @@ func ResolveAttachments(
return attaches, nil
}

// The logic of merging comment is the following. Since each comment comes with
// its original selection text, we just search for that and put back the marker.
// Note that this only finds the first instance, so if you put a comment on a
// very common word, like "this", this comment won't be merged correctly.
func MergeComments(body string, comments *confluence.InlineComments) (string, error) {
for _, comment := range comments.Results {
// TODO: this doesn't handle ' well, as it becomes &lsquo;
selection := html.EscapeString(comment.Extensions.InlineProperties.OriginalSelection)
withComment := fmt.Sprintf(`<ac:inline-comment-marker ac:ref="%s">%s</ac:inline-comment-marker>`,
comment.Extensions.InlineProperties.MarkerRef, selection)
body = strings.Replace(body, selection, withComment, 1)
}

return body, nil
}

func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
links := map[string]string{}
replaces := []string{}
Expand Down