Commit 9c39d1e2 authored by Nick Thomas's avatar Nick Thomas

Make danger PTO-emoji-aware

We also rejig error handling slightly so we only ask for status once,
even if the endpoint fails. This should help to avoid inconsistencies.
parent 5b4930b1
......@@ -64,7 +64,7 @@ It picks reviewers and maintainers from the list at the
page, with these behaviors:
1. It will not pick people whose [GitLab status](../user/profile/index.md#current-status)
contains the string 'OOO'.
contains the string 'OOO', or the emoji is `:palm_tree:` or `:beach:`.
1. [Trainee maintainers](https://about.gitlab.com/handbook/engineering/workflow/code-review/#trainee-maintainer)
are three times as likely to be picked as other reviewers.
1. It always picks the same reviewers and maintainers for the same
......
# frozen_string_literal: true
require 'cgi'
require 'set'
module Gitlab
module Danger
class Teammate
attr_reader :name, :username, :role, :projects
AT_CAPACITY_EMOJI = Set.new(%w[red_circle]).freeze
OOO_EMOJI = Set.new(%w[
palm_tree
beach beach_umbrella beach_with_umbrella
]).freeze
def initialize(options = {})
@username = options['username']
@name = options['name'] || @username
......@@ -37,10 +44,14 @@ module Gitlab
end
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
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
end
# @return [Boolean]
......@@ -50,14 +61,22 @@ module Gitlab
private
def status_api_endpoint
"https://gitlab.com/api/v4/users/#{CGI.escape(username)}/status"
end
def status_emoji
status&.dig("emoji")
end
# @return [Boolean]
def out_of_office?
status&.dig("message")&.match?(/OOO/i) || false
status&.dig("message")&.match?(/OOO/i) || OOO_EMOJI.include?(status_emoji)
end
# @return [Boolean]
def has_capacity?
status&.dig("emoji") != 'red_circle'
!AT_CAPACITY_EMOJI.include?(status_emoji)
end
def has_capability?(project, category, kind, labels)
......
......@@ -163,6 +163,13 @@ describe Gitlab::Danger::Teammate do
{ message: 'OOO: massage' } | false
{ message: 'love it SOOO much' } | false
{ emoji: 'red_circle' } | false
{ emoji: 'palm_tree' } | false
{ emoji: 'beach' } | false
{ emoji: 'beach_umbrella' } | false
{ emoji: 'beach_with_umbrella' } | false
{ emoji: nil } | true
{ emoji: '' } | true
{ emoji: 'dancer' } | true
end
with_them do
......@@ -175,9 +182,9 @@ describe Gitlab::Danger::Teammate do
end
it 'returns true if request fails' do
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
.twice
.and_raise(Gitlab::Danger::RequestHelper::HTTPError.new)
expect(Gitlab::Danger::RequestHelper)
.to receive(:http_get_json)
.and_raise(Gitlab::Danger::RequestHelper::HTTPError.new)
expect(subject.available?).to be true
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment