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

85 lines
2.3 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'
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
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
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
# @return [Boolean]
def available?
!out_of_office? && has_capacity?
end
2019-07-07 11:18:12 +05:30
private
2019-12-21 20:55:43 +05:30
# @return [Boolean]
def out_of_office?
status&.dig("message")&.match?(/OOO/i) || false
end
# @return [Boolean]
def has_capacity?
status&.dig("emoji") != 'red_circle'
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