Skip to content

Commit

Permalink
Merge pull request #82 from ctreminiom/feature/sm-models-migration
Browse files Browse the repository at this point in the history
🚚 Moved the common models to a dedicated shared package
  • Loading branch information
ctreminiom authored Nov 20, 2021
2 parents 65b9498 + 6d47de5 commit 614b9a2
Show file tree
Hide file tree
Showing 33 changed files with 798 additions and 915 deletions.
68 changes: 11 additions & 57 deletions jira/sm/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package sm
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
"net/url"
"regexp"
"strconv"
)

Expand All @@ -16,20 +16,15 @@ type CustomerService struct{ client *Client }
// The display name does not need to be unique. The record's identifiers,
// name and key, are automatically generated from the request details.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/customer#create-customer
func (c *CustomerService) Create(ctx context.Context, email, displayName string) (result *CustomerScheme,
func (c *CustomerService) Create(ctx context.Context, email, displayName string) (result *model.CustomerScheme,
response *ResponseScheme, err error) {

if len(email) == 0 {
return nil, nil, fmt.Errorf("error, please provide a valid email value")
}

//Check the email
if !isEmailValid(email) {
return nil, nil, fmt.Errorf("error, the email (%v) is not valid mail", email)
return nil, nil, model.ErrNoCustomerMailError
}

if len(displayName) == 0 {
return nil, nil, fmt.Errorf("error, please provide a valid displayName value")
return nil, nil, model.ErrNoCustomerDisplayNameError
}

payload := struct {
Expand All @@ -40,9 +35,10 @@ func (c *CustomerService) Create(ctx context.Context, email, displayName string)
Email: email,
}

payloadAsReader, _ := transformStructToReader(&payload)

var endpoint = "rest/servicedeskapi/customer"
var (
payloadAsReader, _ = transformStructToReader(&payload)
endpoint = "rest/servicedeskapi/customer"
)

request, err := c.client.newRequest(ctx, http.MethodPost, endpoint, payloadAsReader)
if err != nil {
Expand All @@ -60,36 +56,9 @@ func (c *CustomerService) Create(ctx context.Context, email, displayName string)
return
}

type CustomerScheme struct {
AccountID string `json:"accountId"`
Name string `json:"name"`
Key string `json:"key"`
EmailAddress string `json:"emailAddress"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
Links struct {
JiraRest string `json:"jiraRest"`
AvatarUrls struct {
Four8X48 string `json:"48x48"`
Two4X24 string `json:"24x24"`
One6X16 string `json:"16x16"`
Three2X32 string `json:"32x32"`
} `json:"avatarUrls"`
Self string `json:"self"`
} `json:"_links"`
}

func isEmailValid(email string) bool {
const emailRegexPattern = "^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"

var regex = regexp.MustCompile(emailRegexPattern)
return regex.MatchString(email)
}

// Gets returns a list of the customers on a service desk.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/customer#get-customers
func (c *CustomerService) Gets(ctx context.Context, serviceDeskID int, query string, start, limit int) (result *CustomerPageScheme,
func (c *CustomerService) Gets(ctx context.Context, serviceDeskID int, query string, start, limit int) (result *model.CustomerPageScheme,
response *ResponseScheme, err error) {

params := url.Values{}
Expand Down Expand Up @@ -128,7 +97,7 @@ func (c *CustomerService) Gets(ctx context.Context, serviceDeskID int, query str
func (c *CustomerService) Add(ctx context.Context, serviceDeskID int, accountIDs []string) (response *ResponseScheme, err error) {

if len(accountIDs) == 0 {
return nil, fmt.Errorf("error, please provide a valid accountIDs slice value")
return nil, model.ErrNoAccountSliceError
}

payload := struct {
Expand Down Expand Up @@ -161,7 +130,7 @@ func (c *CustomerService) Add(ctx context.Context, serviceDeskID int, accountIDs
func (c *CustomerService) Remove(ctx context.Context, serviceDeskID int, accountIDs []string) (response *ResponseScheme, err error) {

if len(accountIDs) == 0 {
return nil, fmt.Errorf("error, please provide a valid accountIDs slice value")
return nil, model.ErrNoAccountSliceError
}

payload := struct {
Expand Down Expand Up @@ -194,18 +163,3 @@ func (c *CustomerService) Remove(ctx context.Context, serviceDeskID int, account

return
}

type CustomerPageScheme struct {
Expands []interface{} `json:"_expands"`
Size int `json:"size"`
Start int `json:"start"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Links struct {
Base string `json:"base"`
Context string `json:"context"`
Next string `json:"next"`
Prev string `json:"prev"`
} `json:"_links"`
Values []*CustomerScheme `json:"values"`
}
69 changes: 0 additions & 69 deletions jira/sm/customer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,6 @@ func TestCustomerService_Create(t *testing.T) {
wantErr: true,
},

{
name: "CreateCustomerWhenTheEmailIsIncorrect",
email: "examplegmail.com",
displayName: "Example",
mockFile: "./mocks/create-customer.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/customer",
context: context.Background(),
wantHTTPCodeReturn: http.StatusCreated,
wantErr: true,
},

{
name: "CreateCustomerWhenTheEmailIsToShort",
email: "exa",
displayName: "Example",
mockFile: "./mocks/create-customer.json",
wantHTTPMethod: http.MethodPost,
endpoint: "/rest/servicedeskapi/customer",
context: context.Background(),
wantHTTPCodeReturn: http.StatusCreated,
wantErr: true,
},

{
name: "CreateCustomerWhenTheRequestMethodIsIncorrect",
email: "[email protected]",
Expand Down Expand Up @@ -210,51 +186,6 @@ func TestCustomerService_Create(t *testing.T) {

}

func Test_isEmailValid(t *testing.T) {

testCases := []struct {
name string
email string
wantErr bool
}{
{
name: "ValidateEmailWhenTheEmailIsCorrect",
email: "[email protected]",
wantErr: false,
},
{
name: "ValidateEmailWhenTheEmailDoesNotHaveFormat",
email: "ex",
wantErr: true,
},
{
name: "ValidateEmailWhenTheEmailIsIncorrect",
email: "exampleeaasc",
wantErr: true,
},
{
name: "ValidateEmailWhenTheEmailIsEmpty",
email: "",
wantErr: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {

isValid := isEmailValid(testCase.email)

if testCase.wantErr {
assert.Equal(t, false, isValid)
} else {
assert.Equal(t, true, isValid)
}

})
}

}

func TestCustomerService_Get(t *testing.T) {

testCases := []struct {
Expand Down
23 changes: 2 additions & 21 deletions jira/sm/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sm

import (
"context"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
)

Expand All @@ -10,7 +11,7 @@ type InfoService struct{ client *Client }
// Get retrieves information about the Jira Service Management instance such as software version,
// builds, and related links.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/info#get-info
func (i *InfoService) Get(ctx context.Context) (result *InfoScheme, response *ResponseScheme, err error) {
func (i *InfoService) Get(ctx context.Context) (result *model.InfoScheme, response *ResponseScheme, err error) {

var endpoint = "rest/servicedeskapi/info"

Expand All @@ -28,23 +29,3 @@ func (i *InfoService) Get(ctx context.Context) (result *InfoScheme, response *Re

return
}

type InfoScheme struct {
Version string `json:"version"`
PlatformVersion string `json:"platformVersion"`
BuildDate *InfoBuildDataScheme `json:"buildDate"`
BuildChangeSet string `json:"buildChangeSet"`
IsLicensedForUse bool `json:"isLicensedForUse"`
Links *InfoLinkScheme `json:"_links"`
}

type InfoBuildDataScheme struct {
Iso8601 string `json:"iso8601"`
Jira string `json:"jira"`
Friendly string `json:"friendly"`
EpochMillis int64 `json:"epochMillis"`
}

type InfoLinkScheme struct {
Self string `json:"self"`
}
50 changes: 7 additions & 43 deletions jira/sm/knowledgebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package sm
import (
"context"
"fmt"
"github.com/ctreminiom/go-atlassian/pkg/infra/models"
"net/http"
"net/url"
"strconv"
)

type KnowledgebaseService struct{ client *Client }
type KnowledgeBaseService struct{ client *Client }

// Search returns articles which match the given query string across all service desks.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/knowledgebase#search-articles
func (k *KnowledgebaseService) Search(ctx context.Context, query string, highlight bool, start, limit int) (
result *ArticlePageScheme, response *ResponseScheme, err error) {
func (k *KnowledgeBaseService) Search(ctx context.Context, query string, highlight bool, start, limit int) (
result *models.ArticlePageScheme, response *ResponseScheme, err error) {

if len(query) == 0 {
return nil, nil, notQueryError
return nil, nil, models.ErrNoKBQueryError
}

params := url.Values{}
Expand Down Expand Up @@ -48,8 +49,8 @@ func (k *KnowledgebaseService) Search(ctx context.Context, query string, highlig

// Gets returns articles which match the given query string across all service desks.
// Docs: https://docs.go-atlassian.io/jira-service-management-cloud/knowledgebase#get-articles
func (k *KnowledgebaseService) Gets(ctx context.Context, serviceDeskID int, query string, highlight bool, start,
limit int) (result *ArticlePageScheme, response *ResponseScheme, err error) {
func (k *KnowledgeBaseService) Gets(ctx context.Context, serviceDeskID int, query string, highlight bool, start,
limit int) (result *models.ArticlePageScheme, response *ResponseScheme, err error) {

params := url.Values{}
params.Add("start", strconv.Itoa(start))
Expand Down Expand Up @@ -80,40 +81,3 @@ func (k *KnowledgebaseService) Gets(ctx context.Context, serviceDeskID int, quer

return
}

type ArticlePageScheme struct {
Size int `json:"size"`
Start int `json:"start"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []*ArticleScheme `json:"values"`
Expands []string `json:"_expands"`
Links *ArticlePageLinkScheme `json:"_links"`
}

type ArticlePageLinkScheme struct {
Self string `json:"self"`
Base string `json:"base"`
Context string `json:"context"`
Next string `json:"next"`
Prev string `json:"prev"`
}

type ArticleScheme struct {
Title string `json:"title,omitempty"`
Excerpt string `json:"excerpt,omitempty"`
Source *ArticleSourceScheme `json:"source,omitempty"`
Content *ArticleContentScheme `json:"content,omitempty"`
}

type ArticleSourceScheme struct {
Type string `json:"type,omitempty"`
}

type ArticleContentScheme struct {
IframeSrc string `json:"iframeSrc,omitempty"`
}

var (
notQueryError = fmt.Errorf("error, please provide a valid query value")
)
6 changes: 3 additions & 3 deletions jira/sm/knowledgebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

func TestKnowledgebaseService_Search(t *testing.T) {
func TestKnowledgeBaseService_Search(t *testing.T) {

testCases := []struct {
name string
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestKnowledgebaseService_Search(t *testing.T) {
t.Fatal(err)
}

service := &KnowledgebaseService{client: mockClient}
service := &KnowledgeBaseService{client: mockClient}
gotResult, gotResponse, err := service.Search(testCase.context, testCase.query, testCase.highlight, testCase.start, testCase.limit)

if testCase.wantErr {
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestKnowledgebaseService_Gets(t *testing.T) {
t.Fatal(err)
}

service := &KnowledgebaseService{client: mockClient}
service := &KnowledgeBaseService{client: mockClient}
gotResult, gotResponse, err := service.Gets(testCase.context, testCase.serviceDeskID, testCase.query, testCase.highlight, testCase.start, testCase.limit)

if testCase.wantErr {
Expand Down
Loading

0 comments on commit 614b9a2

Please sign in to comment.