From fcd723eda6ab1c1f8839570140db54ae3f7ff837 Mon Sep 17 00:00:00 2001
From: Aran Koning <arankoning@gmail.com>
Date: Mon, 23 May 2016 16:35:19 +0200
Subject: [PATCH] Seconds plural slack

---
 CHANGELOG                                     |  1 +
 .../slack_service/build_message.rb            |  4 ++--
 .../slack_service/build_message_spec.rb       | 23 +++++++++++++++----
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index a79c3048b7..104f41e603 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -3,6 +3,7 @@ Please view this file on the master branch, on stable branches it's out of date.
 v 8.9.0 (unreleased)
   - Redesign navigation for project pages
   - Use gitlab-shell v3.0.0
+  - Changed the Slack build message to use the singular duration if necessary (Aran Koning)
 
 v 8.8.2 (unreleased)
   - Fix Error 500 when accessing application settings due to nil disabled OAuth sign-in sources
diff --git a/app/models/project_services/slack_service/build_message.rb b/app/models/project_services/slack_service/build_message.rb
index c124cad4af..69c21b3fc3 100644
--- a/app/models/project_services/slack_service/build_message.rb
+++ b/app/models/project_services/slack_service/build_message.rb
@@ -35,8 +35,8 @@ class SlackService
     private
 
     def message
-      "#{project_link}: Commit #{commit_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} second(s)"
-    end
+      "#{project_link}: Commit #{commit_link} of #{branch_link} #{ref_type} by #{user_name} #{humanized_status} in #{duration} #{'second'.pluralize(duration)}"
+    end   
 
     def format(string)
       Slack::Notifier::LinkFormatter.format(string)
diff --git a/spec/models/project_services/slack_service/build_message_spec.rb b/spec/models/project_services/slack_service/build_message_spec.rb
index 621c83c0cd..7fcfdf0eac 100644
--- a/spec/models/project_services/slack_service/build_message_spec.rb
+++ b/spec/models/project_services/slack_service/build_message_spec.rb
@@ -15,7 +15,7 @@ describe SlackService::BuildMessage do
       commit: {
         status: status,
         author_name: 'hacker',
-        duration: 10,
+        duration: duration,
       },
     }
   end
@@ -23,9 +23,10 @@ describe SlackService::BuildMessage do
   context 'succeeded' do
     let(:status) { 'success' }
     let(:color) { 'good' }
-
+    let(:duration) { 10 }
+    
     it 'returns a message with information about succeeded build' do
-      message = '<somewhere.com|project_name>: Commit <somewhere.com/commit/97de212e80737a608d939f648d959671fb0a0142/builds|97de212e> of <somewhere.com/commits/develop|develop> branch by hacker passed in 10 second(s)'
+      message = '<somewhere.com|project_name>: Commit <somewhere.com/commit/97de212e80737a608d939f648d959671fb0a0142/builds|97de212e> of <somewhere.com/commits/develop|develop> branch by hacker passed in 10 seconds'
       expect(subject.pretext).to be_empty
       expect(subject.fallback).to eq(message)
       expect(subject.attachments).to eq([text: message, color: color])
@@ -35,9 +36,23 @@ describe SlackService::BuildMessage do
   context 'failed' do
     let(:status) { 'failed' }
     let(:color) { 'danger' }
+    let(:duration) { 10 }
 
     it 'returns a message with information about failed build' do
-      message = '<somewhere.com|project_name>: Commit <somewhere.com/commit/97de212e80737a608d939f648d959671fb0a0142/builds|97de212e> of <somewhere.com/commits/develop|develop> branch by hacker failed in 10 second(s)'
+      message = '<somewhere.com|project_name>: Commit <somewhere.com/commit/97de212e80737a608d939f648d959671fb0a0142/builds|97de212e> of <somewhere.com/commits/develop|develop> branch by hacker failed in 10 seconds'
+      expect(subject.pretext).to be_empty
+      expect(subject.fallback).to eq(message)
+      expect(subject.attachments).to eq([text: message, color: color])
+    end
+  end 
+  
+  describe '#seconds_name' do
+    let(:status) { 'failed' }
+    let(:color) { 'danger' }
+    let(:duration) { 1 }
+
+    it 'returns seconds as singular when there is only one' do
+      message = '<somewhere.com|project_name>: Commit <somewhere.com/commit/97de212e80737a608d939f648d959671fb0a0142/builds|97de212e> of <somewhere.com/commits/develop|develop> branch by hacker failed in 1 second'
       expect(subject.pretext).to be_empty
       expect(subject.fallback).to eq(message)
       expect(subject.attachments).to eq([text: message, color: color])
-- 
2.30.9