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

104 lines
2.7 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-12-21 20:55:43 +05:30
require 'cgi'
2020-05-24 23:13:21 +05:30
require 'set'
2019-12-21 20:55:43 +05:30
2019-07-07 11:18:12 +05:30
module Gitlab
module Danger
class Teammate
2019-09-04 21:01:54 +05:30
attr_reader :name, :username, :role, :projects
2019-07-07 11:18:12 +05:30
2020-05-24 23:13:21 +05:30
AT_CAPACITY_EMOJI = Set.new(%w[red_circle]).freeze
OOO_EMOJI = Set.new(%w[
palm_tree
beach beach_umbrella beach_with_umbrella
]).freeze
2019-07-07 11:18:12 +05:30
def initialize(options = {})
@username = options['username']
2019-09-04 21:01:54 +05:30
@name = options['name'] || @username
@role = options['role']
2019-07-07 11:18:12 +05:30
@projects = options['projects']
end
def markdown_name
"[#{name}](https://gitlab.com/#{username}) (`@#{username}`)"
end
def in_project?(name)
projects&.has_key?(name)
end
# Traintainers also count as reviewers
2019-09-04 21:01:54 +05:30
def reviewer?(project, category, labels)
has_capability?(project, category, :reviewer, labels) ||
traintainer?(project, category, 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
2019-12-21 20:55:43 +05:30
def status
2020-05-24 23:13:21 +05:30
return @status if defined?(@status)
@status ||=
begin
Gitlab::Danger::RequestHelper.http_get_json(status_api_endpoint)
rescue Gitlab::Danger::RequestHelper::HTTPError, JSON::ParserError
nil # better no status than a crashing Danger
end
2019-12-21 20:55:43 +05:30
end
# @return [Boolean]
def available?
!out_of_office? && has_capacity?
end
2019-07-07 11:18:12 +05:30
private
2020-05-24 23:13:21 +05:30
def status_api_endpoint
"https://gitlab.com/api/v4/users/#{CGI.escape(username)}/status"
end
def status_emoji
status&.dig("emoji")
end
2019-12-21 20:55:43 +05:30
# @return [Boolean]
def out_of_office?
2020-05-24 23:13:21 +05:30
status&.dig("message")&.match?(/OOO/i) || OOO_EMOJI.include?(status_emoji)
2019-12-21 20:55:43 +05:30
end
# @return [Boolean]
def has_capacity?
2020-05-24 23:13:21 +05:30
!AT_CAPACITY_EMOJI.include?(status_emoji)
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
end
end
end