class Gitlab::Dangerfiles::Teammate

Attributes

available[R]
hungry[R]
name[R]
options[R]
projects[R]
reduced_capacity[R]
role[R]
tz_offset_hours[R]
username[R]

Public Class Methods

new(options = {}) click to toggle source

The options data are produced by gitlab.com/gitlab-org/gitlab-roulette/-/blob/master/lib/team_member.rb

# File lib/gitlab/dangerfiles/teammate.rb, line 9
def initialize(options = {})
  @options = options
  @username = options["username"]
  @name = options["name"]
  @markdown_name = options["markdown_name"]
  @role = options["role"]
  @projects = options["projects"]
  @available = options["available"]
  @hungry = options["hungry"]
  @reduced_capacity = options["reduced_capacity"]
  @tz_offset_hours = options["tz_offset_hours"]
end

Public Instance Methods

==(other) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 26
def ==(other)
  return false unless other.respond_to?(:username)

  other.username == username
end
any_capability?(project, category) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 36
def any_capability?(project, category)
  capabilities(project).any? { |capability| capability.end_with?(category.to_s) }
end
in_project?(name) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 32
def in_project?(name)
  projects&.has_key?(name)
end
local_hour() click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 56
def local_hour
  (Time.now.utc + tz_offset_hours * 3600).hour
end
maintainer?(project, category, labels) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 48
def maintainer?(project, category, labels)
  has_capability?(project, category, :maintainer, labels)
end
markdown_name(author: nil) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 52
def markdown_name(author: nil)
  "#{@markdown_name} (#{utc_offset_text(author)})"
end
reviewer?(project, category, labels) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 40
def reviewer?(project, category, labels)
  has_capability?(project, category, :reviewer, labels)
end
to_h() click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 22
def to_h
  options
end
traintainer?(project, category, labels) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 44
def traintainer?(project, category, labels)
  has_capability?(project, category, :trainee_maintainer, labels)
end

Protected Instance Methods

floored_offset_hours() click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 62
def floored_offset_hours
  floored_offset = tz_offset_hours.floor(0)

  floored_offset == tz_offset_hours ? floored_offset : tz_offset_hours
end

Private Instance Methods

capabilities(project) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 111
def capabilities(project)
  Array(projects.fetch(project, []))
end
has_capability?(project, category, kind, labels) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 92
def has_capability?(project, category, kind, labels)
  case category
  when :test
    area = role[/Software Engineer in Test(?:.*?, (\w+))/, 1]

    area && labels.any?("devops::#{area.downcase}") if kind == :reviewer
  when :tooling, :engineering_productivity # Deprecated as of 2.3.0 in favor of tooling
    return false unless role[/Engineering Productivity/]
    return true if kind == :reviewer
    return true if capabilities(project).include?("#{kind} engineering_productivity")

    capabilities(project).include?("#{kind} backend")
  when nil
    capabilities(project).include?("#{kind}")
  else
    capabilities(project).include?("#{kind} #{category}")
  end
end
offset_diff_compared_to_author(author) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 82
def offset_diff_compared_to_author(author)
  diff = floored_offset_hours - author.floored_offset_hours
  return "same timezone as `@#{author.username}`" if diff == 0

  ahead_or_behind = diff < 0 ? "behind" : "ahead of"
  pluralized_hours = pluralize(diff.abs, "hour", "hours")

  "#{pluralized_hours} #{ahead_or_behind} `@#{author.username}`"
end
pluralize(count, singular, plural) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 115
def pluralize(count, singular, plural)
  word = count == 1 || count.to_s =~ /^1(\.0+)?$/ ? singular : plural

  "#{count || 0} #{word}"
end
utc_offset_text(author = nil) click to toggle source
# File lib/gitlab/dangerfiles/teammate.rb, line 70
def utc_offset_text(author = nil)
  offset_text = if floored_offset_hours >= 0
      "UTC+#{floored_offset_hours}"
    else
      "UTC#{floored_offset_hours}"
    end

  return offset_text unless author

  "#{offset_text}, #{offset_diff_compared_to_author(author)}"
end