Skip to content

Commit f0737b9

Browse files
committed
feat: add feature toggle via environment variable PACT_BROKER_FEATURES
This is a space separated list of active features.
1 parent 695869b commit f0737b9

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

lib/pact_broker/api.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'webmachine/adapters/rack_mapped'
22
require 'pact_broker/api/resources'
3+
require 'pact_broker/feature_toggle'
34

45
module PactBroker
56

@@ -40,7 +41,7 @@ module PactBroker
4041
add ['pacts', 'provider', :provider_name, 'latest', :tag], Api::Resources::LatestProviderPacts, {resource_name: "latest_tagged_provider_pact_publications"}
4142
add ['pacts', 'latest'], Api::Resources::LatestPacts, {resource_name: "latest_pacts"}
4243

43-
if ENV['RACK_ENV'] != 'production'
44+
if PactBroker.feature_enabled?(:wip_pacts)
4445
add ['pacts', 'provider', :provider_name, 'wip'], Api::Resources::WipProviderPacts, {resource_name: "wip_provider_pact_publications"}
4546
end
4647

lib/pact_broker/feature_toggle.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module PactBroker
2+
class FeatureToggle
3+
4+
def self.enabled?(feature)
5+
ENV['RACK_ENV'] != 'production' || (ENV['PACT_BROKER_FEATURES'] || "").include?(feature.to_s)
6+
end
7+
8+
end
9+
10+
def self.feature_enabled?(feature)
11+
FeatureToggle.enabled?(feature)
12+
end
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'pact_broker/feature_toggle'
2+
3+
module PactBroker
4+
describe FeatureToggle do
5+
describe "enabled?" do
6+
before do
7+
allow(ENV).to receive(:[]).and_call_original
8+
end
9+
10+
subject { FeatureToggle.enabled?(:foo) }
11+
12+
context "when RACK_ENV is not production" do
13+
14+
before do
15+
allow(ENV).to receive(:[]).with('RACK_ENV').and_return('development')
16+
end
17+
18+
context "when PACT_BROKER_FEATURES includes the given string" do
19+
before do
20+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('foo bar')
21+
end
22+
23+
it { is_expected.to be true }
24+
end
25+
26+
context "when PACT_BROKER_FEATURES does not include the given string" do
27+
before do
28+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
29+
end
30+
31+
it { is_expected.to be true }
32+
end
33+
end
34+
35+
context "when RACK_ENV is production" do
36+
before do
37+
allow(ENV).to receive(:[]).with('RACK_ENV').and_return('production')
38+
end
39+
40+
context "when PACT_BROKER_FEATURES includes the given string" do
41+
before do
42+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return('foo bar')
43+
end
44+
45+
it { is_expected.to be true }
46+
end
47+
48+
context "when PACT_BROKER_FEATURES does not include the given string" do
49+
before do
50+
allow(ENV).to receive(:[]).with('PACT_BROKER_FEATURES').and_return(nil)
51+
end
52+
53+
it { is_expected.to be false }
54+
end
55+
end
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)