diff --git a/example/config/pact_broker.yml b/example/config/pact_broker.yml index 8eff79c01..f02a0ea9a 100644 --- a/example/config/pact_broker.yml +++ b/example/config/pact_broker.yml @@ -7,3 +7,4 @@ basic_auth_password: "pass" basic_auth_read_only_username: "rouser" basic_auth_read_only_password: "ropass" database_url: sqlite:////tmp/pact_broker.sqlite3 +badge_default_cache_setting: "max-age=30" diff --git a/lib/pact_broker/api/resources/badge_methods.rb b/lib/pact_broker/api/resources/badge_methods.rb index badd81654..11e1d02ab 100644 --- a/lib/pact_broker/api/resources/badge_methods.rb +++ b/lib/pact_broker/api/resources/badge_methods.rb @@ -32,7 +32,7 @@ def policy_name end def moved_temporarily? - response.headers["Cache-Control"] = "no-cache" + set_cache_control("no-cache") begin badge_url rescue StandardError => e @@ -46,6 +46,10 @@ def badge_url raise NotImplementedError end + def set_cache_control(cache_control_str) + response.headers["Cache-Control"] = cache_control_str + end + private def label diff --git a/lib/pact_broker/api/resources/can_i_merge_badge.rb b/lib/pact_broker/api/resources/can_i_merge_badge.rb index 964b74957..13d604533 100644 --- a/lib/pact_broker/api/resources/can_i_merge_badge.rb +++ b/lib/pact_broker/api/resources/can_i_merge_badge.rb @@ -15,6 +15,8 @@ def badge_url # when there is no main branch version, we return an error badge url badge_service.error_badge_url("main branch version", "not found") else + # when badge is available, set cache based on configuration + set_cache_control(default_cache_for_succesful_badge) # we call badge_service to build the badge url badge_service.can_i_merge_badge_url(deployable: results) end @@ -22,6 +24,10 @@ def badge_url private + def default_cache_for_succesful_badge + PactBroker.configuration.badge_default_cahche_setting + end + def results # can_i_merge returns true or false if the main branch version is compatible with all the integrations @results ||= matrix_service.can_i_merge(pacticipant: pacticipant, latest_main_branch_version: version) diff --git a/lib/pact_broker/config/runtime_configuration.rb b/lib/pact_broker/config/runtime_configuration.rb index ba4d6e4b3..da7eb0098 100644 --- a/lib/pact_broker/config/runtime_configuration.rb +++ b/lib/pact_broker/config/runtime_configuration.rb @@ -76,6 +76,7 @@ class RuntimeConfiguration < Anyway::Config badge_provider_mode: :redirect, enable_public_badge_access: false, shields_io_base_url: "https://img.shields.io", + badge_default_cahche_setting: "max-age=30", use_case_sensitive_resource_names: true, pact_content_diff_timeout: 15 ) diff --git a/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb b/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb index b0ebb8e4d..7522909bf 100644 --- a/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb +++ b/spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb @@ -35,6 +35,7 @@ module Resources it "return the badge URL" do expect(badge_service). to receive(:can_i_merge_badge_url).with(deployable: true) expect(subject.headers["Location"]).to eq "http://badge_url" + expect(subject.headers["Cache-Control"]).to eq "max-age=30" end end @@ -44,6 +45,7 @@ module Resources it "returns an error badge URL" do expect(badge_service).to receive(:error_badge_url).with("pacticipant", "not found") expect(subject.headers["Location"]).to eq "http://error_badge_url" + expect(subject.headers["Cache-Control"]).to eq "no-cache" end end