2015-09-03 12:25:21 +02:00
package jira
import (
2016-05-19 23:11:21 +02:00
"bytes"
2016-09-23 16:19:07 +02:00
"encoding/json"
2015-09-03 12:25:21 +02:00
"fmt"
2016-05-19 23:11:21 +02:00
"io"
2016-10-05 14:53:30 +02:00
"io/ioutil"
2016-05-19 23:11:21 +02:00
"mime/multipart"
2016-05-29 17:30:45 +02:00
"net/url"
2016-09-23 16:19:07 +02:00
"reflect"
2016-05-29 17:30:45 +02:00
"strings"
"time"
2016-10-03 13:33:46 +02:00
"github.com/fatih/structs"
2017-01-28 00:04:08 +02:00
"github.com/google/go-querystring/query"
2016-10-03 13:33:46 +02:00
"github.com/trivago/tgo/tcontainer"
2015-09-03 12:25:21 +02:00
)
const (
// AssigneeAutomatic represents the value of the "Assignee: Automatic" of JIRA
AssigneeAutomatic = "-1"
)
// IssueService handles Issues for the JIRA instance / API.
//
2016-03-27 14:24:48 +02:00
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue
2015-09-03 12:25:21 +02:00
type IssueService struct {
client * Client
}
// Issue represents a JIRA issue.
type Issue struct {
2017-01-28 00:04:08 +02:00
Expand string ` json:"expand,omitempty" structs:"expand,omitempty" `
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Self string ` json:"self,omitempty" structs:"self,omitempty" `
Key string ` json:"key,omitempty" structs:"key,omitempty" `
Fields * IssueFields ` json:"fields,omitempty" structs:"fields,omitempty" `
Changelog * Changelog ` json:"changelog,omitempty" structs:"changelog,omitempty" `
}
// ChangelogItems reflects one single changelog item of a history item
type ChangelogItems struct {
Field string ` json:"field" structs:"field" `
FieldType string ` json:"fieldtype" structs:"fieldtype" `
From interface { } ` json:"from" structs:"from" `
FromString string ` json:"fromString" structs:"fromString" `
To interface { } ` json:"to" structs:"to" `
ToString string ` json:"toString" structs:"toString" `
}
// ChangelogHistory reflects one single changelog history entry
type ChangelogHistory struct {
Id string ` json:"id" structs:"id" `
Author User ` json:"author" structs:"author" `
Created string ` json:"created" structs:"created" `
Items [ ] ChangelogItems ` json:"items" structs:"items" `
}
// Changelog reflects the change log of an issue
type Changelog struct {
Histories [ ] ChangelogHistory ` json:"histories,omitempty" `
2015-09-03 12:25:21 +02:00
}
2016-05-19 23:11:21 +02:00
// Attachment represents a JIRA attachment
type Attachment struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self,omitempty" structs:"self,omitempty" `
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Filename string ` json:"filename,omitempty" structs:"filename,omitempty" `
Author * User ` json:"author,omitempty" structs:"author,omitempty" `
Created string ` json:"created,omitempty" structs:"created,omitempty" `
Size int ` json:"size,omitempty" structs:"size,omitempty" `
MimeType string ` json:"mimeType,omitempty" structs:"mimeType,omitempty" `
Content string ` json:"content,omitempty" structs:"content,omitempty" `
Thumbnail string ` json:"thumbnail,omitempty" structs:"thumbnail,omitempty" `
2016-05-19 23:11:21 +02:00
}
2016-05-29 17:30:45 +02:00
2016-07-28 22:06:52 +02:00
// Epic represents the epic to which an issue is associated
// Not that this struct does not process the returned "color" value
type Epic struct {
2016-09-23 16:19:07 +02:00
ID int ` json:"id" structs:"id" `
Key string ` json:"key" structs:"key" `
Self string ` json:"self" structs:"self" `
Name string ` json:"name" structs:"name" `
Summary string ` json:"summary" structs:"summary" `
Done bool ` json:"done" structs:"done" `
2016-07-28 22:06:52 +02:00
}
2015-09-03 12:25:21 +02:00
// IssueFields represents single fields of a JIRA issue.
// Every JIRA issue has several fields attached.
type IssueFields struct {
// TODO Missing fields
2016-10-24 11:06:37 +02:00
// * "aggregatetimespent": null,
// * "workratio": -1,
// * "lastViewed": null,
// * "aggregatetimeoriginalestimate": null,
// * "aggregatetimeestimate": null,
// * "environment": null,
2017-01-28 00:04:08 +02:00
Expand string ` json:"expand,omitempty" structs:"expand,omitempty" `
2016-10-24 11:07:36 +02:00
Type IssueType ` json:"issuetype" structs:"issuetype" `
Project Project ` json:"project,omitempty" structs:"project,omitempty" `
Resolution * Resolution ` json:"resolution,omitempty" structs:"resolution,omitempty" `
Priority * Priority ` json:"priority,omitempty" structs:"priority,omitempty" `
Resolutiondate string ` json:"resolutiondate,omitempty" structs:"resolutiondate,omitempty" `
Created string ` json:"created,omitempty" structs:"created,omitempty" `
Duedate string ` json:"duedate,omitempty" structs:"duedate,omitempty" `
Watches * Watches ` json:"watches,omitempty" structs:"watches,omitempty" `
Assignee * User ` json:"assignee,omitempty" structs:"assignee,omitempty" `
Updated string ` json:"updated,omitempty" structs:"updated,omitempty" `
Description string ` json:"description,omitempty" structs:"description,omitempty" `
Summary string ` json:"summary" structs:"summary" `
Creator * User ` json:"Creator,omitempty" structs:"Creator,omitempty" `
Reporter * User ` json:"reporter,omitempty" structs:"reporter,omitempty" `
Components [ ] * Component ` json:"components,omitempty" structs:"components,omitempty" `
Status * Status ` json:"status,omitempty" structs:"status,omitempty" `
Progress * Progress ` json:"progress,omitempty" structs:"progress,omitempty" `
AggregateProgress * Progress ` json:"aggregateprogress,omitempty" structs:"aggregateprogress,omitempty" `
TimeTracking * TimeTracking ` json:"timetracking,omitempty" structs:"timetracking,omitempty" `
TimeSpent int ` json:"timespent,omitempty" structs:"timespent,omitempty" `
TimeEstimate int ` json:"timeestimate,omitempty" structs:"timeestimate,omitempty" `
TimeOriginalEstimate int ` json:"timeoriginalestimate,omitempty" structs:"timeoriginalestimate,omitempty" `
Worklog * Worklog ` json:"worklog,omitempty" structs:"worklog,omitempty" `
IssueLinks [ ] * IssueLink ` json:"issuelinks,omitempty" structs:"issuelinks,omitempty" `
Comments * Comments ` json:"comment,omitempty" structs:"comment,omitempty" `
FixVersions [ ] * FixVersion ` json:"fixVersions,omitempty" structs:"fixVersions,omitempty" `
Labels [ ] string ` json:"labels,omitempty" structs:"labels,omitempty" `
Subtasks [ ] * Subtasks ` json:"subtasks,omitempty" structs:"subtasks,omitempty" `
Attachments [ ] * Attachment ` json:"attachment,omitempty" structs:"attachment,omitempty" `
Epic * Epic ` json:"epic,omitempty" structs:"epic,omitempty" `
2016-11-04 21:44:23 +02:00
Parent * Parent ` json:"parent,omitempty" structs:"parent,omitempty" `
2016-10-24 11:07:36 +02:00
Unknowns tcontainer . MarshalMap
2016-09-23 16:19:07 +02:00
}
2016-10-03 13:33:46 +02:00
// MarshalJSON is a custom JSON marshal function for the IssueFields structs.
// It handles JIRA custom fields and maps those from / to "Unknowns" key.
2016-09-23 16:19:07 +02:00
func ( i * IssueFields ) MarshalJSON ( ) ( [ ] byte , error ) {
m := structs . Map ( i )
unknowns , okay := m [ "Unknowns" ]
if okay {
// if unknowns present, shift all key value from unkown to a level up
for key , value := range unknowns . ( tcontainer . MarshalMap ) {
m [ key ] = value
}
delete ( m , "Unknowns" )
}
return json . Marshal ( m )
}
2016-10-03 13:33:46 +02:00
// UnmarshalJSON is a custom JSON marshal function for the IssueFields structs.
// It handles JIRA custom fields and maps those from / to "Unknowns" key.
2016-09-23 16:19:07 +02:00
func ( i * IssueFields ) UnmarshalJSON ( data [ ] byte ) error {
// Do the normal unmarshalling first
// Details for this way: http://choly.ca/post/go-json-marshalling/
type Alias IssueFields
aux := & struct {
* Alias
} {
Alias : ( * Alias ) ( i ) ,
}
if err := json . Unmarshal ( data , & aux ) ; err != nil {
return err
}
totalMap := tcontainer . NewMarshalMap ( )
err := json . Unmarshal ( data , & totalMap )
if err != nil {
return err
}
t := reflect . TypeOf ( * i )
for i := 0 ; i < t . NumField ( ) ; i ++ {
field := t . Field ( i )
tagDetail := field . Tag . Get ( "json" )
if tagDetail == "" {
// ignore if there are no tags
continue
}
options := strings . Split ( tagDetail , "," )
if len ( options ) == 0 {
return fmt . Errorf ( "No tags options found for %s" , field . Name )
}
// the first one is the json tag
key := options [ 0 ]
if _ , okay := totalMap . Value ( key ) ; okay {
delete ( totalMap , key )
}
}
i = ( * IssueFields ) ( aux . Alias )
// all the tags found in the struct were removed. Whatever is left are unknowns to struct
i . Unknowns = totalMap
return nil
2015-09-03 12:25:21 +02:00
}
// IssueType represents a type of a JIRA issue.
// Typical types are "Request", "Bug", "Story", ...
type IssueType struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self,omitempty" structs:"self,omitempty" `
2016-10-07 10:52:40 +02:00
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Description string ` json:"description,omitempty" structs:"description,omitempty" `
IconURL string ` json:"iconUrl,omitempty" structs:"iconUrl,omitempty" `
Name string ` json:"name,omitempty" structs:"name,omitempty" `
Subtask bool ` json:"subtask,omitempty" structs:"subtask,omitempty" `
AvatarID int ` json:"avatarId,omitempty" structs:"avatarId,omitempty" `
2015-09-03 12:25:21 +02:00
}
// Resolution represents a resolution of a JIRA issue.
// Typical types are "Fixed", "Suspended", "Won't Fix", ...
type Resolution struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self" structs:"self" `
ID string ` json:"id" structs:"id" `
Description string ` json:"description" structs:"description" `
Name string ` json:"name" structs:"name" `
2015-09-03 12:25:21 +02:00
}
// Priority represents a priority of a JIRA issue.
// Typical types are "Normal", "Moderate", "Urgent", ...
type Priority struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self,omitempty" structs:"self,omitempty" `
IconURL string ` json:"iconUrl,omitempty" structs:"iconUrl,omitempty" `
Name string ` json:"name,omitempty" structs:"name,omitempty" `
ID string ` json:"id,omitempty" structs:"id,omitempty" `
2015-09-03 12:25:21 +02:00
}
// Watches represents a type of how many user are "observing" a JIRA issue to track the status / updates.
type Watches struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self,omitempty" structs:"self,omitempty" `
WatchCount int ` json:"watchCount,omitempty" structs:"watchCount,omitempty" `
IsWatching bool ` json:"isWatching,omitempty" structs:"isWatching,omitempty" `
2015-09-03 12:25:21 +02:00
}
2016-06-03 23:25:18 +02:00
// AvatarUrls represents different dimensions of avatars / images
2016-06-03 23:14:27 +02:00
type AvatarUrls struct {
2016-09-23 16:19:07 +02:00
Four8X48 string ` json:"48x48,omitempty" structs:"48x48,omitempty" `
Two4X24 string ` json:"24x24,omitempty" structs:"24x24,omitempty" `
One6X16 string ` json:"16x16,omitempty" structs:"16x16,omitempty" `
Three2X32 string ` json:"32x32,omitempty" structs:"32x32,omitempty" `
2015-09-03 12:25:21 +02:00
}
// Component represents a "component" of a JIRA issue.
// Components can be user defined in every JIRA instance.
type Component struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self,omitempty" structs:"self,omitempty" `
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Name string ` json:"name,omitempty" structs:"name,omitempty" `
2015-09-03 12:25:21 +02:00
}
// Status represents the current status of a JIRA issue.
// Typical status are "Open", "In Progress", "Closed", ...
// Status can be user defined in every JIRA instance.
type Status struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self" structs:"self" `
Description string ` json:"description" structs:"description" `
IconURL string ` json:"iconUrl" structs:"iconUrl" `
Name string ` json:"name" structs:"name" `
ID string ` json:"id" structs:"id" `
StatusCategory StatusCategory ` json:"statusCategory" structs:"statusCategory" `
2015-09-03 12:25:21 +02:00
}
// StatusCategory represents the category a status belongs to.
// Those categories can be user defined in every JIRA instance.
type StatusCategory struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self" structs:"self" `
ID int ` json:"id" structs:"id" `
Name string ` json:"name" structs:"name" `
Key string ` json:"key" structs:"key" `
ColorName string ` json:"colorName" structs:"colorName" `
2015-09-03 12:25:21 +02:00
}
// Progress represents the progress of a JIRA issue.
type Progress struct {
2016-09-23 16:19:07 +02:00
Progress int ` json:"progress" structs:"progress" `
Total int ` json:"total" structs:"total" `
2015-09-03 12:25:21 +02:00
}
2016-11-01 02:31:51 +02:00
// Parent represents the parent of a JIRA issue, to be used with subtask issue types.
type Parent struct {
2016-11-04 21:44:23 +02:00
ID string ` json:"id,omitempty" structs:"id" `
Key string ` json:"key,omitempty" structs:"key" `
2016-11-01 02:31:51 +02:00
}
2016-06-04 10:41:34 +02:00
// Time represents the Time definition of JIRA as a time.Time of go
type Time time . Time
2016-05-29 21:10:19 +02:00
2016-07-17 11:23:49 +02:00
// Wrapper struct for search result
type transitionResult struct {
2016-09-23 16:19:07 +02:00
Transitions [ ] Transition ` json:"transitions" structs:"transitions" `
2016-07-17 11:23:49 +02:00
}
// Transition represents an issue transition in JIRA
type Transition struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id" structs:"id" `
Name string ` json:"name" structs:"name" `
Fields map [ string ] TransitionField ` json:"fields" structs:"fields" `
2016-07-17 11:23:49 +02:00
}
2016-07-17 11:41:50 +02:00
// TransitionField represents the value of one Transistion
2016-07-17 11:23:49 +02:00
type TransitionField struct {
2016-09-23 16:19:07 +02:00
Required bool ` json:"required" structs:"required" `
2016-07-17 11:23:49 +02:00
}
2016-07-17 11:41:50 +02:00
// CreateTransitionPayload is used for creating new issue transitions
2016-07-17 11:23:49 +02:00
type CreateTransitionPayload struct {
2016-09-23 16:19:07 +02:00
Transition TransitionPayload ` json:"transition" structs:"transition" `
2016-07-17 11:23:49 +02:00
}
2016-07-17 11:41:50 +02:00
// TransitionPayload represents the request payload of Transistion calls like DoTransition
2016-07-17 11:23:49 +02:00
type TransitionPayload struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id" structs:"id" `
2016-07-17 11:23:49 +02:00
}
2016-06-04 10:41:34 +02:00
// UnmarshalJSON will transform the JIRA time into a time.Time
// during the transformation of the JIRA JSON response
func ( t * Time ) UnmarshalJSON ( b [ ] byte ) error {
2016-05-29 17:30:45 +02:00
ti , err := time . Parse ( "\"2006-01-02T15:04:05.999-0700\"" , string ( b ) )
if err != nil {
return err
}
2016-06-04 10:41:34 +02:00
* t = Time ( ti )
2016-05-29 17:30:45 +02:00
return nil
2015-09-03 12:25:21 +02:00
}
// Worklog represents the work log of a JIRA issue.
2016-06-03 23:25:18 +02:00
// One Worklog contains zero or n WorklogRecords
2015-09-03 12:25:21 +02:00
// JIRA Wiki: https://confluence.atlassian.com/jira/logging-work-on-an-issue-185729605.html
type Worklog struct {
2016-09-23 16:19:07 +02:00
StartAt int ` json:"startAt" structs:"startAt" `
MaxResults int ` json:"maxResults" structs:"maxResults" `
Total int ` json:"total" structs:"total" `
Worklogs [ ] WorklogRecord ` json:"worklogs" structs:"worklogs" `
2016-06-03 23:14:27 +02:00
}
2016-06-03 23:25:18 +02:00
// WorklogRecord represents one entry of a Worklog
2016-06-03 23:14:27 +02:00
type WorklogRecord struct {
2016-09-23 16:19:07 +02:00
Self string ` json:"self" structs:"self" `
Author User ` json:"author" structs:"author" `
UpdateAuthor User ` json:"updateAuthor" structs:"updateAuthor" `
Comment string ` json:"comment" structs:"comment" `
Created Time ` json:"created" structs:"created" `
Updated Time ` json:"updated" structs:"updated" `
Started Time ` json:"started" structs:"started" `
TimeSpent string ` json:"timeSpent" structs:"timeSpent" `
TimeSpentSeconds int ` json:"timeSpentSeconds" structs:"timeSpentSeconds" `
ID string ` json:"id" structs:"id" `
IssueID string ` json:"issueId" structs:"issueId" `
2016-05-29 18:42:38 +02:00
}
2016-10-24 11:06:37 +02:00
// TimeTracking represents the timetracking fields of a JIRA issue.
type TimeTracking struct {
2016-10-23 14:50:36 +02:00
OriginalEstimate string ` json:"originalEstimate,omitempty" structs:"originalEstimate,omitempty" `
RemainingEstimate string ` json:"remainingEstimate,omitempty" structs:"remainingEstimate,omitempty" `
TimeSpent string ` json:"timeSpent,omitempty" structs:"timeSpent,omitempty" `
OriginalEstimateSeconds int ` json:"originalEstimateSeconds,omitempty" structs:"originalEstimateSeconds,omitempty" `
RemainingEstimateSeconds int ` json:"remainingEstimateSeconds,omitempty" structs:"remainingEstimateSeconds,omitempty" `
TimeSpentSeconds int ` json:"timeSpentSeconds,omitempty" structs:"timeSpentSeconds,omitempty" `
}
2016-06-03 23:25:18 +02:00
// Subtasks represents all issues of a parent issue.
2016-05-29 18:42:38 +02:00
type Subtasks struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id" structs:"id" `
Key string ` json:"key" structs:"key" `
Self string ` json:"self" structs:"self" `
Fields IssueFields ` json:"fields" structs:"fields" `
2015-09-03 12:25:21 +02:00
}
// IssueLink represents a link between two issues in JIRA.
type IssueLink struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Self string ` json:"self,omitempty" structs:"self,omitempty" `
Type IssueLinkType ` json:"type" structs:"type" `
OutwardIssue * Issue ` json:"outwardIssue" structs:"outwardIssue" `
InwardIssue * Issue ` json:"inwardIssue" structs:"inwardIssue" `
Comment * Comment ` json:"comment,omitempty" structs:"comment,omitempty" `
2015-09-03 12:25:21 +02:00
}
// IssueLinkType represents a type of a link between to issues in JIRA.
// Typical issue link types are "Related to", "Duplicate", "Is blocked by", etc.
type IssueLinkType struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Self string ` json:"self,omitempty" structs:"self,omitempty" `
Name string ` json:"name" structs:"name" `
Inward string ` json:"inward" structs:"inward" `
Outward string ` json:"outward" structs:"outward" `
2015-09-03 12:25:21 +02:00
}
2016-07-08 01:21:14 +02:00
// Comments represents a list of Comment.
type Comments struct {
2016-09-23 16:19:07 +02:00
Comments [ ] * Comment ` json:"comments,omitempty" structs:"comments,omitempty" `
2016-07-08 01:21:14 +02:00
}
2015-09-03 12:25:21 +02:00
// Comment represents a comment by a person to an issue in JIRA.
type Comment struct {
2016-09-23 16:19:07 +02:00
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Self string ` json:"self,omitempty" structs:"self,omitempty" `
2016-10-23 14:50:36 +02:00
Name string ` json:"name,omitempty" structs:"name,omitempty" `
2016-09-23 16:19:07 +02:00
Author User ` json:"author,omitempty" structs:"author,omitempty" `
Body string ` json:"body,omitempty" structs:"body,omitempty" `
UpdateAuthor User ` json:"updateAuthor,omitempty" structs:"updateAuthor,omitempty" `
Updated string ` json:"updated,omitempty" structs:"updated,omitempty" `
Created string ` json:"created,omitempty" structs:"created,omitempty" `
Visibility CommentVisibility ` json:"visibility,omitempty" structs:"visibility,omitempty" `
2015-09-03 12:25:21 +02:00
}
2016-02-14 05:15:32 +02:00
// FixVersion represents a software release in which an issue is fixed.
type FixVersion struct {
2016-09-23 16:19:07 +02:00
Archived * bool ` json:"archived,omitempty" structs:"archived,omitempty" `
ID string ` json:"id,omitempty" structs:"id,omitempty" `
Name string ` json:"name,omitempty" structs:"name,omitempty" `
ProjectID int ` json:"projectId,omitempty" structs:"projectId,omitempty" `
ReleaseDate string ` json:"releaseDate,omitempty" structs:"releaseDate,omitempty" `
Released * bool ` json:"released,omitempty" structs:"released,omitempty" `
Self string ` json:"self,omitempty" structs:"self,omitempty" `
UserReleaseDate string ` json:"userReleaseDate,omitempty" structs:"userReleaseDate,omitempty" `
2016-02-14 05:15:32 +02:00
}
2016-03-27 14:03:40 +02:00
// CommentVisibility represents he visibility of a comment.
// E.g. Type could be "role" and Value "Administrators"
type CommentVisibility struct {
2016-09-23 16:19:07 +02:00
Type string ` json:"type,omitempty" structs:"type,omitempty" `
Value string ` json:"value,omitempty" structs:"value,omitempty" `
2016-05-29 17:30:45 +02:00
}
2016-06-19 15:08:53 +02:00
// SearchOptions specifies the optional parameters to various List methods that
// support pagination.
// Pagination is used for the JIRA REST APIs to conserve server resources and limit
// response size for resources that return potentially large collection of items.
// A request to a pages API will result in a values array wrapped in a JSON object with some paging metadata
// Default Pagination options
2016-06-16 11:10:30 +02:00
type SearchOptions struct {
2016-06-19 15:08:53 +02:00
// StartAt: The starting index of the returned projects. Base index: 0.
StartAt int ` url:"startAt,omitempty" `
// MaxResults: The maximum number of projects to return per page. Default: 50.
MaxResults int ` url:"maxResults,omitempty" `
2017-01-28 00:04:08 +02:00
// Expand: Expand specific sections in the returned issues
2017-02-24 02:48:06 +02:00
Expand string ` url:"expand,omitempty" `
2016-06-16 11:10:30 +02:00
}
2017-01-29 18:28:04 +02:00
// searchResult is only a small wrapper around the Search (with JQL) method
2016-06-04 10:41:34 +02:00
// to be able to parse the results
type searchResult struct {
2016-09-23 16:19:07 +02:00
Issues [ ] Issue ` json:"issues" structs:"issues" `
StartAt int ` json:"startAt" structs:"startAt" `
MaxResults int ` json:"maxResults" structs:"maxResults" `
Total int ` json:"total" structs:"total" `
2016-06-04 10:41:34 +02:00
}
2017-01-28 00:04:08 +02:00
// GetQueryOptions specifies the optional parameters for the Get Issue methods
type GetQueryOptions struct {
// Fields is the list of fields to return for the issue. By default, all fields are returned.
Fields string ` url:"fields,omitempty" `
Expand string ` url:"expand,omitempty" `
// Properties is the list of properties to return for the issue. By default no properties are returned.
Properties string ` url:"properties,omitempty" `
// FieldsByKeys if true then fields in issues will be referenced by keys instead of ids
FieldsByKeys bool ` url:"fieldsByKeys,omitempty" `
UpdateHistory bool ` url:"updateHistory,omitempty" `
}
2016-06-04 10:41:34 +02:00
// CustomFields represents custom fields of JIRA
// This can heavily differ between JIRA instances
type CustomFields map [ string ] string
2015-09-03 12:25:21 +02:00
// 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.
// If the issue cannot be found via an exact match, JIRA will also look for the issue in a case-insensitive way, or by looking to see if the issue was moved.
//
2017-01-28 00:04:08 +02:00
// The given options will be appended to the query string
//
2016-03-27 14:24:48 +02:00
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-getIssue
2017-01-28 00:04:08 +02:00
func ( s * IssueService ) Get ( issueID string , options * GetQueryOptions ) ( * Issue , * Response , error ) {
2015-09-03 12:25:21 +02:00
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s" , issueID )
req , err := s . client . NewRequest ( "GET" , apiEndpoint , nil )
if err != nil {
return nil , nil , err
}
2017-01-28 00:04:08 +02:00
if options != nil {
q , err := query . Values ( options )
if err != nil {
return nil , nil , err
}
req . URL . RawQuery = q . Encode ( )
}
2015-09-03 12:25:21 +02:00
issue := new ( Issue )
resp , err := s . client . Do ( req , issue )
if err != nil {
return nil , resp , err
}
return issue , resp , nil
}
2016-06-15 17:09:13 +02:00
// DownloadAttachment returns a Response of an attachment for a given attachmentID.
// The attachment is in the Response.Body of the response.
2016-05-27 14:14:09 +02:00
// This is an io.ReadCloser.
// The caller should close the resp.Body.
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) DownloadAttachment ( attachmentID string ) ( * Response , error ) {
2016-05-19 23:11:21 +02:00
apiEndpoint := fmt . Sprintf ( "secure/attachment/%s/" , attachmentID )
2016-05-29 17:30:45 +02:00
req , err := s . client . NewRequest ( "GET" , apiEndpoint , nil )
if err != nil {
2016-05-19 23:11:21 +02:00
return nil , err
2016-05-29 17:30:45 +02:00
}
2016-05-27 14:14:09 +02:00
resp , err := s . client . Do ( req , nil )
2016-05-29 17:30:45 +02:00
if err != nil {
2016-05-19 23:11:21 +02:00
return resp , err
2016-05-29 17:30:45 +02:00
}
2016-05-19 23:11:21 +02:00
return resp , nil
}
2016-05-27 14:14:09 +02:00
// PostAttachment uploads r (io.Reader) as an attachment to a given attachmentID
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) PostAttachment ( attachmentID string , r io . Reader , attachmentName string ) ( * [ ] Attachment , * Response , error ) {
2016-05-19 23:11:21 +02:00
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s/attachments" , attachmentID )
b := new ( bytes . Buffer )
writer := multipart . NewWriter ( b )
fw , err := writer . CreateFormFile ( "file" , attachmentName )
if err != nil {
return nil , nil , err
2016-05-29 17:30:45 +02:00
}
2016-05-25 09:47:10 +02:00
if r != nil {
// Copy the file
if _ , err = io . Copy ( fw , r ) ; err != nil {
return nil , nil , err
2016-05-29 17:30:45 +02:00
}
}
2016-05-19 23:11:21 +02:00
writer . Close ( )
req , err := s . client . NewMultiPartRequest ( "POST" , apiEndpoint , b )
if err != nil {
return nil , nil , err
}
req . Header . Set ( "Content-Type" , writer . FormDataContentType ( ) )
2016-05-25 09:04:23 +02:00
// PostAttachment response returns a JSON array (as multiple attachments can be posted)
attachment := new ( [ ] Attachment )
resp , err := s . client . Do ( req , attachment )
2016-05-19 23:11:21 +02:00
if err != nil {
return nil , resp , err
}
2016-05-25 09:04:23 +02:00
return attachment , resp , nil
2016-05-29 17:30:45 +02:00
}
2015-09-03 12:25:21 +02:00
// Create creates an issue or a sub-task from a JSON representation.
// Creating a sub-task is similar to creating a regular issue, with two important differences:
// The issueType field must correspond to a sub-task issue type and you must provide a parent field in the issue create request containing the id or key of the parent issue.
//
2016-03-27 14:24:48 +02:00
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-createIssues
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) Create ( issue * Issue ) ( * Issue , * Response , error ) {
2015-09-03 12:25:21 +02:00
apiEndpoint := "rest/api/2/issue/"
req , err := s . client . NewRequest ( "POST" , apiEndpoint , issue )
if err != nil {
return nil , nil , err
}
2016-10-05 14:53:30 +02:00
resp , err := s . client . Do ( req , nil )
2015-09-03 12:25:21 +02:00
if err != nil {
2016-10-05 14:53:30 +02:00
// incase of error return the resp for further inspection
2015-09-03 12:25:21 +02:00
return nil , resp , err
}
2016-10-05 14:53:30 +02:00
responseIssue := new ( Issue )
defer resp . Body . Close ( )
data , err := ioutil . ReadAll ( resp . Body )
if err != nil {
return nil , resp , fmt . Errorf ( "Could not read the returned data" )
}
err = json . Unmarshal ( data , responseIssue )
if err != nil {
return nil , resp , fmt . Errorf ( "Could not unmarshall the data into struct" )
}
2015-09-03 12:25:21 +02:00
return responseIssue , resp , nil
}
2016-01-05 16:22:21 +02:00
2016-03-27 14:03:40 +02:00
// AddComment adds a new comment to issueID.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-addComment
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) AddComment ( issueID string , comment * Comment ) ( * Comment , * Response , error ) {
2016-01-05 16:22:21 +02:00
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s/comment" , issueID )
2016-03-27 14:03:40 +02:00
req , err := s . client . NewRequest ( "POST" , apiEndpoint , comment )
2016-01-05 16:22:21 +02:00
if err != nil {
return nil , nil , err
}
2016-03-27 14:03:40 +02:00
responseComment := new ( Comment )
2016-02-29 09:29:44 +02:00
resp , err := s . client . Do ( req , responseComment )
if err != nil {
return nil , resp , err
}
2016-01-05 16:22:21 +02:00
2016-03-27 14:03:40 +02:00
return responseComment , resp , nil
}
2016-02-29 09:29:44 +02:00
// AddLink adds a link between two issues.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issueLink
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) AddLink ( issueLink * IssueLink ) ( * Response , error ) {
2016-02-29 09:29:44 +02:00
apiEndpoint := fmt . Sprintf ( "rest/api/2/issueLink" )
req , err := s . client . NewRequest ( "POST" , apiEndpoint , issueLink )
if err != nil {
return nil , err
}
resp , err := s . client . Do ( req , nil )
return resp , err
}
2016-05-29 17:30:45 +02:00
2016-06-04 10:41:34 +02:00
// Search will search for tickets according to the jql
//
2016-05-29 17:30:45 +02:00
// JIRA API docs: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues
2016-06-16 11:10:30 +02:00
func ( s * IssueService ) Search ( jql string , options * SearchOptions ) ( [ ] Issue , * Response , error ) {
var u string
if options == nil {
u = fmt . Sprintf ( "rest/api/2/search?jql=%s" , url . QueryEscape ( jql ) )
} else {
u = fmt . Sprintf ( "rest/api/2/search?jql=%s&startAt=%d&maxResults=%d" , url . QueryEscape ( jql ) ,
options . StartAt , options . MaxResults )
}
2016-06-04 10:41:34 +02:00
req , err := s . client . NewRequest ( "GET" , u , nil )
2016-05-29 17:30:45 +02:00
if err != nil {
2016-06-04 10:41:34 +02:00
return [ ] Issue { } , nil , err
2016-05-29 17:30:45 +02:00
}
2016-06-04 02:51:44 +02:00
2016-06-04 10:41:34 +02:00
v := new ( searchResult )
resp , err := s . client . Do ( req , v )
return v . Issues , resp , err
}
2016-06-04 02:51:44 +02:00
2016-06-04 10:41:34 +02:00
// GetCustomFields returns a map of customfield_* keys with string values
2016-06-15 17:09:13 +02:00
func ( s * IssueService ) GetCustomFields ( issueID string ) ( CustomFields , * Response , error ) {
2016-06-04 02:51:44 +02:00
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s" , issueID )
req , err := s . client . NewRequest ( "GET" , apiEndpoint , nil )
if err != nil {
return nil , nil , err
}
issue := new ( map [ string ] interface { } )
resp , err := s . client . Do ( req , issue )
if err != nil {
return nil , resp , err
}
2016-06-04 10:41:34 +02:00
2016-06-04 02:51:44 +02:00
m := * issue
f := m [ "fields" ]
cf := make ( CustomFields )
if f == nil {
return cf , resp , nil
}
if rec , ok := f . ( map [ string ] interface { } ) ; ok {
for key , val := range rec {
if strings . Contains ( key , "customfield" ) {
2016-09-07 01:14:15 +02:00
if valMap , ok := val . ( map [ string ] interface { } ) ; ok {
if v , ok := valMap [ "value" ] ; ok {
val = v
}
}
2016-06-04 02:51:44 +02:00
cf [ key ] = fmt . Sprint ( val )
}
}
}
return cf , resp , nil
2016-05-29 17:30:45 +02:00
}
2016-07-17 11:23:49 +02:00
// GetTransitions gets a list of the transitions possible for this issue by the current user,
// along with fields that are required and their types.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-getTransitions
func ( s * IssueService ) GetTransitions ( id string ) ( [ ] Transition , * Response , error ) {
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s/transitions?expand=transitions.fields" , id )
req , err := s . client . NewRequest ( "GET" , apiEndpoint , nil )
if err != nil {
return nil , nil , err
}
result := new ( transitionResult )
resp , err := s . client . Do ( req , result )
return result . Transitions , resp , err
}
// DoTransition performs a transition on an issue.
// When performing the transition you can update or set other issue fields.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-doTransition
func ( s * IssueService ) DoTransition ( ticketID , transitionID string ) ( * Response , error ) {
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s/transitions" , ticketID )
payload := CreateTransitionPayload {
Transition : TransitionPayload {
ID : transitionID ,
} ,
}
req , err := s . client . NewRequest ( "POST" , apiEndpoint , payload )
if err != nil {
return nil , err
}
resp , err := s . client . Do ( req , nil )
if err != nil {
return nil , err
}
return resp , nil
2016-07-17 11:24:23 +02:00
}
2016-10-05 18:04:48 +02:00
// InitIssueWithMetaAndFields returns Issue with with values from fieldsConfig properly set.
// * metaProject should contain metaInformation about the project where the issue should be created.
// * metaIssuetype is the MetaInformation about the Issuetype that needs to be created.
// * fieldsConfig is a key->value pair where key represents the name of the field as seen in the UI
// And value is the string value for that particular key.
// Note: This method doesn't verify that the fieldsConfig is complete with mandatory fields. The fieldsConfig is
// supposed to be already verified with MetaIssueType.CheckCompleteAndAvailable. It will however return
// error if the key is not found.
// All values will be packed into Unknowns. This is much convenient. If the struct fields needs to be
// configured as well, marshalling and unmarshalling will set the proper fields.
func InitIssueWithMetaAndFields ( metaProject * MetaProject , metaIssuetype * MetaIssueType , fieldsConfig map [ string ] string ) ( * Issue , error ) {
issue := new ( Issue )
issueFields := new ( IssueFields )
issueFields . Unknowns = tcontainer . NewMarshalMap ( )
// map the field names the User presented to jira's internal key
allFields , _ := metaIssuetype . GetAllFields ( )
for key , value := range fieldsConfig {
jiraKey , found := allFields [ key ]
if ! found {
return nil , fmt . Errorf ( "Key %s is not found in the list of fields." , key )
}
valueType , err := metaIssuetype . Fields . String ( jiraKey + "/schema/type" )
if err != nil {
return nil , err
}
switch valueType {
case "array" :
elemType , err := metaIssuetype . Fields . String ( jiraKey + "/schema/items" )
if err != nil {
return nil , err
}
switch elemType {
case "component" :
issueFields . Unknowns [ jiraKey ] = [ ] Component { Component { Name : value } }
default :
issueFields . Unknowns [ jiraKey ] = [ ] string { value }
}
case "string" :
issueFields . Unknowns [ jiraKey ] = value
case "date" :
issueFields . Unknowns [ jiraKey ] = value
case "any" :
// Treat any as string
issueFields . Unknowns [ jiraKey ] = value
case "project" :
issueFields . Unknowns [ jiraKey ] = Project {
Name : metaProject . Name ,
ID : metaProject . Id ,
}
case "priority" :
issueFields . Unknowns [ jiraKey ] = Priority { Name : value }
case "user" :
issueFields . Unknowns [ jiraKey ] = User {
Name : value ,
}
case "issuetype" :
issueFields . Unknowns [ jiraKey ] = IssueType {
Name : value ,
}
default :
return nil , fmt . Errorf ( "Unknown issue type encountered: %s for %s" , valueType , key )
}
}
issue . Fields = issueFields
return issue , nil
}
2017-02-24 03:41:52 +02:00
// Delete will delete a specified issue.
func ( s * IssueService ) Delete ( issueID string ) ( * Response , error ) {
apiEndpoint := fmt . Sprintf ( "rest/api/2/issue/%s" , issueID )
// to enable deletion of subtasks; without this, the request will fail if the issue has subtasks
deletePayload := make ( map [ string ] interface { } )
deletePayload [ "deleteSubtasks" ] = "true"
content , _ := json . Marshal ( deletePayload )
req , err := s . client . NewRequest ( "DELETE" , apiEndpoint , content )
if err != nil {
return nil , err
}
resp , err := s . client . Do ( req , nil )
return resp , err
}