From 16cca3a0ea7f4b95e99d7b3e8d4953334fa7bec7 Mon Sep 17 00:00:00 2001
From: Toon Claes <toon@gitlab.com>
Date: Fri, 17 Mar 2017 17:25:17 +0100
Subject: [PATCH] Expose if action is playable in JSON

To avoid a manual build action being played (resulting in a 404),
expose `playable?` in the JSON so the frontend can disable/hide the
play button if it's not playable.
---
 .../javascripts/environments/components/environment_item.js   | 1 +
 app/serializers/build_action_entity.rb                        | 2 ++
 app/serializers/build_entity.rb                               | 1 +
 spec/serializers/build_action_entity_spec.rb                  | 4 ++++
 spec/serializers/build_entity_spec.rb                         | 4 ++++
 5 files changed, 12 insertions(+)

diff --git a/app/assets/javascripts/environments/components/environment_item.js b/app/assets/javascripts/environments/components/environment_item.js
index 93919d41c60..9d753b4f808 100644
--- a/app/assets/javascripts/environments/components/environment_item.js
+++ b/app/assets/javascripts/environments/components/environment_item.js
@@ -141,6 +141,7 @@ export default {
           const parsedAction = {
             name: gl.text.humanize(action.name),
             play_path: action.play_path,
+            playable: action.playable,
           };
           return parsedAction;
         });
diff --git a/app/serializers/build_action_entity.rb b/app/serializers/build_action_entity.rb
index 184f5fd4b52..184b4b7a681 100644
--- a/app/serializers/build_action_entity.rb
+++ b/app/serializers/build_action_entity.rb
@@ -11,4 +11,6 @@ class BuildActionEntity < Grape::Entity
       build.project,
       build)
   end
+
+  expose :playable?, as: :playable
 end
diff --git a/app/serializers/build_entity.rb b/app/serializers/build_entity.rb
index 5bcbe285052..2c116102888 100644
--- a/app/serializers/build_entity.rb
+++ b/app/serializers/build_entity.rb
@@ -16,6 +16,7 @@ class BuildEntity < Grape::Entity
     path_to(:play_namespace_project_build, build)
   end
 
+  expose :playable?, as: :playable
   expose :created_at
   expose :updated_at
 
diff --git a/spec/serializers/build_action_entity_spec.rb b/spec/serializers/build_action_entity_spec.rb
index 0f7be8b2c39..54ac17447b1 100644
--- a/spec/serializers/build_action_entity_spec.rb
+++ b/spec/serializers/build_action_entity_spec.rb
@@ -17,5 +17,9 @@ describe BuildActionEntity do
     it 'contains path to the action play' do
       expect(subject[:path]).to include "builds/#{build.id}/play"
     end
+
+    it 'contains whether it is playable' do
+      expect(subject[:playable]).to eq build.playable?
+    end
   end
 end
diff --git a/spec/serializers/build_entity_spec.rb b/spec/serializers/build_entity_spec.rb
index 60c9642ee2c..eed957fa5ef 100644
--- a/spec/serializers/build_entity_spec.rb
+++ b/spec/serializers/build_entity_spec.rb
@@ -18,6 +18,10 @@ describe BuildEntity do
     expect(subject).not_to include(/variables/)
   end
 
+  it 'contains whether it is playable' do
+    expect(subject[:playable]).to eq build.playable?
+  end
+
   it 'contains timestamps' do
     expect(subject).to include(:created_at, :updated_at)
   end
-- 
GitLab