Replace Gitlab::REVISION with Gitlab.revision and handle installations without a .git directory

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2018-05-24 09:57:54 +02:00
parent c52db71e36
commit d50a80a282
No known key found for this signature in database
GPG key ID: 98DFFD1C0C62B70B
16 changed files with 95 additions and 14 deletions

View file

@ -618,7 +618,7 @@ module Ci
variables.append(key: 'GITLAB_FEATURES', value: project.licensed_features.join(','))
variables.append(key: 'CI_SERVER_NAME', value: 'GitLab')
variables.append(key: 'CI_SERVER_VERSION', value: Gitlab::VERSION)
variables.append(key: 'CI_SERVER_REVISION', value: Gitlab::REVISION)
variables.append(key: 'CI_SERVER_REVISION', value: Gitlab.revision)
variables.append(key: 'CI_JOB_NAME', value: name)
variables.append(key: 'CI_JOB_STAGE', value: stage)
variables.append(key: 'CI_COMMIT_SHA', value: sha)

View file

@ -132,7 +132,7 @@
GitLab
%span.pull-right
= Gitlab::VERSION
= "(#{Gitlab::REVISION})"
= "(#{Gitlab.revision})"
%p
GitLab Shell
%span.pull-right

View file

@ -8,7 +8,7 @@
Community Edition
- if user_signed_in?
%span= Gitlab::VERSION
%small= link_to Gitlab::REVISION, Gitlab::COM_URL + namespace_project_commits_path('gitlab-org', 'gitlab-ce', Gitlab::REVISION)
%small= link_to Gitlab.revision, Gitlab::COM_URL + namespace_project_commits_path('gitlab-org', 'gitlab-ce', Gitlab.revision)
= version_status_badge
%hr

View file

@ -0,0 +1,6 @@
---
title: Replace Gitlab::REVISION with Gitlab.revision and handle installations without
a .git directory
merge_request: 19125
author:
type: fixed

View file

@ -3,7 +3,7 @@ if defined?(Rails::Console)
# note that this will not print out when using `spring`
justify = 15
puts "-------------------------------------------------------------------------------------"
puts " Gitlab:".ljust(justify) + "#{Gitlab::VERSION} (#{Gitlab::REVISION})"
puts " Gitlab:".ljust(justify) + "#{Gitlab::VERSION} (#{Gitlab.revision})"
puts " Gitlab Shell:".ljust(justify) + Gitlab::Shell.new.version
puts " #{Gitlab::Database.adapter_name}:".ljust(justify) + Gitlab::Database.version
puts "-------------------------------------------------------------------------------------"

View file

@ -13,7 +13,7 @@ def configure_sentry
if sentry_enabled
Raven.configure do |config|
config.dsn = Gitlab::CurrentSettings.current_application_settings.sentry_dsn
config.release = Gitlab::REVISION
config.release = Gitlab.revision
# Sanitize fields based on those sanitized from Rails.
config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s)

View file

@ -113,7 +113,7 @@ module API
{
api_version: API.version,
gitlab_version: Gitlab::VERSION,
gitlab_rev: Gitlab::REVISION,
gitlab_rev: Gitlab.revision,
redis: redis_ping
}
end

View file

@ -6,7 +6,7 @@ module API
detail 'This feature was introduced in GitLab 8.13.'
end
get '/version' do
{ version: Gitlab::VERSION, revision: Gitlab::REVISION }
{ version: Gitlab::VERSION, revision: Gitlab.revision }
end
end
end

View file

@ -13,11 +13,26 @@ module Gitlab
@_migrations_hash ||= Digest::MD5.hexdigest(ActiveRecord::Migrator.get_all_versions.to_s)
end
def self.revision
@_revision ||= begin
if File.exist?(root.join("REVISION"))
File.read(root.join("REVISION")).strip.freeze
else
result = Gitlab::Popen.popen_with_detail(%W[#{config.git.bin_path} log --pretty=format:%h -n 1])
if result.status.success?
result.stdout.chomp.freeze
else
"Unknown".freeze
end
end
end
end
COM_URL = 'https://gitlab.com'.freeze
APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))}
SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z}
VERSION = File.read(root.join("VERSION")).strip.freeze
REVISION = Gitlab::Popen.popen(%W(#{config.git.bin_path} log --pretty=format:%h -n 1)).first.chomp.freeze
def self.com?
# Check `gl_subdomain?` as well to keep parity with gitlab.com

View file

@ -15,7 +15,7 @@ module Gitlab
gon.user_color_scheme = Gitlab::ColorSchemes.for_user(current_user).css_class
gon.sentry_dsn = Gitlab::CurrentSettings.clientside_sentry_dsn if Gitlab::CurrentSettings.clientside_sentry_enabled
gon.gitlab_url = Gitlab.config.gitlab.url
gon.revision = Gitlab::REVISION
gon.revision = Gitlab.revision
gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png')
gon.sprite_icons = IconsHelper.sprite_icon_path
gon.sprite_file_icons = IconsHelper.sprite_file_icons_path

View file

@ -47,7 +47,7 @@ namespace :gitlab do
puts ""
puts "GitLab information".color(:yellow)
puts "Version:\t#{Gitlab::VERSION}"
puts "Revision:\t#{Gitlab::REVISION}"
puts "Revision:\t#{Gitlab.revision}"
puts "Directory:\t#{Rails.root}"
puts "DB Adapter:\t#{database_adapter}"
puts "URL:\t\t#{Gitlab.config.gitlab.url}"

View file

@ -8,6 +8,66 @@ describe Gitlab do
expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__)))
end
end
describe '.revision' do
let(:cmd) { %W[#{described_class.config.git.bin_path} log --pretty=format:%h -n 1] }
around do |example|
described_class.instance_variable_set(:@_revision, nil)
example.run
described_class.instance_variable_set(:@_revision, nil)
end
context 'when a REVISION file exists' do
before do
expect(File).to receive(:exist?)
.with(described_class.root.join('REVISION'))
.and_return(true)
end
it 'returns the actual Git revision' do
expect(File).to receive(:read)
.with(described_class.root.join('REVISION'))
.and_return("abc123\n")
expect(described_class.revision).to eq('abc123')
end
it 'memoizes the revision' do
expect(File).to receive(:read)
.once
.with(described_class.root.join('REVISION'))
.and_return("abc123\n")
2.times { described_class.revision }
end
end
context 'when no REVISION file exist' do
context 'when the Git command succeeds' do
before do
expect(Gitlab::Popen).to receive(:popen_with_detail)
.with(cmd)
.and_return(Gitlab::Popen::Result.new(cmd, 'abc123', '', double(success?: true)))
end
it 'returns the actual Git revision' do
expect(described_class.revision).to eq('abc123')
end
end
context 'when the Git command fails' do
before do
expect(Gitlab::Popen).to receive(:popen_with_detail)
.with(cmd)
.and_return(Gitlab::Popen::Result.new(cmd, '', 'fatal: Not a git repository', double('Process::Status', success?: false)))
end
it 'returns "Unknown"' do
expect(described_class.revision).to eq('Unknown')
end
end
end
end
describe '.com?' do
it 'is true when on GitLab.com' do

View file

@ -1546,7 +1546,7 @@ describe Ci::Build do
{ key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true },
{ key: 'CI_SERVER_NAME', value: 'GitLab', public: true },
{ key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true },
{ key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true },
{ key: 'CI_SERVER_REVISION', value: Gitlab.revision, public: true },
{ key: 'CI_JOB_NAME', value: 'test', public: true },
{ key: 'CI_JOB_STAGE', value: 'test', public: true },
{ key: 'CI_COMMIT_SHA', value: build.sha, public: true },

View file

@ -18,7 +18,7 @@ describe API::Version do
expect(response).to have_gitlab_http_status(200)
expect(json_response['version']).to eq(Gitlab::VERSION)
expect(json_response['revision']).to eq(Gitlab::REVISION)
expect(json_response['revision']).to eq(Gitlab.revision)
end
end
end

View file

@ -22,6 +22,6 @@ describe 'admin/dashboard/index.html.haml' do
it "includes revision of GitLab" do
render
expect(rendered).to have_content "#{Gitlab::VERSION} (#{Gitlab::REVISION})"
expect(rendered).to have_content "#{Gitlab::VERSION} (#{Gitlab.revision})"
end
end

View file

@ -39,7 +39,7 @@ describe 'help/index' do
def stub_version(version, revision)
stub_const('Gitlab::VERSION', version)
stub_const('Gitlab::REVISION', revision)
allow(Gitlab).to receive(:revision).and_return(revision)
end
def stub_helpers