Repository: ctreminiom/go-atlassian
Pull request #48 (2021-06-12 06:58:17)

Dev


Patch:
diff --git a/jira/examples/myself/details/details.go b/jira/examples/myself/details/details.go
new file mode 100644
index 0000000..96bde4c
--- /dev/null
+++ b/jira/examples/myself/details/details.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+	"context"
+	"fmt"
+	"github.com/ctreminiom/go-atlassian/jira"
+	"log"
+	"os"
+)
+
+func main() {
+
+	var (
+		host  = os.Getenv("HOST")
+		mail  = os.Getenv("MAIL")
+		token = os.Getenv("TOKEN")
+	)
+
+	atlassian, err := jira.New(nil, host)
+	if err != nil {
+		return
+	}
+
+	atlassian.Auth.SetBasicAuth(mail, token)
+
+	currentUser, response, err := atlassian.MySelf.Details(context.Background(), nil)
+	if err != nil {
+		if response != nil {
+			log.Println("Response HTTP Response", string(response.BodyAsBytes))
+		}
+		log.Fatal(err)
+	}
+
+	log.Println("Response HTTP Code", response.StatusCode)
+	log.Println("HTTP Endpoint Used", response.Endpoint)
+	log.Println(currentUser.DisplayName, currentUser.Active, currentUser.AccountID)
+
+	fmt.Println(string(response.BodyAsBytes))
+}
diff --git a/jira/jira.go b/jira/jira.go
index d8ac49b..b87a686 100644
--- a/jira/jira.go
+++ b/jira/jira.go
@@ -32,6 +32,7 @@ type Client struct {
 	Server     *ServerService
 	Task       *TaskService
 	User       *UserService
+	MySelf     *MySelfService
 
 	//Service Management Module
 	ServiceManagement *sm.Client
@@ -163,6 +164,8 @@ func New(httpClient *http.Client, site string) (client *Client, err error) {
 		Search: &UserSearchService{client: client},
 	}
 
+	client.MySelf = &MySelfService{client: client}
+
 	return
 }
 
diff --git a/jira/mocks/get-myselft.json b/jira/mocks/get-myselft.json
new file mode 100644
index 0000000..08a5890
--- /dev/null
+++ b/jira/mocks/get-myselft.json
@@ -0,0 +1,46 @@
+{
+  "self": "https://ctreminiom.atlassian.net/rest/api/3/user?accountId=5b86be50b8e3cb5895860d6d",
+  "accountId": "5b86be50b8e3cb5895860d6d",
+  "emailAddress": "ctreminiom079@gmail.com",
+  "avatarUrls": {
+    "48x48": "https://secure.gravatar.com/avatar/b830f79c6cc32dcbcb9842f98cd3d3cd?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FCT-6.png",
+    "24x24": "https://secure.gravatar.com/avatar/b830f79c6cc32dcbcb9842f98cd3d3cd?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FCT-6.png",
+    "16x16": "https://secure.gravatar.com/avatar/b830f79c6cc32dcbcb9842f98cd3d3cd?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FCT-6.png",
+    "32x32": "https://secure.gravatar.com/avatar/b830f79c6cc32dcbcb9842f98cd3d3cd?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FCT-6.png"
+  },
+  "displayName": "Carlos Treminio",
+  "active": true,
+  "timeZone": "America/Guatemala",
+  "locale": "en_US",
+  "groups": {
+    "size": 3,
+    "items": [
+      {
+        "name": "jira-administrators",
+        "self": "https://ctreminiom.atlassian.net/rest/api/3/group?groupname=jira-administrators"
+      },
+      {
+        "name": "jira-software-users",
+        "self": "https://ctreminiom.atlassian.net/rest/api/3/group?groupname=jira-software-users"
+      },
+      {
+        "name": "site-admins",
+        "self": "https://ctreminiom.atlassian.net/rest/api/3/group?groupname=site-admins"
+      }
+    ]
+  },
+  "applicationRoles": {
+    "size": 2,
+    "items": [
+      {
+        "key": "jira-servicedesk",
+        "name": "Jira Service Desk"
+      },
+      {
+        "key": "jira-software",
+        "name": "Jira Software"
+      }
+    ]
+  },
+  "expand": "groups,applicationRoles"
+}
\ No newline at end of file
diff --git a/jira/myself.go b/jira/myself.go
new file mode 100644
index 0000000..a492de8
--- /dev/null
+++ b/jira/myself.go
@@ -0,0 +1,59 @@
+package jira
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"net/http"
+	"net/url"
+)
+
+type MySelfService struct{ client *Client }
+
+// Details returns details for the current user.
+// Docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-myself/#api-rest-api-3-myself-get
+func (m *MySelfService) Details(ctx context.Context, expands []string) (result *UserScheme, response *Response, err error) {
+
+	params := url.Values{}
+
+	var expand string
+	for index, value := range expands {
+
+		if index == 0 {
+			expand = value
+			continue
+		}
+
+		expand += "," + value
+	}
+
+	if len(expand) != 0 {
+		params.Add("expand", expand)
+	}
+
+	var endpoint string
+	if params.Encode() != "" {
+		endpoint = fmt.Sprintf("rest/api/3/myself?%v", params.Encode())
+	} else {
+		endpoint = "rest/api/3/myself"
+	}
+
+	request, err := m.client.newRequest(ctx, http.MethodGet, endpoint, nil)
+	if err != nil {
+		return
+	}
+
+	request.Header.Set("Accept", "application/json")
+
+	response, err = m.client.Do(request)
+	if err != nil {
+		return
+	}
+
+	result = new(UserScheme)
+	if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
+		return
+	}
+
+	return
+}
diff --git a/jira/permissionScheme.go b/jira/permissionScheme.go
index 61a44ce..a8b42f0 100644
--- a/jira/permissionScheme.go
+++ b/jira/permissionScheme.go
@@ -92,10 +92,6 @@ func (p *PermissionSchemeService) Get(ctx context.Context, permissionSchemeID in
 // Docs: https://docs.go-atlassian.io/jira-software-cloud/permissions/scheme#delete-permission-scheme
 func (p *PermissionSchemeService) Delete(ctx context.Context, permissionSchemeID int) (response *Response, err error) {
 
-	if permissionSchemeID == 0 {
-		return nil, fmt.Errorf("error!, please provide a valid permissionSchemeID value")
-	}
-
 	var endpoint = fmt.Sprintf("rest/api/3/permissionscheme/%v", permissionSchemeID)
 
 	request, err := p.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
@@ -151,10 +147,6 @@ func (p *PermissionSchemeService) Create(ctx context.Context, payload *Permissio
 // Docs: https://docs.go-atlassian.io/jira-software-cloud/permissions/scheme#update-permission-scheme
 func (p *PermissionSchemeService) Update(ctx context.Context, schemeID int, payload *PermissionSchemeScheme) (result *PermissionSchemeScheme, response *Response, err error) {
 
-	if schemeID == 0 {
-		return nil, nil, fmt.Errorf("error, please provide a valid schemeID value")
-	}
-
 	if payload == nil {
 		return nil, nil, fmt.Errorf("error, please provide a payload pointer")
 	}


Test patch:
diff --git a/jira/myself_test.go b/jira/myself_test.go
new file mode 100644
index 0000000..622b9cd
--- /dev/null
+++ b/jira/myself_test.go
@@ -0,0 +1,161 @@
+package jira
+
+import (
+	"context"
+	"fmt"
+	"github.com/stretchr/testify/assert"
+	"net/http"
+	"net/url"
+	"testing"
+)
+
+func TestMySelfService_Details(t *testing.T) {
+
+	testCases := []struct {
+		name               string
+		expands            []string
+		mockFile           string
+		wantHTTPMethod     string
+		endpoint           string
+		context            context.Context
+		wantHTTPCodeReturn int
+		wantErr            bool
+	}{
+		{
+			name:               "GetMySelfDetailsWhenTheParamsAreCorrect",
+			expands:            []string{"applicationRoles", "groups"},
+			mockFile:           "./mocks/get-myselft.json",
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/myself?expand=applicationRoles%2Cgroups",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "GetMySelfDetailsWhenTheContextIsNil",
+			expands:            []string{"applicationRoles", "groups"},
+			mockFile:           "./mocks/get-myselft.json",
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/myself?expand=applicationRoles%2Cgroups",
+			context:            nil,
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "GetMySelfDetailsWhenTheExpandsAreNotSet",
+			expands:            nil,
+			mockFile:           "./mocks/get-myselft.json",
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/myself",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            false,
+		},
+
+		{
+			name:               "GetMySelfDetailsWhenTheResponseBodyIsEmpty",
+			expands:            []string{"applicationRoles", "groups"},
+			mockFile:           "./mocks/empty_json.json",
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/myself?expand=applicationRoles%2Cgroups",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "GetMySelfDetailsWhenTheRequestMethodIsIncorrect",
+			expands:            []string{"applicationRoles", "groups"},
+			mockFile:           "./mocks/get-myselft.json",
+			wantHTTPMethod:     http.MethodHead,
+			endpoint:           "/rest/api/3/myself?expand=applicationRoles%2Cgroups",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusOK,
+			wantErr:            true,
+		},
+
+		{
+			name:               "GetMySelfDetailsWhenTheStatusCodeIsIncorrect",
+			expands:            []string{"applicationRoles", "groups"},
+			mockFile:           "./mocks/get-myselft.json",
+			wantHTTPMethod:     http.MethodGet,
+			endpoint:           "/rest/api/3/myself?expand=applicationRoles%2Cgroups",
+			context:            context.Background(),
+			wantHTTPCodeReturn: http.StatusBadRequest,
+			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 := &MySelfService{client: mockClient}
+
+			gotResult, gotResponse, err := i.Details(testCase.context, testCase.expands)
+
+			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)
+
+				for _, group := range gotResult.Groups.Items {
+					t.Log(group.Self, group.Name)
+				}
+
+				for _, item := range gotResult.ApplicationRoles.Items {
+					t.Log(item.Key, item.Name)
+				}
+			}
+		})
+
+	}
+
+}
diff --git a/jira/permissionScheme_test.go b/jira/permissionScheme_test.go
index 25ace65..a197fed 100644
--- a/jira/permissionScheme_test.go
+++ b/jira/permissionScheme_test.go
@@ -299,16 +299,6 @@ func TestPermissionSchemeService_Delete(t *testing.T) {
 			wantErr:            false,
 		},
 
-		{
-			name:               "DeletePermissionSchemeWhenThePermissionSchemeIDIsNotProvided",
-			permissionSchemeID: 0,
-			wantHTTPMethod:     http.MethodDelete,
-			endpoint:           "/rest/api/3/permissionscheme/1000",
-			context:            context.Background(),
-			wantHTTPCodeReturn: http.StatusNoContent,
-			wantErr:            true,
-		},
-
 		{
 			name:               "DeletePermissionSchemeWhenTheEndpointIsIncorrect",
 			permissionSchemeID: 1000,
@@ -436,17 +426,6 @@ func TestPermissionSchemeService_Get(t *testing.T) {
 			wantErr:            false,
 		},
 
-		{
-			name:               "GetPermissionSchemeWhenThePermissionSchemeIDIsNotProvided",
-			permissionSchemeID: 0,
-			wantHTTPMethod:     http.MethodGet,
-			endpoint:           "/rest/api/3/permissionscheme/1000",
-			mockFile:           "./mocks/get-permission-scheme.json",
-			context:            context.Background(),
-			wantHTTPCodeReturn: http.StatusOK,
-			wantErr:            true,
-		},
-
 		{
 			name:               "GetPermissionSchemeWhenTheEndpointIsIncorrect",
 			permissionSchemeID: 1000,
@@ -729,31 +708,6 @@ func TestPermissionSchemeService_Update(t *testing.T) {
 			wantErr:            false,
 		},
 
-		{
-			name:     "UpdatePermissionSchemeWhenThePermissionSchemeIDIsNotProvided",
-			schemeID: 0,
-			payload: &PermissionSchemeScheme{
-				Name:        "EF Permission Scheme - UPDATED",
-				Description: "EF Permission Scheme description - UPDATED",
-
-				Permissions: []*PermissionGrantScheme{
-					{
-						Permission: "CLOSE_ISSUES",
-						Holder: &PermissionGrantHolderScheme{
-							Parameter: "jira-administrators-system",
-							Type:      "group",
-						},
-					},
-				},
-			},
-			mockFile:           "./mocks/get-permission-scheme.json",
-			wantHTTPMethod:     http.MethodPut,
-			endpoint:           "/rest/api/3/permissionscheme/1000",
-			context:            context.Background(),
-			wantHTTPCodeReturn: http.StatusOK,
-			wantErr:            true,
-		},
-
 		{
 			name:               "UpdatePermissionSchemeWhenThePayloadIsNotProvided",
 			schemeID:           1000,