From 5d4183e58d08a03dfc73c4bf1705aa926270052d Mon Sep 17 00:00:00 2001
From: "Z.J. van de Weg" <git@zjvandeweg.nl>
Date: Wed, 3 May 2017 09:25:13 +0200
Subject: [PATCH] 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.
---
 .../project_services/chat_message/base_message.rb | 11 +++++++++++
 .../chat_message/pipeline_message.rb              | 10 +++-------
 .../unreleased/zj-chat-message-pretty-time.yml    |  4 ++++
 .../chat_message/pipeline_message_spec.rb         | 15 ++++++---------
 4 files changed, 24 insertions(+), 16 deletions(-)
 create mode 100644 changelogs/unreleased/zj-chat-message-pretty-time.yml

diff --git a/app/models/project_services/chat_message/base_message.rb b/app/models/project_services/chat_message/base_message.rb
index 7621a5fa2d8..e2ad586aea7 100644
--- a/app/models/project_services/chat_message/base_message.rb
+++ b/app/models/project_services/chat_message/base_message.rb
@@ -50,5 +50,16 @@ module ChatMessage
     def link(text, url)
       "[#{text}](#{url})"
     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
diff --git a/app/models/project_services/chat_message/pipeline_message.rb b/app/models/project_services/chat_message/pipeline_message.rb
index 4628d9b1a7b..47b68f00cff 100644
--- a/app/models/project_services/chat_message/pipeline_message.rb
+++ b/app/models/project_services/chat_message/pipeline_message.rb
@@ -15,7 +15,7 @@ module ChatMessage
       @ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch'
       @ref = pipeline_attributes[:ref]
       @status = pipeline_attributes[:status]
-      @duration = pipeline_attributes[:duration]
+      @duration = pipeline_attributes[:duration].to_i
       @pipeline_id = pipeline_attributes[:id]
     end
 
@@ -37,7 +37,7 @@ module ChatMessage
       {
         title: "Pipeline #{pipeline_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status}",
         subtitle: "in #{project_link}",
-        text: "in #{duration} #{time_measure}",
+        text: "in #{pretty_duration(duration)}",
         image: user_avatar || ''
       }
     end
@@ -45,7 +45,7 @@ module ChatMessage
     private
 
     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
 
     def humanized_status
@@ -84,9 +84,5 @@ module ChatMessage
     def pipeline_link
       "[##{pipeline_id}](#{pipeline_url})"
     end
-
-    def time_measure
-      'second'.pluralize(duration)
-    end
   end
 end
diff --git a/changelogs/unreleased/zj-chat-message-pretty-time.yml b/changelogs/unreleased/zj-chat-message-pretty-time.yml
new file mode 100644
index 00000000000..68bc647bab2
--- /dev/null
+++ b/changelogs/unreleased/zj-chat-message-pretty-time.yml
@@ -0,0 +1,4 @@
+---
+title: Pipeline chat notifications convert seconds to minutes and hours
+merge_request:
+author:
diff --git a/spec/models/project_services/chat_message/pipeline_message_spec.rb b/spec/models/project_services/chat_message/pipeline_message_spec.rb
index ec5c6c5e0ed..e005be42b0d 100644
--- a/spec/models/project_services/chat_message/pipeline_message_spec.rb
+++ b/spec/models/project_services/chat_message/pipeline_message_spec.rb
@@ -4,6 +4,7 @@ describe ChatMessage::PipelineMessage do
   subject { described_class.new(args) }
 
   let(:user) { { name: 'hacker' } }
+  let(:duration) { 7210 }
   let(:args) do
     {
       object_attributes: {
@@ -26,7 +27,6 @@ describe ChatMessage::PipelineMessage do
     context 'pipeline succeeded' do
       let(:status) { 'success' }
       let(:color) { 'good' }
-      let(:duration) { 10 }
       let(:message) { build_message('passed') }
 
       it 'returns a message with information about succeeded build' do
@@ -39,7 +39,6 @@ describe ChatMessage::PipelineMessage do
     context 'pipeline failed' do
       let(:status) { 'failed' }
       let(:color) { 'danger' }
-      let(:duration) { 10 }
       let(:message) { build_message }
 
       it 'returns a message with information about failed build' do
@@ -64,7 +63,7 @@ describe ChatMessage::PipelineMessage do
       "<http://example.gitlab.com|project_name>:" \
         " Pipeline <http://example.gitlab.com/pipelines/123|#123>" \
         " 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
 
@@ -76,7 +75,6 @@ describe ChatMessage::PipelineMessage do
     context 'pipeline succeeded' do
       let(:status) { 'success' }
       let(:color) { 'good' }
-      let(:duration) { 10 }
       let(:message) { build_markdown_message('passed') }
 
       it 'returns a message with information about succeeded build' do
@@ -85,7 +83,7 @@ describe ChatMessage::PipelineMessage do
         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',
           subtitle: 'in [project_name](http://example.gitlab.com)',
-          text: 'in 10 seconds',
+          text: 'in 02:00:10',
           image: ''
         })
       end
@@ -94,7 +92,6 @@ describe ChatMessage::PipelineMessage do
     context 'pipeline failed' do
       let(:status) { 'failed' }
       let(:color) { 'danger' }
-      let(:duration) { 10 }
       let(:message) { build_markdown_message }
 
       it 'returns a message with information about failed build' do
@@ -103,7 +100,7 @@ describe ChatMessage::PipelineMessage do
         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',
           subtitle: 'in [project_name](http://example.gitlab.com)',
-          text: 'in 10 seconds',
+          text: 'in 02:00:10',
           image: ''
         })
       end
@@ -118,7 +115,7 @@ describe ChatMessage::PipelineMessage do
           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',
             subtitle: 'in [project_name](http://example.gitlab.com)',
-            text: 'in 10 seconds',
+            text: 'in 02:00:10',
             image: ''
           })
         end
@@ -129,7 +126,7 @@ describe ChatMessage::PipelineMessage do
       "[project_name](http://example.gitlab.com):" \
         " Pipeline [#123](http://example.gitlab.com/pipelines/123)" \
         " 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
-- 
GitLab