Skip to content

Commit 26490c7

Browse files
authored
Merge branch 'master' into epic-fast-track-build
2 parents e611478 + c040a15 commit 26490c7

File tree

8 files changed

+76
-36
lines changed

8 files changed

+76
-36
lines changed

lib/travis/api/app/middleware/user_agent_tracker.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,33 @@ class UserAgentTracker < Middleware
1111
"Opera", "Mozilla"
1212
]
1313

14+
PATHS_IGNORED = [
15+
'/uptime'
16+
]
17+
18+
attr_reader :metrik_prefix
19+
20+
before do
21+
@metrik_prefix = if version = request.env['HTTP_TRAVIS_API_VERSION'].to_s.match(/^\d+(\.\d+)*/)
22+
"api.v#{version}"
23+
else
24+
"api.v2"
25+
end
26+
end
27+
1428
before(agent: /^$/) do
15-
::Metriks.meter("api.v2.user_agent.missing").mark
29+
return if PATHS_IGNORED.include?(request.path)
30+
::Metriks.meter("#{metrik_prefix}.user_agent.missing").mark
1631
halt(400, "error" => "missing User-Agent header") if Travis::Features.feature_active?(:require_user_agent)
1732
end
1833

1934
before(agent: /^.+$/) do
35+
return if PATHS_IGNORED.include?(request.path)
2036
agent = UserAgent.parse(request.user_agent)
2137
case agent.browser
22-
when *WEB_BROWSERS then mark_browser
38+
when *WEB_BROWSERS
39+
# if X-User-Agent header is set, honor that instead
40+
mark :browser, UserAgent.parse(env['HTTP_X_USER_AGENT'] || request.user_agent).browser
2341
when "curl", "Wget" then mark(:console, agent.browser)
2442
when "travis-api-wrapper" then mark(:script, :node_js, agent.browser)
2543
when "TravisPy" then mark(:script, :python, agent.browser)
@@ -30,12 +48,6 @@ class UserAgentTracker < Middleware
3048
end
3149
end
3250

33-
def mark_browser
34-
# allows a JavaScript Client to set X-User-Agent, for instance to "travis-web" in travis-web
35-
x_agent = UserAgent.parse(env['HTTP_X_USER_AGENT'] || 'unknown').browser
36-
mark(:browser, x_agent)
37-
end
38-
3951
def mark_travis(agent)
4052
command = agent.application.comment.detect { |c| c.start_with? "command " } if agent.application.comment
4153

@@ -54,7 +66,7 @@ def mark_unknown
5466
end
5567

5668
def mark(*keys)
57-
key = "api.v2.user_agent." << keys.map { |k| k.to_s.downcase.gsub(/[^a-z0-9\-\.]+/, '_') }.join('.')
69+
key = "#{metrik_prefix}.user_agent." << keys.map { |k| k.to_s.downcase.gsub(/[^a-z0-9\-\.]+/, '_') }.join('.')
5870
::Metriks.meter(key).mark
5971
end
6072
end

lib/travis/api/v3/github.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def deactivate_service_hook(repo)
8888
end
8989

9090
def service_hook(repo)
91-
hooks(repo).detect { |h| h['name'] == 'travis' && h.dig('config', 'domain') == service_hook_url.host }
91+
hooks(repo).detect { |h| h['name'] == 'travis' && h.dig('config', 'domain') == service_hook_url.to_s }
9292
end
9393

9494
def service_hook_url?(repo)

lib/travis/api/v3/services/user_setting/update.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ class Services::UserSetting::Update < Service
55

66
def run!
77
repository = check_login_and_find(:repository)
8-
user_setting = query.update(repository)
98
return repo_migrated if migrated?(repository)
10-
9+
10+
user_setting = query.find(repository)
1111
access_control.permissions(user_setting).write!
12+
13+
user_setting = query.update(repository)
1214
result user_setting
1315
end
1416
end

spec/unit/middleware/user_agent_tracker_spec.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
mock_app do
44
use Travis::Api::App::Middleware::UserAgentTracker
55
get('/') { 'ok' }
6+
get('/uptime') { 'OK' }
67
end
78
end
89

910
def expect_meter(name)
1011
allow(Metriks).to receive(:meter).with(name).and_return(double("meter", mark: nil))
1112
end
1213

13-
def get(env = {})
14+
def get(env = {}, path = '/')
1415
env['HTTP_USER_AGENT'] ||= agent if agent
15-
super('/', {}, env)
16+
super(path, {}, env)
1617
end
1718

1819
context 'missing User-Agent' do
@@ -27,13 +28,18 @@ def get(env = {})
2728
allow(Travis::Features).to receive(:feature_active?).with(:require_user_agent).and_return(true)
2829
expect(get.status).to eq(400)
2930
end
31+
32+
specify do
33+
expect(Metriks).to_not receive(:meter)
34+
get({}, '/uptime')
35+
end
3036
end
3137

3238
context 'web browser' do
3339
let(:agent) { "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.36 Safari/537.36" }
3440

3541
specify 'without X-User-Agent' do
36-
expect_meter("api.v2.user_agent.browser.unknown")
42+
expect_meter("api.v2.user_agent.browser.chrome")
3743
get
3844
end
3945

@@ -89,6 +95,18 @@ def get(env = {})
8995
expect_meter("api.v2.user_agent.script.ruby.travis.version.1.6.8")
9096
get
9197
end
98+
99+
context 'Travis-API-Version header' do
100+
specify "with Travis-API-Version: 3" do
101+
expect_meter("api.v3.user_agent.script.ruby.travis.version.1.6.8")
102+
get('HTTP_TRAVIS_API_VERSION' => '3')
103+
end
104+
105+
specify "with Travis-API-Version: 1.7f" do
106+
expect_meter("api.v1.7.user_agent.script.ruby.travis.version.1.6.8")
107+
get('HTTP_TRAVIS_API_VERSION' => '1.7')
108+
end
109+
end
92110
end
93111

94112
context 'Travis CLI' do
@@ -99,4 +117,12 @@ def get(env = {})
99117
get
100118
end
101119
end
120+
121+
context 'get /uptime' do
122+
let(:agent) { 'Travis/1.6.8 (Mac OS X 10.9.2 like Darwin; Ruby 2.1.1p42; RubyGems 2.0.14) Faraday/0.8.9 Typhoeus/0.6.7' }
123+
specify do
124+
expect(Metriks).to_not receive(:meter)
125+
get({}, '/uptime')
126+
end
127+
end
102128
end

spec/v3/services/repository/activate_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
stub_request(:get, "https://api.github.com/repositories/#{repo.github_id}/hooks?per_page=100").to_return(
7272
status: 200, body: JSON.dump(
7373
[
74-
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'url.of.listener.something' } },
74+
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'https://url.of.listener.something' } },
7575
{ name: 'web', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/456", config: { url: Travis.config.service_hook_url } }
7676
]
7777
)

spec/v3/services/repository/deactivate_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
stub_request(:get, "https://api.github.com/repositories/#{repo.github_id}/hooks?per_page=100").to_return(
6363
status: 200, body: JSON.dump(
6464
[
65-
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'url.of.listener.something' } },
65+
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'https://url.of.listener.something' } },
6666
{ name: 'web', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/456", config: { url: Travis.config.service_hook_url } }
6767
]
6868
)
@@ -104,7 +104,7 @@
104104
stub_request(:get, "https://api.github.com/repositories/#{repo.github_id}/hooks?per_page=100").to_return(
105105
status: 200, body: JSON.dump(
106106
[
107-
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'url.of.listener.something' } }
107+
{ name: 'travis', url: "https://api.github.com/repositories/#{repo.github_id}/hooks/123", config: { domain: 'https://url.of.listener.something' } }
108108
]
109109
)
110110
)

spec/v3/services/user_setting/update_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
'@href' => "/v3/repo/#{repo.id}/setting/build_pushes",
9797
'@representation' => 'minimal',
9898
'name' => 'build_pushes',
99-
'value' => false
99+
'value' => true
100100
}
101101
)
102102
end

spec/v3/services/user_settings/for_repository_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let(:auth_headers) { { 'HTTP_AUTHORIZATION' => "token #{token}" } }
55
let(:json_headers) { { 'CONTENT_TYPE' => 'application/json' } }
66

7-
before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, pull: true, admin: false) }
7+
before { Travis::API::V3::Models::Permission.create(repository: repo, user: repo.owner, pull: true, push: true, admin: false) }
88

99
describe 'not authenticated' do
1010
before { get("/v3/repo/#{repo.id}/settings") }
@@ -42,13 +42,13 @@
4242
'@href' => "/v3/repo/#{repo.id}/settings",
4343
'@representation' => 'standard',
4444
'settings' => [
45-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/builds_only_with_travis_yml", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'builds_only_with_travis_yml', 'value' => false },
46-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'build_pushes', 'value' => true },
47-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'build_pull_requests', 'value' => true },
48-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/maximum_number_of_builds", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'maximum_number_of_builds', 'value' => 0 },
49-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'auto_cancel_pushes', 'value' => false },
50-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
51-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'config_validation', 'value' => false },
45+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/builds_only_with_travis_yml", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'builds_only_with_travis_yml', 'value' => false },
46+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'build_pushes', 'value' => true },
47+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'build_pull_requests', 'value' => true },
48+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/maximum_number_of_builds", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'maximum_number_of_builds', 'value' => 0 },
49+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pushes', 'value' => false },
50+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
51+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'config_validation', 'value' => false },
5252
]
5353
)
5454
end
@@ -60,7 +60,7 @@
6060

6161
example do
6262
expect(JSON.load(body)['settings']).to include(
63-
{ '@type' => 'setting', '@permissions' => { 'read' => true, 'write' => false }, '@href' => "/v3/repo/#{repo.id}/setting/allow_config_imports", '@representation' => 'standard', 'name' => 'allow_config_imports', 'value' => false },
63+
{ '@type' => 'setting', '@permissions' => { 'read' => true, 'write' => true }, '@href' => "/v3/repo/#{repo.id}/setting/allow_config_imports", '@representation' => 'standard', 'name' => 'allow_config_imports', 'value' => false },
6464
)
6565
end
6666
end
@@ -79,13 +79,13 @@
7979
'@href' => "/v3/repo/#{repo.id}/settings",
8080
'@representation' => 'standard',
8181
'settings' => [
82-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/builds_only_with_travis_yml", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'builds_only_with_travis_yml', 'value' => false },
83-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'build_pushes', 'value' => false },
84-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'build_pull_requests', 'value' => true },
85-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/maximum_number_of_builds", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'maximum_number_of_builds', 'value' => 0 },
86-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'auto_cancel_pushes', 'value' => false },
87-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
88-
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => false }, 'name' => 'config_validation', 'value' => false },
82+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/builds_only_with_travis_yml", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'builds_only_with_travis_yml', 'value' => false },
83+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'build_pushes', 'value' => false },
84+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/build_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'build_pull_requests', 'value' => true },
85+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/maximum_number_of_builds", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'maximum_number_of_builds', 'value' => 0 },
86+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pushes", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pushes', 'value' => false },
87+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/auto_cancel_pull_requests", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'auto_cancel_pull_requests', 'value' => false },
88+
{ '@type' => 'setting', '@href' => "/v3/repo/#{repo.id}/setting/config_validation", '@representation' => 'standard', '@permissions' => { 'read' => true, 'write' => true }, 'name' => 'config_validation', 'value' => false },
8989
]
9090
)
9191
end
@@ -104,7 +104,7 @@
104104
'@type' => 'setting',
105105
'@href' => "/v3/repo/#{repo.id}/setting/build_pushes",
106106
'@representation' => 'standard',
107-
'@permissions' => { 'read' => true, 'write' => false },
107+
'@permissions' => { 'read' => true, 'write' => true },
108108
'name' => 'build_pushes',
109109
'value' => false
110110
)

0 commit comments

Comments
 (0)