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
160
161
162
163
164
165
166
|
# frozen_string_literal: true
module QA
module Vendor
module Jenkins
class Job
include Helpers
REQUIRED_BUILD_FIELDS = %i[name description shell_command].freeze
attr_accessor(
:name,
:description,
:keep_deps,
:can_roam,
:disabled,
:repo_url,
:gitlab_connection,
:shell_command
)
# Prefer Jenkins::Client#jobs and Jenkins::Client.create_job over this constructor
#
# @param name [String] the name of the job
# @param client [Jenkins::Client] the jenkins client
def initialize(name, client)
@name = name
@client = client
end
# Saves the Job in Jenkins
def create
validate_required_fields!
response = @client.post_xml(build, path: '/createItem', params: { name: name })
check_network_error(response)
response.body
end
# Triggers a build for the job
def run
@client.build(@name)
end
# Returns the jobs last build status
def status
@client.last_build_status(@name)
end
# Returns the jobs last log
#
# @param start [Integer] the log offset to query
def log(start: 0)
@client.last_build_log(@name, start)
end
# Returns whether the job is running
#
# @return [Boolean]
def running?
@client.job_running?(@name)
end
# Returns the count of active builds
#
# @return [Integer]
def active_runs
@client.number_of_jobs_running(@name)
end
private
def validate_required_fields!
error = REQUIRED_BUILD_FIELDS.each_with_object("") do |field, memo|
memo << "#{field} is required\n" unless send(field)
end
raise ArgumentError, error unless error.empty?
end
def build
builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.project do
xml.actions
xml.description description
xml.keepDependencies false
xml.properties do |props|
build_gitlab_connection(props)
end
xml.canRoam true
xml.disabled false
xml.blockBuildWhenDownstreamBuilding false
xml.blockBuildWhenUpstreamBuilding false
xml.triggers do |triggers|
build_gitlab_triggers(triggers)
end
xml.concurrentBuild false
xml.builders do
xml.send('hudson.tasks.Shell') do
xml.command shell_command
xml.configuredLocalRules
end
end
xml.publishers do |publishers|
build_gitlab_publishers(publishers)
end
xml.buildWrappers
build_scm(xml)
end
end
builder.to_xml
end
def build_scm(xml)
if repo_url
xml.scm(class: 'hudson.plugins.git.GitSCM') do
xml.userRemoteConfigs do
xml.send('hudson.plugins.git.UserRemoteConfig') do
xml.url repo_url
end
end
xml.branches do
xml.send('hudson.plugins.git.BranchSpec') do
xml.name
end
end
xml.configVersion 2
xml.doGenerateSubmoduleConfiguration false
xml.gitTool 'Default'
end
end
end
def build_gitlab_connection(xml)
if gitlab_connection
xml.send('com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty') do
xml.gitLabConnection gitlab_connection
end
end
end
def build_gitlab_triggers(xml)
if gitlab_connection
xml.send('com.dabsquared.gitlabjenkins.GitLabPushTrigger') do
xml.spec
xml.triggerOnPush true
xml.triggerOnMergeRequest true
xml.includeBranchesSpec 'main,master'
xml.branchFilterType 'NameBasedFilter'
xml.ciSkip true
end
end
end
def build_gitlab_publishers(xml)
if gitlab_connection
xml.send('com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher') do
xml.name 'jenkins'
xml.markUnstableAsSuccess false
end
end
end
end
end
end
end
|