1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-03-21 21:07:03 +02:00

Adjusted a few things to be in line with other methods

This commit is contained in:
Andy Grunwald 2016-06-04 10:41:34 +02:00
parent d77b32f3dd
commit 1628d1b1d3
2 changed files with 43 additions and 29 deletions

View File

@ -179,14 +179,17 @@ type Progress struct {
Total int `json:"total"` Total int `json:"total"`
} }
type JiraTime time.Time // Time represents the Time definition of JIRA as a time.Time of go
type Time time.Time
func (t *JiraTime) UnmarshalJSON(b []byte) error { // 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 {
ti, err := time.Parse("\"2006-01-02T15:04:05.999-0700\"", string(b)) ti, err := time.Parse("\"2006-01-02T15:04:05.999-0700\"", string(b))
if err != nil { if err != nil {
return err return err
} }
*t = JiraTime(ti) *t = Time(ti)
return nil return nil
} }
@ -202,17 +205,17 @@ type Worklog struct {
// WorklogRecord represents one entry of a Worklog // WorklogRecord represents one entry of a Worklog
type WorklogRecord struct { type WorklogRecord struct {
Self string `json:"self"` Self string `json:"self"`
Author User `json:"author"` Author User `json:"author"`
UpdateAuthor User `json:"updateAuthor"` UpdateAuthor User `json:"updateAuthor"`
Comment string `json:"comment"` Comment string `json:"comment"`
Created JiraTime `json:"created"` Created Time `json:"created"`
Updated JiraTime `json:"updated"` Updated Time `json:"updated"`
Started string `json:"started"` Started string `json:"started"`
TimeSpent string `json:"timeSpent"` TimeSpent string `json:"timeSpent"`
TimeSpentSeconds int `json:"timeSpentSeconds"` TimeSpentSeconds int `json:"timeSpentSeconds"`
ID string `json:"id"` ID string `json:"id"`
IssueID string `json:"issueId"` IssueID string `json:"issueId"`
} }
// Subtasks represents all issues of a parent issue. // Subtasks represents all issues of a parent issue.
@ -274,6 +277,16 @@ type CommentVisibility struct {
Value string `json:"value,omitempty"` Value string `json:"value,omitempty"`
} }
// searchResult is only a small wrapper arround the Search (with JQL) method
// to be able to parse the results
type searchResult struct {
Issues []Issue `json:"issues"`
}
// CustomFields represents custom fields of JIRA
// This can heavily differ between JIRA instances
type CustomFields map[string]string
// Get returns a full representation of the issue for the given issue key. // 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. // JIRA will attempt to identify the issue by the issueIdOrKey path parameter.
// This can be an issue id, or an issue key. // This can be an issue id, or an issue key.
@ -406,25 +419,22 @@ func (s *IssueService) AddLink(issueLink *IssueLink) (*http.Response, error) {
return resp, err return resp, err
} }
type searchResult struct { // Search will search for tickets according to the jql
Issues []Issue `json:"issues"` //
}
// Search for tickets
// JIRA API docs: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues // JIRA API docs: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-query-issues
func (s *IssueService) Search(jql string) ([]Issue, error) { func (s *IssueService) Search(jql string) ([]Issue, *http.Response, error) {
req, err := s.client.NewRequest("GET", "rest/api/2/search?jql="+url.QueryEscape(jql), nil) u := fmt.Sprintf("rest/api/2/search?jql=%s", url.QueryEscape(jql))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil { if err != nil {
panic(err) return []Issue{}, nil, err
} }
resp := new(searchResult)
_, err = s.client.Do(req, resp) v := new(searchResult)
return resp.Issues, err resp, err := s.client.Do(req, v)
return v.Issues, resp, err
} }
type CustomFields map[string]string // GetCustomFields returns a map of customfield_* keys with string values
// Returns a map of customfield_* keys with string values
func (s *IssueService) GetCustomFields(issueID string) (CustomFields, *http.Response, error) { func (s *IssueService) GetCustomFields(issueID string) (CustomFields, *http.Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s", issueID) apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s", issueID)
req, err := s.client.NewRequest("GET", apiEndpoint, nil) req, err := s.client.NewRequest("GET", apiEndpoint, nil)
@ -437,6 +447,7 @@ func (s *IssueService) GetCustomFields(issueID string) (CustomFields, *http.Resp
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
} }
m := *issue m := *issue
f := m["fields"] f := m["fields"]
cf := make(CustomFields) cf := make(CustomFields)

View File

@ -320,8 +320,11 @@ func TestIssue_Search(t *testing.T) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{"expand": "schema,names","startAt": 0,"maxResults": 50,"total": 6,"issues": [{"expand": "html","id": "10230","self": "http://kelpie9:8081/rest/api/2/issue/BULK-62","key": "BULK-62","fields": {"summary": "testing","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/5","id": "5","description": "The sub-task of the issue","iconUrl": "http://kelpie9:8081/images/icons/issue_subtask.gif","name": "Sub-task","subtask": true},"customfield_10071": null}},{"expand": "html","id": "10004","self": "http://kelpie9:8081/rest/api/2/issue/BULK-47","key": "BULK-47","fields": {"summary": "Cheese v1 2.0 issue","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/3","id": "3","description": "A task that needs to be done.","iconUrl": "http://kelpie9:8081/images/icons/task.gif","name": "Task","subtask": false}}}]}`) fmt.Fprint(w, `{"expand": "schema,names","startAt": 0,"maxResults": 50,"total": 6,"issues": [{"expand": "html","id": "10230","self": "http://kelpie9:8081/rest/api/2/issue/BULK-62","key": "BULK-62","fields": {"summary": "testing","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/5","id": "5","description": "The sub-task of the issue","iconUrl": "http://kelpie9:8081/images/icons/issue_subtask.gif","name": "Sub-task","subtask": true},"customfield_10071": null}},{"expand": "html","id": "10004","self": "http://kelpie9:8081/rest/api/2/issue/BULK-47","key": "BULK-47","fields": {"summary": "Cheese v1 2.0 issue","timetracking": null,"issuetype": {"self": "http://kelpie9:8081/rest/api/2/issuetype/3","id": "3","description": "A task that needs to be done.","iconUrl": "http://kelpie9:8081/images/icons/task.gif","name": "Task","subtask": false}}}]}`)
}) })
_, err := testClient.Issue.Search("something") _, resp, err := testClient.Issue.Search("something")
if resp == nil {
t.Errorf("Response given: %+v", resp)
}
if err != nil { if err != nil {
t.Errorf("Error given: %s", err) t.Errorf("Error given: %s", err)
} }