Skip to content

Commit

Permalink
added method getTicketComments
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Apr 22, 2020
1 parent 56461f7 commit 60bad98
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
58 changes: 58 additions & 0 deletions ticketcomments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tracker

type TicketComments []TicketComment
type TicketComment map[string]interface{}

// Return last comment
func (t TicketComments) GetLast() TicketComment {
countComments := len(t)
if countComments == 0 {
return TicketComment{}
}

return t[len(t)-1]
}

// Get comment author
func (t TicketComment) CreatedBy() User {
if createdBy, ok := t["createdBy"].(map[string]interface{}); ok {
return User{
Self: toString(createdBy["self"]),
ID: toString(createdBy["id"]),
Display: toString(createdBy["display"]),
}
}

return User{}
}

// Get comment text
func (t TicketComment) Text() string {
return t.GetField("text")
}

// Get comment author
func (t TicketComment) Summonees() Users {
if summonees, ok := t["summonees"].([]interface{}); ok {
users := make(Users, len(summonees))
for i := range summonees {
users[i] = User{
Self: toString(summonees[i].(map[string]interface{})["self"]),
ID: toString(summonees[i].(map[string]interface{})["id"]),
Display: toString(summonees[i].(map[string]interface{})["display"]),
}
}
return users
}

return Users{}
}

// Get any custom ticket field
func (t TicketComment) GetField(field string) string {
if key, ok := t[field]; ok {
return toString(key)
}

return ""
}
30 changes: 27 additions & 3 deletions tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

const (
TRACKER_URL = "https://api.tracker.yandex.net/v2/issues/"
ticketUrl = "https://api.tracker.yandex.net/v2/issues/"
ticketComments = "/comments"
)

type Tracker struct {
Expand All @@ -26,7 +27,7 @@ func New(token string, xOrgID string) *Tracker {

// Get Yandex.Tracker ticket by ticket key
func (t *Tracker) GetTicket(ticketKey string) (ticket Ticket, err error) {
resp, err := t.request.Get(TRACKER_URL + ticketKey)
resp, err := t.request.Get(ticketUrl + ticketKey)
if resp != nil {
defer func() {
if err := resp.RawBody().Close(); err != nil {
Expand All @@ -42,14 +43,15 @@ func (t *Tracker) GetTicket(ticketKey string) (ticket Ticket, err error) {
if err != nil {
return
}

return
}

// Patch Yandex.Tracker ticket by ticket key
func (t *Tracker) PatchTicket(ticketKey string, body map[string]string) (ticket Ticket, err error) {
resp, err := t.request.
SetBody(body).
Patch(TRACKER_URL + ticketKey)
Patch(ticketUrl + ticketKey)
if resp != nil {
defer func() {
if err := resp.RawBody().Close(); err != nil {
Expand All @@ -67,3 +69,25 @@ func (t *Tracker) PatchTicket(ticketKey string, body map[string]string) (ticket
}
return
}

// Get Yandex.Tracker ticket comments by ticket key
func (t *Tracker) GetTicketComments(ticketKey string) (comments TicketComments, err error) {
resp, err := t.request.Get(ticketUrl + ticketKey + ticketComments)
if resp != nil {
defer func() {
if err := resp.RawBody().Close(); err != nil {
return
}
}()
}
if err != nil {
return
}

err = json.Unmarshal(resp.Body(), &comments)
if err != nil {
return
}

return
}

0 comments on commit 60bad98

Please sign in to comment.