Class: Gitlab::Dangerfiles::Teammate
- Inherits:
-
Object
- Object
- Gitlab::Dangerfiles::Teammate
- Defined in:
- lib/gitlab/dangerfiles/teammate.rb
Constant Summary collapse
- ROULETTE_DATA_URL =
"https://gitlab-org.gitlab.io/gitlab-roulette/roulette.json"
Instance Attribute Summary collapse
-
#available ⇒ Object
readonly
Returns the value of attribute available.
-
#hungry ⇒ Object
readonly
Returns the value of attribute hungry.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only_maintainer_reviews ⇒ Object
readonly
Returns the value of attribute only_maintainer_reviews.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#projects ⇒ Object
readonly
Returns the value of attribute projects.
-
#reduced_capacity ⇒ Object
readonly
Returns the value of attribute reduced_capacity.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#specialty ⇒ Object
readonly
Returns the value of attribute specialty.
-
#tz_offset_hours ⇒ Object
readonly
Returns the value of attribute tz_offset_hours.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.company_members ⇒ Array<Gitlab::Dangerfiles::Teammate>
Looks up the current list of GitLab team members and parses it into a useful form.
- .fetch_company_members ⇒ Object
- .find_member(username, project: nil) ⇒ Object
- .has_member_for_the_group?(category, labels:, **arguments) ⇒ Boolean
-
.http_get_json(url) ⇒ Hash, ...
Fetches the given
url
and parse its response as JSON. - .warnings ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #capabilities(project) ⇒ Object
- #floored_offset_hours ⇒ Object protected
- #import_integrate_be?(project, category, labels) ⇒ Boolean
- #import_integrate_fe?(project, category, labels) ⇒ Boolean
- #in_project?(name) ⇒ Boolean
-
#initialize(options = {}) ⇒ Teammate
constructor
The options data are produced by gitlab.com/gitlab-org/gitlab-roulette/-/blob/main/lib/team_member.rb.
- #inspect ⇒ Object
- #local_hour ⇒ Object
- #maintainer?(project, category, labels) ⇒ Boolean
- #markdown_name(author: nil) ⇒ Object
- #member_of_the_group?(labels) ⇒ Boolean
- #reviewer?(project, category, labels) ⇒ Boolean
- #to_h ⇒ Object
- #traintainer?(project, category, labels) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Teammate
The options data are produced by gitlab.com/gitlab-org/gitlab-roulette/-/blob/main/lib/team_member.rb
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 84 def initialize( = {}) @options = @username = ["username"] @name = ["name"] @markdown_name = ["markdown_name"] || default_markdown_name(["username"]) @role = ["role"] @specialty = ["specialty"] @projects = process_projects(["projects"]) @available = ["available"] @hungry = ["hungry"] @reduced_capacity = ["reduced_capacity"] @tz_offset_hours = ["tz_offset_hours"] @only_maintainer_reviews = ["only_maintainer_reviews"] end |
Instance Attribute Details
#available ⇒ Object (readonly)
Returns the value of attribute available.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def available @available end |
#hungry ⇒ Object (readonly)
Returns the value of attribute hungry.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def hungry @hungry end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def name @name end |
#only_maintainer_reviews ⇒ Object (readonly)
Returns the value of attribute only_maintainer_reviews.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def only_maintainer_reviews @only_maintainer_reviews end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def @options end |
#projects ⇒ Object (readonly)
Returns the value of attribute projects.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def projects @projects end |
#reduced_capacity ⇒ Object (readonly)
Returns the value of attribute reduced_capacity.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def reduced_capacity @reduced_capacity end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def role @role end |
#specialty ⇒ Object (readonly)
Returns the value of attribute specialty.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def specialty @specialty end |
#tz_offset_hours ⇒ Object (readonly)
Returns the value of attribute tz_offset_hours.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def tz_offset_hours @tz_offset_hours end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
80 81 82 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80 def username @username end |
Class Method Details
.company_members ⇒ Array<Gitlab::Dangerfiles::Teammate>
Looks up the current list of GitLab team members and parses it into a useful form.
38 39 40 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 38 def self.company_members @company_members ||= fetch_company_members end |
.fetch_company_members ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 42 def self.fetch_company_members data = http_get_json(ROULETTE_DATA_URL) || [] data.map { |hash| Teammate.new(hash) } rescue JSON::ParserError warnings << "Failed to parse JSON response from #{ROULETTE_DATA_URL}" [] end |
.find_member(username, project: nil) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 13 def self.find_member(username, project: nil) company_members.find do |member| member.username == username && (project.nil? || member.in_project?(project)) end end |
.has_member_for_the_group?(category, labels:, **arguments) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 20 def self.has_member_for_the_group?(category, labels:, **arguments) capabilities = %i[reviewer maintainer].map do |kind| # Use new to use the base class for original has_capability? method Capability.new(category: category, kind: kind, labels: labels, **arguments) end company_members.any? do |teammate| capabilities.any? do |capability| capability.has_capability?(teammate) && teammate.member_of_the_group?(labels) end end end |
.http_get_json(url) ⇒ Hash, ...
Fetches the given url
and parse its response as JSON.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 55 def self.http_get_json(url) rsp = Net::HTTP.get_response(URI.parse(url)) if rsp.is_a?(Net::HTTPRedirection) uri = URI.parse(rsp.header["location"]) uri.query = nil if uri warnings << "Redirection detected: #{uri}." return nil end unless rsp.is_a?(Net::HTTPOK) = rsp.[0, 30] warnings << "HTTPError: Failed to read #{url}: #{rsp.code} #{}." return nil end JSON.parse(rsp.body) end |
.warnings ⇒ Object
76 77 78 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 76 def self.warnings @warnings ||= [] end |
Instance Method Details
#==(other) ⇒ Object
122 123 124 125 126 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 122 def ==(other) return false unless other.respond_to?(:username) other.username == username end |
#capabilities(project) ⇒ Object
164 165 166 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 164 def capabilities(project) projects.fetch(project, []) end |
#floored_offset_hours ⇒ Object (protected)
170 171 172 173 174 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 170 def floored_offset_hours floored_offset = tz_offset_hours.floor(0) floored_offset == tz_offset_hours ? floored_offset : tz_offset_hours end |
#import_integrate_be?(project, category, labels) ⇒ Boolean
144 145 146 147 148 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 144 def import_integrate_be?(project, category, labels) return false unless category == :import_integrate_be has_capability?(project, category, :reviewer, labels) end |
#import_integrate_fe?(project, category, labels) ⇒ Boolean
150 151 152 153 154 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 150 def import_integrate_fe?(project, category, labels) return false unless category == :import_integrate_fe has_capability?(project, category, :reviewer, labels) end |
#in_project?(name) ⇒ Boolean
128 129 130 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 128 def in_project?(name) projects&.has_key?(name) end |
#inspect ⇒ Object
118 119 120 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 118 def inspect "#<#{self.class} @username=#{username.inspect}>" end |
#local_hour ⇒ Object
160 161 162 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 160 def local_hour (Time.now.utc + tz_offset_hours * 3600).hour end |
#maintainer?(project, category, labels) ⇒ Boolean
140 141 142 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 140 def maintainer?(project, category, labels) has_capability?(project, category, :maintainer, labels) end |
#markdown_name(author: nil) ⇒ Object
156 157 158 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 156 def markdown_name(author: nil) "#{@markdown_name}#{utc_offset_text()}" end |
#member_of_the_group?(labels) ⇒ Boolean
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 100 def member_of_the_group?(labels) # Specialty can be: # Source Code # [Growth: Activation, Growth: Expansion] # Runner group_labels = Array(specialty).map do |field| group = field.strip.sub(/^.+: ?/, "").downcase "group::#{group}" end (group_labels & labels).any? end |
#reviewer?(project, category, labels) ⇒ Boolean
132 133 134 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 132 def reviewer?(project, category, labels) has_capability?(project, category, :reviewer, labels) end |
#to_h ⇒ Object
114 115 116 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 114 def to_h end |
#traintainer?(project, category, labels) ⇒ Boolean
136 137 138 |
# File 'lib/gitlab/dangerfiles/teammate.rb', line 136 def traintainer?(project, category, labels) has_capability?(project, category, :trainee_maintainer, labels) end |