Skip to content

Commit e256d12

Browse files
committed
Merge branch 'ctreminiom-feature/552'
* ctreminiom-feature/552: CHANGELOG: Workflow status categories: Revisited and fully implemented for Cloud and On Premise (incl. examples) On premise/Status category: Added an additional test check On premise/Status category: Fixed godoc and links to Jira documentation Cloud/Status category: Added empty line to improve readability Cloud/Status category: fixed error message if no status category id is given On premise/Status category: Add comments for usage example Cloud/Status category: Add comments for usage example Cloud/Status category: Added two additional testing checks Cloud/Status Category: Smaller godoc changes README: API-Version: Official support for Jira Cloud API in version 3 PersonalAccessToken Auth: Used fmt.Sprintf to concat a string Updated changelog with latest changes README: Add chapter about executing unit tests Makefile: Add commands for testing coverage Makefile: Switch to latest version of staticcheck README: Removed the installation part for gopkg.in Fix all URLs in the README README.md: fix example URLs ♻️ IssueCategory Service reviewed
2 parents 2565dae + c2943e2 commit e256d12

File tree

8 files changed

+237
-20
lines changed

8 files changed

+237
-20
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ client.Project.GetAll(ctx, &GetQueryOptions{})
348348
### Bug Fixes
349349

350350
* README: Fixed all (broken) links
351+
<<<<<<< HEAD
352+
=======
353+
354+
### API-Endpoints
355+
356+
* Workflow status categories: Revisited and fully implemented for Cloud and On Premise (incl. examples)
357+
>>>>>>> ctreminiom-feature/552
351358
352359
### Other
353360

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
jira "github.com/andygrunwald/go-jira/v2/cloud"
8+
)
9+
10+
func main() {
11+
jiraClient, err := jira.NewClient("https://mattermost.atlassian.net/", nil)
12+
if err != nil {
13+
panic(err)
14+
}
15+
16+
// Showcase of StatusCategory.GetList:
17+
// Getting all status categories
18+
categories, resp, err := jiraClient.StatusCategory.GetList(context.TODO())
19+
if err != nil {
20+
log.Println(resp.StatusCode)
21+
panic(err)
22+
}
23+
24+
for _, statusCategory := range categories {
25+
log.Println(statusCategory)
26+
}
27+
28+
// Showcase of StatusCategory.Get
29+
// Getting a single status category
30+
category, resp, err := jiraClient.StatusCategory.Get(context.TODO(), "1")
31+
if err != nil {
32+
log.Println(resp.StatusCode)
33+
panic(err)
34+
}
35+
36+
log.Println(category)
37+
}

cloud/statuscategory.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package cloud
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"net/http"
68
)
79

810
// StatusCategoryService handles status categories for the Jira instance / API.
911
//
10-
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-Statuscategory
12+
// Use it to obtain a list of all status categories and the details of a category.
13+
// Status categories provided a mechanism for categorizing statuses.
14+
//
15+
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-workflow-status-categories/#api-group-workflow-status-categories
1116
type StatusCategoryService service
1217

1318
// StatusCategory represents the category a status belongs to.
@@ -28,20 +33,47 @@ const (
2833
StatusCategoryUndefined = "undefined"
2934
)
3035

31-
// GetList gets all status categories from Jira
36+
// GetList returns a list of all status categories.
3237
//
33-
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-statuscategory-get
38+
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-workflow-status-categories/#api-rest-api-3-statuscategory-get
3439
func (s *StatusCategoryService) GetList(ctx context.Context) ([]StatusCategory, *Response, error) {
35-
apiEndpoint := "rest/api/2/statuscategory"
40+
apiEndpoint := "/rest/api/3/statuscategory"
3641
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
3742
if err != nil {
3843
return nil, nil, err
3944
}
4045

41-
statusCategoryList := []StatusCategory{}
42-
resp, err := s.client.Do(req, &statusCategoryList)
46+
var statusCategories []StatusCategory
47+
resp, err := s.client.Do(req, &statusCategories)
4348
if err != nil {
4449
return nil, resp, NewJiraError(resp, err)
4550
}
46-
return statusCategoryList, resp, nil
51+
52+
return statusCategories, resp, nil
53+
}
54+
55+
// Get returns a status category.
56+
// Status categories provided a mechanism for categorizing statuses.
57+
//
58+
// statusCategoryID represents the ID or key of the status category.
59+
//
60+
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-workflow-status-categories/#api-rest-api-3-statuscategory-idorkey-get
61+
func (s *StatusCategoryService) Get(ctx context.Context, statusCategoryID string) (*StatusCategory, *Response, error) {
62+
if statusCategoryID == "" {
63+
return nil, nil, errors.New("no status category id set")
64+
}
65+
66+
apiEndpoint := fmt.Sprintf("/rest/api/3/statuscategory/%v", statusCategoryID)
67+
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
68+
if err != nil {
69+
return nil, nil, err
70+
}
71+
72+
statusCategory := new(StatusCategory)
73+
resp, err := s.client.Do(req, statusCategory)
74+
if err != nil {
75+
return nil, resp, NewJiraError(resp, err)
76+
}
77+
78+
return statusCategory, resp, nil
4779
}

cloud/statuscategory_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,56 @@ import (
1111
func TestStatusCategoryService_GetList(t *testing.T) {
1212
setup()
1313
defer teardown()
14-
testAPIEdpoint := "/rest/api/2/statuscategory"
14+
testAPIEndpoint := "/rest/api/3/statuscategory"
1515

1616
raw, err := os.ReadFile("../testing/mock-data/all_statuscategories.json")
1717
if err != nil {
1818
t.Error(err.Error())
1919
}
20-
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) {
20+
21+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
2122
testMethod(t, r, http.MethodGet)
22-
testRequestURL(t, r, testAPIEdpoint)
23+
testRequestURL(t, r, testAPIEndpoint)
2324
fmt.Fprint(w, string(raw))
2425
})
2526

2627
statusCategory, _, err := testClient.StatusCategory.GetList(context.Background())
2728
if statusCategory == nil {
2829
t.Error("Expected statusCategory list. StatusCategory list is nil")
2930
}
31+
if l := len(statusCategory); l != 4 {
32+
t.Errorf("Expected 4 statusCategory list items. Got %d", l)
33+
}
34+
if err != nil {
35+
t.Errorf("Error given: %s", err)
36+
}
37+
}
38+
39+
func TestStatusCategoryService_Get(t *testing.T) {
40+
setup()
41+
defer teardown()
42+
testAPIEndpoint := "/rest/api/3/statuscategory/1"
43+
44+
raw, err := os.ReadFile("../testing/mock-data/status_category.json")
45+
if err != nil {
46+
t.Error(err.Error())
47+
}
48+
49+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
50+
testMethod(t, r, http.MethodGet)
51+
testRequestURL(t, r, testAPIEndpoint)
52+
fmt.Fprint(w, string(raw))
53+
})
54+
55+
statusCategory, _, err := testClient.StatusCategory.Get(context.Background(), "1")
3056
if err != nil {
3157
t.Errorf("Error given: %s", err)
58+
59+
} else if statusCategory == nil {
60+
t.Error("Expected status category. StatusCategory is nil")
61+
62+
// Checking testdata
63+
} else if statusCategory.ColorName != "medium-gray" {
64+
t.Errorf("Expected statusCategory.ColorName to be 'medium-gray'. Got '%s'", statusCategory.ColorName)
3265
}
3366
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
jira "github.com/andygrunwald/go-jira/v2/onpremise"
8+
)
9+
10+
func main() {
11+
jiraClient, err := jira.NewClient("https://issues.apache.org/jira/", nil)
12+
if err != nil {
13+
panic(err)
14+
}
15+
16+
// Showcase of StatusCategory.GetList:
17+
// Getting all status categories
18+
categories, resp, err := jiraClient.StatusCategory.GetList(context.TODO())
19+
if err != nil {
20+
log.Println(resp.StatusCode)
21+
panic(err)
22+
}
23+
24+
for _, statusCategory := range categories {
25+
log.Println(statusCategory)
26+
}
27+
28+
// Showcase of StatusCategory.Get
29+
// Getting a single status category
30+
category, resp, err := jiraClient.StatusCategory.Get(context.TODO(), "1")
31+
if err != nil {
32+
log.Println(resp.StatusCode)
33+
panic(err)
34+
}
35+
36+
log.Println(category)
37+
}

onpremise/statuscategory.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package onpremise
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"net/http"
68
)
79

810
// StatusCategoryService handles status categories for the Jira instance / API.
911
//
10-
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-Statuscategory
12+
// Use it to obtain a list of all status categories and the details of a category.
13+
// Status categories provided a mechanism for categorizing statuses.
14+
//
15+
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-status-categories/#api-group-workflow-status-categories
1116
type StatusCategoryService service
1217

1318
// StatusCategory represents the category a status belongs to.
@@ -28,20 +33,46 @@ const (
2833
StatusCategoryUndefined = "undefined"
2934
)
3035

31-
// GetList gets all status categories from Jira
36+
// GetList returns a list of all status categories.
3237
//
33-
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-statuscategory-get
38+
// Jira API docs: https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/statuscategory-getStatusCategories
3439
func (s *StatusCategoryService) GetList(ctx context.Context) ([]StatusCategory, *Response, error) {
35-
apiEndpoint := "rest/api/2/statuscategory"
40+
apiEndpoint := "/rest/api/2/statuscategory"
3641
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
3742
if err != nil {
3843
return nil, nil, err
3944
}
4045

41-
statusCategoryList := []StatusCategory{}
42-
resp, err := s.client.Do(req, &statusCategoryList)
46+
var statusCategories []StatusCategory
47+
resp, err := s.client.Do(req, &statusCategories)
4348
if err != nil {
4449
return nil, resp, NewJiraError(resp, err)
4550
}
46-
return statusCategoryList, resp, nil
51+
52+
return statusCategories, resp, nil
53+
}
54+
55+
// Get returns a full representation of the StatusCategory having the given id or key.
56+
//
57+
// statusCategoryID represents the ID or key of the status category.
58+
//
59+
// Jira API docs: https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/statuscategory-getStatusCategory
60+
func (s *StatusCategoryService) Get(ctx context.Context, statusCategoryID string) (*StatusCategory, *Response, error) {
61+
if statusCategoryID == "" {
62+
return nil, nil, errors.New("no status category id set")
63+
}
64+
65+
apiEndpoint := fmt.Sprintf("/rest/api/2/statuscategory/%v", statusCategoryID)
66+
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
67+
if err != nil {
68+
return nil, nil, err
69+
}
70+
71+
statusCategory := new(StatusCategory)
72+
resp, err := s.client.Do(req, statusCategory)
73+
if err != nil {
74+
return nil, resp, NewJiraError(resp, err)
75+
}
76+
77+
return statusCategory, resp, nil
4778
}

onpremise/statuscategory_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,56 @@ import (
1111
func TestStatusCategoryService_GetList(t *testing.T) {
1212
setup()
1313
defer teardown()
14-
testAPIEdpoint := "/rest/api/2/statuscategory"
14+
testAPIEndpoint := "/rest/api/2/statuscategory"
1515

1616
raw, err := os.ReadFile("../testing/mock-data/all_statuscategories.json")
1717
if err != nil {
1818
t.Error(err.Error())
1919
}
20-
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) {
20+
21+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
2122
testMethod(t, r, http.MethodGet)
22-
testRequestURL(t, r, testAPIEdpoint)
23+
testRequestURL(t, r, testAPIEndpoint)
2324
fmt.Fprint(w, string(raw))
2425
})
2526

2627
statusCategory, _, err := testClient.StatusCategory.GetList(context.Background())
2728
if statusCategory == nil {
2829
t.Error("Expected statusCategory list. StatusCategory list is nil")
2930
}
31+
if l := len(statusCategory); l != 4 {
32+
t.Errorf("Expected 4 statusCategory list items. Got %d", l)
33+
}
34+
if err != nil {
35+
t.Errorf("Error given: %s", err)
36+
}
37+
}
38+
39+
func TestStatusCategoryService_Get(t *testing.T) {
40+
setup()
41+
defer teardown()
42+
testAPIEndpoint := "/rest/api/2/statuscategory/1"
43+
44+
raw, err := os.ReadFile("../testing/mock-data/status_category.json")
45+
if err != nil {
46+
t.Error(err.Error())
47+
}
48+
49+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
50+
testMethod(t, r, http.MethodGet)
51+
testRequestURL(t, r, testAPIEndpoint)
52+
fmt.Fprint(w, string(raw))
53+
})
54+
55+
statusCategory, _, err := testClient.StatusCategory.Get(context.Background(), "1")
3056
if err != nil {
3157
t.Errorf("Error given: %s", err)
58+
59+
} else if statusCategory == nil {
60+
t.Error("Expected status category. StatusCategory is nil")
61+
62+
// Checking testdata
63+
} else if statusCategory.ColorName != "medium-gray" {
64+
t.Errorf("Expected statusCategory.ColorName to be 'medium-gray'. Got '%s'", statusCategory.ColorName)
3265
}
3366
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"self": "https://issues.apache.org/jira/rest/api/2/resolution/1",
3+
"id": 1,
4+
"key": "undefined",
5+
"colorName": "medium-gray",
6+
"name": "No Category"
7+
}

0 commit comments

Comments
 (0)