Skip to content

Commit d71a340

Browse files
AndriiMyskovitalie
authored andcommitted
[PRD] TCI/Assembla Handshake with SVN/P4 (#259)
1 parent b09edd4 commit d71a340

File tree

16 files changed

+101
-61
lines changed

16 files changed

+101
-61
lines changed

lib/travis/yml/configs/config/api.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ module Travis
55
module Yml
66
module Configs
77
module Config
8-
class Api < Struct.new(:ctx, :parent, :slug, :ref, :defns, :mode, :provider)
8+
class Api < Struct.new(:ctx, :parent, :slug, :ref, :defns, :mode, :provider, :vcs_id)
99
include Base, Memoize
1010

1111
attr_reader :defn, :path, :input
1212

13-
def initialize(ctx, parent, slug, ref, defns, mode = nil, provider = nil)
14-
super(ctx, parent, slug, ref, defns, mode, provider)
13+
def initialize(ctx, parent, slug, ref, defns, mode = nil, provider = nil, vcs_id = nil)
14+
super(ctx, parent, slug, ref, defns, mode, provider, vcs_id)
1515
@defn = defns.shift
1616
defn.update(source: source)
1717
end
@@ -74,11 +74,11 @@ def child
7474
end
7575

7676
def api
77-
Api.new(ctx, self, slug, ref, defns, defn[:mode], provider)
77+
Api.new(ctx, self, slug, ref, defns, defn[:mode], provider, vcs_id)
7878
end
7979

8080
def travis_yml
81-
TravisYml.new(ctx, self, slug, ref, defn[:mode], provider)
81+
TravisYml.new(ctx, self, slug, ref, defn[:mode], provider, vcs_id)
8282
end
8383
end
8484
end

lib/travis/yml/configs/config/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def travis_yml?
2525
end
2626

2727
def repo
28-
@repo ||= ctx.repos[slug, provider]
28+
@repo ||= ctx.repos[vcs_id, provider]
2929
end
3030

3131
def config
@@ -47,7 +47,7 @@ def imports
4747
imports = Array(config['import'])
4848
imports = imports.select { |import| import.is_a?(Hash) }
4949
imports.map do |import|
50-
Config::File.new(ctx, self, provider, import)
50+
Config::File.new(ctx, self, vcs_id, provider, import)
5151
end
5252
end
5353
memoize :imports

lib/travis/yml/configs/config/file.rb

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,25 @@ module Travis
55
module Yml
66
module Configs
77
module Config
8-
class File < Obj.new(:ctx, :parent, :provider, :defn)
8+
class File < Obj.new(:ctx, :parent, :vcs_id, :provider, :defn)
99
include Base
1010

1111
attr_reader :path, :ref, :raw
1212

13-
def initialize(ctx, parent, provider, defn)
13+
def initialize(ctx, parent, vcs_id, provider, defn)
1414
defn = stringify(defn)
15+
@vcs_id = vcs_id
1516
super
1617
end
1718

19+
def vcs_id
20+
if parent.nil? || (!parent.nil? && parent.repo.slug == slug)
21+
@vcs_id
22+
else
23+
slug
24+
end
25+
end
26+
1827
def load(&block)
1928
super
2029
_, @path, @ref = expand(source)
@@ -66,14 +75,33 @@ def serialize
6675

6776
private
6877

78+
def path_suffix
79+
return '' if repo.vcs_type != 'AssemblaRepository'
80+
81+
branch_name = ctx.data[:branch] || repo.default_branch
82+
83+
case ctx.data[:server_type]
84+
when 'subversion'
85+
if branch_name == 'trunk'
86+
"#{branch_name}/"
87+
elsif !ctx.data[:tag].nil?
88+
"tags/#{branch_name}/"
89+
else
90+
"branches/#{branch_name}/"
91+
end
92+
when 'perforce'
93+
"//depot/#{branch_name}/"
94+
end
95+
end
96+
6997
def expand(source)
7098
ref = local? ? parent&.ref : repo.default_branch
7199
ref = Ref.new(source, repo: repo.slug, ref: ref, path: parent&.path)
72100
ref.parts
73101
end
74102

75103
def fetch
76-
Content.new(repo, path, ref).content
104+
Content.new(repo, "#{path_suffix}#{path}", ref).content
77105
rescue FileNotFound => e
78106
required? ? raise : nil
79107
end

lib/travis/yml/configs/config/travis_yml.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module Yml
55
module Configs
66
module Config
77
class TravisYml < File
8-
def initialize(ctx, parent, slug, ref, mode = nil, provider = nil)
9-
super(ctx, parent, provider, source: "#{slug}:.travis.yml@#{ref}", mode: mode)
8+
def initialize(ctx, parent, slug, ref, mode = nil, provider = nil, vcs_id = nil)
9+
super(ctx, parent, vcs_id, provider, source: "#{slug}:.travis.yml@#{ref}", mode: mode)
1010
end
1111

1212
def travis_yml?

lib/travis/yml/configs/configs.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,17 @@ def unique_jobs
136136
end
137137

138138
def api
139-
Config::Api.new(ctx, nil, repo.slug, ref, defns, nil, repo.provider)
139+
Config::Api.new(ctx, nil, repo.slug, ref, defns, nil, repo.provider, repo.vcs_id)
140140
end
141141

142142
def travis_yml
143-
Config::TravisYml.new(ctx, nil, repo.slug, ref, nil, repo.provider)
143+
Config::TravisYml.new(ctx, nil, repo.slug, ref, nil, repo.provider, repo.vcs_id)
144144
end
145145

146146
def repo
147147
repo = Model::Repo.new(super || {})
148-
repo.complete? ? ctx.repos[repo.slug] = repo : ctx.repos[repo.slug, repo.provider]
148+
key = "#{repo.provider}_#{repo.vcs_id}"
149+
repo.complete? ? ctx.repos[key] = repo : ctx.repos[repo.vcs_id, repo.provider]
149150
end
150151
memoize :repo
151152

lib/travis/yml/configs/model/repo.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def github_id
1818
attrs[:github_id]
1919
end
2020

21+
def vcs_id
22+
attrs[:vcs_id] || github_id
23+
end
24+
2125
def slug
2226
attrs[:slug]
2327
end

lib/travis/yml/configs/model/repos.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,26 @@ def initialize
1919
@mutexes = {}
2020
end
2121

22-
def [](slug, provider = 'github')
23-
mutex_for(slug).synchronize do
24-
return repos[slug] if repos.key?(slug)
25-
repo = repos[slug] = Repo.new(fetch(slug, provider))
22+
def [](vcs_id, provider = 'github')
23+
key = "#{provider}_#{vcs_id}"
24+
mutex_for(key).synchronize do
25+
return repos[key] if repos.key?(key)
26+
repo = repos[key] = Repo.new(fetch(vcs_id, provider))
2627
repo
2728
end
2829
end
2930

30-
def []=(slug, repo)
31-
repos[slug] = repo
31+
def []=(key, repo)
32+
repos[key] = repo
3233
end
3334

34-
def fetch(slug, provider)
35-
logger.info "Get Repo for #{slug} #{provider}"
36-
Travis::Repo.new(slug, provider).fetch
35+
def fetch(vcs_id, provider)
36+
logger.info "Get Repo for #{vcs_id} #{provider}"
37+
Travis::Repo.new(vcs_id, provider).fetch
3738
end
3839

39-
def mutex_for(slug)
40-
mutexes[slug] ||= Mutex.new
40+
def mutex_for(key)
41+
mutexes[key] ||= Mutex.new
4142
end
4243
synchronize :mutex
4344

lib/travis/yml/configs/travis/repo.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,39 @@ module Travis
88
module Yml
99
module Configs
1010
module Travis
11-
class Repo < Struct.new(:slug, :provider)
11+
class Repo < Struct.new(:vcs_id, :provider)
1212
include Errors, Helper::Obj
1313

1414
def fetch
1515
get(path, representation: :internal)
1616
end
1717

1818
def to_s
19-
slug
19+
vcs_id
2020
end
2121

2222
private
2323

2424
def path
25-
"repo/#{provider}/#{url_encode(slug)}"
25+
if url_encode(vcs_id.to_s).match(%r{[^/]+%2[fF][^/]+})
26+
"repo/#{provider}/#{url_encode(vcs_id)}"
27+
else
28+
"repo_vcs/#{provider}/#{url_encode(vcs_id.to_s)}"
29+
end
2630
end
2731

2832
def get(path, opts)
2933
resp = client(opts).get(path, only(opts, :representation))
3034
map(Oj.load(resp.body) || {})
3135
rescue Error => e
32-
api_error('Travis CI', :repo, slug, e)
36+
api_error('Travis CI', :repo, vcs_id, e)
3337
end
3438

3539
def map(attrs)
3640
{
3741
id: attrs['id'],
3842
vcs_type: attrs['vcs_type'],
43+
vcs_id: attrs['vcs_id'],
3944
github_id: attrs['github_id'],
4045
slug: attrs['slug'],
4146
private: attrs['private'],

spec/support/webmock.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ def stub_content(id, path, data)
2323
stub_request(:get, url).to_return(body: body, status: status)
2424
end
2525

26-
def stub_repo(slug, data = {}, provider = 'github')
27-
url = "https://api.travis-ci.com/repo/#{provider}/#{slug.sub('/', '%2F')}"
26+
def stub_repo(vcs_id, slug, data: {}, provider: 'github', by_slug: false)
27+
url = by_slug ? "https://api.travis-ci.com/repo/#{provider}/#{CGI.escape(slug)}" : "https://api.travis-ci.com/repo_vcs/#{provider}/#{vcs_id}"
2828
url = "#{url}?representation=internal" if data[:internal]
2929

3030
body = data[:body] && JSON.dump(data[:body].merge(
3131
slug: slug,
32+
vcs_id: vcs_id,
3233
id: data[:id] || 1,
3334
default_branch: { name: data[:body][:default_branch] },
3435
user_settings: { settings: data[:body].delete(:config_imports) ? [name: 'allow_config_imports', value: true] : [] }

spec/travis/yml/configs/allow_failures_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
describe Travis::Yml::Configs, 'allow_failures' do
2-
let(:repo) { { id: 1, github_id: 1, slug: 'travis-ci/travis-yml', private: false, default_branch: 'master', token: 'repo-token', private_key: 'key', allow_config_imports: true } }
2+
let(:repo) { { id: 1, github_id: 1, vcs_id: 1, slug: 'travis-ci/travis-yml', private: false, default_branch: 'master', token: 'repo-token', private_key: 'key', allow_config_imports: true } }
33
let(:data) { {} }
44
let(:configs) { described_class.new(repo, 'ref', nil, data, {}) }
55

6-
before { stub_repo(repo[:slug], token: 'user-token') } # authorization
6+
before { stub_repo(repo[:vcs_id], repo[:slug], data: { token: 'user-token' }) } # authorization
77
before { stub_content(repo[:id], '.travis.yml', yaml) }
88

99
subject { configs.tap(&:load).jobs }

0 commit comments

Comments
 (0)