Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
add timeout thread. remove culprits
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Dec 18, 2012
1 parent 5f996a2 commit a2b575e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 84 deletions.
36 changes: 21 additions & 15 deletions application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#
get '/projects.json' do
load_projects

content_type 'application/json'
render :rabl, :index, :format => 'json'
end

Expand Down Expand Up @@ -46,23 +48,27 @@ def load_projects
@servers ||= YAML::load(File.read('config/servers.yml'))

@projects = @servers.collect do |server|
server_projects = get_server(server).projects
Thread.new do
server_projects = Timeout::timeout(5) do
get_server(server).projects
end

# if server['projects'] is defined, we only want those projects
if server['projects']
regexes = collect_regexes(server['projects'])
server_projects.select { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
elsif server['ignored_projects']
regexes = collect_regexes(server['ignored_projects'])
server_projects.reject { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
else
server_projects
# if server['projects'] is defined, we only want those projects
Thread.current['projects'] = if server['projects']
regexes = collect_regexes(server['projects'])
server_projects.select { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
elsif server['ignored_projects']
regexes = collect_regexes(server['ignored_projects'])
server_projects.reject { |server_project|
regexes.any?{ |regex| server_project.name =~ regex }
}
else
server_projects
end
end
end.compact.flatten.sort{ |a,b| a.name.downcase <=> b.name.downcase }
end.map(&:join).collect{ |t| t['projects'] }.compact.flatten.sort
end

def collect_regexes(projects)
Expand Down
1 change: 1 addition & 0 deletions config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler'
require 'timeout'

Bundler.require
require 'active_support/inflector'
Expand Down
7 changes: 0 additions & 7 deletions config/servers.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
# The `projects` and `ignored_projects` options are optional. By default, Stoplight
# will use all projects returned from the given url. If you want to blacklist,
# use `ignored_projects`. If you want to whitelist, use `projects`.
#
# The `culprits` option is false by default. If enabled, it will provide
# show the gravatars of the user's who broke the builds. However, for most
# providers, this entails making additional HTTP request, which can slow
# Stoplight's performance.

# Example Jenkis configuration
# -
# type: 'jenkins'
# url: https://ci.jenkins-ci.org
# username: my_username
# password: my_password
# culprits: false
# projects:
# - mail # build named "mail"
# - /^test/ # all builds starting with "test"
Expand All @@ -31,7 +25,6 @@
# type: 'travis'
# url: http://travis-ci.org
# owner_name: my_projects # usually your github username
# culprits: true
# projects:
# - mail # build named "mail"
# - /^test/ # all builds starting with "test"
Expand Down
9 changes: 5 additions & 4 deletions lib/stoplight/project.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Stoplight
class Project
attr_accessor :name, :build_url, :last_build_id, :last_build_time, :last_build_status, :current_status, :culprits
attr_accessor :name, :build_url, :last_build_id, :last_build_time, :last_build_status, :current_status

# Initialize (new) takes in a hash of options in the following format:
#
Expand All @@ -26,8 +26,6 @@ class Project
# - -1: unknown
# - 0: done (sleeping, waiting)
# - 1: building (building, working, compiling)
# - `culprits` - the people who broke the build. This should be a hash in the following format:
# { 'name' => '...', 'gravatar' => '...' }
def initialize(options = {})
@options = options

Expand All @@ -37,7 +35,6 @@ def initialize(options = {})
@last_build_time = parse_date(options[:last_build_time])
@last_build_status = parse_last_build_status(options[:last_build_status])
@current_status = parse_current_status(options[:current_status])
@culprits = options[:culprits]
end

def building?
Expand All @@ -56,6 +53,10 @@ def failed?
@last_build_status == 'failed'
end

def <=>(other_project)
self.name.downcase <=> other_project.name.downcase
end

private
# returns the correct time from the given project time
def parse_date(date_time)
Expand Down
30 changes: 2 additions & 28 deletions lib/stoplight/providers/jenkins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,17 @@ def projects
@projects ||= []
else
# Jenkins doesn't return an array when there's only one job...
@projects ||= [@response.parsed_response['Projects']['Project']].flatten.collect do |project|
@projects ||= [ @response.parsed_response['Projects']['Project'] ].flatten.collect do |project|
Stoplight::Project.new({
:name => project['name'],
:build_url => project['webUrl'],
:last_build_id => project['lastBuildLabel'],
:last_build_time => project['lastBuildTime'],
:last_build_status => status_to_int(project['lastBuildStatus']),
:current_status => activity_to_int(project['activity']),
:culprits => @options['culprits'] ? get_culprits(project['name']) : []
:current_status => activity_to_int(project['activity'])
})
end
end
end

private
def get_culprits(name)
# grab fullName, and the address property from the users who are culprits on the last build
response = load_server_data(:path => "/job/#{name}/lastBuild/api/json?tree=culprits[fullName,property[address]]")

return [] if response.nil? || response.parsed_response.nil? || response.parsed_response['culprits'].nil?

# for each culprit in culprits get their fullName and email and store it as a hash
culprits = response.parsed_response['culprits'].collect do |culprit|
user = { 'name' => culprit['fullName'] }

culprit['property'].each do |property|
unless property['address'].nil?
# create a hash of users email to use for their gravatar
hash = Digest::MD5.hexdigest(property['address'].downcase)
user['gravatar'] = "http://www.gravatar.com/avatar/#{hash}.jpg"
end
end
# reuse culprit object
culprit = user
end

culprits.uniq
end
end
end
16 changes: 1 addition & 15 deletions lib/stoplight/providers/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def projects
:last_build_id => project['last_build_number'].to_s,
:last_build_time => project['last_build_finished_at'],
:last_build_status => status_to_int(project['last_build_status']),
:current_status => current_status_to_int(project['last_build_finished_at']),
:culprits => @options['culprits'] ? get_culprits(project['slug']) : []
:current_status => current_status_to_int(project['last_build_finished_at'])
})
end
end
Expand All @@ -47,18 +46,5 @@ def current_status_to_int(status)
-1
end
end

def get_culprits(slug)
response = load_server_data(:path => "/#{slug}/builds.json")

return [] if !response.parsed_response.is_a?(Array) || response.parsed_response.first.nil?

culprits = response.parsed_response.first['matrix'].collect do |commit|
hash = Digest::MD5.hexdigest(commit['author_email'].downcase)
{ 'user' => commit['author_name'], 'gravatar' => "http://www.gravatar.com/avatar/#{hash}.jpg" }
end

culprits.uniq
end
end
end
14 changes: 0 additions & 14 deletions spec/unit/jenkins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,4 @@
provider.provider.should == 'jenkins'
end
end

context 'projects' do
context 'culprits' do
it 'should return a list of the culprits who broke the build' do
provider = Jenkins.new('url' => 'http://ci.jenkins-ci.org', 'culprits' => true)
project = provider.projects.select{ |p| !p.culprits.empty? }.first

project.culprits.class.should == Array

project.culprits.first['name'].should == 'Kohsuke Kawaguchi'
project.culprits.first['gravatar'].should == 'http://www.gravatar.com/avatar/0cb9832a01c22c083390f3c5dcb64105.jpg'
end
end
end
end
2 changes: 1 addition & 1 deletion views/index.rabl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
collection @projects
attributes :name, :build_url, :last_build_id, :last_build_time, :last_build_status, :current_status, :culprits
attributes :name, :build_url, :last_build_id, :last_build_time, :last_build_status, :current_status

0 comments on commit a2b575e

Please sign in to comment.