From f40a31fe85035ea52d1bc0bb7d1a5e3b4676d9f6 Mon Sep 17 00:00:00 2001 From: Andy Grunwald Date: Sun, 27 Mar 2016 14:03:40 +0200 Subject: [PATCH] Added AddComment to IssueService --- issue.go | 84 +++++++++++++++++++++++++++------------------------ issue_test.go | 29 +++++++++++++++++- 2 files changed, 72 insertions(+), 41 deletions(-) diff --git a/issue.go b/issue.go index 5f1854c..3262976 100644 --- a/issue.go +++ b/issue.go @@ -44,26 +44,26 @@ type IssueFields struct { // * "subtasks": [], // * "environment": null, // * "duedate": null, - Type IssueType `json:"issuetype"` - Project Project `json:"project,omitempty"` - Resolution *Resolution `json:"resolution,omitempty"` - Priority *Priority `json:"priority,omitempty"` - Resolutiondate string `json:"resolutiondate,omitempty"` - Created string `json:"created,omitempty"` - Watches *Watches `json:"watches,omitempty"` - Assignee *Assignee `json:"assignee,omitempty"` - Updated int `json:"updated,omitempty"` - Description string `json:"description,omitempty"` - Summary string `json:"summary"` - Creator *Assignee `json:"Creator,omitempty"` - Reporter *Assignee `json:"reporter,omitempty"` - Components []*Component `json:"components,omitempty"` - Status *Status `json:"status,omitempty"` - Progress *Progress `json:"progress,omitempty"` - AggregateProgress *Progress `json:"aggregateprogress,omitempty"` - Worklog []*Worklog `json:"worklog,omitempty"` - IssueLinks []*IssueLink `json:"issuelinks,omitempty"` - Comments []*Comment `json:"comment,omitempty"` + Type IssueType `json:"issuetype"` + Project Project `json:"project,omitempty"` + Resolution *Resolution `json:"resolution,omitempty"` + Priority *Priority `json:"priority,omitempty"` + Resolutiondate string `json:"resolutiondate,omitempty"` + Created string `json:"created,omitempty"` + Watches *Watches `json:"watches,omitempty"` + Assignee *Assignee `json:"assignee,omitempty"` + Updated int `json:"updated,omitempty"` + Description string `json:"description,omitempty"` + Summary string `json:"summary"` + Creator *Assignee `json:"Creator,omitempty"` + Reporter *Assignee `json:"reporter,omitempty"` + Components []*Component `json:"components,omitempty"` + Status *Status `json:"status,omitempty"` + Progress *Progress `json:"progress,omitempty"` + AggregateProgress *Progress `json:"aggregateprogress,omitempty"` + Worklog []*Worklog `json:"worklog,omitempty"` + IssueLinks []*IssueLink `json:"issuelinks,omitempty"` + Comments []*Comment `json:"comment,omitempty"` FixVersions []*FixVersion `json:"fixVersions,omitempty"` } @@ -185,13 +185,14 @@ type IssueLinkType struct { // Comment represents a comment by a person to an issue in JIRA. type Comment struct { - Self string `json:"self"` - Name string `json:"name"` - Author Assignee `json:"author"` - Body string `json:"body"` - UpdateAuthor Assignee `json:"updateAuthor"` - Updated string `json:"updated"` - Created string `json:"created"` + Self string `json:"self"` + Name string `json:"name"` + Author Assignee `json:"author"` + Body string `json:"body"` + UpdateAuthor Assignee `json:"updateAuthor"` + Updated string `json:"updated"` + Created string `json:"created"` + Visibility CommentVisibility `json:"visibility"` } // FixVersion represents a software release in which an issue is fixed. @@ -206,6 +207,13 @@ type FixVersion struct { UserReleaseDate string `json:"userReleaseDate,omitempty"` } +// CommentVisibility represents he visibility of a comment. +// E.g. Type could be "role" and Value "Administrators" +type CommentVisibility struct { + Type string `json:"type"` + Value string `json:"value"` +} + // Get returns a full representation of the issue for the given issue key. // JIRA will attempt to identify the issue by the issueIdOrKey path parameter. // This can be an issue id, or an issue key. @@ -249,22 +257,18 @@ func (s *IssueService) Create(issue *Issue) (*Issue, *http.Response, error) { return responseIssue, resp, nil } -func (s *IssueService) AddComment(issueID string, comment string) (*Issue, *http.Response, error) { +// AddComment adds a new comment to issueID. +// +// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-addComment +func (s *IssueService) AddComment(issueID string, comment *Comment) (*Comment, *http.Response, error) { apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/comment", issueID) - - type CommentBody struct { - Body string `json:"body"` - } - - params := CommentBody{Body: comment} - - req, err := s.client.NewRequest("POST", apiEndpoint, params) + req, err := s.client.NewRequest("POST", apiEndpoint, comment) if err != nil { return nil, nil, err } - responseIssue := new(Issue) - resp, _ := s.client.Do(req, responseIssue) + responseComment := new(Comment) + resp, _ := s.client.Do(req, responseComment) - return responseIssue, resp, nil -} \ No newline at end of file + return responseComment, resp, nil +} diff --git a/issue_test.go b/issue_test.go index 685c313..2b9474a 100644 --- a/issue_test.go +++ b/issue_test.go @@ -25,7 +25,7 @@ func TestIssueGet_Success(t *testing.T) { } } -func TestIssueCreate_Success(t *testing.T) { +func TestIssueCreate(t *testing.T) { setup() defer teardown() testMux.HandleFunc("/rest/api/2/issue/", func(w http.ResponseWriter, r *http.Request) { @@ -49,3 +49,30 @@ func TestIssueCreate_Success(t *testing.T) { t.Error("Error given: %s", err) } } + +func TestIssueAddComment(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/issue/10000/comment", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testRequestURL(t, r, "/rest/api/2/issue/10000/comment") + + w.WriteHeader(http.StatusCreated) + fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/issue/10010/comment/10000","id":"10000","author":{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name":"fred","displayName":"Fred F. User","active":false},"body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.","updateAuthor":{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name":"fred","displayName":"Fred F. User","active":false},"created":"2016-03-16T04:22:37.356+0000","updated":"2016-03-16T04:22:37.356+0000","visibility":{"type":"role","value":"Administrators"}}`) + }) + + c := &Comment{ + Body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.", + Visibility: CommentVisibility{ + Type: "role", + Value: "Administrators", + }, + } + comment, _, err := testClient.Issue.AddComment("10000", c) + if comment == nil { + t.Errorf("Expected Comment. Comment is nil") + } + if err != nil { + t.Error("Error given: %s", err) + } +}