2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2021-03-10 16:25:26 +00:00
|
|
|
class GitHub
|
|
|
|
class LinkedIssue
|
|
|
|
|
|
|
|
STATES_MAPPING = {
|
|
|
|
'OPEN' => 'open',
|
|
|
|
'CLOSED' => 'closed'
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
QUERY = <<-'GRAPHQL'.freeze
|
|
|
|
query($repositor_owner: String!, $repository_name: String!, $issue_id: Int!) {
|
|
|
|
repository(owner: $repositor_owner, name: $repository_name) {
|
|
|
|
issue(number: $issue_id) {
|
|
|
|
number
|
|
|
|
title
|
|
|
|
state
|
|
|
|
milestone {
|
|
|
|
title
|
|
|
|
}
|
|
|
|
assignees(last: 100) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labels(last: 100) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
name
|
|
|
|
color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GRAPHQL
|
|
|
|
|
|
|
|
attr_reader :client
|
|
|
|
|
|
|
|
def initialize(client)
|
|
|
|
@client = client
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by(url)
|
|
|
|
@result = query_by_url(url)
|
|
|
|
return if @result.blank?
|
|
|
|
|
|
|
|
to_h.merge(url: url)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def to_h
|
|
|
|
{
|
|
|
|
id: @result['number'].to_s,
|
|
|
|
title: @result['title'],
|
|
|
|
icon_state: STATES_MAPPING.fetch(@result['state'], @result['state']),
|
|
|
|
milestone: milestone,
|
|
|
|
assignees: assignees,
|
|
|
|
labels: labels,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def assignees
|
|
|
|
@result['assignees']['edges'].map do |assignee|
|
|
|
|
assignee['node']['name']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def labels
|
|
|
|
@result['labels']['edges'].map do |label|
|
|
|
|
{
|
|
|
|
text_color: text_color(label['node']['color']),
|
|
|
|
color: "##{label['node']['color']}",
|
|
|
|
title: label['node']['name']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_color(background_color)
|
|
|
|
background_color.to_i(16) > 0xFFF / 2 ? '#000000' : '#FFFFFF'
|
|
|
|
end
|
|
|
|
|
|
|
|
def milestone
|
2021-03-18 15:10:30 +00:00
|
|
|
@result.dig('milestone', 'title')
|
2021-03-10 16:25:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def query_by_url(url)
|
2021-03-24 16:41:05 +00:00
|
|
|
response = client.perform(
|
|
|
|
query: GitHub::LinkedIssue::QUERY,
|
|
|
|
variables: variables!(url)
|
|
|
|
)
|
2021-03-10 16:25:26 +00:00
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
response.dig('data', 'repository', 'issue')
|
2021-03-10 16:25:26 +00:00
|
|
|
end
|
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
def variables!(url)
|
2021-04-16 17:00:54 +00:00
|
|
|
if url !~ %r{^https?://([^/]+)/([^/]+)/([^/]+)/issues/(\d+)$}
|
2021-11-15 15:58:19 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, __('Invalid GitHub issue link format')
|
2021-03-24 16:41:05 +00:00
|
|
|
end
|
2021-03-10 16:25:26 +00:00
|
|
|
|
|
|
|
host = $1
|
|
|
|
repositor_owner = $2
|
|
|
|
repository_name = $3
|
|
|
|
id = $4
|
|
|
|
|
2021-03-24 16:41:05 +00:00
|
|
|
if client.endpoint.exclude?(host)
|
|
|
|
raise Exceptions::UnprocessableEntity, "Issue link doesn't match configured GitHub endpoint '#{client.endpoint}'"
|
|
|
|
end
|
2021-03-10 16:25:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
repositor_owner: repositor_owner,
|
|
|
|
repository_name: repository_name,
|
|
|
|
issue_id: id.to_i,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|