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,50 +217,61 @@ module Github ...@@ -208,50 +217,61 @@ 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?
next if Issue.where(iid: representation.iid, project_id: project.id).exists?
begin begin
issue = Issue.new # Every pull request is an issue, but not every issue
issue.iid = representation.iid # is a pull request. For this reason, "shared" actions
issue.project_id = project.id # for both features, like manipulating assignees, labels
issue.title = representation.title # and milestones, are provided within the Issues API.
issue.description = representation.description if representation.pull_request?
issue.state = representation.state next unless representation.has_labels?
issue.milestone_id = milestone_id(representation.milestone)
issue.author_id = user_id(representation.author, project.creator_id) merge_request = MergeRequest.find_by!(target_project_id: project.id, iid: representation.iid)
issue.assignee_id = user_id(representation.assignee) merge_request.update_attribute(:label_ids, label_ids(representation.labels))
issue.created_at = representation.created_at else
issue.updated_at = representation.updated_at next if Issue.where(iid: representation.iid, project_id: project.id).exists?
issue.save(validate: false)
issue = Issue.new
if issue.has_comments? issue.iid = representation.iid
# Fetch comments issue.project_id = project.id
comments_url = "/repos/#{owner}/#{repo}/issues/#{issue.iid}/comments" issue.title = representation.title
issue.description = representation.description
loop do issue.state = representation.state
comments = Github::Client.new.get(comments_url) issue.label_ids = label_ids(representation.labels)
issue.milestone_id = milestone_id(representation.milestone)
ActiveRecord::Base.no_touching do issue.author_id = user_id(representation.author, project.creator_id)
comments.body.each do |raw| issue.assignee_id = user_id(representation.assignee)
begin issue.created_at = representation.created_at
comment = Github::Representation::Comment.new(raw) issue.updated_at = representation.updated_at
issue.save(validate: false)
note = Note.new
note.project_id = project.id if representation.has_comments?
note.noteable = issue # Fetch comments
note.note = comment.note comments_url = "/repos/#{owner}/#{repo}/issues/#{issue.iid}/comments"
note.author_id = user_id(comment.author, project.creator_id)
note.created_at = comment.created_at loop do
note.updated_at = comment.updated_at comments = Github::Client.new.get(comments_url)
note.save!(validate: false)
rescue => e ActiveRecord::Base.no_touching do
error(:comment, comment.url, e.message) comments.body.each do |raw|
begin
comment = Github::Representation::Comment.new(raw)
note = Note.new
note.project_id = project.id
note.noteable = issue
note.note = comment.note
note.author_id = user_id(comment.author, project.creator_id)
note.created_at = comment.created_at
note.updated_at = comment.updated_at
note.save!(validate: false)
rescue => e
error(:comment, comment.url, e.message)
end
end end
end end
end
break unless comments_url = comments.rels[:next] break unless comments_url = comments.rels[:next]
end
end end
end end
rescue => e rescue => e
...@@ -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