From 7ade445cf08d745f50ff197cab7e290b10795f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 11 Nov 2019 18:25:16 +0000 Subject: [PATCH] fix(ui): prefer author from headers when acking alerts --- ui/src/Components/AlertAck/index.js | 4 +- ui/src/Components/AlertAck/index.test.js | 59 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/ui/src/Components/AlertAck/index.js b/ui/src/Components/AlertAck/index.js index 603f53ae6..9f517e7af 100644 --- a/ui/src/Components/AlertAck/index.js +++ b/ui/src/Components/AlertAck/index.js @@ -207,7 +207,9 @@ const AlertAck = observer( toJS(group), toJS(clusterMembers), toJS(alertStore.settings.values.alertAcknowledgement.durationSeconds), - silenceFormStore.data.author !== "" + alertStore.settings.values.silenceForm.author !== "" + ? alertStore.settings.values.silenceForm.author + : silenceFormStore.data.author !== "" ? toJS(silenceFormStore.data.author) : toJS(alertStore.settings.values.alertAcknowledgement.author), toJS(alertStore.settings.values.alertAcknowledgement.commentPrefix) diff --git a/ui/src/Components/AlertAck/index.test.js b/ui/src/Components/AlertAck/index.test.js index 56579e1c6..b36a4bf2f 100644 --- a/ui/src/Components/AlertAck/index.test.js +++ b/ui/src/Components/AlertAck/index.test.js @@ -200,6 +200,65 @@ describe("", () => { }); }); + it("uses author from alertStore if present", () => { + alertStore.settings.values.silenceForm.author = "john@example.com"; + alertStore.settings.values.alertAcknowledgement.durationSeconds = 222; + alertStore.settings.values.alertAcknowledgement.author = "me"; + alertStore.settings.values.alertAcknowledgement.commentPrefix = "FOO:"; + MountAndClick(); + expect(JSON.parse(fetch.mock.calls[0][1].body)).toEqual({ + comment: + "FOO: This alert was acknowledged using karma on Tue Feb 01 2000 00:00:00 GMT+0000", + createdBy: "john@example.com", + endsAt: "2000-02-01T00:03:42.000Z", + matchers: [ + { isRegex: false, name: "alertname", value: "Fake Alert" }, + { isRegex: true, name: "foo", value: "(bar|baz)" } + ], + startsAt: "2000-02-01T00:00:00.000Z" + }); + }); + + it("uses author from silenceFormStore if alertStore is empty", () => { + alertStore.settings.values.silenceForm.author = ""; + alertStore.settings.values.alertAcknowledgement.durationSeconds = 222; + alertStore.settings.values.alertAcknowledgement.author = "me"; + alertStore.settings.values.alertAcknowledgement.commentPrefix = "FOO:"; + silenceFormStore.data.author = "bob@example.com"; + MountAndClick(); + expect(JSON.parse(fetch.mock.calls[0][1].body)).toEqual({ + comment: + "FOO: This alert was acknowledged using karma on Tue Feb 01 2000 00:00:00 GMT+0000", + createdBy: "bob@example.com", + endsAt: "2000-02-01T00:03:42.000Z", + matchers: [ + { isRegex: false, name: "alertname", value: "Fake Alert" }, + { isRegex: true, name: "foo", value: "(bar|baz)" } + ], + startsAt: "2000-02-01T00:00:00.000Z" + }); + }); + + it("uses default author as fallback", () => { + alertStore.settings.values.silenceForm.author = ""; + alertStore.settings.values.alertAcknowledgement.durationSeconds = 222; + alertStore.settings.values.alertAcknowledgement.author = "me"; + alertStore.settings.values.alertAcknowledgement.commentPrefix = "FOO:"; + silenceFormStore.data.author = ""; + MountAndClick(); + expect(JSON.parse(fetch.mock.calls[0][1].body)).toEqual({ + comment: + "FOO: This alert was acknowledged using karma on Tue Feb 01 2000 00:00:00 GMT+0000", + createdBy: "me", + endsAt: "2000-02-01T00:03:42.000Z", + matchers: [ + { isRegex: false, name: "alertname", value: "Fake Alert" }, + { isRegex: true, name: "foo", value: "(bar|baz)" } + ], + startsAt: "2000-02-01T00:00:00.000Z" + }); + }); + it("[v1] sends POST request to /api/v1/silences", () => { MountAndClick(); const uri = fetch.mock.calls[0][0];