Skip to content

Commit

Permalink
Merge pull request #53 from travis-ci/ha-feature-prefer-https
Browse files Browse the repository at this point in the history
Let Travis.config.prefer_https override git URL scheme
  • Loading branch information
BanzaiMan authored Feb 14, 2017
2 parents 2d018a9 + f420d63 commit 32dd13f
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/travis/scheduler/serialize/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def data
repository: repository_data,
ssh_key: ssh_key,
timeouts: repo.timeouts,
cache_settings: cache_settings
cache_settings: cache_settings,
oauth_token: github_oauth_token
}
end

Expand Down Expand Up @@ -117,6 +118,13 @@ def cache_config
def format_date(date)
date && date.strftime('%Y-%m-%dT%H:%M:%SZ')
end

def github_oauth_token
candidates = job.repository.users.where("github_oauth_token IS NOT NULL").
order("updated_at DESC")
admin = candidates.first
admin && admin.github_oauth_token
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/scheduler/serialize/worker/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def api_url
end

def source_url
private? || force_private? ? source_git_url : source_http_url
((private? || force_private?) && !Travis.config.prefer_https) ? source_git_url : source_http_url
end

private
Expand Down
2 changes: 2 additions & 0 deletions spec/support/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

factory :user do
login 'svenfuchs'
github_oauth_token 'token'
end

factory :org, :class => 'Organization' do
Expand All @@ -21,6 +22,7 @@
owner { User.first || FactoryGirl.create(:user) }
owner_name { owner.login }
key { SslKey.create(public_key: REPO_KEY.public_key, private_key: REPO_KEY.to_pem) }
users { [ owner ] }

# TODO why is the worker payload interested in these at all?
last_build_id 1
Expand Down
34 changes: 34 additions & 0 deletions spec/travis/scheduler/serialize/worker/repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,39 @@
it { expect(subject.source_url).to eq 'git@localhost:travis-ci/travis-ci.git' }
end
end

context "when config prefers HTTPS source_url" do
before(:all) { @before = Travis.config.prefer_https }
before(:each) { Travis.config.prefer_https = true }
after(:all) { Travis.config.prefer_https = @before }

describe 'default source endpoint' do
before { config[:github][:source_host] = 'github.com' }

describe 'on a public repo' do
before { repo.private = false }
it { expect(subject.source_url).to eq 'https://github.com/travis-ci/travis-ci.git' }
end

describe 'on a private repo' do
before { repo.private = true }
it { expect(subject.source_url).to eq 'https://github.com/travis-ci/travis-ci.git' }
end
end

describe 'custom source endpoint' do
before { config[:github][:source_host] = 'localhost' }

describe 'on a public repo' do
before { repo.private = false }
it { expect(subject.source_url).to eq 'https://localhost/travis-ci/travis-ci.git' }
end

describe 'on a private repo' do
before { repo.private = true }
it { expect(subject.source_url).to eq 'https://localhost/travis-ci/travis-ci.git' }
end
end
end
end
end
71 changes: 69 additions & 2 deletions spec/travis/scheduler/serialize/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,74 @@ def encrypted(value)
hard_limit: 180 * 60, # worker handles timeouts in seconds
log_silence: 20 * 60
},
cache_settings: s3
cache_settings: s3,
oauth_token: 'token'
)
end

context 'when prefer_https is set' do
before { Travis.config.prefer_https = true }
after { Travis.config.prefer_https = false }

it 'contains the expected data' do
expect(data.except('job', 'build', 'repository')).to eq(
type: :test,
vm_type: :default,
queue: 'builds.gce',
config: {
rvm: '1.8.7',
gemfile: 'Gemfile.rails'
},
env_vars: [
{ name: 'FOO', value: 'foo', public: false },
{ name: 'BAR', value: 'bar', public: true }
],
job: {
id: job.id,
number: '2.1',
commit: '62aaef',
commit_range: '0cd9ff...62aaef',
commit_message: 'message',
branch: 'master',
ref: nil,
tag: 'v1.2.3',
pull_request: false,
state: 'queued',
secure_env_enabled: true,
debug_options: {},
queued_at: '2016-01-01T10:30:00Z',
allow_failure: false,
},
source: {
id: build.id,
number: '2',
event_type: 'push'
},
repository: {
id: repo.id,
github_id: 549743,
slug: 'svenfuchs/gem-release',
source_url: 'https://github.com/svenfuchs/gem-release.git',
api_url: 'https://api.github.com/repos/svenfuchs/gem-release',
last_build_id: 1,
last_build_started_at: '2016-01-01T10:00:00Z',
last_build_finished_at: '2016-01-01T11:00:00Z',
last_build_number: '2',
last_build_duration: 60,
last_build_state: 'passed',
default_branch: 'branch',
description: 'description',
},
ssh_key: nil,
timeouts: {
hard_limit: 180 * 60, # worker handles timeouts in seconds
log_silence: 20 * 60
},
cache_settings: s3,
oauth_token: 'token'
)
end
end
end

describe 'vm_type' do
Expand Down Expand Up @@ -181,7 +246,8 @@ def encrypted(value)
hard_limit: 180 * 60, # worker handles timeouts in seconds
log_silence: 20 * 60
},
cache_settings: s3
cache_settings: s3,
oauth_token: 'token'
)
end

Expand Down Expand Up @@ -249,3 +315,4 @@ def encrypted(value)
end
end
end

0 comments on commit 32dd13f

Please sign in to comment.