From a892b2354c953d62445a5cf9ac747ee166256c66 Mon Sep 17 00:00:00 2001
From: Peter Leitzen <pl@neopoly.de>
Date: Sat, 21 Jul 2018 12:24:07 +0200
Subject: [PATCH] Tag a commit via `/tag 1.2.3 message`

---
 app/services/commits/update_service.rb       | 21 ++++++++-
 spec/services/commits/update_service_spec.rb | 47 ++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 spec/services/commits/update_service_spec.rb

diff --git a/app/services/commits/update_service.rb b/app/services/commits/update_service.rb
index 9cd5ddc7f91..7ec678ff1cf 100644
--- a/app/services/commits/update_service.rb
+++ b/app/services/commits/update_service.rb
@@ -3,7 +3,26 @@
 module Commits
   class UpdateService < BaseService
     def execute(commit)
-      # TODO authorize user
+      tag_commit(commit)
+    end
+
+    private
+
+    def tag_commit(commit)
+      # TODO authorize
+      return unless params[:tag_name]
+
+      tag_name = params[:tag_name]
+      message = params[:tag_message]
+      release_description = nil
+
+      result = Tags::CreateService
+        .new(commit.project, current_user)
+        .execute(tag_name, commit.sha, message, release_description)
+
+      if result[:status]
+        commit
+      end
     end
   end
 end
diff --git a/spec/services/commits/update_service_spec.rb b/spec/services/commits/update_service_spec.rb
new file mode 100644
index 00000000000..cfca3a85236
--- /dev/null
+++ b/spec/services/commits/update_service_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Commits::UpdateService do
+  let(:project) { create(:project, :repository) }
+  let(:user) { create(:user) }
+
+  let(:commit) { create(:commit, project: project) }
+
+  before do
+    project.add_maintainer(user)
+  end
+
+  describe 'execute' do
+    let(:service) { described_class.new(project, user, opts) }
+
+    context 'valid params' do
+      let(:opts) do
+        {
+          tag_name: '1.2.3',
+          tag_message: 'Release'
+        }
+      end
+
+      it 'tags a commit' do
+        tag_stub = instance_double(Tags::CreateService)
+        allow(Tags::CreateService).to receive(:new).and_return(tag_stub)
+        allow(tag_stub).to receive(:execute)
+          .with(opts[:tag_name], commit.sha, opts[:tag_message], nil)
+          .and_return({ status: :success })
+
+        service.execute(commit)
+      end
+    end
+
+    context 'invalid params' do
+      let(:opts) do
+        {}
+      end
+
+      it 'does not call the tag create service' do
+        expect(Tags::CreateService).not_to receive(:new)
+
+        service.execute(commit)
+      end
+    end
+  end
+end
-- 
2.30.9