Apply labels to issues/merge requests

parent db322009
...@@ -27,11 +27,12 @@ module Github ...@@ -27,11 +27,12 @@ module Github
self.reset_callbacks :validate self.reset_callbacks :validate
end end
attr_reader :project, :repository, :cached_user_ids, :errors attr_reader :project, :repository, :cached_label_ids, :cached_user_ids, :errors
def initialize(project) def initialize(project)
@project = project @project = project
@repository = project.repository @repository = project.repository
@cached_label_ids = {}
@cached_user_ids = {} @cached_user_ids = {}
@errors = [] @errors = []
end end
...@@ -40,7 +41,7 @@ module Github ...@@ -40,7 +41,7 @@ module Github
# Fetch repository # Fetch repository
begin begin
project.create_repository project.create_repository
project.repository.add_remote('github', "https://881a01d03026458e51285a4c7038c9fe4daa5561@github.com/#{owner}/#{repo}.git") project.repository.add_remote('github', "https://{token}@github.com/#{owner}/#{repo}.git")
project.repository.set_remote_as_mirror('github') project.repository.set_remote_as_mirror('github')
project.repository.fetch_remote('github', forced: true) project.repository.fetch_remote('github', forced: true)
project.repository.remove_remote('github') project.repository.remove_remote('github')
...@@ -57,6 +58,8 @@ module Github ...@@ -57,6 +58,8 @@ module Github
response.body.each do |raw| response.body.each do |raw|
begin begin
label = Github::Representation::Label.new(raw) label = Github::Representation::Label.new(raw)
# TODO: we should take group labels in account
next if project.labels.where(title: label.title).exists? next if project.labels.where(title: label.title).exists?
project.labels.create!(title: label.title, color: label.color) project.labels.create!(title: label.title, color: label.color)
...@@ -68,6 +71,12 @@ module Github ...@@ -68,6 +71,12 @@ module Github
break unless url = response.rels[:next] break unless url = response.rels[:next]
end end
# Cache labels
# TODO: we should take group labels in account
project.labels.select(:id, :title).find_each do |label|
@cached_label_ids[label.title] = label.id
end
# Fetch milestones # Fetch milestones
url = "/repos/#{owner}/#{repo}/milestones" url = "/repos/#{owner}/#{repo}/milestones"
...@@ -208,16 +217,26 @@ module Github ...@@ -208,16 +217,26 @@ module Github
response.body.each do |raw| response.body.each do |raw|
representation = Github::Representation::Issue.new(raw) representation = Github::Representation::Issue.new(raw)
next if representation.pull_request? begin
# Every pull request is an issue, but not every issue
# is a pull request. For this reason, "shared" actions
# for both features, like manipulating assignees, labels
# and milestones, are provided within the Issues API.
if representation.pull_request?
next unless representation.has_labels?
merge_request = MergeRequest.find_by!(target_project_id: project.id, iid: representation.iid)
merge_request.update_attribute(:label_ids, label_ids(representation.labels))
else
next if Issue.where(iid: representation.iid, project_id: project.id).exists? next if Issue.where(iid: representation.iid, project_id: project.id).exists?
begin
issue = Issue.new issue = Issue.new
issue.iid = representation.iid issue.iid = representation.iid
issue.project_id = project.id issue.project_id = project.id
issue.title = representation.title issue.title = representation.title
issue.description = representation.description issue.description = representation.description
issue.state = representation.state issue.state = representation.state
issue.label_ids = label_ids(representation.labels)
issue.milestone_id = milestone_id(representation.milestone) issue.milestone_id = milestone_id(representation.milestone)
issue.author_id = user_id(representation.author, project.creator_id) issue.author_id = user_id(representation.author, project.creator_id)
issue.assignee_id = user_id(representation.assignee) issue.assignee_id = user_id(representation.assignee)
...@@ -225,7 +244,7 @@ module Github ...@@ -225,7 +244,7 @@ module Github
issue.updated_at = representation.updated_at issue.updated_at = representation.updated_at
issue.save(validate: false) issue.save(validate: false)
if issue.has_comments? if representation.has_comments?
# Fetch comments # Fetch comments
comments_url = "/repos/#{owner}/#{repo}/issues/#{issue.iid}/comments" comments_url = "/repos/#{owner}/#{repo}/issues/#{issue.iid}/comments"
...@@ -254,6 +273,7 @@ module Github ...@@ -254,6 +273,7 @@ module Github
break unless comments_url = comments.rels[:next] break unless comments_url = comments.rels[:next]
end end
end end
end
rescue => e rescue => e
error(:issue, representation.url, e.message) error(:issue, representation.url, e.message)
end end
...@@ -290,6 +310,10 @@ module Github ...@@ -290,6 +310,10 @@ module Github
remove_branch(pull_request.target_branch_name) unless pull_request.target_branch_exists? remove_branch(pull_request.target_branch_name) unless pull_request.target_branch_exists?
end end
def label_ids(issuable)
issuable.map { |attrs| cached_label_ids[attrs.fetch('name')] }.compact
end
def milestone_id(milestone) def milestone_id(milestone)
return unless milestone.present? return unless milestone.present?
......
...@@ -13,6 +13,10 @@ module Github ...@@ -13,6 +13,10 @@ module Github
raw['body'] || '' raw['body'] || ''
end end
def labels
raw['labels']
end
def milestone def milestone
return unless raw['milestone'].present? return unless raw['milestone'].present?
...@@ -53,6 +57,10 @@ module Github ...@@ -53,6 +57,10 @@ module Github
raw['comments'] > 0 raw['comments'] > 0
end end
def has_labels?
labels.count > 0
end
def pull_request? def pull_request?
raw['pull_request'].present? raw['pull_request'].present?
end 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