Skip to content

Commit

Permalink
feat: implemented basic cache of 30 sec in can-i-merge badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Phanindra Srungavarapu authored and Phanindra Srungavarapu committed Sep 6, 2024
1 parent 8871956 commit c15c47b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/pact_broker/api/resources/badge_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/api/resources/can_i_merge_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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
set_cache_control("max-age=30") # override the cache control header to a default 30 seconds
# we call badge_service to build the badge url
badge_service.can_i_merge_badge_url(deployable: results)
end
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/pact_broker/api/resources/can_i_merge_badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit c15c47b

Please sign in to comment.