Skip to content

Commit

Permalink
Merge pull request #79 from ctreminiom/feature/workflow-scheme
Browse files Browse the repository at this point in the history
✨ Added the workflow scheme service.
  • Loading branch information
ctreminiom committed Nov 15, 2021
2 parents 3268c52 + 1fbbb91 commit 2d79444
Show file tree
Hide file tree
Showing 11 changed files with 1,715 additions and 1 deletion.
38 changes: 38 additions & 0 deletions jira/mocks/get-worflow-scheme-associations.json
@@ -0,0 +1,38 @@
{
"values": [
{
"projectIds": [
"10001"
],
"workflowScheme": {
"id": 10002,
"name": "Jira Service Management IT Support Workflow Scheme generated for Project DESK",
"description": "This Jira Service Management IT Support Workflow Scheme was generated for Project DESK",
"defaultWorkflow": "jira",
"issueTypeMappings": {
"10002": "DESK: Jira Service Management default workflow",
"10003": "DESK: Jira Service Management default workflow",
"10005": "DESK: Service Request Fulfilment workflow for Jira Service Management",
"10006": "DESK: Service Request Fulfilment workflow for Jira Service Management",
"10007": "DESK: Service Request Fulfilment with Approvals workflow for Jira Service Management"
},
"self": "https://ctreminiom.atlassian.net/rest/api/2/workflowscheme/10002"
}
},
{
"projectIds": [
"10003"
],
"workflowScheme": {
"id": 10004,
"name": "K2: Software Simplified Workflow Scheme",
"description": "Generated by JIRA Software version 1001.0.0-SNAPSHOT. This workflow scheme is managed internally by Jira Software. Do not manually modify this workflow scheme.",
"defaultWorkflow": "Software Simplified Workflow for Project K2",
"issueTypeMappings": {

},
"self": "https://ctreminiom.atlassian.net/rest/api/2/workflowscheme/10004"
}
}
]
}
12 changes: 12 additions & 0 deletions jira/mocks/get-workflow-scheme.json
@@ -0,0 +1,12 @@
{
"id": 101010,
"name": "Example workflow scheme",
"description": "The description of the example workflow scheme.",
"defaultWorkflow": "jira",
"issueTypeMappings": {
"10000": "scrum workflow",
"10001": "builds workflow"
},
"draft": false,
"self": "https://your-domain.atlassian.net/rest/api/2/workflowscheme/101010"
}
30 changes: 30 additions & 0 deletions jira/mocks/get-workflow-schemes.json
@@ -0,0 +1,30 @@
{
"maxResults": 50,
"startAt": 0,
"total": 2,
"isLast": true,
"values": [
{
"id": 101010,
"name": "Example workflow scheme",
"description": "The description of the example workflow scheme.",
"defaultWorkflow": "jira",
"issueTypeMappings": {
"10000": "scrum workflow",
"10001": "builds workflow"
},
"self": "https://your-domain.atlassian.net/rest/api/2/workflowscheme/101010"
},
{
"id": 101011,
"name": "Another example workflow scheme",
"description": "The description of the another example workflow scheme.",
"defaultWorkflow": "jira",
"issueTypeMappings": {
"10000": "scrum workflow",
"10001": "builds workflow"
},
"self": "https://your-domain.atlassian.net/rest/api/2/workflowscheme/101011"
}
]
}
6 changes: 5 additions & 1 deletion jira/v2/jira.go
Expand Up @@ -149,7 +149,11 @@ func New(httpClient *http.Client, site string) (client *Client, err error) {
}

client.MySelf = &MySelfService{client: client}
client.Workflow = &WorkflowService{client: client}
client.Workflow = &WorkflowService{
client: client,
Scheme: &WorkflowSchemeService{client: client},
}

return
}

Expand Down
1 change: 1 addition & 0 deletions jira/v2/workflow.go
Expand Up @@ -12,6 +12,7 @@ import (

type WorkflowService struct {
client *Client
Scheme *WorkflowSchemeService
}

func (w *WorkflowService) Create(ctx context.Context, payload *models.WorkflowPayloadScheme) (result *models.WorkflowCreatedResponseScheme,
Expand Down
224 changes: 224 additions & 0 deletions jira/v2/workflowScheme.go
@@ -0,0 +1,224 @@
package v2

import (
"context"
"fmt"
models "github.com/ctreminiom/go-atlassian/pkg/infra/models/jira"
"net/http"
"net/url"
"strconv"
"strings"
)

type WorkflowSchemeService struct {
client *Client
}

// Gets returns a paginated list of all workflow schemes, not including draft workflow schemes.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-schemes/#api-rest-api-2-workflowscheme-get
func (w *WorkflowSchemeService) Gets(ctx context.Context, startAt, maxResults int) (result *models.WorkflowSchemePageScheme,
response *ResponseScheme, err error) {

params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))

endpoint := fmt.Sprintf("rest/api/2/workflowscheme?%v", params.Encode())

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

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

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

return
}

// Create creates a workflow scheme.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-schemes/#api-rest-api-2-workflowscheme-post
func (w *WorkflowSchemeService) Create(ctx context.Context, payload *models.WorkflowSchemePayloadScheme) (result *models.WorkflowSchemeScheme,
response *ResponseScheme, err error) {

payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}

endpoint := "/rest/api/2/workflowscheme"
request, err := w.client.newRequest(ctx, http.MethodPost, endpoint, payloadAsReader)
if err != nil {
return
}

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

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

return
}

// Get returns a workflow scheme.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-schemes/#api-rest-api-2-workflowscheme-id-get
func (w *WorkflowSchemeService) Get(ctx context.Context, workflowSchemeID int, returnDraftIfExists bool) (result *models.WorkflowSchemeScheme,
response *ResponseScheme, err error) {

params := url.Values{}
if returnDraftIfExists {
params.Add("returnDraftIfExists", "true")
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("rest/api/2/workflowscheme/%v", workflowSchemeID))

if params.Encode() != "" {
endpoint.WriteString(fmt.Sprintf("?%v", params.Encode()))
}

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

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

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

return
}

// Update updates a workflow scheme, including the name, default workflow, issue type to project mappings, and more.
// If the workflow scheme is active (that is, being used by at least one project), then a draft workflow scheme is
// created or updated instead, provided that updateDraftIfNeeded is set to true.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-schemes/#api-rest-api-2-workflowscheme-id-put
func (w *WorkflowSchemeService) Update(ctx context.Context, workflowSchemeID int, payload *models.WorkflowSchemePayloadScheme) (
result *models.WorkflowSchemeScheme, response *ResponseScheme, err error) {

payloadAsReader, err := transformStructToReader(payload)
if err != nil {
return nil, nil, err
}

var endpoint strings.Builder
endpoint.WriteString(fmt.Sprintf("rest/api/2/workflowscheme/%v", workflowSchemeID))

request, err := w.client.newRequest(ctx, http.MethodPut, endpoint.String(), payloadAsReader)
if err != nil {
return
}

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

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

return
}

// Delete deletes a workflow scheme.
// Note that a workflow scheme cannot be deleted if it is active (that is, being used by at least one project).
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-schemes/#api-rest-api-2-workflowscheme-id-delete
func (w *WorkflowSchemeService) Delete(ctx context.Context, workflowSchemeID int) (response *ResponseScheme, err error) {

endpoint := fmt.Sprintf("rest/api/2/workflowscheme/%v", workflowSchemeID)

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

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

return
}

// Associations returns a list of the workflow schemes associated with a list of projects.
// Each returned workflow scheme includes a list of the requested projects associated with it.
// Any team-managed or non-existent projects in the request are ignored and no errors are returned.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-scheme-project-associations/#api-rest-api-2-workflowscheme-project-get
func (w *WorkflowSchemeService) Associations(ctx context.Context, projectIDs []int) (result *models.WorkflowSchemeAssociationPageScheme,
response *ResponseScheme, err error) {

if len(projectIDs) == 0 {
return nil, nil, models.ErrNoProjectsError
}

params := url.Values{}
for _, projectID := range projectIDs {
params.Add("projectId", strconv.Itoa(projectID))
}

endpoint := fmt.Sprintf("rest/api/2/workflowscheme/project?%v", params.Encode())

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

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

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

return
}

// Assign assigns a workflow scheme to a project.
// This operation is performed only when there are no issues in the project.
// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-workflow-scheme-project-associations/#api-rest-api-2-workflowscheme-project-put
func (w *WorkflowSchemeService) Assign(ctx context.Context, workflowSchemeID, projectID string) (response *ResponseScheme, err error) {

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

if len(workflowSchemeID) == 0 {
return nil, models.ErrNoWorkflowSchemeIDError
}

payload := struct {
WorkflowSchemeID string `json:"workflowSchemeId"`
ProjectID string `json:"projectId"`
}{
WorkflowSchemeID: workflowSchemeID,
ProjectID: projectID,
}

payloadAsReader, _ := transformStructToReader(&payload)
endpoint := "rest/api/2/workflowscheme/project"

request, err := w.client.newRequest(ctx, http.MethodPut, endpoint, payloadAsReader)
if err != nil {
return
}

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

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

return
}

0 comments on commit 2d79444

Please sign in to comment.