Skip to content

Commit

Permalink
✨ Added the v3.Project.Version.Gets method
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreminiom committed Nov 8, 2021
1 parent 615a55b commit 2ef0fbe
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 8 deletions.
30 changes: 30 additions & 0 deletions jira/mocks/get-project-versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"self": "https://your-domain.atlassian.net/rest/api/3/version/10000",
"id": "10000",
"description": "An excellent version",
"name": "New Version 1",
"archived": false,
"released": true,
"releaseDate": "2010-07-06",
"overdue": true,
"userReleaseDate": "6/Jul/2010",
"projectId": 10000
},
{
"self": "https://your-domain.atlassian.net/rest/api/3/version/10010",
"id": "10010",
"description": "Minor Bugfix version",
"name": "Next Version",
"archived": false,
"released": false,
"overdue": false,
"projectId": 10000,
"issuesStatusForFixVersion": {
"unmapped": 0,
"toDo": 10,
"inProgress": 20,
"done": 100
}
}
]
27 changes: 27 additions & 0 deletions jira/v3/projectVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ import (

type ProjectVersionService struct{ client *Client }

// Gets returns all versions in a project.
// The response is not paginated.
// Use Get project versions paginated if you want to get the versions in a project with pagination.
// Atlassian Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-versions/#api-rest-api-3-project-projectidorkey-versions-get
func (p *ProjectVersionService) Gets(ctx context.Context, projectKeyOrID string) (result []*models.VersionScheme, response *ResponseScheme, err error) {

if len(projectKeyOrID) == 0 {
return nil, nil, models.ErrNoProjectIDError
}

endpoint := fmt.Sprintf("rest/api/3/project/%v/versions", projectKeyOrID)

request, err := p.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}

request.Header.Set("Accept", "application/json")

response, err = p.client.call(request, &result)
if err != nil {
return
}

return
}

// Search returns a paginated list of all versions in a project.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/projects/versions#get-project-versions-paginated
// Atlassian Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-versions/#api-rest-api-3-project-projectidorkey-version-get
Expand Down
170 changes: 162 additions & 8 deletions jira/v3/projectVersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,160 @@ func TestProjectVersionService_Get(t *testing.T) {

func TestProjectVersionService_Gets(t *testing.T) {

testCases := []struct {
name string
projectKeyOrID string
mockFile string
wantHTTPMethod string
endpoint string
context context.Context
wantHTTPCodeReturn int
wantErr bool
}{
{
name: "GetProjectVersionsWhenTheParamsAreCorrect",
projectKeyOrID: "10001",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: false,
},

{
name: "GetProjectVersionsWhenTheProjectKeyIsNotProvided",
projectKeyOrID: "",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "GetProjectVersionsWhenTheExpandsIsNil",
projectKeyOrID: "10001",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: false,
},

{
name: "GetProjectVersionsWhenTheRequestMethodIsIncorrect",
projectKeyOrID: "10001",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "GetProjectVersionsWhenTheStatusCodeIsIncorrect",
projectKeyOrID: "10001",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusBadRequest,
wantErr: true,
},

{
name: "GetProjectVersionsWhenTheContextIsNil",
projectKeyOrID: "10001",
mockFile: "../mocks/get-project-versions.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: nil,
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "GetProjectVersionsWhenTheResponseBodyHasADifferentFormat",
projectKeyOrID: "10001",
mockFile: "./mocks/empty_json.json",
wantHTTPMethod: http.MethodGet,
endpoint: "/rest/api/3/project/10001/versions",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},
}

for _, testCase := range testCases {

t.Run(testCase.name, func(t *testing.T) {

//Init a new HTTP mock server
mockOptions := mockServerOptions{
Endpoint: testCase.endpoint,
MockFilePath: testCase.mockFile,
MethodAccepted: testCase.wantHTTPMethod,
ResponseCodeWanted: testCase.wantHTTPCodeReturn,
}

mockServer, err := startMockServer(&mockOptions)
if err != nil {
t.Fatal(err)
}

defer mockServer.Close()

//Init the library instance
mockClient, err := startMockClient(mockServer.URL)
if err != nil {
t.Fatal(err)
}

i := &ProjectVersionService{client: mockClient}

gotResult, gotResponse, err := i.Gets(testCase.context, testCase.projectKeyOrID)

if testCase.wantErr {

if err != nil {
t.Logf("error returned: %v", err.Error())
}

assert.Error(t, err)
} else {

assert.NoError(t, err)
assert.NotEqual(t, gotResponse, nil)
assert.NotEqual(t, gotResult, nil)

apiEndpoint, err := url.Parse(gotResponse.Endpoint)
if err != nil {
t.Fatal(err)
}

var endpointToAssert string

if apiEndpoint.Query().Encode() != "" {
endpointToAssert = fmt.Sprintf("%v?%v", apiEndpoint.Path, apiEndpoint.Query().Encode())
} else {
endpointToAssert = apiEndpoint.Path
}

t.Logf("HTTP Endpoint Wanted: %v, HTTP Endpoint Returned: %v", testCase.endpoint, endpointToAssert)
assert.Equal(t, testCase.endpoint, endpointToAssert)
}
})

}

}

func TestProjectVersionService_Search(t *testing.T) {

testCases := []struct {
name string
projectKeyOrID string
Expand All @@ -403,7 +557,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
wantErr bool
}{
{
name: "GetProjectVersionsWhenTheParamsAreCorrect",
name: "SearchProjectVersionsWhenTheParamsAreCorrect",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand All @@ -422,7 +576,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheOptionsAreNotProvided",
name: "SearchProjectVersionsWhenTheOptionsAreNotProvided",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "",
Expand All @@ -441,7 +595,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheOptionsIsNil",
name: "SearchProjectVersionsWhenTheOptionsIsNil",
projectKeyOrID: "DUMMY",
options: nil,
startAt: 0,
Expand All @@ -455,7 +609,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheRequestMethodIsIncorrect",
name: "SearchProjectVersionsWhenTheRequestMethodIsIncorrect",
projectKeyOrID: "",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand All @@ -474,7 +628,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheStatusCodeIsIncorrect",
name: "SearchProjectVersionsWhenTheStatusCodeIsIncorrect",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand All @@ -493,7 +647,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheContextIsNil",
name: "SearchProjectVersionsWhenTheContextIsNil",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand All @@ -512,7 +666,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheResponseBodyHasADifferentFormat",
name: "SearchProjectVersionsWhenTheResponseBodyHasADifferentFormat",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand All @@ -531,7 +685,7 @@ func TestProjectVersionService_Gets(t *testing.T) {
},

{
name: "GetProjectVersionsWhenTheEndpointIsIncorrect",
name: "SearchProjectVersionsWhenTheEndpointIsIncorrect",
projectKeyOrID: "DUMMY",
options: &models.VersionGetsOptions{
OrderBy: "description",
Expand Down

0 comments on commit 2ef0fbe

Please sign in to comment.