Class: Janky::Build

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/janky/build.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.base_urlObject

The full URL of the web app as a String, including the protocol.



224
225
226
# File 'lib/janky/build.rb', line 224

def base_url
  @base_url
end

.urlObject (readonly)

The full URL to the Jenkins build page, as a String.



227
228
229
# File 'lib/janky/build.rb', line 227

def url
  @url
end

Class Method Details

.complete(id, green) ⇒ Object

Transition the Build to the completed state.

id - the Fixnum ID used to find the build. green - Boolean indicating build success.

Returns nothing or raises an Error for inexistant builds.



38
39
40
41
42
43
44
# File 'lib/janky/build.rb', line 38

def self.complete(id, green)
  if build = find_by_id(id)
    build.complete(green, Time.now)
  else
    raise Error, "Unknown build: #{id.inspect}"
  end
end

.completedObject

Find all completed builds, most recent first.

Returns an Array of Builds.



63
64
65
66
# File 'lib/janky/build.rb', line 63

def self.completed
  started.
    where("completed_at IS NOT NULL")
end

.greenObject

Find all green builds, most recent first.

Returns an Array of Builds.



71
72
73
# File 'lib/janky/build.rb', line 71

def self.green
  completed.where(:green => true)
end

.queuedObject

Find all builds that have been queued in Jenkins, most recent first.

Returns an Array of Build objects.



49
50
51
# File 'lib/janky/build.rb', line 49

def self.queued
  where("queued_at IS NOT NULL").order("queued_at DESC, id DESC")
end

.start(id, url) ⇒ Object

Transition the Build to the started state.

id - the Fixnum ID used to find the build. url - the full String URL of the build.

Returns nothing or raises an Error for inexistant builds.



24
25
26
27
28
29
30
# File 'lib/janky/build.rb', line 24

def self.start(id, url)
  if build = find_by_id(id)
    build.start(url, Time.now)
  else
    raise Error, "Unknown build: #{id.inspect}"
  end
end

.startedObject

Find all started builds, most recent first.

Returns an Array of Builds.



56
57
58
# File 'lib/janky/build.rb', line 56

def self.started
  where("started_at IS NOT NULL").order("started_at DESC, id DESC")
end

Instance Method Details

#branch_nameObject



294
295
296
# File 'lib/janky/build.rb', line 294

def branch_name
  branch.name
end

#branch_urlObject

URL of the web page for this build’s branch, served by Janky::App.

Returns the URL as a String.



241
242
243
244
# File 'lib/janky/build.rb', line 241

def branch_url
  return if new_record?
  self.class.base_url + "#{repo_name}/#{branch_name}"
end

#builderObject

See Repository#builder.



127
128
129
# File 'lib/janky/build.rb', line 127

def builder
  branch.repository.builder
end

#building?Boolean

Is this build currently being built?

Returns a Boolean.

Returns:

  • (Boolean)


93
94
95
# File 'lib/janky/build.rb', line 93

def building?
  started? && !completed?
end

#commit_authorObject



286
287
288
# File 'lib/janky/build.rb', line 286

def commit_author
  commit.author
end

#commit_messageObject



282
283
284
# File 'lib/janky/build.rb', line 282

def commit_message
  commit.message
end

#commit_urlObject



278
279
280
# File 'lib/janky/build.rb', line 278

def commit_url
  commit.url
end

#complete(green, now) ⇒ Object

Mark the build as complete, store the build output and notify Campfire.

green - Boolean indicating build success. now - the Time at which the build completed.

Returns nothing or raise an Error for weird transitions.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/janky/build.rb', line 189

def complete(green, now)
  if ! started?
    raise Error, "Build #{id} not started"
  elsif completed?
    raise Error, "Build #{id} already completed"
  else
    update_attributes!(
      :green        => green,
      :completed_at => now,
      :output       => output_remote
    )
    Notifier.completed(self)
  end
end

#completed?Boolean

Did this build complete?

Returns a Boolean.

Returns:

  • (Boolean)


114
115
116
# File 'lib/janky/build.rb', line 114

def completed?
  ! completed_at.nil?
end

#durationObject

The time it took to peform this build in seconds.

Returns an Integer seconds.



207
208
209
210
211
# File 'lib/janky/build.rb', line 207

def duration
  if completed?
    Integer(completed_at - started_at)
  end
end

#numberObject



290
291
292
# File 'lib/janky/build.rb', line 290

def number
  id.to_s
end

#outputObject

Cached or remote build output.

Returns the String output.



147
148
149
150
151
152
153
154
155
# File 'lib/janky/build.rb', line 147

def output
  if completed?
    read_attribute(:output)
  elsif started?
    output_remote
  else
    ""
  end
end

#output_remoteObject

Retrieve the build output from the Jenkins server.

Returns the String output.



160
161
162
163
164
# File 'lib/janky/build.rb', line 160

def output_remote
  if started?
    builder.output(self)
  end
end

#pending?Boolean

Is this build currently sitting in the queue waiting to be built?

Returns true if the build is queued and not started, false otherwise.

Returns:

  • (Boolean)


86
87
88
# File 'lib/janky/build.rb', line 86

def pending?
  queued? && !started?
end

#queued?Boolean

Has this build been queued in Jenkins?

Returns true when the build is complete or currently being built,

false otherwise.

Returns:

  • (Boolean)


79
80
81
# File 'lib/janky/build.rb', line 79

def queued?
  ! queued_at.nil?
end

#red?Boolean

Is this build red?

Returns a Boolean, nothing when the build hasn’t completed yet.

Returns:

  • (Boolean)


100
101
102
# File 'lib/janky/build.rb', line 100

def red?
  completed? && !green?
end

#repoObject



266
267
268
# File 'lib/janky/build.rb', line 266

def repo
  branch.repository
end

#repo_idObject



246
247
248
# File 'lib/janky/build.rb', line 246

def repo_id
  repository.id
end

#repo_job_nameObject



250
251
252
# File 'lib/janky/build.rb', line 250

def repo_job_name
  repository.job_name
end

#repo_nameObject



254
255
256
# File 'lib/janky/build.rb', line 254

def repo_name
  repository.name
end

#repo_nwoObject



258
259
260
# File 'lib/janky/build.rb', line 258

def repo_nwo
  repository.nwo
end

#repositoryObject



262
263
264
# File 'lib/janky/build.rb', line 262

def repository
  branch.repository
end

#rerun(new_room_id = nil) ⇒ Object

Run a copy of itself. Typically used to force a build in case of temporary test failure or when auto-build is disabled.

new_room_id - optional Campfire room String ID. Defaults to the room of the

build being re-run.

Returns the build copy.



138
139
140
141
142
# File 'lib/janky/build.rb', line 138

def rerun(new_room_id = nil)
  build = branch.build_for(commit, new_room_id)
  build.run
  build
end

#room_nameObject

The name of the Campfire room where notifications are sent.

Returns the String room name.



216
217
218
219
220
# File 'lib/janky/build.rb', line 216

def room_name
  if room_id && !room_id.empty?
    ChatService.room_name(room_id)
  end
end

#runObject

Trigger a Jenkins build using the appropriate builder.

Returns nothing.



121
122
123
124
# File 'lib/janky/build.rb', line 121

def run
  builder.run(self)
  update_attributes!(:queued_at => Time.now)
end

#sha1Object



270
271
272
# File 'lib/janky/build.rb', line 270

def sha1
  commit.sha1
end

#short_sha1Object



274
275
276
# File 'lib/janky/build.rb', line 274

def short_sha1
  sha1[0,7]
end

#start(url, now) ⇒ Object

Mark the build as started.

url - the full String URL of the build on the Jenkins server. now - the Time at which the build started.

Returns nothing or raise an Error for weird transitions.



172
173
174
175
176
177
178
179
180
181
# File 'lib/janky/build.rb', line 172

def start(url, now)
  if started?
    raise Error, "Build #{id} already started"
  elsif completed?
    raise Error, "Build #{id} already completed"
  else
    update_attributes!(:url => url, :started_at => now)
    Notifier.started(self)
  end
end

#started?Boolean

Was this build ever started?

Returns a Boolean.

Returns:

  • (Boolean)


107
108
109
# File 'lib/janky/build.rb', line 107

def started?
  ! started_at.nil?
end

#web_urlObject

URL of this build’s web page, served by Janky::App.

Returns the URL as a String.



233
234
235
236
# File 'lib/janky/build.rb', line 233

def web_url
  return if new_record?
  self.class.base_url + "#{id}/output"
end