Repository: ctreminiom/go-atlassian (go)

Issue:
Question on adding fixVersions
Hello,

Thank you for your project. It is very useful!

I've a question; I would like to update the fixVersions field of an issue. I thought I could use the same approach as described in the cookbook 'edit jira issue (implicit)'. So using a AddStringOperation like in:

```
	var operations = &models.UpdateOperations{}
	err = operations.AddArrayOperation("fixVersions", map[string]string{
		"v1": "add",
	})

```
However I get an error that I need to "Specify a valid version 'id' or 'name' for fixVersions"".

In examples from other languages I see people explicitly including 'name'. However this is not possible with an arrayOperation as it expects a map[string]string.

Do you know how to set the fixVersions field?

thanks

Stephan


PS in the cookbook code it says "atlassian", but it should say "client"

Pull request:
:sparkles: Support the possibility to add multiobject on the issue u…
…pdate with operations

1. Fixes #255

Patch:
diff --git a/pkg/infra/models/jira_issue_operations.go b/pkg/infra/models/jira_issue_operations.go
index ca8663b..ca5577c 100644
--- a/pkg/infra/models/jira_issue_operations.go
+++ b/pkg/infra/models/jira_issue_operations.go
@@ -57,3 +57,22 @@ func (u *UpdateOperations) AddStringOperation(customFieldID, operation, value st
 
 	return nil
 }
+
+func (u *UpdateOperations) AddMultiRawOperation(customFieldID string, mappings []map[string]interface{}) error {
+
+	if len(customFieldID) == 0 {
+		return ErrNoFieldIDError
+	}
+
+	var operations []map[string]interface{}
+	operations = append(operations, mappings...)
+
+	var fieldNode = map[string]interface{}{}
+	fieldNode[customFieldID] = operations
+
+	var updateNode = map[string]interface{}{}
+	updateNode["update"] = fieldNode
+
+	u.Fields = append(u.Fields, updateNode)
+	return nil
+}


Test patch:
diff --git a/pkg/infra/models/jira_issue_operations_test.go b/pkg/infra/models/jira_issue_operations_test.go
index 22cd330..fe19b14 100644
--- a/pkg/infra/models/jira_issue_operations_test.go
+++ b/pkg/infra/models/jira_issue_operations_test.go
@@ -1,6 +1,9 @@
 package models
 
 import (
+	"encoding/json"
+	"fmt"
+	"github.com/stretchr/testify/assert"
 	"reflect"
 	"testing"
 )
@@ -141,3 +144,79 @@ func TestUpdateOperations_AddStringOperation(t *testing.T) {
 		})
 	}
 }
+
+func TestUpdateOperations_AddMultiRawOperation(t *testing.T) {
+
+	expectedJson := `[{"update":{"custom_field_id":[{"add":{"id":"10001"}},{"remove":{"name":"Version 00"}},{"add":{"id":"1010"}}]}}]`
+
+	type fields struct {
+		Fields []map[string]interface{}
+	}
+	type args struct {
+		customFieldID string
+		mappings      []map[string]interface{}
+	}
+
+	tests := []struct {
+		name        string
+		fields      fields
+		args        args
+		wantErr     assert.ErrorAssertionFunc
+		expectedErr bool
+	}{
+		{
+			name:   "when the parameters are correct",
+			fields: fields{},
+			args: args{
+				customFieldID: "custom_field_id",
+				mappings: []map[string]interface{}{
+					{
+						"add": map[string]interface{}{
+							"id": "10001",
+						},
+					},
+					{
+						"remove": map[string]interface{}{
+							"name": "Version 00",
+						},
+					},
+					{
+						"add": map[string]interface{}{
+							"id": "1010",
+						},
+					},
+				},
+			},
+			wantErr: assert.NoError,
+		},
+
+		{
+			name:   "when the customfieldID is not provided",
+			fields: fields{},
+			args: args{
+				customFieldID: "",
+			},
+			wantErr:     assert.Error,
+			expectedErr: true,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			u := &UpdateOperations{
+				Fields: tt.fields.Fields,
+			}
+
+			tt.wantErr(t, u.AddMultiRawOperation(tt.args.customFieldID, tt.args.mappings),
+				fmt.Sprintf("AddMultiRawOperation(%v, %v)", tt.args.customFieldID, tt.args.mappings))
+
+			if !tt.expectedErr {
+				actualJson, err := json.Marshal(u.Fields)
+				if err != nil {
+					t.Fatal(err)
+				}
+
+				assert.Equal(t, expectedJson, string(actualJson))
+			}
+		})
+	}
+}