Class: Gitlab::Dangerfiles::Teammate

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Teammate



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 = {})
  @options = options
  @username = options["username"]
  @name = options["name"]
  @markdown_name = options["markdown_name"] ||
    default_markdown_name(options["username"])
  @role = options["role"]
  @specialty = options["specialty"]
  @projects = process_projects(options["projects"])
  @available = options["available"]
  @hungry = options["hungry"]
  @reduced_capacity = options["reduced_capacity"]
  @tz_offset_hours = options["tz_offset_hours"]
  @only_maintainer_reviews = options["only_maintainer_reviews"]
end

Instance Attribute Details

#availableObject (readonly)

Returns the value of attribute available.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def available
  @available
end

#hungryObject (readonly)

Returns the value of attribute hungry.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def hungry
  @hungry
end

#nameObject (readonly)

Returns the value of attribute name.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def name
  @name
end

#only_maintainer_reviewsObject (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

#optionsObject (readonly)

Returns the value of attribute options.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def options
  @options
end

#projectsObject (readonly)

Returns the value of attribute projects.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def projects
  @projects
end

#reduced_capacityObject (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

#roleObject (readonly)

Returns the value of attribute role.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def role
  @role
end

#specialtyObject (readonly)

Returns the value of attribute specialty.



80
81
82
# File 'lib/gitlab/dangerfiles/teammate.rb', line 80

def specialty
  @specialty
end

#tz_offset_hoursObject (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

#usernameObject (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_membersArray<Gitlab::Dangerfiles::Teammate>

Looks up the current list of GitLab team members and parses it into a useful form.

Returns:



38
39
40
# File 'lib/gitlab/dangerfiles/teammate.rb', line 38

def self.company_members
  @company_members ||= fetch_company_members
end

.fetch_company_membersObject



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

Returns:

  • (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.

Parameters:

  • url (String)

Returns:

  • (Hash, Array, NilClass)


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)
    message = rsp.message[0, 30]
    warnings << "HTTPError: Failed to read #{url}: #{rsp.code} #{message}."
    return nil
  end

  JSON.parse(rsp.body)
end

.warningsObject



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_hoursObject (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


128
129
130
# File 'lib/gitlab/dangerfiles/teammate.rb', line 128

def in_project?(name)
  projects&.has_key?(name)
end

#inspectObject



118
119
120
# File 'lib/gitlab/dangerfiles/teammate.rb', line 118

def inspect
  "#<#{self.class} @username=#{username.inspect}>"
end

#local_hourObject



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

Returns:

  • (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(author)}"
end

#member_of_the_group?(labels) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_hObject



114
115
116
# File 'lib/gitlab/dangerfiles/teammate.rb', line 114

def to_h
  options
end

#traintainer?(project, category, labels) ⇒ Boolean

Returns:

  • (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