2016-06-24 10:43:32 +02:00
package jira
import (
2020-05-03 15:38:32 +02:00
"context"
2016-06-24 10:43:32 +02:00
"fmt"
2018-06-08 18:00:22 +02:00
2018-04-03 07:29:29 +02:00
"github.com/google/go-querystring/query"
2016-06-24 10:43:32 +02:00
)
2020-05-14 17:18:31 +02:00
// SprintService handles sprints in Jira Agile API.
2016-07-17 11:17:29 +02:00
// See https://docs.atlassian.com/jira-software/REST/cloud/
2016-06-24 10:43:32 +02:00
type SprintService struct {
client * Client
}
2016-07-17 11:17:29 +02:00
// IssuesWrapper represents a wrapper struct for moving issues to sprint
2016-06-24 10:43:32 +02:00
type IssuesWrapper struct {
Issues [ ] string ` json:"issues" `
}
2016-07-29 19:43:34 +02:00
// IssuesInSprintResult represents a wrapper struct for search result
2016-07-26 22:18:58 +02:00
type IssuesInSprintResult struct {
Issues [ ] Issue ` json:"issues" `
}
2020-05-03 15:38:32 +02:00
// MoveIssuesToSprintWithContext moves issues to a sprint, for a given sprint Id.
2016-07-17 11:17:29 +02:00
// Issues can only be moved to open or active sprints.
// The maximum number of issues that can be moved in one operation is 50.
2016-06-24 10:43:32 +02:00
//
2020-05-14 17:18:31 +02:00
// Jira API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/sprint-moveIssuesToSprint
2020-05-03 15:38:32 +02:00
func ( s * SprintService ) MoveIssuesToSprintWithContext ( ctx context . Context , sprintID int , issueIDs [ ] string ) ( * Response , error ) {
2016-06-24 10:43:32 +02:00
apiEndpoint := fmt . Sprintf ( "rest/agile/1.0/sprint/%d/issue" , sprintID )
payload := IssuesWrapper { Issues : issueIDs }
2020-05-03 15:38:32 +02:00
req , err := s . client . NewRequestWithContext ( ctx , "POST" , apiEndpoint , payload )
2016-06-24 10:43:32 +02:00
if err != nil {
return nil , err
}
resp , err := s . client . Do ( req , nil )
2017-11-04 00:22:32 +02:00
if err != nil {
err = NewJiraError ( resp , err )
}
2016-06-24 10:43:32 +02:00
return resp , err
}
2016-07-26 22:18:58 +02:00
2020-05-03 15:38:32 +02:00
// MoveIssuesToSprint wraps MoveIssuesToSprintWithContext using the background context.
func ( s * SprintService ) MoveIssuesToSprint ( sprintID int , issueIDs [ ] string ) ( * Response , error ) {
return s . MoveIssuesToSprintWithContext ( context . Background ( ) , sprintID , issueIDs )
}
// GetIssuesForSprintWithContext returns all issues in a sprint, for a given sprint Id.
2016-07-29 19:43:34 +02:00
// This only includes issues that the user has permission to view.
// By default, the returned issues are ordered by rank.
2016-07-26 22:18:58 +02:00
//
2020-05-14 17:18:31 +02:00
// Jira API Docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/sprint-getIssuesForSprint
2020-05-03 15:38:32 +02:00
func ( s * SprintService ) GetIssuesForSprintWithContext ( ctx context . Context , sprintID int ) ( [ ] Issue , * Response , error ) {
2016-07-26 22:18:58 +02:00
apiEndpoint := fmt . Sprintf ( "rest/agile/1.0/sprint/%d/issue" , sprintID )
2020-05-03 15:38:32 +02:00
req , err := s . client . NewRequestWithContext ( ctx , "GET" , apiEndpoint , nil )
2016-07-26 22:18:58 +02:00
if err != nil {
return nil , nil , err
}
result := new ( IssuesInSprintResult )
resp , err := s . client . Do ( req , result )
2017-11-04 00:22:32 +02:00
if err != nil {
err = NewJiraError ( resp , err )
}
2016-07-26 22:18:58 +02:00
return result . Issues , resp , err
}
2018-04-03 07:29:29 +02:00
2020-05-03 15:38:32 +02:00
// GetIssuesForSprint wraps GetIssuesForSprintWithContext using the background context.
func ( s * SprintService ) GetIssuesForSprint ( sprintID int ) ( [ ] Issue , * Response , error ) {
return s . GetIssuesForSprintWithContext ( context . Background ( ) , sprintID )
}
// GetIssueWithContext returns a full representation of the issue for the given issue key.
2020-05-14 17:18:31 +02:00
// Jira will attempt to identify the issue by the issueIdOrKey path parameter.
2018-04-03 07:29:29 +02:00
// This can be an issue id, or an issue key.
2020-05-14 17:18:31 +02:00
// 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.
2018-04-03 07:29:29 +02:00
//
// The given options will be appended to the query string
//
2020-05-14 17:18:31 +02:00
// Jira API docs: https://docs.atlassian.com/jira-software/REST/7.3.1/#agile/1.0/issue-getIssue
2018-04-03 07:29:29 +02:00
//
// TODO: create agile service for holding all agile apis' implementation
2020-05-03 15:38:32 +02:00
func ( s * SprintService ) GetIssueWithContext ( ctx context . Context , issueID string , options * GetQueryOptions ) ( * Issue , * Response , error ) {
2018-04-03 07:29:29 +02:00
apiEndpoint := fmt . Sprintf ( "rest/agile/1.0/issue/%s" , issueID )
2020-05-03 15:38:32 +02:00
req , err := s . client . NewRequestWithContext ( ctx , "GET" , apiEndpoint , nil )
2018-04-03 07:29:29 +02:00
if err != nil {
return nil , nil , err
}
if options != nil {
q , err := query . Values ( options )
if err != nil {
return nil , nil , err
}
req . URL . RawQuery = q . Encode ( )
}
issue := new ( Issue )
resp , err := s . client . Do ( req , issue )
if err != nil {
jerr := NewJiraError ( resp , err )
return nil , resp , jerr
}
return issue , resp , nil
}
2020-05-03 15:38:32 +02:00
// GetIssue wraps GetIssueWithContext using the background context.
func ( s * SprintService ) GetIssue ( issueID string , options * GetQueryOptions ) ( * Issue , * Response , error ) {
return s . GetIssueWithContext ( context . Background ( ) , issueID , options )
}