2019-02-05 12:16:18 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-16 05:07:51 -04:00
|
|
|
require 'cgi'
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
module Gitlab
|
|
|
|
module Danger
|
|
|
|
class Teammate
|
2019-05-23 14:07:59 -04:00
|
|
|
attr_reader :name, :username, :role, :projects
|
2019-02-05 12:16:18 -05:00
|
|
|
|
|
|
|
def initialize(options = {})
|
|
|
|
@username = options['username']
|
2019-05-29 11:15:09 -04:00
|
|
|
@name = options['name'] || @username
|
2019-05-23 14:07:59 -04:00
|
|
|
@role = options['role']
|
2019-02-05 12:16:18 -05:00
|
|
|
@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-05-23 14:07:59 -04:00
|
|
|
def reviewer?(project, category, labels)
|
|
|
|
has_capability?(project, category, :reviewer, labels) ||
|
|
|
|
traintainer?(project, category, labels)
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
2019-05-23 14:07:59 -04:00
|
|
|
def traintainer?(project, category, labels)
|
|
|
|
has_capability?(project, category, :trainee_maintainer, labels)
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
2019-05-23 14:07:59 -04:00
|
|
|
def maintainer?(project, category, labels)
|
|
|
|
has_capability?(project, category, :maintainer, labels)
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
|
2019-10-16 05:07:51 -04:00
|
|
|
def status
|
|
|
|
api_endpoint = "https://gitlab.com/api/v4/users/#{CGI.escape(username)}/status"
|
|
|
|
@status ||= Gitlab::Danger::RequestHelper.http_get_json(api_endpoint)
|
|
|
|
rescue Gitlab::Danger::RequestHelper::HTTPError, JSON::ParserError
|
|
|
|
nil # better no status than a crashing Danger
|
|
|
|
end
|
|
|
|
|
2019-10-16 17:07:22 -04:00
|
|
|
# @return [Boolean]
|
|
|
|
def available?
|
|
|
|
!out_of_office? && has_capacity?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-16 05:07:51 -04:00
|
|
|
# @return [Boolean]
|
|
|
|
def out_of_office?
|
|
|
|
status&.dig("message")&.match?(/OOO/i) || false
|
|
|
|
end
|
|
|
|
|
2019-10-16 17:07:22 -04:00
|
|
|
# @return [Boolean]
|
|
|
|
def has_capacity?
|
|
|
|
status&.dig("emoji") != 'red_circle'
|
|
|
|
end
|
2019-02-05 12:16:18 -05:00
|
|
|
|
2019-05-23 14:07:59 -04:00
|
|
|
def has_capability?(project, category, kind, labels)
|
|
|
|
case category
|
|
|
|
when :test
|
2019-11-22 16:06:19 -05:00
|
|
|
area = role[/Software Engineer in Test(?:.*?, (\w+))/, 1]
|
2019-05-23 14:07:59 -04:00
|
|
|
|
2019-08-27 13:13:55 -04:00
|
|
|
area && labels.any?("devops::#{area.downcase}") if kind == :reviewer
|
2019-09-03 18:02:55 -04:00
|
|
|
when :engineering_productivity
|
2019-10-31 08:06:26 -04:00
|
|
|
return false unless role[/Engineering Productivity/]
|
|
|
|
return true if kind == :reviewer
|
|
|
|
|
|
|
|
capabilities(project).include?("#{kind} backend")
|
2019-05-23 14:07:59 -04:00
|
|
|
else
|
|
|
|
capabilities(project).include?("#{kind} #{category}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-05 12:16:18 -05:00
|
|
|
def capabilities(project)
|
2019-03-06 11:00:27 -05:00
|
|
|
Array(projects.fetch(project, []))
|
2019-02-05 12:16:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|