Commit 5d4183e5 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Convert seconds to minutes and hours on chat notifations

In the pipeline message, the duration of the pipeline was shown as:

> gitlab-org/gitlab-ee: Pipeline #8002259 of master branch by Douwe Maan
failed in 8612 seconds

But this many seconds only gave me a clue how long the pipeline took,
so now this will be shown as something like: 02:20:12 which gives a much
better measure of time.
parent 0f976727
...@@ -50,5 +50,16 @@ module ChatMessage ...@@ -50,5 +50,16 @@ module ChatMessage
def link(text, url) def link(text, url)
"[#{text}](#{url})" "[#{text}](#{url})"
end end
def pretty_duration(seconds)
parse_string =
if duration < 1.hour
'%M:%S'
else
'%H:%M:%S'
end
Time.at(seconds).utc.strftime(parse_string)
end
end end
end end
...@@ -15,7 +15,7 @@ module ChatMessage ...@@ -15,7 +15,7 @@ module ChatMessage
@ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch' @ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch'
@ref = pipeline_attributes[:ref] @ref = pipeline_attributes[:ref]
@status = pipeline_attributes[:status] @status = pipeline_attributes[:status]
@duration = pipeline_attributes[:duration] @duration = pipeline_attributes[:duration].to_i
@pipeline_id = pipeline_attributes[:id] @pipeline_id = pipeline_attributes[:id]
end end
...@@ -37,7 +37,7 @@ module ChatMessage ...@@ -37,7 +37,7 @@ module ChatMessage
{ {
title: "Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}", title: "Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
subtitle: "in #{project_link}", subtitle: "in #{project_link}",
text: "in #{duration} #{time_measure}", text: "in #{pretty_duration(duration)}",
image: user_avatar || '' image: user_avatar || ''
} }
end end
...@@ -45,7 +45,7 @@ module ChatMessage ...@@ -45,7 +45,7 @@ module ChatMessage
private private
def message def message
"#{project_link}: Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} #{time_measure}" "#{project_link}: Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{pretty_duration(duration)}"
end end
def humanized_status def humanized_status
...@@ -84,9 +84,5 @@ module ChatMessage ...@@ -84,9 +84,5 @@ module ChatMessage
def pipeline_link def pipeline_link
"[##{pipeline_id}](#{pipeline_url})" "[##{pipeline_id}](#{pipeline_url})"
end end
def time_measure
'second'.pluralize(duration)
end
end end
end end
---
title: Pipeline chat notifications convert seconds to minutes and hours
merge_request:
author:
...@@ -4,6 +4,7 @@ describe ChatMessage::PipelineMessage do ...@@ -4,6 +4,7 @@ describe ChatMessage::PipelineMessage do
subject { described_class.new(args) } subject { described_class.new(args) }
let(:user) { { name: 'hacker' } } let(:user) { { name: 'hacker' } }
let(:duration) { 7210 }
let(:args) do let(:args) do
{ {
object_attributes: { object_attributes: {
...@@ -26,7 +27,6 @@ describe ChatMessage::PipelineMessage do ...@@ -26,7 +27,6 @@ describe ChatMessage::PipelineMessage do
context 'pipeline succeeded' do context 'pipeline succeeded' do
let(:status) { 'success' } let(:status) { 'success' }
let(:color) { 'good' } let(:color) { 'good' }
let(:duration) { 10 }
let(:message) { build_message('passed') } let(:message) { build_message('passed') }
it 'returns a message with information about succeeded build' do it 'returns a message with information about succeeded build' do
...@@ -39,7 +39,6 @@ describe ChatMessage::PipelineMessage do ...@@ -39,7 +39,6 @@ describe ChatMessage::PipelineMessage do
context 'pipeline failed' do context 'pipeline failed' do
let(:status) { 'failed' } let(:status) { 'failed' }
let(:color) { 'danger' } let(:color) { 'danger' }
let(:duration) { 10 }
let(:message) { build_message } let(:message) { build_message }
it 'returns a message with information about failed build' do it 'returns a message with information about failed build' do
...@@ -64,7 +63,7 @@ describe ChatMessage::PipelineMessage do ...@@ -64,7 +63,7 @@ describe ChatMessage::PipelineMessage do
"<http://example.gitlab.com|project_name>:" \ "<http://example.gitlab.com|project_name>:" \
" Pipeline <http://example.gitlab.com/pipelines/123|#123>" \ " Pipeline <http://example.gitlab.com/pipelines/123|#123>" \
" of <http://example.gitlab.com/commits/develop|develop> branch" \ " of <http://example.gitlab.com/commits/develop|develop> branch" \
" by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}" " by #{name} #{status_text} in 02:00:10"
end end
end end
...@@ -76,7 +75,6 @@ describe ChatMessage::PipelineMessage do ...@@ -76,7 +75,6 @@ describe ChatMessage::PipelineMessage do
context 'pipeline succeeded' do context 'pipeline succeeded' do
let(:status) { 'success' } let(:status) { 'success' }
let(:color) { 'good' } let(:color) { 'good' }
let(:duration) { 10 }
let(:message) { build_markdown_message('passed') } let(:message) { build_markdown_message('passed') }
it 'returns a message with information about succeeded build' do it 'returns a message with information about succeeded build' do
...@@ -85,7 +83,7 @@ describe ChatMessage::PipelineMessage do ...@@ -85,7 +83,7 @@ describe ChatMessage::PipelineMessage do
expect(subject.activity).to eq({ expect(subject.activity).to eq({
title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by hacker passed', title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by hacker passed',
subtitle: 'in [project_name](http://example.gitlab.com)', subtitle: 'in [project_name](http://example.gitlab.com)',
text: 'in 10 seconds', text: 'in 02:00:10',
image: '' image: ''
}) })
end end
...@@ -94,7 +92,6 @@ describe ChatMessage::PipelineMessage do ...@@ -94,7 +92,6 @@ describe ChatMessage::PipelineMessage do
context 'pipeline failed' do context 'pipeline failed' do
let(:status) { 'failed' } let(:status) { 'failed' }
let(:color) { 'danger' } let(:color) { 'danger' }
let(:duration) { 10 }
let(:message) { build_markdown_message } let(:message) { build_markdown_message }
it 'returns a message with information about failed build' do it 'returns a message with information about failed build' do
...@@ -103,7 +100,7 @@ describe ChatMessage::PipelineMessage do ...@@ -103,7 +100,7 @@ describe ChatMessage::PipelineMessage do
expect(subject.activity).to eq({ expect(subject.activity).to eq({
title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by hacker failed', title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by hacker failed',
subtitle: 'in [project_name](http://example.gitlab.com)', subtitle: 'in [project_name](http://example.gitlab.com)',
text: 'in 10 seconds', text: 'in 02:00:10',
image: '' image: ''
}) })
end end
...@@ -118,7 +115,7 @@ describe ChatMessage::PipelineMessage do ...@@ -118,7 +115,7 @@ describe ChatMessage::PipelineMessage do
expect(subject.activity).to eq({ expect(subject.activity).to eq({
title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by API failed', title: 'Pipeline [#123](http://example.gitlab.com/pipelines/123) of [develop](http://example.gitlab.com/commits/develop) branch by API failed',
subtitle: 'in [project_name](http://example.gitlab.com)', subtitle: 'in [project_name](http://example.gitlab.com)',
text: 'in 10 seconds', text: 'in 02:00:10',
image: '' image: ''
}) })
end end
...@@ -129,7 +126,7 @@ describe ChatMessage::PipelineMessage do ...@@ -129,7 +126,7 @@ describe ChatMessage::PipelineMessage do
"[project_name](http://example.gitlab.com):" \ "[project_name](http://example.gitlab.com):" \
" Pipeline [#123](http://example.gitlab.com/pipelines/123)" \ " Pipeline [#123](http://example.gitlab.com/pipelines/123)" \
" of [develop](http://example.gitlab.com/commits/develop)" \ " of [develop](http://example.gitlab.com/commits/develop)" \
" branch by #{name} #{status_text} in #{duration} #{'second'.pluralize(duration)}" " branch by #{name} #{status_text} in 02:00:10"
end end
end end
end end
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