Skip to content

Commit

Permalink
Merge pull request #121 from ctreminiom/feature/sm-request-create
Browse files Browse the repository at this point in the history
🔢 SM Customer Request creation
  • Loading branch information
ctreminiom authored Jun 9, 2022
2 parents 142df49 + d285fc8 commit 77c86cf
Show file tree
Hide file tree
Showing 8 changed files with 678 additions and 30 deletions.
34 changes: 4 additions & 30 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
coverage:
range: 80..100
round: down
precisi.on: 2

status:
project: # measuring the overall project coverage
default: # context, you can create multiple ones with custom titles
enabled: yes # must be yes|true to enable this status
target: 90 # specify the target coverage for each commit status
# option: "auto" (must increase from parent commit or pull request base)
# option: "X%" a static target percentage to hit
threshold: null # allowed to drop X% and still result in a "success" commit status
if_not_found: success # if parent is not found report status as success, error, or failure
if_ci_failed: error # if ci fails report status as success, error, or failure
patch:
default:
enabled: no
=======
# Team Yaml
coverage:
round: down
precision: 5

# Repository Yaml
coverage:
round: up
range: 0..10

# Used in Codecov after updating
coverage:
round: up
range: 0..10
precision: 5
target: 80%
project: # measuring the overall project coverage
default: # context, you can create multiple ones with custom titles
target: 90% # specify the target coverage for each commit status
38 changes: 38 additions & 0 deletions jira/sm/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,44 @@ type RequestGetOptionsScheme struct {
Expand []string
}

// Create creates a customer request in a service desk.
// The JSON request must include the service desk and customer request type, as well as any fields that are required for
// the request type.
// Docs: https://docs.go-atlassian.io/jira-service-management/request#create-customer-request
func (r *RequestService) Create(ctx context.Context, payload *model.CreateCustomerRequestPayloadScheme, fields *model.CustomerRequestFields) (
result *model.CustomerRequestScheme, response *ResponseScheme, err error) {

if fields == nil {
return nil, nil, model.ErrNoCustomRequestFieldsError
}

payloadWithCustomFields, err := payload.MergeFields(fields)
if err != nil {
return nil, nil, err
}

reader, err := transformStructToReader(&payloadWithCustomFields)
if err != nil {
return nil, nil, err
}

endpoint := "rest/servicedeskapi/request"
request, err := r.client.newRequest(ctx, http.MethodPost, endpoint, reader)
if err != nil {
return
}

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

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

return
}

// Gets returns all customer requests for the user executing the query.
// The returned customer requests are ordered chronologically by the latest activity on each request. For example, the latest status transition or comment.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/request#get-customer-requests
Expand Down
183 changes: 183 additions & 0 deletions jira/sm/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,195 @@ package sm
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"testing"
)

func TestRequestService_Create(t *testing.T) {

var customFieldMockedWithFields = model.CustomerRequestFields{}

// Add a new custom field
err := customFieldMockedWithFields.Text("summary", "Request JSD help via REST")
if err != nil {
t.Fatal(err)
}

err = customFieldMockedWithFields.Text("description", "I need a new *mouse* for my Mac")
if err != nil {
t.Fatal(err)
}

var customFieldMockedWithoutFields = model.CustomerRequestFields{}

testCases := []struct {
name string
payload *model.CreateCustomerRequestPayloadScheme
fields *model.CustomerRequestFields
mockFile string
wantHTTPMethod string
endpoint string
context context.Context
wantHTTPCodeReturn int
wantErr bool
}{
{
name: "CreateCustomerRequestWhenTheParametersAreCorrect",
payload: &model.CreateCustomerRequestPayloadScheme{
RequestParticipants: []string{"sample-uuid", "sample-uuid"},
ServiceDeskID: "300202",
RequestTypeID: "211991",
},
fields: &customFieldMockedWithFields,
mockFile: "./mocks/get-customer-request.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/request",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: false,
},

{
name: "CreateCustomerRequestWhenTheFieldsDoNotContainValues",
payload: &model.CreateCustomerRequestPayloadScheme{
RequestParticipants: []string{"sample-uuid", "sample-uuid"},
ServiceDeskID: "300202",
RequestTypeID: "211991",
},
fields: &customFieldMockedWithoutFields,
mockFile: "./mocks/get-customer-request.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/request",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "CreateCustomerRequestWhenTheResponseIsEmpty",
payload: &model.CreateCustomerRequestPayloadScheme{
RequestParticipants: []string{"sample-uuid", "sample-uuid"},
ServiceDeskID: "300202",
RequestTypeID: "211991",
},
fields: &customFieldMockedWithFields,
mockFile: "./mocks/empty_json.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/request",
context: context.Background(),
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "CreateCustomerRequestWhenTheContextIsNotProvided",
payload: &model.CreateCustomerRequestPayloadScheme{
RequestParticipants: []string{"sample-uuid", "sample-uuid"},
ServiceDeskID: "300202",
RequestTypeID: "211991",
},
fields: &customFieldMockedWithFields,
mockFile: "./mocks/get-customer-request.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/request",
context: nil,
wantHTTPCodeReturn: http.StatusOK,
wantErr: true,
},

{
name: "CreateCustomerRequestWhenTheFieldsAreNotProvided",
payload: &model.CreateCustomerRequestPayloadScheme{
RequestParticipants: []string{"sample-uuid", "sample-uuid"},
ServiceDeskID: "300202",
RequestTypeID: "211991",
},
fields: nil,
mockFile: "./mocks/get-customer-request.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/request",
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)
}

service := &RequestService{client: mockClient}
gotResult, gotResponse, err := service.Create(testCase.context, testCase.payload, testCase.fields)

if testCase.wantErr {

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

if gotResponse != nil {
t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
}
} 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)

t.Logf("HTTP Code Wanted: %v, HTTP Code Returned: %v", testCase.wantHTTPCodeReturn, gotResponse.Code)
assert.Equal(t, gotResponse.Code, testCase.wantHTTPCodeReturn)

t.Log("-------------------------------------------")
t.Logf("Custom Request Issue Key: %v", gotResult.IssueKey)
t.Logf("Custom Request Type Name: %v", gotResult.RequestType)
t.Log("-------------------------------------------")
}

})
}

}

func TestRequestService_Gets(t *testing.T) {

testCases := []struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/infra/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
ErrNoRequestTypeIDError = errors.New("sm: no request type id set")
ErrNoFileNameError = errors.New("sm: no file name set")
ErrNoFileReaderError = errors.New("sm: no io.Reader set")
ErrNoCustomRequestFieldsError = errors.New("sm: no customer request fields set")

ErrNoContentIDError = errors.New("confluence: no content id set")
ErrNoCQLError = errors.New("confluence: no CQL query set")
Expand Down
2 changes: 2 additions & 0 deletions pkg/infra/models/jira_issue_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type IssueFieldsSchemeV2 struct {
Components []*ComponentScheme `json:"components,omitempty"`
Creator *UserScheme `json:"creator,omitempty"`
Reporter *UserScheme `json:"reporter,omitempty"`
Assignee *UserScheme `json:"assignee,omitempty"`
Resolution *ResolutionScheme `json:"resolution,omitempty"`
Resolutiondate string `json:"resolutiondate,omitempty"`
Workratio int `json:"workratio,omitempty"`
Expand All @@ -99,6 +100,7 @@ type IssueFieldsSchemeV2 struct {
Description string `json:"description,omitempty"`
Comment *IssueCommentPageSchemeV2 `json:"comment,omitempty"`
Subtasks []*IssueScheme `json:"subtasks,omitempty"`
Security *SecurityScheme `json:"security,omitempty"`
}

type IssueResponseScheme struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/infra/models/jira_issue_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type IssueFieldsScheme struct {
Components []*ComponentScheme `json:"components,omitempty"`
Creator *UserScheme `json:"creator,omitempty"`
Reporter *UserScheme `json:"reporter,omitempty"`
Assignee *UserScheme `json:"assignee,omitempty"`
Resolution *ResolutionScheme `json:"resolution,omitempty"`
Resolutiondate string `json:"resolutiondate,omitempty"`
Workratio int `json:"workratio,omitempty"`
Expand All @@ -99,6 +100,7 @@ type IssueFieldsScheme struct {
Description *CommentNodeScheme `json:"description,omitempty"`
Comment *IssueCommentPageScheme `json:"comment,omitempty"`
Subtasks []*IssueScheme `json:"subtasks,omitempty"`
Security *SecurityScheme `json:"security,omitempty"`
}

type IssueTransitionScheme struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/infra/models/jira_security.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

type SecurityScheme struct {
Self string `json:"self,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
}
Loading

0 comments on commit 77c86cf

Please sign in to comment.