mirror of
				https://github.com/interviewstreet/go-jira.git
				synced 2025-10-30 23:47:46 +02:00 
			
		
		
		
	Moved Board-Functionality to BoardService and renamed Sprint function according to the API
This commit is contained in:
		
							
								
								
									
										38
									
								
								board.go
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								board.go
									
									
									
									
									
								
							| @@ -1,6 +1,9 @@ | ||||
| package jira | ||||
|  | ||||
| import "fmt" | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| // BoardService handles Agile Boards for the JIRA instance / API. | ||||
| // | ||||
| @@ -41,6 +44,23 @@ type BoardListOptions struct { | ||||
| 	SearchOptions | ||||
| } | ||||
|  | ||||
| // Wrapper struct for search result | ||||
| type sprintsResult struct { | ||||
| 	Sprints []Sprint `json:"values"` | ||||
| } | ||||
|  | ||||
| // Sprint represents a sprint on JIRA agile board | ||||
| type Sprint struct { | ||||
| 	ID            int        `json:"id"` | ||||
| 	Name          string     `json:"name"` | ||||
| 	CompleteDate  *time.Time `json:"completeDate"` | ||||
| 	EndDate       *time.Time `json:"endDate"` | ||||
| 	StartDate     *time.Time `json:"startDate"` | ||||
| 	OriginBoardID int        `json:"originBoardId"` | ||||
| 	Self          string     `json:"self"` | ||||
| 	State         string     `json:"state"` | ||||
| } | ||||
|  | ||||
| // GetAllBoards will returns all boards. This only includes boards that the user has permission to view. | ||||
| // | ||||
| // JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board-getAllBoards | ||||
| @@ -117,3 +137,19 @@ func (s *BoardService) DeleteBoard(boardID int) (*Board, *Response, error) { | ||||
| 	resp, err := s.client.Do(req, nil) | ||||
| 	return nil, resp, err | ||||
| } | ||||
|  | ||||
| // GetAllSprints will returns all sprints from a board, for a given board Id. | ||||
| // This only includes sprints that the user has permission to view. | ||||
| // | ||||
| // JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board/{boardId}/sprint | ||||
| func (s *BoardService) GetAllSprints(boardID string) ([]Sprint, *Response, error) { | ||||
| 	apiEndpoint := fmt.Sprintf("rest/agile/1.0/board/%s/sprint", boardID) | ||||
| 	req, err := s.client.NewRequest("GET", apiEndpoint, nil) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, err | ||||
| 	} | ||||
|  | ||||
| 	result := new(sprintsResult) | ||||
| 	resp, err := s.client.Do(req, result) | ||||
| 	return result.Sprints, resp, err | ||||
| } | ||||
| @@ -7,7 +7,7 @@ import ( | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestBoardsGetAll(t *testing.T) { | ||||
| func TestBoardService_GetAllBoards(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testAPIEdpoint := "/rest/agile/1.0/board" | ||||
| @@ -32,7 +32,7 @@ func TestBoardsGetAll(t *testing.T) { | ||||
| } | ||||
|  | ||||
| // Test with params | ||||
| func TestBoardsGetFiltered(t *testing.T) { | ||||
| func TestBoardService_GetAllBoards_WithFilter(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testAPIEdpoint := "/rest/agile/1.0/board" | ||||
| @@ -64,7 +64,7 @@ func TestBoardsGetFiltered(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBoardGet(t *testing.T) { | ||||
| func TestBoardService_GetBoard(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testAPIEdpoint := "/rest/agile/1.0/board/1" | ||||
| @@ -84,7 +84,7 @@ func TestBoardGet(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBoardGet_NoBoard(t *testing.T) { | ||||
| func TestBoardService_GetBoard_WrongID(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testAPIEndpoint := "/rest/api/2/board/99999999" | ||||
| @@ -108,7 +108,7 @@ func TestBoardGet_NoBoard(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBoardCreate(t *testing.T) { | ||||
| func TestBoardService_CreateBoard(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testMux.HandleFunc("/rest/agile/1.0/board", func(w http.ResponseWriter, r *http.Request) { | ||||
| @@ -133,7 +133,7 @@ func TestBoardCreate(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBoardDelete(t *testing.T) { | ||||
| func TestBoardService_DeleteBoard(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
| 	testMux.HandleFunc("/rest/agile/1.0/board/1", func(w http.ResponseWriter, r *http.Request) { | ||||
| @@ -152,3 +152,35 @@ func TestBoardDelete(t *testing.T) { | ||||
| 		t.Errorf("Error given: %s", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestBoardService_GetAllSprints(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
|  | ||||
| 	testAPIEndpoint := "/rest/agile/1.0/board/123/sprint" | ||||
|  | ||||
| 	raw, err := ioutil.ReadFile("./mocks/sprints.json") | ||||
| 	if err != nil { | ||||
| 		t.Error(err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) { | ||||
| 		testMethod(t, r, "GET") | ||||
| 		testRequestURL(t, r, testAPIEndpoint) | ||||
| 		fmt.Fprint(w, string(raw)) | ||||
| 	}) | ||||
|  | ||||
| 	sprints, _, err := testClient.Board.GetAllSprints("123") | ||||
|  | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Got error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if sprints == nil { | ||||
| 		t.Error("Expected sprint list. Got nil.") | ||||
| 	} | ||||
|  | ||||
| 	if len(sprints) != 4 { | ||||
| 		t.Errorf("Expected 4 transitions. Got %d", len(sprints)) | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										41
									
								
								sprint.go
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								sprint.go
									
									
									
									
									
								
							| @@ -2,52 +2,25 @@ package jira | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| // SprintService handles sprints in JIRA Agile API. | ||||
| // See https://docs.atlassian.com/jira-software/REST/cloud/ | ||||
| type SprintService struct { | ||||
| 	client *Client | ||||
| } | ||||
|  | ||||
| // Wrapper struct for search result | ||||
| type sprintsResult struct { | ||||
| 	Sprints []Sprint `json:"values"` | ||||
| } | ||||
|  | ||||
| // Sprint represents a sprint on JIRA agile board | ||||
| type Sprint struct { | ||||
| 	ID            int        `json:"id"` | ||||
| 	Name          string     `json:"name"` | ||||
| 	CompleteDate  *time.Time `json:"completeDate"` | ||||
| 	EndDate       *time.Time `json:"endDate"` | ||||
| 	StartDate     *time.Time `json:"startDate"` | ||||
| 	OriginBoardID int        `json:"originBoardId"` | ||||
| 	Self          string     `json:"self"` | ||||
| 	State         string     `json:"state"` | ||||
| } | ||||
|  | ||||
| // Wrapper struct for moving issues to sprint | ||||
| // IssuesWrapper represents a wrapper struct for moving issues to sprint | ||||
| type IssuesWrapper struct { | ||||
| 	Issues []string `json:"issues"` | ||||
| } | ||||
|  | ||||
| // GetList gets sprints for given board | ||||
| // MoveIssuesToSprint moves issues to a sprint, for a given sprint Id. | ||||
| // Issues can only be moved to open or active sprints. | ||||
| // The maximum number of issues that can be moved in one operation is 50. | ||||
| // | ||||
| // JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board/{boardId}/sprint | ||||
| func (s *SprintService) GetList(boardID string) ([]Sprint, *Response, error) { | ||||
| 	apiEndpoint := fmt.Sprintf("rest/agile/1.0/board/%s/sprint", boardID) | ||||
| 	req, err := s.client.NewRequest("GET", apiEndpoint, nil) | ||||
| 	if err != nil { | ||||
| 		return nil, nil, err | ||||
| 	} | ||||
|  | ||||
| 	result := new(sprintsResult) | ||||
| 	resp, err := s.client.Do(req, result) | ||||
| 	return result.Sprints, resp, err | ||||
| } | ||||
|  | ||||
| func (s *SprintService) AddIssuesToSprint(sprintID int, issueIDs []string) (*Response, error) { | ||||
| // JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/sprint-moveIssuesToSprint | ||||
| func (s *SprintService) MoveIssuesToSprint(sprintID int, issueIDs []string) (*Response, error) { | ||||
| 	apiEndpoint := fmt.Sprintf("rest/agile/1.0/sprint/%d/issue", sprintID) | ||||
|  | ||||
| 	payload := IssuesWrapper{Issues: issueIDs} | ||||
|   | ||||
| @@ -2,45 +2,11 @@ package jira | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestSprintGetList(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
|  | ||||
| 	testAPIEndpoint := "/rest/agile/1.0/board/123/sprint" | ||||
|  | ||||
| 	raw, err := ioutil.ReadFile("./mocks/sprints.json") | ||||
| 	if err != nil { | ||||
| 		t.Error(err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) { | ||||
| 		testMethod(t, r, "GET") | ||||
| 		testRequestURL(t, r, testAPIEndpoint) | ||||
| 		fmt.Fprint(w, string(raw)) | ||||
| 	}) | ||||
|  | ||||
| 	sprints, _, err := testClient.Sprint.GetList("123") | ||||
|  | ||||
| 	if err != nil { | ||||
| 		t.Errorf("Got error: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	if sprints == nil { | ||||
| 		t.Error("Expected sprint list. Got nil.") | ||||
| 	} | ||||
|  | ||||
| 	if len(sprints) != 4 { | ||||
| 		t.Errorf("Expected 4 transitions. Got %d", len(sprints)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestMoveIssueToSprint(t *testing.T) { | ||||
| func TestSprintService_MoveIssuesToSprint(t *testing.T) { | ||||
| 	setup() | ||||
| 	defer teardown() | ||||
|  | ||||
| @@ -63,7 +29,7 @@ func TestMoveIssueToSprint(t *testing.T) { | ||||
| 			t.Errorf("Expected %s to be in payload, got %s instead", issuesToMove[0], payload.Issues[0]) | ||||
| 		} | ||||
| 	}) | ||||
| 	_, err := testClient.Sprint.AddIssuesToSprint(123, issuesToMove) | ||||
| 	_, err := testClient.Sprint.MoveIssuesToSprint(123, issuesToMove) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		t.Error("Got error: %v", err) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user