Skip to content

Commit 4ebb217

Browse files
author
Shairyar Baig
authored
Merge pull request #1132 from travis-ci/epic-fast-track-build
Fast track feature going live
2 parents c040a15 + ac0e148 commit 4ebb217

File tree

20 files changed

+325
-31
lines changed

20 files changed

+325
-31
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22

33
import:
4-
- travis-ci/build-configs:db-setup.yml
4+
- travis-ci/build-configs:db-setup.yml@bionic
55

66
rvm: 2.6.5
77

lib/travis/api/v3/models/build.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ class Models::BuildConfig < Model
33
end
44

55
class Models::Build < Model
6+
7+
HIGH_PRIORITY = 5
8+
69
belongs_to :commit
710
belongs_to :tag
811
belongs_to :pull_request
@@ -30,6 +33,8 @@ class Models::Build < Model
3033
primary_key: [:repository_id, :branch],
3134
class_name: 'Travis::API::V3::Models::Branch'.freeze
3235

36+
scope :running_builds, -> { where.not(state: ['passed', 'failed', 'errored', 'cancelled']) }
37+
3338
def created_by
3439
return unless sender
3540
sender.becomes(created_by_class)
@@ -88,5 +93,9 @@ def clear_debug_options!
8893
def log_complete
8994
jobs.all?(&:log_complete)
9095
end
96+
97+
def high_priority?
98+
jobs.where(priority: HIGH_PRIORITY).present?
99+
end
91100
end
92101
end

lib/travis/api/v3/models/organization.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def education
2121
Travis::Features.owner_active?(:educational_org, self)
2222
end
2323

24+
def builds
25+
Models::Build.where(owner_type: 'Organization', owner_id: id)
26+
end
27+
28+
def build_priorities_enabled?
29+
Travis::Features.owner_active?(:build_priorities_org, self)
30+
end
31+
2432
alias members users
2533
end
2634
end

lib/travis/api/v3/permissions/build.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ def cancel?
99
def restart?
1010
restartable?
1111
end
12+
13+
def prioritize?
14+
read? && build_priorities?
15+
end
1216
end
1317
end

lib/travis/api/v3/permissions/generic.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,10 @@ def adminable?
7575
access_control.adminable? object
7676
end
7777

78+
def build_priorities?
79+
return false if object.owner_type != 'Organization'
80+
object.owner.build_priorities_enabled?
81+
end
82+
7883
end
7984
end

lib/travis/api/v3/permissions/job.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ def debug?
1717
def delete_log?
1818
write?
1919
end
20+
21+
def prioritize?
22+
read? && build_priorities?
23+
end
2024
end
2125
end

lib/travis/api/v3/queries/build.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33

44
module Travis::API::V3
55
class Queries::Build < Query
6-
params :id
6+
params :id, :cancel_all
7+
8+
PRIORITY = { high: 5, low: -5, medium: nil }
79

810
def find
911
return Models::Build.find_by_id(id) if id
1012
raise WrongParams, 'missing build.id'.freeze
1113
end
1214

13-
def cancel(user)
15+
def cancel(user, build_id)
1416
raise BuildNotCancelable if %w(passed failed canceled errored).include? find.state
1517

16-
payload = { id: id, user_id: user.id, source: 'api' }
17-
service = Travis::Enqueue::Services::CancelModel.new(user, { build_id: id })
18+
payload = { id: build_id, user_id: user.id, source: 'api' }
19+
service = Travis::Enqueue::Services::CancelModel.new(user, { build_id: build_id })
1820
service.push("build:cancel", payload)
1921
payload
2022
end
@@ -33,5 +35,15 @@ def restart(user)
3335
payload
3436
end
3537
end
38+
39+
def prioritize_and_cancel(user)
40+
raise NotFound, "Jobs are not found" if find.jobs.blank?
41+
find.jobs.update_all(priority: PRIORITY[:high])
42+
return if find.owner_type != "Organization"
43+
if bool(cancel_all)
44+
low_priority_builds = find.owner.builds.running_builds.select { |build| !build.high_priority? }
45+
low_priority_builds.each { |build| cancel(user, build.id) } if low_priority_builds
46+
end
47+
end
3648
end
3749
end

lib/travis/api/v3/renderer/build.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
module Travis::API::V3
22
class Renderer::Build < ModelRenderer
3-
representation(:minimal, :id, :number, :state, :duration, :event_type, :previous_state, :pull_request_title, :pull_request_number, :started_at, :finished_at, :private)
3+
representation(:minimal, :id, :number, :state, :duration, :event_type, :previous_state, :pull_request_title, :pull_request_number, :started_at, :finished_at, :private, :priority)
44
representation(:standard, *representations[:minimal], :repository, :branch, :tag, :commit, :jobs, :stages, :created_by, :updated_at)
55
representation(:active, *representations[:standard])
66

77
hidden_representations(:active)
88

9+
HIGH_PRIORITY = 5
10+
911
def self.available_attributes
1012
super + ['request', 'log_complete']
1113
end
@@ -44,6 +46,10 @@ def log_complete
4446
end
4547
end
4648

49+
def priority
50+
model.jobs.where(priority: HIGH_PRIORITY).present?
51+
end
52+
4753
private def created_by_href(creator)
4854
case creator
4955
when V3::Models::Organization then Renderer.href(:organization, script_name: script_name, id: creator.id)

lib/travis/api/v3/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Routes
1515

1616
post :cancel, '/cancel'
1717
post :restart, '/restart'
18+
post :priority, '/priority'
1819

1920
resource :jobs do
2021
route '/jobs'

lib/travis/api/v3/services/build/cancel.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def run
55
build = check_login_and_find(:build)
66
access_control.permissions(build).cancel!
77

8-
query.cancel(access_control.user)
8+
query.cancel(access_control.user, build.id)
99
accepted(build: build, state_change: :cancel)
1010
end
1111
end

0 commit comments

Comments
 (0)