Commit a97c8792 authored by Stan Hu's avatar Stan Hu

Merge branch 'com-code-skeleton' into 'master'

Skeleton for segregation of GL com module

Closes customers-gitlab-com#722

See merge request gitlab-org/gitlab!17711
parents d313af37 481631e5
# frozen_string_literal: true
module Com
module Gitlab
module Patch
module DrawRoute
extend ::Gitlab::Utils::Override
override :draw_com
def draw_com(routes_name)
draw_route(route_path("com/config/routes/#{routes_name}.rb"))
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'com_spec_helper'
describe Gitlab::Patch::DrawRoute do
subject do
Class.new do
include Gitlab::Patch::DrawRoute
def route_path(route_name)
File.expand_path("../../../../../#{route_name}", __dir__)
end
end.new
end
before do
allow(subject).to receive(:instance_eval)
end
it 'raises an error when nothing is drawn' do
expect { subject.draw(:non_existing) }
.to raise_error(described_class::RoutesNotFound)
end
end
......@@ -22,6 +22,7 @@ module Gitlab
require_dependency Rails.root.join('lib/gitlab/current_settings')
require_dependency Rails.root.join('lib/gitlab/middleware/read_only')
require_dependency Rails.root.join('lib/gitlab/middleware/basic_health_check')
require_dependency Rails.root.join('config/light_settings')
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
......@@ -62,6 +63,15 @@ module Gitlab
config.paths['app/views'].unshift "#{config.root}/ee/app/views"
end
if LightSettings.com?
com_paths = config.eager_load_paths.each_with_object([]) do |path, memo|
com_path = config.root.join('com', Pathname.new(path).relative_path_from(config.root))
memo << com_path.to_s
end
config.eager_load_paths.push(*com_paths)
end
# Rake tasks ignore the eager loading settings, so we need to set the
# autoload paths explicitly
config.autoload_paths = config.eager_load_paths.dup
......
# frozen_string_literal: true
require 'active_support/inflector'
module InjectComModule
def prepend_if_com(constant, with_descendants: false)
return unless Gitlab.com?
com_module = constant.constantize
prepend(com_module)
if with_descendants
descendants.each { |descendant| descendant.prepend(com_module) }
end
end
def extend_if_com(constant)
extend(constant.constantize) if Gitlab.com?
end
def include_if_com(constant)
include(constant.constantize) if Gitlab.com?
end
end
Module.prepend(InjectComModule)
# frozen_string_literal: true
class LightSettings
GL_HOST ||= 'gitlab.com'
GL_SUBDOMAIN_REGEX ||= %r{\A[a-z0-9]+\.gitlab\.com\z}.freeze
class << self
def com?
return Thread.current[:is_com] unless Thread.current[:is_com].nil?
Thread.current[:is_com] = host == GL_HOST || gl_subdomain?
end
private
def config
YAML.safe_load(File.read(settings_path), aliases: true)[Rails.env]
end
def settings_path
Rails.root.join('config', 'gitlab.yml')
end
def host
config['gitlab']['host']
end
def gl_subdomain?
GL_SUBDOMAIN_REGEX === host
end
end
end
# frozen_string_literal: true
require 'pathname'
require_relative '../config/light_settings'
module Gitlab
def self.root
......@@ -37,24 +38,18 @@ module Gitlab
COM_URL = 'https://gitlab.com'
APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))}.freeze
SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z}.freeze
VERSION = File.read(root.join("VERSION")).strip.freeze
INSTALLATION_TYPE = File.read(root.join("INSTALLATION_TYPE")).strip.freeze
HTTP_PROXY_ENV_VARS = %w(http_proxy https_proxy HTTP_PROXY HTTPS_PROXY).freeze
def self.com?
# Check `gl_subdomain?` as well to keep parity with gitlab.com
Gitlab.config.gitlab.url == COM_URL || gl_subdomain?
LightSettings.com?
end
def self.org?
Gitlab.config.gitlab.url == 'https://dev.gitlab.org'
end
def self.gl_subdomain?
SUBDOMAIN_REGEX === Gitlab.config.gitlab.url
end
def self.dev_env_org_or_com?
dev_env_or_com? || org?
end
......@@ -79,6 +74,10 @@ module Gitlab
yield if ee?
end
def self.com
yield if com?
end
def self.http_proxy_env?
HTTP_PROXY_ENV_VARS.any? { |name| ENV[name] }
end
......
......@@ -6,11 +6,12 @@ module Gitlab
module Patch
module DrawRoute
prepend_if_ee('EE::Gitlab::Patch::DrawRoute') # rubocop: disable Cop/InjectEnterpriseEditionModule
prepend_if_com('Com::Gitlab::Patch::DrawRoute')
RoutesNotFound = Class.new(StandardError)
def draw(routes_name)
drawn_any = draw_ce(routes_name) | draw_ee(routes_name)
drawn_any = draw_ce(routes_name) | draw_ee(routes_name) | draw_com(routes_name)
drawn_any || raise(RoutesNotFound.new("Cannot find #{routes_name}"))
end
......@@ -23,6 +24,10 @@ module Gitlab
true
end
def draw_com(_)
false
end
def route_path(routes_name)
Rails.root.join(routes_name)
end
......
# frozen_string_literal: true
Settings.gitlab[:url] = "https://test.gitlab.com"
......@@ -5,10 +5,12 @@ ENV['IN_MEMORY_APPLICATION_SETTINGS'] = 'true'
require 'active_support/dependencies'
require_relative '../config/initializers/0_inject_enterprise_edition_module'
require_relative '../config/initializers/0_inject_com_module'
require_relative '../config/settings'
require_relative 'support/rspec'
require 'active_support/all'
ActiveSupport::Dependencies.autoload_paths << 'lib'
ActiveSupport::Dependencies.autoload_paths << 'ee/lib'
ActiveSupport::Dependencies.autoload_paths << 'com/lib'
ActiveSupport::XmlMini.backend = 'Nokogiri'
......@@ -71,26 +71,30 @@ describe Gitlab do
end
describe '.com?' do
before do
Thread.current[:is_com] = nil
end
it 'is true when on GitLab.com' do
stub_config_setting(url: 'https://gitlab.com')
allow(LightSettings).to receive(:host).and_return('gitlab.com')
expect(described_class.com?).to eq true
end
it 'is true when on staging' do
stub_config_setting(url: 'https://staging.gitlab.com')
allow(LightSettings).to receive(:host).and_return('staging.gitlab.com')
expect(described_class.com?).to eq true
end
it 'is true when on other gitlab subdomain' do
stub_config_setting(url: 'https://example.gitlab.com')
allow(LightSettings).to receive(:host).and_return('example.gitlab.com')
expect(described_class.com?).to eq true
end
it 'is false when not on GitLab.com' do
stub_config_setting(url: 'http://example.com')
allow(LightSettings).to receive(:host).and_return('example.com')
expect(described_class.com?).to eq false
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment