Commit a55e1501 authored by Rémy Coutable's avatar Rémy Coutable

Set `label_ids` and `assignee_ids` after the initial `save!`.

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent f96b4f41
......@@ -99,7 +99,7 @@ module Github
label.color = representation.color
end
cached[:label_ids][label.title] = label.id
cached[:label_ids][representation.title] = label.id
rescue => e
error(:label, representation.url, e.message)
end
......@@ -211,11 +211,11 @@ module Github
# for both features, like manipulating assignees, labels
# and milestones, are provided within the Issues API.
if representation.pull_request?
return unless representation.has_labels? || representation.has_comments?
return unless representation.labels? || representation.has_comments?
merge_request = MergeRequest.find_by!(target_project_id: project.id, iid: representation.iid)
if representation.has_labels?
if representation.labels?
merge_request.update_attribute(:label_ids, label_ids(representation.labels))
end
......@@ -230,14 +230,16 @@ module Github
issue.title = representation.title
issue.description = format_description(representation.description, representation.author)
issue.state = representation.state
issue.label_ids = label_ids(representation.labels)
issue.milestone_id = milestone_id(representation.milestone)
issue.author_id = author_id
issue.assignee_ids = [user_id(representation.assignee)]
issue.created_at = representation.created_at
issue.updated_at = representation.updated_at
issue.save!(validate: false)
issue.update(
label_ids: label_ids(representation.labels),
assignee_ids: assignee_ids(representation.assignees))
fetch_comments_conditionally(issue, representation)
end
rescue => e
......@@ -310,7 +312,11 @@ module Github
end
def label_ids(labels)
labels.map { |attrs| cached[:label_ids][attrs.fetch('name')] }.compact
labels.map { |label| cached[:label_ids][label.title] }.compact
end
def assignee_ids(assignees)
assignees.map { |assignee| user_id(assignee) }.compact
end
def milestone_id(milestone)
......
......@@ -23,14 +23,16 @@ module Github
@author ||= Github::Representation::User.new(raw['user'], options)
end
def assignee
return unless assigned?
@assignee ||= Github::Representation::User.new(raw['assignee'], options)
def labels?
raw['labels'].any?
end
def assigned?
raw['assignee'].present?
def labels
return [] unless labels?
@labels ||= raw['labels'].map do |label|
Github::Representation::Label.new(label, options)
end
end
end
end
......
module Github
module Representation
class Issue < Representation::Issuable
def labels
raw['labels']
end
def state
raw['state'] == 'closed' ? 'closed' : 'opened'
end
......@@ -13,13 +9,21 @@ module Github
raw['comments'] > 0
end
def has_labels?
labels.count > 0
end
def pull_request?
raw['pull_request'].present?
end
def assigned?
raw['assignees'].present?
end
def assignees
return [] unless assigned?
@assignees ||= raw['assignees'].map do |user|
Github::Representation::User.new(user, options)
end
end
end
end
end
......@@ -37,6 +37,16 @@ module Github
source_branch.valid? && target_branch.valid?
end
def assigned?
raw['assignee'].present?
end
def assignee
return unless assigned?
@assignee ||= Github::Representation::User.new(raw['assignee'], options)
end
private
def project
......
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