Commit 07b3eceb authored by Nick Thomas's avatar Nick Thomas

Use ReactiveCaching in the JenkinsDeprecatedService

parent bf497b14
require 'uri' require 'uri'
class JenkinsDeprecatedService < CiService class JenkinsDeprecatedService < CiService
include ReactiveService
prop_accessor :project_url prop_accessor :project_url
boolean_accessor :multiproject_enabled boolean_accessor :multiproject_enabled
boolean_accessor :pass_unstable boolean_accessor :pass_unstable
...@@ -61,6 +63,10 @@ class JenkinsDeprecatedService < CiService ...@@ -61,6 +63,10 @@ class JenkinsDeprecatedService < CiService
end end
end end
def commit_status(sha, ref = nil)
with_reactive_cache(sha, ref) {|cached| cached[:commit_status] }
end
# When multi-project is enabled we need to have a different URL. Rather than # When multi-project is enabled we need to have a different URL. Rather than
# relying on the user to provide the proper URL depending on multi-project # relying on the user to provide the proper URL depending on multi-project
# we just parse the URL and make sure it's how we want it. # we just parse the URL and make sure it's how we want it.
...@@ -69,7 +75,13 @@ class JenkinsDeprecatedService < CiService ...@@ -69,7 +75,13 @@ class JenkinsDeprecatedService < CiService
URI.join(url, '/job').to_s URI.join(url, '/job').to_s
end end
def commit_status(sha, ref = nil) def calculate_reactive_cache(sha, ref)
{ commit_status: read_commit_status(sha, ref) }
end
private
def read_commit_status(sha, ref)
parsed_url = URI.parse(build_page(sha, ref)) parsed_url = URI.parse(build_page(sha, ref))
if parsed_url.userinfo.blank? if parsed_url.userinfo.blank?
......
require 'spec_helper' require 'spec_helper'
describe JenkinsDeprecatedService do describe JenkinsDeprecatedService, caching: true do
include ReactiveCachingHelpers
describe "Associations" do describe "Associations" do
it { is_expected.to belong_to :project } it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook } it { is_expected.to have_one :service_hook }
...@@ -16,42 +18,46 @@ describe JenkinsDeprecatedService do ...@@ -16,42 +18,46 @@ describe JenkinsDeprecatedService do
eos eos
end end
describe :commit_status do describe '#calculate_reactive_cache' do
let(:pass_unstable) { '0' }
before do before do
@service = JenkinsDeprecatedService.new @service = JenkinsDeprecatedService.new
allow(@service).to receive_messages( allow(@service).to receive_messages(
service_hook: true, service_hook: true,
project_url: 'http://jenkins.gitlab.org/job/2', project_url: 'http://jenkins.gitlab.org/job/2',
multiproject_enabled: '0', multiproject_enabled: '0',
pass_unstable: '0', pass_unstable: pass_unstable,
token: 'verySecret' token: 'verySecret'
) )
end end
statuses = { 'blue.png' => 'success', 'yellow.png' => 'failed', 'red.png' => 'failed', 'aborted.png' => 'failed', 'blue-anime.gif' => 'running', 'grey.png' => 'pending' } statuses = { 'blue.png' => 'success', 'yellow.png' => 'failed', 'red.png' => 'failed', 'aborted.png' => 'failed', 'blue-anime.gif' => 'running', 'grey.png' => 'pending' }
statuses.each do |icon, state| statuses.each do |icon, state|
it "has a status of #{state} when the icon #{icon} exists." do it "has a commit_status of #{state} when the icon #{icon} exists." do
stub_request(:get, "http://jenkins.gitlab.org/job/2/scm/bySHA1/2ab7834c").to_return(status: 200, body: status_body_for_icon(icon), headers: {}) stub_request(:get, "http://jenkins.gitlab.org/job/2/scm/bySHA1/2ab7834c").to_return(status: 200, body: status_body_for_icon(icon), headers: {})
expect(@service.commit_status("2ab7834c", 'master')).to eq(state)
end expect(@service.calculate_reactive_cache('2ab7834c', 'master')).to eq(commit_status: state)
end end
end end
describe 'commit status with passing unstable' do context 'with passing unstable' do
before do let(:pass_unstable) { '1' }
@service = JenkinsDeprecatedService.new
allow(@service).to receive_messages(
service_hook: true,
project_url: 'http://jenkins.gitlab.org/job/2',
multiproject_enabled: '0',
pass_unstable: '1',
token: 'verySecret'
)
end
it "has a status of success when the icon yellow exists." do it 'has a commit_status of success when the icon yellow exists' do
stub_request(:get, "http://jenkins.gitlab.org/job/2/scm/bySHA1/2ab7834c").to_return(status: 200, body: status_body_for_icon('yellow.png'), headers: {}) stub_request(:get, "http://jenkins.gitlab.org/job/2/scm/bySHA1/2ab7834c").to_return(status: 200, body: status_body_for_icon('yellow.png'), headers: {})
expect(@service.commit_status("2ab7834c", 'master')).to eq('success')
expect(@service.calculate_reactive_cache('2ab7834c', 'master')).to eq(commit_status: 'success')
end
end
end
describe '#commit_status' do
subject(:service) { described_class.new(project_id: 666) }
it 'returns the contents of the reactive cache' do
stub_reactive_cache(service, { commit_status: 'foo' }, 'sha', 'ref')
expect(service.commit_status('sha', 'ref')).to eq('foo')
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment