debian-mirror-gitlab/lib/gitlab/danger/teammate.rb

116 lines
3.2 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
module Gitlab
module Danger
class Teammate
2021-01-03 14:25:43 +05:30
attr_reader :options, :username, :name, :role, :projects, :available, :hungry, :tz_offset_hours
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
# The options data are produced by https://gitlab.com/gitlab-org/gitlab-roulette/-/blob/master/lib/team_member.rb
2019-07-07 11:18:12 +05:30
def initialize(options = {})
2020-10-24 23:57:45 +05:30
@options = options
2019-07-07 11:18:12 +05:30
@username = options['username']
2020-07-28 23:09:34 +05:30
@name = options['name']
@markdown_name = options['markdown_name']
2019-09-04 21:01:54 +05:30
@role = options['role']
2019-07-07 11:18:12 +05:30
@projects = options['projects']
2020-07-28 23:09:34 +05:30
@available = options['available']
2021-01-03 14:25:43 +05:30
@hungry = options['hungry']
2020-07-28 23:09:34 +05:30
@tz_offset_hours = options['tz_offset_hours']
2019-07-07 11:18:12 +05:30
end
2020-10-24 23:57:45 +05:30
def to_h
options
end
def ==(other)
return false unless other.respond_to?(:username)
other.username == username
end
2019-07-07 11:18:12 +05:30
def in_project?(name)
projects&.has_key?(name)
end
2019-09-04 21:01:54 +05:30
def reviewer?(project, category, labels)
2021-01-03 14:25:43 +05:30
has_capability?(project, category, :reviewer, labels)
2019-07-07 11:18:12 +05:30
end
2019-09-04 21:01:54 +05:30
def traintainer?(project, category, labels)
has_capability?(project, category, :trainee_maintainer, labels)
2019-07-07 11:18:12 +05:30
end
2019-09-04 21:01:54 +05:30
def maintainer?(project, category, labels)
has_capability?(project, category, :maintainer, labels)
2019-07-07 11:18:12 +05:30
end
2020-11-24 15:15:51 +05:30
def markdown_name(author: nil)
2020-07-28 23:09:34 +05:30
"#{@markdown_name} (#{utc_offset_text(author)})"
2019-12-21 20:55:43 +05:30
end
2020-07-28 23:09:34 +05:30
def local_hour
(Time.now.utc + tz_offset_hours * 3600).hour
2019-12-21 20:55:43 +05:30
end
2020-07-28 23:09:34 +05:30
protected
2019-07-07 11:18:12 +05:30
2020-07-28 23:09:34 +05:30
def floored_offset_hours
floored_offset = tz_offset_hours.floor(0)
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
floored_offset == tz_offset_hours ? floored_offset : tz_offset_hours
2020-05-24 23:13:21 +05:30
end
2020-07-28 23:09:34 +05:30
private
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)}"
2019-12-21 20:55:43 +05:30
end
2020-07-28 23:09:34 +05:30
def offset_diff_compared_to_author(author)
diff = floored_offset_hours - author.floored_offset_hours
2020-10-24 23:57:45 +05:30
return "same timezone as `@#{author.username}`" if diff == 0
2020-07-28 23:09:34 +05:30
2020-10-24 23:57:45 +05:30
ahead_or_behind = diff < 0 ? 'behind' : 'ahead of'
2020-07-28 23:09:34 +05:30
pluralized_hours = pluralize(diff.abs, 'hour', 'hours')
"#{pluralized_hours} #{ahead_or_behind} `@#{author.username}`"
2019-12-21 20:55:43 +05:30
end
2019-09-04 21:01:54 +05:30
def has_capability?(project, category, kind, labels)
case category
when :test
2020-01-01 13:55:28 +05:30
area = role[/Software Engineer in Test(?:.*?, (\w+))/, 1]
2019-09-04 21:01:54 +05:30
2019-12-04 20:38:33 +05:30
area && labels.any?("devops::#{area.downcase}") if kind == :reviewer
when :engineering_productivity
2019-12-26 22:10:19 +05:30
return false unless role[/Engineering Productivity/]
return true if kind == :reviewer
capabilities(project).include?("#{kind} backend")
2019-09-04 21:01:54 +05:30
else
capabilities(project).include?("#{kind} #{category}")
end
end
2019-07-07 11:18:12 +05:30
def capabilities(project)
Array(projects.fetch(project, []))
end
2020-07-28 23:09:34 +05:30
def pluralize(count, singular, plural)
word = count == 1 || count.to_s =~ /^1(\.0+)?$/ ? singular : plural
"#{count || 0} #{word}"
end
2019-07-07 11:18:12 +05:30
end
end
end