Repository: ankitpokhrel/jira-cli
Pull request #380 (2022-06-02 19:06:51)

fix: Issue with query param in user assignment
Related #198



Patch:
diff --git a/README.md b/README.md
index d39c796..30bb9d8 100644
--- a/README.md
+++ b/README.md
@@ -28,9 +28,10 @@
     </p>
     <img align="center" alt="JiraCLI Demo" src=".github/assets/demo.gif" /><br/><br/>
     <p align="center">
-         Financial support from private and corporate sponsors ensures the tool's continued development.<br/>
-         Please <a href="https://opencollective.com/jira-cli#backers">consider becoming a sponsor</a> if you or your company rely on JiraCLI.
-         <br/><br/>
+         <sub>
+            Financial support from private and corporate sponsors ensures the tool's continued development.<br/>
+            Please <a href="https://opencollective.com/jira-cli#backers">consider sponsoring the project</a> if you or your company rely on JiraCLI.
+         </sub><br/><br/>
         <a href="https://opencollective.com/jira-cli#backers" target="_blank">
             <img src="https://opencollective.com/jira-cli/backers.svg" alt="jira-cli open collective badge">
         </a>
diff --git a/pkg/jira/user.go b/pkg/jira/user.go
index 98c4269..b793b8d 100644
--- a/pkg/jira/user.go
+++ b/pkg/jira/user.go
@@ -29,6 +29,17 @@ func (c *Client) UserSearch(opt *UserSearchOptions) ([]*User, error) {
 
 // UserSearchV2 search for user details using v2 version of the GET /user/assignable/search endpoint.
 func (c *Client) UserSearchV2(opt *UserSearchOptions) ([]*User, error) {
+	// The `username` query param is deprecated since Jira API v2 and is not available in v3.
+	// Since the` query` parameter doesn't seem to return expected results, we will use the
+	// `username` param in call to v2. Chances are the `query` param may stop working in
+	// later v2 updates so we might have to revisit this in the future. Note that the
+	// `username` param is not as flexible as the `query` param available in v3.
+	//
+	// See https://github.com/ankitpokhrel/jira-cli/issues/198
+	if opt.Query != "" && opt.Username == "" {
+		opt.Username = opt.Query
+		opt.Query = ""
+	}
 	return c.userSearch(opt, apiVersion2)
 }
 


Test patch:
diff --git a/pkg/jira/user_test.go b/pkg/jira/user_test.go
index d13107d..63ccf48 100644
--- a/pkg/jira/user_test.go
+++ b/pkg/jira/user_test.go
@@ -12,17 +12,10 @@ import (
 )
 
 func TestUserSearch(t *testing.T) {
-	var (
-		apiVersion2          bool
-		unexpectedStatusCode bool
-	)
+	var unexpectedStatusCode bool
 
 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		if apiVersion2 {
-			assert.Equal(t, "/rest/api/2/user/assignable/search", r.URL.Path)
-		} else {
-			assert.Equal(t, "/rest/api/3/user/assignable/search", r.URL.Path)
-		}
+		assert.Equal(t, "/rest/api/3/user/assignable/search", r.URL.Path)
 
 		if unexpectedStatusCode {
 			w.WriteHeader(400)
@@ -78,9 +71,61 @@ func TestUserSearch(t *testing.T) {
 
 	_, err = client.UserSearch(&UserSearchOptions{})
 	assert.Error(t, ErrInvalidSearchOption, err)
+}
+
+func TestUserSearchV2(t *testing.T) {
+	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, "/rest/api/2/user/assignable/search", r.URL.Path)
+
+		assert.Equal(t, url.Values{
+			"username":   []string{"doe"},
+			"startAt":    []string{"1"},
+			"maxResults": []string{"5"},
+			"accountId":  []string{"a123b"},
+		}, r.URL.Query())
 
-	apiVersion2 = true
+		resp, err := ioutil.ReadFile("./testdata/users.json")
+		assert.NoError(t, err)
+
+		w.Header().Set("Content-Type", "application/json")
+		w.WriteHeader(200)
+		_, _ = w.Write(resp)
+	}))
+	defer server.Close()
+
+	client := NewClient(Config{Server: server.URL}, WithTimeout(3*time.Second))
+
+	actual, err := client.UserSearchV2(&UserSearchOptions{
+		Query:      "doe",
+		AccountID:  "a123b",
+		StartAt:    1,
+		MaxResults: 5,
+	})
+	assert.NoError(t, err)
+
+	expected := []*User{
+		{
+			AccountID:   "5fb82376aca10c006949f35b",
+			Email:       "jane@domain.tld",
+			Name:        "janedoe",
+			DisplayName: "Jane Doe",
+			Active:      true,
+		},
+		{
+			AccountID:   "5fb82376aca10c006949f35c",
+			Email:       "jon@domain.tld",
+			DisplayName: "Jon Doe",
+			Active:      false,
+		},
+	}
+	assert.Equal(t, expected, actual)
 
-	_, err = client.UserSearchV2(&UserSearchOptions{})
-	assert.Error(t, &ErrUnexpectedResponse{}, err)
+	actual, err = client.UserSearchV2(&UserSearchOptions{
+		Query:      "doe",
+		AccountID:  "a123b",
+		StartAt:    1,
+		MaxResults: 5,
+	})
+	assert.NoError(t, err)
+	assert.Equal(t, expected, actual)
 }