From 80b740e11684ad74de21a78f54d86525941e4cc5 Mon Sep 17 00:00:00 2001 From: Daniel Kerwin Date: Thu, 14 Nov 2013 10:21:09 +0100 Subject: [PATCH] Method to get promotions of given job. Returns a hash with processes and the promoted build numbers --- lib/jenkins_api_client/job.rb | 25 +++++++++++++++++++++++++ spec/unit_tests/job_spec.rb | 19 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/jenkins_api_client/job.rb b/lib/jenkins_api_client/job.rb index aaec3d6f..9f33884a 100644 --- a/lib/jenkins_api_client/job.rb +++ b/lib/jenkins_api_client/job.rb @@ -1207,6 +1207,31 @@ def chain(job_names, threshold, criteria, parallel = 1) filtered_job_names[0..parallel-1] end + # Get a list of promoted builds for given job + # + # @param [String] job_name + # @return [Hash] Hash map of promitions and the promoted builds. Promotions that didn't took place yet + # return nil + def get_promotions(job_name) + result = {} + + @logger.info "Obtaining the promotions of '#{job_name}'" + response_json = @client.api_get_request("/job/#{job_name}/promotion") + + response_json["processes"].each do |promotion| + @logger.info "Getting promotion details of '#{promotion['name']}'" + + if promotion['color'] == 'notbuilt' + result[promotion['name']] = nil + else + promo_json = @client.api_get_request("/job/#{job_name}/promotion/latest/#{promotion['name']}") + result[promotion['name']] = promo_json['target']['number'] + end + end + + result + end + private # Obtains the threshold params used by jenkins in the XML file diff --git a/spec/unit_tests/job_spec.rb b/spec/unit_tests/job_spec.rb index 04775960..16678bbf 100644 --- a/spec/unit_tests/job_spec.rb +++ b/spec/unit_tests/job_spec.rb @@ -515,6 +515,25 @@ end end + describe "#get_promotions" do + it "accepts the jon name and returns the promotions" do + mock_job_promotions_response = { + "processes" => [ { "name" => "dev", + "url" => "not_required", + "color" => "blue", + }, + { "name" => "stage", + "url" => "not_required", + "color" => "notbuilt", + }, + ], } + + @client.should_receive(:api_get_request).with('/job/test_job/promotion').and_return( + mock_job_promotions_response) + @client.should_receive(:api_get_request).and_return({'target' => {'number' => 42}}) + @job.get_promotions("test_job").should == {'dev' => 42, 'stage' => nil} + end + end end end end