-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathworker.rb
159 lines (141 loc) · 5.03 KB
/
worker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
module Travis
module Scheduler
module Serialize
class Worker < Struct.new(:job, :config)
require 'travis/scheduler/serialize/worker/build'
require 'travis/scheduler/serialize/worker/commit'
require 'travis/scheduler/serialize/worker/config'
require 'travis/scheduler/serialize/worker/job'
require 'travis/scheduler/serialize/worker/request'
require 'travis/scheduler/serialize/worker/repo'
require 'travis/scheduler/serialize/worker/ssh_key'
def data
data = {
type: :test,
vm_config: job.vm_config,
vm_type: repo.vm_type,
queue: job.queue,
config: job.decrypted_config,
env_vars: job.env_vars,
job: job_data,
host: Travis::Scheduler.config.host,
source: build_data,
repository: repository_data,
ssh_key: ssh_key.data,
timeouts: repo.timeouts,
cache_settings: cache_settings,
enterprise: !!config[:enterprise],
prefer_https: !!config[:prefer_https]
}
data[:trace] = true if job.trace?
data[:warmer] = true if job.warmer?
data[:oauth_token] = github_oauth_token if config[:prefer_https]
data
end
private
def build_data
{
id: build.id,
number: build.number,
event_type: build.event_type
}
end
def job_data
data = {
id: job.id,
number: job.number,
commit: commit.commit,
commit_range: commit.range,
commit_message: commit.message,
branch: commit.branch,
ref: commit.pull_request? ? commit.ref : nil,
tag: commit.tag,
pull_request: build.pull_request? ? build.pull_request_number : false,
state: job.state.to_s,
secure_env_enabled: job.secure_env?,
secure_env_removed: job.secure_env_removed?,
debug_options: job.debug_options || {},
queued_at: format_date(job.queued_at),
allow_failure: job.allow_failure,
stage_name: job.stage&.name,
name: job.name,
}
if build.pull_request?
data = data.merge(
pull_request_head_branch: request.pull_request_head_ref,
pull_request_head_sha: request.pull_request_head_sha,
pull_request_head_slug: request.pull_request_head_slug,
pull_request_title: request.pull_request_title,
)
end
data
end
def repository_data
compact(
id: repo.id,
github_id: repo.github_id,
installation_id: repo.installation_id,
private: repo.private?,
slug: repo.slug,
source_url: source_url,
source_host: source_host,
api_url: repo.api_url,
# TODO how come the worker needs all these?
last_build_id: repo.last_build_id,
last_build_number: repo.last_build_number,
last_build_started_at: format_date(repo.last_build_started_at),
last_build_finished_at: format_date(repo.last_build_finished_at),
last_build_duration: repo.last_build_duration,
last_build_state: repo.last_build_state.to_s,
default_branch: repo.default_branch,
description: repo.description
)
end
def source_url
# TODO move these things to Build
return repo.source_git_url if repo.private? && ssh_key.custom?
repo.source_url
end
def job
@job ||= Job.new(super, config)
end
def repo
@repo ||= Repo.new(job.repository, config)
end
def request
@request ||= Request.new(build.request)
end
def commit
@commit ||= Commit.new(job.commit)
end
def build
@build ||= Build.new(job.source)
end
def ssh_key
@ssh_key ||= SshKey.new(repo, job, config)
end
def source_host
config[:github][:source_host] || 'github.com'
end
def cache_settings
cache_config[job.queue].to_h if cache_config[job.queue]
end
def cache_config
config[:cache_settings] || {}
end
def format_date(date)
date && date.strftime('%Y-%m-%dT%H:%M:%SZ')
end
def github_oauth_token
scope = job.repository.users
scope = scope.where("github_oauth_token IS NOT NULL").order("updated_at DESC")
admin = scope.first
admin && admin.github_oauth_token
end
def compact(hash)
hash.reject { |_, value| value.nil? }
end
end
end
end
end