Commit d944da0c authored by Robert Speicher's avatar Robert Speicher

Merge branch 'jenkins-deprecated-service-reactive-caching' into 'master'

Use ReactiveCaching in the JenkinsDeprecatedService

See merge request !1053
parents 759a2eb8 07b3eceb
require 'uri'
class JenkinsDeprecatedService < CiService
include ReactiveService
prop_accessor :project_url
boolean_accessor :multiproject_enabled
boolean_accessor :pass_unstable
......@@ -61,6 +63,10 @@ class JenkinsDeprecatedService < CiService
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
# 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.
......@@ -69,7 +75,13 @@ class JenkinsDeprecatedService < CiService
URI.join(url, '/job').to_s
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))
if parsed_url.userinfo.blank?
......
require 'spec_helper'
describe JenkinsDeprecatedService do
describe JenkinsDeprecatedService, caching: true do
include ReactiveCachingHelpers
describe "Associations" do
it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook }
......@@ -16,42 +18,46 @@ describe JenkinsDeprecatedService do
eos
end
describe :commit_status do
describe '#calculate_reactive_cache' do
let(:pass_unstable) { '0' }
before do
@service = JenkinsDeprecatedService.new
allow(@service).to receive_messages(
service_hook: true,
project_url: 'http://jenkins.gitlab.org/job/2',
multiproject_enabled: '0',
pass_unstable: '0',
pass_unstable: pass_unstable,
token: 'verySecret'
)
end
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|
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: {})
expect(@service.commit_status("2ab7834c", 'master')).to eq(state)
expect(@service.calculate_reactive_cache('2ab7834c', 'master')).to eq(commit_status: state)
end
end
end
describe 'commit status with passing unstable' do
before do
@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'
)
context 'with passing unstable' do
let(:pass_unstable) { '1' }
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: {})
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')
it "has a 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: {})
expect(@service.commit_status("2ab7834c", 'master')).to eq('success')
expect(service.commit_status('sha', 'ref')).to eq('foo')
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