-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjob_board.rb
More file actions
84 lines (70 loc) · 1.95 KB
/
job_board.rb
File metadata and controls
84 lines (70 loc) · 1.95 KB
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
# frozen_string_literal: true
require 'faraday'
require 'json'
module Travis
module Service
class JobBoard < Struct.new(:job_id, :data, :config, :logger)
PATH = '/jobs/add'
MSGS = {
response: 'POST to %<url>s responded %<status>s %<info>s'
}.freeze
LEVEL = {
201 => :info,
204 => :warn
}.freeze
INFO = {
201 => 'job %<id>s created',
204 => 'job %<id>s already exists',
400 => 'bad request: %<msg>s',
412 => 'site header missing',
401 => 'auth header missing',
403 => 'auth header invalid',
500 => 'internal error'
}.freeze
def post
response = http.post(PATH, JSON.dump(payload))
log response.status
rescue Faraday::ClientError, Faraday::ServerError => e
log e.response[:status], e.response[:body]
raise
rescue Faraday::ServerError => e
log e.response[:status], e.response[:body]
raise
end
private
def payload
{ '@type' => 'job', 'id' => job_id, 'data' => data }
end
def http
Faraday.new(url: host, headers:, ssl: ssl_options) do |c|
# c.response :logger
c.request :authorization, :basic, *auth.split(':')
c.request :retry
c.response :raise_error
c.adapter :net_http
end
end
def host
config.job_board[:url]
end
def auth
config.job_board[:auth]
end
def headers
{
'Content-Type' => 'application/json',
'Travis-Site' => Scheduler.config.site
}
end
def ssl_options
{}
end
def log(status, msg = nil)
level = LEVEL[status] || :error
info = format((INFO[status] || ''), id: job_id, msg:)
logger.send level,
format(MSGS[:response], url: [host, PATH].join, status:, info: info ? "(#{info})" : nil)
end
end
end
end