Commit b4645b55 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Parse date in NotificationPayloadParser

Use `DateTime.parse` to support dates of various formats
parent 178c00c5
......@@ -42,12 +42,14 @@ module Gitlab
payload[:hosts] && Array(payload[:hosts])
end
def starts_at
payload[:start_time].presence || current_time
def current_time
Time.current.change(usec: 0).rfc3339
end
def current_time
Time.now.change(usec: 0).rfc3339
def starts_at
DateTime.parse(payload[:start_time].to_s).rfc3339
rescue ArgumentError
current_time
end
end
end
......
......@@ -4,7 +4,7 @@ require 'fast_spec_helper'
describe Gitlab::Alerting::NotificationPayloadParser do
describe '.call' do
let(:starts_at) { Time.now.change(usec: 0) }
let(:starts_at) { Time.current.change(usec: 0) }
let(:payload) do
{
'title' => 'alert title',
......@@ -53,6 +53,16 @@ describe Gitlab::Alerting::NotificationPayloadParser do
end
end
context 'when the time is in unsupported format' do
before do
payload[:start_time] = 'invalid/date/format'
end
it 'sets startsAt to a currurrent time in RFC3339 format' do
expect(subject['startsAt']).to eq(starts_at.rfc3339)
end
end
context 'when payload is blank' do
let(:payload) { {} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment