Skip to content

Commit d5ca1e5

Browse files
authored
🐛 Fix MergeCustomFields and MergeOperations returning empty map when input is nil (#437)
When CustomFields and Operations are both nil, the standard fields set in the issue scheme were being lost. This happened because MergeCustomFields and MergeOperations returned an empty map instead of the issue scheme fields. Now these methods return the issue scheme as a map when input is nil/empty, preserving standard fields like Resolution when moving issues. Added test cases for the nil input scenario. Fixes #436
1 parent 3422ce8 commit d5ca1e5

File tree

4 files changed

+114
-26
lines changed

4 files changed

+114
-26
lines changed

pkg/infra/models/jira_issue_v2.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ type IssueSchemeV2 struct {
1919

2020
// MergeCustomFields merges custom fields into the issue scheme.
2121
// It returns a map representation of the issue scheme with the merged fields.
22-
// If the provided fields are nil or empty, it returns an error.
22+
// If the provided fields are nil or empty, it returns the issue scheme as a map.
2323
func (i *IssueSchemeV2) MergeCustomFields(fields *CustomFields) (map[string]interface{}, error) {
2424

25-
if fields == nil || len(fields.Fields) == 0 {
26-
return map[string]interface{}{}, nil
27-
}
28-
2925
// Convert the IssueScheme struct to map[string]interface{}
3026
issueSchemeAsBytes, err := json.Marshal(i)
3127
if err != nil {
@@ -37,6 +33,10 @@ func (i *IssueSchemeV2) MergeCustomFields(fields *CustomFields) (map[string]inte
3733
return nil, err
3834
}
3935

36+
if fields == nil || len(fields.Fields) == 0 {
37+
return issueSchemeAsMap, nil
38+
}
39+
4040
// For each customField created, merge it into the eAsMap
4141
for _, customField := range fields.Fields {
4242
if err := mergo.Merge(&issueSchemeAsMap, customField, mergo.WithOverride); err != nil {
@@ -49,20 +49,16 @@ func (i *IssueSchemeV2) MergeCustomFields(fields *CustomFields) (map[string]inte
4949

5050
// MergeOperations merges operations into the issue scheme.
5151
// It returns a map representation of the issue scheme with the merged operations.
52-
// If the provided operations are nil or empty, it returns an error.
52+
// If the provided operations are nil or empty, it returns the issue scheme as a map.
5353
//
5454
// Parameters:
5555
// - operations: A pointer to UpdateOperations containing the operations to be merged.
5656
//
5757
// Returns:
5858
// - A map[string]interface{} representing the issue scheme with the merged operations.
59-
// - An error if the operations are nil, empty, or if there is an issue during the merging process.
59+
// - An error if there is an issue during the merging process.
6060
func (i *IssueSchemeV2) MergeOperations(operations *UpdateOperations) (map[string]interface{}, error) {
6161

62-
if operations == nil || len(operations.Fields) == 0 {
63-
return map[string]interface{}{}, nil
64-
}
65-
6662
// Convert the IssueScheme struct to map[string]interface{}
6763
issueSchemeAsBytes, err := json.Marshal(i)
6864
if err != nil {
@@ -74,6 +70,10 @@ func (i *IssueSchemeV2) MergeOperations(operations *UpdateOperations) (map[strin
7470
return nil, err
7571
}
7672

73+
if operations == nil || len(operations.Fields) == 0 {
74+
return issueSchemeAsMap, nil
75+
}
76+
7777
// For each customField created, merge it into the eAsMap
7878
for _, customField := range operations.Fields {
7979
if err := mergo.Merge(&issueSchemeAsMap, customField, mergo.WithOverride); err != nil {

pkg/infra/models/jira_issue_v2_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ func TestIssueSchemeV2_MergeCustomFields(t *testing.T) {
4848
wantErr: false,
4949
Err: nil,
5050
},
51+
{
52+
name: "when custom fields is nil, should preserve issue scheme fields",
53+
fields: fields{
54+
ID: "10001",
55+
Key: "TEST-1",
56+
Fields: &IssueFieldsSchemeV2{
57+
Summary: "Test Summary",
58+
},
59+
},
60+
args: args{
61+
fields: nil,
62+
},
63+
want: map[string]interface{}{
64+
"id": "10001",
65+
"key": "TEST-1",
66+
"fields": map[string]interface{}{
67+
"summary": "Test Summary",
68+
},
69+
},
70+
wantErr: false,
71+
Err: nil,
72+
},
5173
}
5274
for _, testCase := range testCases {
5375
t.Run(testCase.name, func(t *testing.T) {
@@ -121,6 +143,28 @@ func TestIssueSchemeV2_MergeOperations(t *testing.T) {
121143
wantErr: false,
122144
Err: nil,
123145
},
146+
{
147+
name: "when operations is nil, should preserve issue scheme fields",
148+
fields: fields{
149+
ID: "10001",
150+
Key: "TEST-1",
151+
Fields: &IssueFieldsSchemeV2{
152+
Summary: "Test Summary",
153+
},
154+
},
155+
args: args{
156+
operations: nil,
157+
},
158+
want: map[string]interface{}{
159+
"id": "10001",
160+
"key": "TEST-1",
161+
"fields": map[string]interface{}{
162+
"summary": "Test Summary",
163+
},
164+
},
165+
wantErr: false,
166+
Err: nil,
167+
},
124168
}
125169
for _, testCase := range testCases {
126170
t.Run(testCase.name, func(t *testing.T) {

pkg/infra/models/jira_issue_v3.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ type IssueScheme struct {
1919

2020
// MergeCustomFields merges custom fields into the issue scheme.
2121
// It returns a map representation of the issue scheme with the merged fields.
22-
// If the provided fields are nil or empty, it returns an error.
22+
// If the provided fields are nil or empty, it returns the issue scheme as a map.
2323
func (i *IssueScheme) MergeCustomFields(fields *CustomFields) (map[string]interface{}, error) {
2424

25-
if fields == nil || len(fields.Fields) == 0 {
26-
return map[string]interface{}{}, nil
27-
}
28-
29-
//Convert the IssueScheme struct to map[string]interface{}
25+
// Convert the IssueScheme struct to map[string]interface{}
3026
issueSchemeAsBytes, err := json.Marshal(i)
3127
if err != nil {
3228
return nil, err
@@ -37,7 +33,11 @@ func (i *IssueScheme) MergeCustomFields(fields *CustomFields) (map[string]interf
3733
return nil, err
3834
}
3935

40-
//For each customField created, merge it into the eAsMap
36+
if fields == nil || len(fields.Fields) == 0 {
37+
return issueSchemeAsMap, nil
38+
}
39+
40+
// For each customField created, merge it into the eAsMap
4141
for _, customField := range fields.Fields {
4242
if err := mergo.Merge(&issueSchemeAsMap, customField, mergo.WithOverride); err != nil {
4343
return nil, err
@@ -49,21 +49,17 @@ func (i *IssueScheme) MergeCustomFields(fields *CustomFields) (map[string]interf
4949

5050
// MergeOperations merges operations into the issue scheme.
5151
// It returns a map representation of the issue scheme with the merged operations.
52-
// If the provided operations are nil or empty, it returns an error.
52+
// If the provided operations are nil or empty, it returns the issue scheme as a map.
5353
//
5454
// Parameters:
5555
// - operations: A pointer to UpdateOperations containing the operations to be merged.
5656
//
5757
// Returns:
5858
// - A map[string]interface{} representing the issue scheme with the merged operations.
59-
// - An error if the operations are nil, empty, or if there is an issue during the merging process.
59+
// - An error if there is an issue during the merging process.
6060
func (i *IssueScheme) MergeOperations(operations *UpdateOperations) (map[string]interface{}, error) {
6161

62-
if operations == nil || len(operations.Fields) == 0 {
63-
return map[string]interface{}{}, nil
64-
}
65-
66-
//Convert the IssueScheme struct to map[string]interface{}
62+
// Convert the IssueScheme struct to map[string]interface{}
6763
issueSchemeAsBytes, err := json.Marshal(i)
6864
if err != nil {
6965
return nil, err
@@ -74,7 +70,11 @@ func (i *IssueScheme) MergeOperations(operations *UpdateOperations) (map[string]
7470
return nil, err
7571
}
7672

77-
//For each customField created, merge it into the eAsMap
73+
if operations == nil || len(operations.Fields) == 0 {
74+
return issueSchemeAsMap, nil
75+
}
76+
77+
// For each customField created, merge it into the eAsMap
7878
for _, customField := range operations.Fields {
7979
if err := mergo.Merge(&issueSchemeAsMap, customField, mergo.WithOverride); err != nil {
8080
return nil, err

pkg/infra/models/jira_issue_v3_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ func TestIssueScheme_MergeCustomFields(t *testing.T) {
4747
wantErr: false,
4848
Err: nil,
4949
},
50+
{
51+
name: "when custom fields is nil, should preserve issue scheme fields",
52+
fields: fields{
53+
ID: "10001",
54+
Key: "TEST-1",
55+
Fields: &IssueFieldsScheme{
56+
Summary: "Test Summary",
57+
},
58+
},
59+
args: args{
60+
fields: nil,
61+
},
62+
want: map[string]interface{}{
63+
"id": "10001",
64+
"key": "TEST-1",
65+
"fields": map[string]interface{}{
66+
"summary": "Test Summary",
67+
},
68+
},
69+
wantErr: false,
70+
Err: nil,
71+
},
5072
}
5173
for _, testCase := range testCases {
5274
t.Run(testCase.name, func(t *testing.T) {
@@ -120,6 +142,28 @@ func TestIssueScheme_MergeOperations(t *testing.T) {
120142
wantErr: false,
121143
Err: nil,
122144
},
145+
{
146+
name: "when operations is nil, should preserve issue scheme fields",
147+
fields: fields{
148+
ID: "10001",
149+
Key: "TEST-1",
150+
Fields: &IssueFieldsScheme{
151+
Summary: "Test Summary",
152+
},
153+
},
154+
args: args{
155+
operations: nil,
156+
},
157+
want: map[string]interface{}{
158+
"id": "10001",
159+
"key": "TEST-1",
160+
"fields": map[string]interface{}{
161+
"summary": "Test Summary",
162+
},
163+
},
164+
wantErr: false,
165+
Err: nil,
166+
},
123167
}
124168
for _, testCase := range testCases {
125169
t.Run(testCase.name, func(t *testing.T) {

0 commit comments

Comments
 (0)