Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying the gemlock file and enabling building status on jenkins #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
gem 'dashing'
gem 'dotenv'
gem 'teamcity-ruby-client'
gem 'coffee-script-source', '= 1.8.0'

group :test do
gem 'rspec'
Expand Down
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GEM
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.10.0)
coffee-script-source (1.8.0)
crack (0.4.3)
safe_yaml (~> 1.0.0)
daemons (1.2.3)
Expand Down Expand Up @@ -94,9 +94,13 @@ PLATFORMS
ruby

DEPENDENCIES
coffee-script-source (= 1.8.0)
dashing
dotenv
rspec
rspec-mocks
teamcity-ruby-client
webmock

BUNDLED WITH
1.10.6
17 changes: 15 additions & 2 deletions jobs/build_health.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SUCCESS = 'Successful'
FAILED = 'Failed'
BUILDING = 'Building'

def api_functions
return {
Expand Down Expand Up @@ -108,16 +109,28 @@ def get_bamboo_build_health(build_id)
}
end

def get_jenkins_build_status(current_build, latest_build)
if current_build['building'] then
return BUILDING
else
return latest_build['result'] == 'SUCCESS' ? SUCCESS : FAILED
end
end

def get_jenkins_build_health(build_id)
url = "#{Builds::BUILD_CONFIG['jenkinsBaseUrl']}/job/#{build_id}/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]"
url = "#{Builds::BUILD_CONFIG['jenkinsBaseUrl']}/job/#{build_id}/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]"
build_info = get_url URI.encode(url)
builds = build_info['builds']
builds_with_status = builds.select { |build| !build['result'].nil? }
successful_count = builds_with_status.count { |build| build['result'] == 'SUCCESS' }
latest_build = builds_with_status.first
current_build = builds.first

status = get_jenkins_build_status(current_build, latest_build)

return {
name: latest_build['fullDisplayName'],
status: latest_build['result'] == 'SUCCESS' ? SUCCESS : FAILED,
status: status,
duration: latest_build['duration'] / 1000,
link: latest_build['url'],
health: calculate_health(successful_count, builds_with_status.count),
Expand Down
56 changes: 50 additions & 6 deletions spec/jenkins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
before(:each) do
@jenkins_response = {
"builds" => [{
"building" => false,
"duration" => 10000,
"fullDisplayName" => "a build",
"id" => "15",
Expand All @@ -14,13 +15,13 @@
"url" => "urlOfSorts"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]').
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => @jenkins_response.to_json, :headers => {})
end

it 'should get jenkins build info from jenkins api' do
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(WebMock.a_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]')).to have_been_made
expect(WebMock.a_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]')).to have_been_made
end

it 'should return the name of the build' do
Expand All @@ -36,6 +37,7 @@
it 'should return the status of the latest build when Failed' do
failed_build = {
"builds" => [{
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -44,7 +46,7 @@
"url" => "someurl/1/"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]').
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => failed_build.to_json, :headers => {})
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(build_health[:status]).to eq('Failed')
Expand All @@ -68,13 +70,15 @@
it 'should return the build health' do
so_so = {
"builds" => [{
"building" => false,
"duration" => 459766,
"fullDisplayName" => "a build",
"id" => "15",
"result" => "SUCCESS",
"timestamp" => 1453489268807,
"url" => "someurl/15/"
}, {
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -83,7 +87,7 @@
"url" => "someurl/1/"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]').
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => so_so.to_json, :headers => {})
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(build_health[:health]).to eq(50)
Expand All @@ -92,13 +96,15 @@
it 'should return the build status of the latest non-nil build' do
running_builds = {
"builds" => [{
"building" => false,
"duration" => 459766,
"fullDisplayName" => "a build",
"id" => "15",
"result" => nil,
"timestamp" => 1453489268807,
"url" => "someurl/15/"
}, {
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -107,6 +113,7 @@
"url" => "someurl/1/"
},
{
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -115,7 +122,7 @@
"url" => "someurl/1/"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]').
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => running_builds.to_json, :headers => {})
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(build_health[:status]).to eq('Successful')
Expand All @@ -124,13 +131,15 @@
it 'should not include running builds in the build health calculation' do
running_builds = {
"builds" => [{
"building" => false,
"duration" => 459766,
"fullDisplayName" => "a build",
"id" => "15",
"result" => nil,
"timestamp" => 1453489268807,
"url" => "someurl/15/"
}, {
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -139,6 +148,7 @@
"url" => "someurl/1/"
},
{
"building" => false,
"duration" => 427875,
"fullDisplayName" => "a build",
"id" => "1",
Expand All @@ -147,10 +157,44 @@
"url" => "someurl/1/"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,timestamp,id,result,duration,url,fullDisplayName]').
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => running_builds.to_json, :headers => {})
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(build_health[:health]).to eq(100)
end

it 'should return status of the build as building when the current build is in a building state' do
running_build = {
"builds" => [{
"building" => true,
"duration" => 459766,
"fullDisplayName" => "a build",
"id" => "15",
"result" => "",
"timestamp" => 1453489268807,
"url" => "someurl/15/"
}, {
"building" => false,
"duration" => 427875,
"fullDisplayName" => "second build",
"id" => "1",
"result" => "FAILURE",
"timestamp" => 1451584715235,
"url" => "someurl/1/"
}, {
"building" => false,
"duration" => 427875,
"fullDisplayName" => "another build",
"id" => "2",
"result" => "SUCCESS",
"timestamp" => 1451584715235,
"url" => "someurl/1/"
}]
}
stub_request(:get, 'http://jenkins-url/job/jenkins-build/api/json?tree=builds[status,building,timestamp,id,result,duration,url,fullDisplayName]').
to_return(:status => 200, :body => running_build.to_json, :headers => {})
build_health = get_build_health 'id' => 'jenkins-build', 'server' => 'Jenkins'
expect(build_health[:status]).to eq('Building')
end

end
9 changes: 5 additions & 4 deletions widgets/build_window/build_window.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ Batman.Filters.durationFormat = (duration) ->

class Dashing.BuildWindow extends Dashing.Widget
onData: (data) ->
if data.status == 'Failed'
$(@node).css('background-color', '#a73737')
else if data.status == 'Successful'
$(@node).css('background-color', '#03A06E')
status = data.status
if (status == 'Failed') then $(@node).css('background-color', '#a73737')
else if (status == 'Successful') then $(@node).css('background-color', '#03A06E')
else if (status == 'Building') then $(@node).css('background-color', '#999900')
else $(@node).css('background-color', '#808080')

@accessor 'image', ->
health = @get('health')
Expand Down