From 71306f14f69b9798a21905cfa8160c4990ca5d5d Mon Sep 17 00:00:00 2001
From: Toon Claes <toon@gitlab.com>
Date: Mon, 20 Mar 2017 22:05:53 +0100
Subject: [PATCH] Make level_value accept string integers

When a VisibilityLevel is an integer formatted as a string, convert it
to an integer, instead of looking it up in the hash map.

When the value is not recognized, default to PRIVATE.
---
 lib/gitlab/visibility_level.rb           |  4 ++--
 spec/lib/gitlab/visibility_level_spec.rb | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 spec/lib/gitlab/visibility_level_spec.rb

diff --git a/lib/gitlab/visibility_level.rb b/lib/gitlab/visibility_level.rb
index 2248763c10..8f1d1fdc02 100644
--- a/lib/gitlab/visibility_level.rb
+++ b/lib/gitlab/visibility_level.rb
@@ -96,8 +96,8 @@ module Gitlab
       end
 
       def level_value(level)
-        return string_options[level] if level.is_a? String
-        level
+        return level.to_i if level.to_i.to_s == level.to_s && string_options.key(level.to_i)
+        string_options[level] || PRIVATE
       end
 
       def string_level(level)
diff --git a/spec/lib/gitlab/visibility_level_spec.rb b/spec/lib/gitlab/visibility_level_spec.rb
new file mode 100644
index 0000000000..3255c6f1ef
--- /dev/null
+++ b/spec/lib/gitlab/visibility_level_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Gitlab::VisibilityLevel, lib: true do
+  describe '.level_value' do
+    it 'converts "public" to integer value' do
+      expect(described_class.level_value('public')).to eq(Gitlab::VisibilityLevel::PUBLIC)
+    end
+
+    it 'converts string integer to integer value' do
+      expect(described_class.level_value('20')).to eq(20)
+    end
+
+    it 'defaults to PRIVATE when string value is not valid' do
+      expect(described_class.level_value('invalid')).to eq(Gitlab::VisibilityLevel::PRIVATE)
+    end
+
+    it 'defaults to PRIVATE when integer value is not valid' do
+      expect(described_class.level_value(100)).to eq(Gitlab::VisibilityLevel::PRIVATE)
+    end
+  end
+end
-- 
2.30.9