diff --git a/pkg/infra/models/jira_issue_operations.go b/pkg/infra/models/jira_issue_operations.go index ca8663b7..ca5577cf 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 +} diff --git a/pkg/infra/models/jira_issue_operations_test.go b/pkg/infra/models/jira_issue_operations_test.go index 22cd330f..fe19b145 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)) + } + }) + } +}