Skip to content

Commit 67e6395

Browse files
fix(teamsnotifier): Sort update keys to avoid flaky tests (#2642)
* fix(teamsnotifier): Sort update keys to avoid flaky tests * add placeholder for hostname
1 parent 9901f0e commit 67e6395

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

notifier/microsoftteamsnotifier/notifier.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ func convertToMicrosoftTeamsMessage(diffCache notifier.DiffCache) string {
6969
value := diffCache.Updated[key]
7070
msgText += fmt.Sprintf("\n * ✏️ Flag **%s** updated", key)
7171
changelog, _ := diff.Diff(value.Before, value.After, diff.AllowTypeMismatch(true))
72+
73+
// sort the changelog by path
74+
sort.Slice(changelog, func(i, j int) bool {
75+
return strings.Join(changelog[i].Path, ".") < strings.Join(changelog[j].Path, ".")
76+
})
77+
7278
for _, change := range changelog {
7379
if change.Type == "update" {
7480
msgText += fmt.Sprintf("\n * %s: %s => %s", strings.Join(change.Path, "."),

notifier/microsoftteamsnotifier/notifier_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package microsoftteamsnotifier
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"net/http"
78
"os"
@@ -100,9 +101,9 @@ func TestMicrosoftTeamsNotifier_Notify(t *testing.T) {
100101
},
101102
After: &flag.InternalFlag{
102103
Variations: &map[string]*interface{}{
103-
"Default": testconvert.Interface("strDefault"),
104-
"False": testconvert.Interface("strFalse"),
105-
"True": testconvert.Interface("strTrue"),
104+
"Default": testconvert.Interface(true),
105+
"False": testconvert.Interface(false),
106+
"True": testconvert.Interface(true),
106107
},
107108
Rules: &[]flag.Rule{
108109
{
@@ -173,6 +174,7 @@ func TestMicrosoftTeamsNotifier_Notify(t *testing.T) {
173174
hostname, _ := os.Hostname()
174175
wantBody, err := os.ReadFile(tt.want)
175176
require.NoError(t, err)
177+
fmt.Println(tt.roundTripper.requestBody)
176178
assert.JSONEq(t, strings.ReplaceAll(string(wantBody), "{{hostname}}", hostname), tt.roundTripper.requestBody)
177179
})
178180
}

notifier/microsoftteamsnotifier/testdata/should_call_webhook_and_have_valid_results.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
{
2020
"type": "TextBlock",
21-
"text": "Changes detected in your feature flag file on: **{{hostname}}**\n * ❌ Flag **test-flag** deleted\n * 🆕 Flag **test-flag3** created\n * ✏️ Flag **test-flag2** updated\n * Variations.Default: false =\u003e \"strDefault\"\n * Variations.False: false =\u003e \"strFalse\"\n * Variations.True: true =\u003e \"strTrue\"\n * Rules: nil =\u003e (*[]flag.Rule){flag.Rule{Name:(*string)(\"rule1\"), Query:(*string)(\"key eq \\\"not-a-ke\\\"\"), VariationResult:(*string)(nil), Percentages:(*map[string]float64){\"False\":20, \"True\":80}, ProgressiveRollout:(*flag.ProgressiveRollout)(nil), Disable:(*bool)(nil)}}\n * DefaultRule.VariationResult: nil =\u003e (*string)(\"Default\")\n * DefaultRule.Percentages: (*map[string]float64){\"False\":0, \"True\":100} =\u003e nil\n * Experimentation: (*flag.ExperimentationRollout){Start:(*time.Time){wall:0, ext:63230976200, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}, End:(*time.Time){wall:0, ext:63230967800, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}} =\u003e nil\n * TrackEvents: nil =\u003e (*bool)(false)\n * Disable: nil =\u003e (*bool)(true)\n * Version: nil =\u003e (*string)(\"1.1\")",
21+
"text": "Changes detected in your feature flag file on: **{{hostname}}**\n * ❌ Flag **test-flag** deleted\n * 🆕 Flag **test-flag3** created\n * ✏️ Flag **test-flag2** updated\n * DefaultRule.Percentages: (*map[string]float64){\"False\":0, \"True\":100} =\u003e nil\n * DefaultRule.VariationResult: nil =\u003e (*string)(\"Default\")\n * Disable: nil =\u003e (*bool)(true)\n * Experimentation: (*flag.ExperimentationRollout){Start:(*time.Time){wall:0, ext:63230976200, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}, End:(*time.Time){wall:0, ext:63230967800, loc:(*time.Location){name:\"\", zone:[]time.zone(nil), tx:[]time.zoneTrans(nil), extend:\"\", cacheStart:0, cacheEnd:0, cacheZone:(*time.zone)(nil)}}} =\u003e nil\n * Rules: nil =\u003e (*[]flag.Rule){flag.Rule{Name:(*string)(\"rule1\"), Query:(*string)(\"key eq \\\"not-a-ke\\\"\"), VariationResult:(*string)(nil), Percentages:(*map[string]float64){\"False\":20, \"True\":80}, ProgressiveRollout:(*flag.ProgressiveRollout)(nil), Disable:(*bool)(nil)}}\n * TrackEvents: nil =\u003e (*bool)(false)\n * Variations.Default: false =\u003e true\n * Version: nil =\u003e (*string)(\"1.1\")",
2222
"wrap": true
2323
}
2424
],

0 commit comments

Comments
 (0)