forked from travis-ci/travis-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.rb
More file actions
127 lines (101 loc) · 2.99 KB
/
Copy pathfeatures.rb
File metadata and controls
127 lines (101 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require 'redis'
require 'rollout'
require 'active_support/deprecation'
require 'active_support/core_ext/module'
module Travis
# Wraps feature flips for Travis.
# This allows enabling/disabling certain features for users
# and repositories only.
#
# The start method needs to be called before using this module.
# You can then ask for features to be enabled for repositories:
#
# Repository.find_by_slug('rails/rails')
# if Travis::Features.active?(:pull_requests, repository)
# ...
# end
#
# This wraps checking if a repository is enabled and then delegates
# to the rollout library, where features can be enabled for users,
# groups and based on percentages.
module Features
class << self
methods = (Rollout.public_instance_methods(false) - [:active?, "active?"]) << {:to => :rollout}
delegate(*methods)
end
def redis
Travis.redis
end
def rollout
@rollout ||= ::Rollout.new(redis)
end
def active?(feature, repository)
feature_active?(feature) or
(rollout.active?(feature, repository.owner) or
repository_active?(feature, repository))
end
def activate_repository(feature, repository)
redis.sadd(repository_key(feature), repository.id)
end
def deactivate_repository(feature, repository)
redis.srem(repository_key(feature), repository.id)
end
def repository_active?(feature, repository)
redis.sismember(repository_key(feature), repository.id)
end
def user_active?(feature, user)
rollout.active?(feature, user)
end
def activate_all(feature)
redis.del(disabled_key(feature))
end
def feature_active?(feature)
enabled_for_all?(feature) and !feature_inactive?(feature)
end
def feature_inactive?(feature)
redis.get(disabled_key(feature)) != "1"
end
def feature_deactivated?(feature)
redis.get(disabled_key(feature)) == '0'
end
def deactivate_all(feature)
redis.set(disabled_key(feature), 0)
end
def enabled_for_all?(feature)
redis.get(enabled_for_all_key(feature)) == '1'
end
def enable_for_all(feature)
redis.set(enabled_for_all_key(feature), 1)
end
def disable_for_all(feature)
redis.set(enabled_for_all_key(feature), 0)
end
def activate_owner(feature, owner)
redis.sadd(owner_key(feature, owner), owner.id)
end
def deactivate_owner(feature, owner)
redis.srem(owner_key(feature, owner), owner.id)
end
def owner_active?(feature, owner)
redis.sismember(owner_key(feature, owner), owner.id)
end
extend self
private
def key(name)
"feature:#{name}"
end
def owner_key(feature, owner)
suffix = owner.class.table_name
"#{key(feature)}:#{suffix}"
end
def repository_key(feature)
"#{key(feature)}:repositories"
end
def disabled_key(feature)
"#{key(feature)}:disabled"
end
def enabled_for_all_key(feature)
"#{key(feature)}:disabled"
end
end
end