Skip to content

Commit

Permalink
Merge pull request #8 from ctreminiom/feature-adf-comments
Browse files Browse the repository at this point in the history
✨ feature-adf-comments
  • Loading branch information
ctreminiom authored Mar 24, 2021
2 parents c2de6bf + c47be5e commit 1a70593
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.idea/
jira/examples/
/jira/examples/
64 changes: 64 additions & 0 deletions jira/examples/issue/comment/add/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"context"
"github.com/ctreminiom/go-atlassian/jira"
"log"
"os"
)

func main() {

var (
host = os.Getenv("HOST")
mail = os.Getenv("MAIL")
token = os.Getenv("TOKEN")
)

atlassian, err := jira.New(nil, host)
if err != nil {
return
}

atlassian.Auth.SetBasicAuth(mail, token)
atlassian.Auth.SetUserAgent("curl/7.54.0")

commentBody := jira.CommentNodeScheme{}
commentBody.Version = 1
commentBody.Type = "doc"

commentBody.AppendNode(&jira.CommentNodeScheme{
Type: "paragraph",
Content: []*jira.CommentNodeScheme{
{
Type: "text",
Text: "Carlos Test",
},
{
Type: "emoji",
Attrs: map[string]interface{}{
"shortName": ":grin",
"id": "1f601",
"text": "😁",
},
},
{
Type: "text",
Text: " ",
},
},
})

newComment, response, err := atlassian.Issue.Comment.Add(context.Background(), "KP-2", "role", "Administrators", &commentBody, nil)
if err != nil {
if response != nil {
log.Println("Response HTTP Response", string(response.BodyAsBytes))
}
log.Fatal(err)
}

log.Println("Response HTTP Code", response.StatusCode)
log.Println("HTTP Endpoint Used", response.Endpoint)

log.Println(newComment.ID)
}
81 changes: 81 additions & 0 deletions jira/issueComment.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,84 @@ func (c *CommentService) Delete(ctx context.Context, issueKeyOrID, commentID str

return
}

func (c *CommentService) Add(ctx context.Context, issueKeyOrID, visibilityType, visibilityValue string, body *CommentNodeScheme, expands []string) (result *CommentScheme, response *Response, err error) {

if len(issueKeyOrID) == 0 {
return nil, nil, fmt.Errorf("error, please provide a valid issueKeyOrID value")
}

if body == nil {
return nil, nil, fmt.Errorf("error, please provide a valid CommentNodeScheme pointer")
}

var commentPayload = map[string]interface{}{}
commentPayload["body"] = body

var visibilityPayload = map[string]interface{}{}
visibilityPayload["type"] = visibilityType
visibilityPayload["value"] = visibilityValue

commentPayload["visibility"] = visibilityPayload

params := url.Values{}
var expand string
for index, value := range expands {

if index == 0 {
expand = value
continue
}

expand += "," + value
}

if len(expand) != 0 {
params.Add("expand", expand)
}

var endpoint string
if len(params.Encode()) != 0 {
endpoint = fmt.Sprintf("rest/api/3/issue/%v/comment?%v", issueKeyOrID, params.Encode())
} else {
endpoint = fmt.Sprintf("rest/api/3/issue/%v/comment", issueKeyOrID)
}

request, err := c.client.newRequest(ctx, http.MethodPost, endpoint, &commentPayload)
if err != nil {
return
}

request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")

response, err = c.client.Do(request)
if err != nil {
return
}

result = new(CommentScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return
}

return
}

type CommentNodeScheme struct {
Version int `json:"version,omitempty"`
Type string `json:"type,omitempty"`
Content []*CommentNodeScheme `json:"content,omitempty"`
Text string `json:"text,omitempty"`
Attrs map[string]interface{} `json:"attrs,omitempty"`
Marks []*MarkScheme `json:"marks,omitempty"`
}

func (n *CommentNodeScheme) AppendNode(node *CommentNodeScheme) {
n.Content = append(n.Content, node)
}

type MarkScheme struct {
Type string `json:"type"`
Attrs map[string]interface{} `json:"attrs"`
}
Loading

0 comments on commit 1a70593

Please sign in to comment.