Commit 19ed1d98 authored by DJ Mountney's avatar DJ Mountney Committed by DJ Mountney

Merge branch 'dev-version-file-location' into 'master'

Use API to fetch VERSION files from dev

See merge request charts/gitlab!726
parent 52e2a941
Pipeline #56421376 failed with stages
in 21 minutes and 43 seconds
......@@ -2,19 +2,29 @@ require_relative 'version'
require 'open-uri'
require 'uri'
require 'cgi'
class VersionFetcher
def initialize(version, repo)
@version = Version.new(version)
@repo = repo
@api_token = ENV['FETCH_DEV_ARTIFACTS_PAT']
@api_url = if @repo.start_with?('gitlab/')
'https://dev.gitlab.org/api/v4'
elsif @repo.start_with?('gitlab-org/')
'https://gitlab.com/api/v4'
else
ENV['CI_API_V4_URL']
end
end
# GitLab Shell Version
def gitlab_shell
return @version if @version == 'master'
url = "#{@repo}/raw/#{ref(@version)}/GITLAB_SHELL_VERSION"
new_version = URI.parse(url).read.strip
url = "#{@api_url}/projects/#{CGI.escape(@repo)}/repository/files/GITLAB_SHELL_VERSION/raw?ref=#{ref(@version)}"
$stdout.puts "Getting GitLab Shell version from #{url}"
new_version = open(url, 'PRIVATE-TOKEN' => @api_token).read.strip
$stdout.puts "# Shell appVersion: #{new_version}"
new_version
end
......@@ -23,8 +33,9 @@ class VersionFetcher
def gitaly
return @version if @version == 'master'
url = "#{@repo}/raw/#{ref(@version)}/GITALY_SERVER_VERSION"
new_version = URI.parse(url).read.strip
url = "#{@api_url}/projects/#{CGI.escape(@repo)}/repository/files/GITALY_SERVER_VERSION/raw?ref=#{ref(@version)}"
$stdout.puts "Getting Gitaly version from #{url}"
new_version = open(url, 'PRIVATE-TOKEN' => @api_token).read.strip
$stdout.puts "# Gitaly appVersion: #{new_version}"
new_version
end
......
......@@ -24,7 +24,7 @@ class VersionOptionsParser
# defaults
options.working_dir = Dir.pwd
options.include_subcharts = false
options.gitlab_repo = "https://gitlab.com/gitlab-org/gitlab-ee"
options.gitlab_repo = "gitlab-org/gitlab-ee"
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options] \n\n"
......
......@@ -2,13 +2,46 @@ require 'spec_helper'
require_relative '../../../scripts/lib/version_fetcher.rb'
describe VersionFetcher do
let(:repo_url) { 'https://gitlab.com/gitlab-org/gitlab-ce' }
let(:uri_response) { URI.parse(repo_url) }
let(:version_fetcher) { VersionFetcher.new('v11.8.0', repo_url) }
let(:com_path) { 'gitlab-org%2Fgitlab-ce/repository/files/GITLAB_SHELL_VERSION/raw?ref=v11.8.0' }
let(:dev_path) { 'gitlab%2Fgitlabhq/repository/files/GITLAB_SHELL_VERSION/raw?ref=v11.8.0' }
let(:custom_path) { 'johndoe%2Fgitlab-ee/repository/files/GITLAB_SHELL_VERSION/raw?ref=v11.8.0' }
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('FETCH_DEV_ARTIFACTS_PAT').and_return(nil)
end
describe 'detecting API URL' do
it 'works correctly gitlab.com registry' do
version_fetcher = VersionFetcher.new('v11.8.0', 'gitlab-org/gitlab-ce')
allow(version_fetcher).to receive_message_chain(:open, :read).and_return("1.2.3\n")
expect(version_fetcher).to receive(:open).with("https://gitlab.com/api/v4/projects/#{com_path}", { 'PRIVATE-TOKEN' => nil })
version_fetcher.fetch('gitlab-shell')
end
it 'works correctly dev registry' do
allow(ENV).to receive(:[]).with('FETCH_DEV_ARTIFACTS_PAT').and_return('myrandomtoken')
version_fetcher = VersionFetcher.new('v11.8.0', 'gitlab/gitlabhq')
allow(version_fetcher).to receive_message_chain(:open, :read).and_return("1.2.3\n")
expect(version_fetcher).to receive(:open).with("https://dev.gitlab.org/api/v4/projects/#{dev_path}", { 'PRIVATE-TOKEN' => 'myrandomtoken'})
version_fetcher.fetch('gitlab-shell')
end
it 'falls back correctly to current registry for unknown projects' do
version_fetcher = VersionFetcher.new('v11.8.0', 'johndoe/gitlab-ee')
allow(version_fetcher).to receive_message_chain(:open, :read).and_return("1.2.3\n")
expect(version_fetcher).to receive(:open).with("#{ENV['CI_API_V4_URL']}/projects/#{custom_path}", { 'PRIVATE-TOKEN' => nil})
version_fetcher.fetch('gitlab-shell')
end
end
describe 'instance methods' do
let(:version_fetcher) { VersionFetcher.new('v11.8.0', 'gitlab-org/gitlab-ce') }
before do
allow(URI).to receive(:parse).and_return(uri_response)
allow(uri_response).to receive(:read).and_return("1.2.3\n")
allow(version_fetcher).to receive_message_chain(:open, :read).and_return("1.2.3\n")
allow(version_fetcher).to receive(:gitlab_shell).and_call_original
allow(version_fetcher).to receive(:gitaly).and_call_original
end
......@@ -17,15 +50,16 @@ describe VersionFetcher do
it 'returns correct value' do
expect(version_fetcher.gitlab_shell).to eq('1.2.3')
end
end
describe '#gitaly' do
it 'returns correct value' do
expect(version_fetcher.fetch('gitaly')).to eq('1.2.3')
expect(version_fetcher.gitaly).to eq('1.2.3')
end
end
describe '#fetch' do
it 'callse subchart methods' do
it 'calls subchart methods' do
expect(version_fetcher).to receive(:gitlab_shell)
expect(version_fetcher).to receive(:gitaly)
version_fetcher.fetch('gitlab-shell')
......
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