2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
module Gitlab
|
|
|
|
module GoogleCodeImport
|
|
|
|
class Importer
|
2017-04-26 06:40:28 -04:00
|
|
|
attr_reader :project, :repo, :closed_statuses
|
|
|
|
|
|
|
|
NICE_LABEL_COLOR_HASH =
|
|
|
|
{
|
|
|
|
'Status: New' => '#428bca',
|
|
|
|
'Status: Accepted' => '#5cb85c',
|
|
|
|
'Status: Started' => '#8e44ad',
|
|
|
|
'Priority: Critical' => '#ffcfcf',
|
|
|
|
'Priority: High' => '#deffcf',
|
|
|
|
'Priority: Medium' => '#fff5cc',
|
|
|
|
'Priority: Low' => '#cfe9ff',
|
|
|
|
'Type: Defect' => '#d9534f',
|
|
|
|
'Type: Enhancement' => '#44ad8e',
|
|
|
|
'Type: Task' => '#4b6dd0',
|
|
|
|
'Type: Review' => '#8e44ad',
|
|
|
|
'Type: Other' => '#7f8c8d'
|
|
|
|
}.freeze
|
2015-04-03 09:29:27 -04:00
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2015-04-17 08:31:24 -04:00
|
|
|
|
|
|
|
import_data = project.import_data.try(:data)
|
|
|
|
repo_data = import_data["repo"] if import_data
|
|
|
|
@repo = GoogleCodeImport::Repository.new(repo_data)
|
2015-04-03 09:29:27 -04:00
|
|
|
|
|
|
|
@closed_statuses = []
|
|
|
|
@known_labels = Set.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return true unless repo.valid?
|
|
|
|
|
|
|
|
import_status_labels
|
|
|
|
|
|
|
|
import_labels
|
|
|
|
|
|
|
|
import_issues
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-04-14 08:50:56 -04:00
|
|
|
def user_map
|
|
|
|
@user_map ||= begin
|
2015-11-12 18:55:20 -05:00
|
|
|
user_map = Hash.new do |hash, user|
|
2015-04-21 04:32:48 -04:00
|
|
|
# Replace ... by \.\.\., so `johnsm...@gmail.com` isn't autolinked.
|
|
|
|
Client.mask_email(user).sub("...", "\\.\\.\\.")
|
|
|
|
end
|
2015-04-14 08:50:56 -04:00
|
|
|
|
2015-04-17 08:31:24 -04:00
|
|
|
import_data = project.import_data.try(:data)
|
|
|
|
stored_user_map = import_data["user_map"] if import_data
|
2015-04-14 08:50:56 -04:00
|
|
|
user_map.update(stored_user_map) if stored_user_map
|
|
|
|
|
|
|
|
user_map
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
def import_status_labels
|
|
|
|
repo.raw_data["issuesConfig"]["statuses"].each do |status|
|
|
|
|
closed = !status["meansOpen"]
|
|
|
|
@closed_statuses << status["status"] if closed
|
|
|
|
|
|
|
|
name = nice_status_name(status["status"])
|
|
|
|
create_label(name)
|
|
|
|
@known_labels << name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_labels
|
|
|
|
repo.raw_data["issuesConfig"]["labels"].each do |label|
|
|
|
|
name = nice_label_name(label["label"])
|
|
|
|
create_label(name)
|
|
|
|
@known_labels << name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-04-03 09:29:27 -04:00
|
|
|
def import_issues
|
2015-04-14 08:50:56 -04:00
|
|
|
return unless repo.issues
|
2015-04-03 09:29:27 -04:00
|
|
|
|
2015-04-17 08:55:51 -04:00
|
|
|
while raw_issue = repo.issues.shift
|
2015-04-14 08:50:56 -04:00
|
|
|
author = user_map[raw_issue["author"]["name"]]
|
2015-04-14 07:10:25 -04:00
|
|
|
date = DateTime.parse(raw_issue["published"]).to_formatted_s(:long)
|
2015-04-03 09:29:27 -04:00
|
|
|
|
|
|
|
comments = raw_issue["comments"]["items"]
|
|
|
|
issue_comment = comments.shift
|
|
|
|
|
2015-04-14 17:13:48 -04:00
|
|
|
content = format_content(issue_comment["content"])
|
2015-04-03 09:29:27 -04:00
|
|
|
attachments = format_attachments(raw_issue["id"], 0, issue_comment["attachments"])
|
2015-04-14 17:13:48 -04:00
|
|
|
|
|
|
|
body = format_issue_body(author, date, content, attachments)
|
2015-11-12 18:55:20 -05:00
|
|
|
labels = import_issue_labels(raw_issue)
|
2015-04-03 09:29:27 -04:00
|
|
|
|
2015-04-14 17:22:14 -04:00
|
|
|
assignee_id = nil
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_issue.key?("owner")
|
2015-04-14 17:22:14 -04:00
|
|
|
username = user_map[raw_issue["owner"]["name"]]
|
|
|
|
|
|
|
|
if username.start_with?("@")
|
|
|
|
username = username[1..-1]
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
if user = UserFinder.new(username).find_by_username
|
2015-04-14 17:22:14 -04:00
|
|
|
assignee_id = user.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-17 08:55:51 -04:00
|
|
|
issue = Issue.create!(
|
2017-05-04 08:11:15 -04:00
|
|
|
iid: raw_issue['id'],
|
|
|
|
project_id: project.id,
|
|
|
|
title: raw_issue['title'],
|
|
|
|
description: body,
|
|
|
|
author_id: project.creator_id,
|
|
|
|
assignee_ids: [assignee_id],
|
|
|
|
state: raw_issue['state'] == 'closed' ? 'closed' : 'opened'
|
2015-04-03 09:29:27 -04:00
|
|
|
)
|
2015-11-12 18:55:20 -05:00
|
|
|
|
2016-10-28 05:31:45 -04:00
|
|
|
issue_labels = ::LabelsFinder.new(nil, project_id: project.id, title: labels).execute(skip_authorization: true)
|
2016-10-19 15:27:58 -04:00
|
|
|
issue.update_attribute(:label_ids, issue_labels.pluck(:id))
|
2015-04-17 08:55:51 -04:00
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
import_issue_comments(issue, comments)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-03 09:29:27 -04:00
|
|
|
|
2015-11-12 18:55:20 -05:00
|
|
|
def import_issue_labels(raw_issue)
|
|
|
|
labels = []
|
|
|
|
|
|
|
|
raw_issue["labels"].each do |label|
|
|
|
|
name = nice_label_name(label)
|
|
|
|
labels << name
|
|
|
|
|
|
|
|
unless @known_labels.include?(name)
|
|
|
|
create_label(name)
|
|
|
|
@known_labels << name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
labels << nice_status_name(raw_issue["status"])
|
|
|
|
labels
|
|
|
|
end
|
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
def import_issue_comments(issue, comments)
|
2015-04-17 08:55:51 -04:00
|
|
|
Note.transaction do
|
|
|
|
while raw_comment = comments.shift
|
2017-06-02 13:11:26 -04:00
|
|
|
next if raw_comment.key?("deletedBy")
|
2015-04-17 08:55:51 -04:00
|
|
|
|
|
|
|
content = format_content(raw_comment["content"])
|
|
|
|
updates = format_updates(raw_comment["updates"])
|
|
|
|
attachments = format_attachments(issue.iid, raw_comment["id"], raw_comment["attachments"])
|
|
|
|
|
|
|
|
next if content.blank? && updates.blank? && attachments.blank?
|
|
|
|
|
|
|
|
author = user_map[raw_comment["author"]["name"]]
|
|
|
|
date = DateTime.parse(raw_comment["published"]).to_formatted_s(:long)
|
|
|
|
|
|
|
|
body = format_issue_comment_body(
|
|
|
|
raw_comment["id"],
|
|
|
|
author,
|
|
|
|
date,
|
|
|
|
content,
|
|
|
|
updates,
|
|
|
|
attachments
|
|
|
|
)
|
2015-04-03 09:29:27 -04:00
|
|
|
|
2015-04-17 08:55:51 -04:00
|
|
|
# Needs to match order of `comment_columns` below.
|
|
|
|
Note.create!(
|
|
|
|
project_id: project.id,
|
|
|
|
noteable_type: "Issue",
|
|
|
|
noteable_id: issue.id,
|
|
|
|
author_id: project.creator_id,
|
|
|
|
note: body
|
|
|
|
)
|
|
|
|
end
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nice_label_color(name)
|
2017-04-26 06:40:28 -04:00
|
|
|
NICE_LABEL_COLOR_HASH[name] ||
|
|
|
|
case name
|
|
|
|
when /\AComponent:/
|
|
|
|
'#fff39e'
|
|
|
|
when /\AOpSys:/
|
|
|
|
'#e2e2e2'
|
|
|
|
when /\AMilestone:/
|
|
|
|
'#fee3ff'
|
|
|
|
when *closed_statuses.map { |s| nice_status_name(s) }
|
|
|
|
'#cfcfcf'
|
|
|
|
else
|
|
|
|
'#e2e2e2'
|
|
|
|
end
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def nice_label_name(name)
|
|
|
|
name.sub("-", ": ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def nice_status_name(name)
|
|
|
|
"Status: #{name}"
|
|
|
|
end
|
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def linkify_issues(str)
|
|
|
|
str = str.gsub(/([Ii]ssue) ([0-9]+)/, '\1 #\2')
|
|
|
|
str = str.gsub(/([Cc]omment) #([0-9]+)/, '\1 \2')
|
|
|
|
str
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
def escape_for_markdown(str)
|
2015-04-21 04:31:15 -04:00
|
|
|
# No headings and lists
|
2018-07-04 10:02:01 -04:00
|
|
|
str = str.gsub(/^#/, "\\#")
|
|
|
|
str = str.gsub(/^-/, "\\-")
|
2015-04-21 04:31:15 -04:00
|
|
|
|
|
|
|
# No inline code
|
2018-07-04 10:02:01 -04:00
|
|
|
str = str.gsub("`", "\\`")
|
2015-04-21 04:31:15 -04:00
|
|
|
|
|
|
|
# Carriage returns make me sad
|
2018-07-04 10:02:01 -04:00
|
|
|
str = str.delete("\r")
|
2015-04-21 04:31:15 -04:00
|
|
|
|
|
|
|
# Markdown ignores single newlines, but we need them as <br />.
|
2018-07-04 10:02:01 -04:00
|
|
|
str = str.gsub("\n", " \n")
|
2015-04-21 04:31:15 -04:00
|
|
|
|
2018-07-04 10:02:01 -04:00
|
|
|
str
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_label(name)
|
2016-10-17 22:27:10 -04:00
|
|
|
params = { name: name, color: nice_label_color(name) }
|
2016-10-28 05:31:45 -04:00
|
|
|
::Labels::FindOrCreateService.new(nil, project, params).execute(skip_authorization: true)
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_content(raw_content)
|
|
|
|
linkify_issues(escape_for_markdown(raw_content))
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_updates(raw_updates)
|
|
|
|
updates = []
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("status")
|
2015-04-03 09:29:27 -04:00
|
|
|
updates << "*Status: #{raw_updates["status"]}*"
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("owner")
|
2015-04-14 08:50:56 -04:00
|
|
|
updates << "*Owner: #{user_map[raw_updates["owner"]]}*"
|
2015-04-03 10:21:32 -04:00
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("cc")
|
2015-11-12 18:55:20 -05:00
|
|
|
cc = raw_updates["cc"].map do |l|
|
|
|
|
deleted = l.start_with?("-")
|
2015-04-03 09:29:27 -04:00
|
|
|
l = l[1..-1] if deleted
|
2015-04-14 08:50:56 -04:00
|
|
|
l = user_map[l]
|
2015-04-03 09:29:27 -04:00
|
|
|
l = "~~#{l}~~" if deleted
|
|
|
|
l
|
|
|
|
end
|
|
|
|
|
|
|
|
updates << "*Cc: #{cc.join(", ")}*"
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("labels")
|
2015-11-12 18:55:20 -05:00
|
|
|
labels = raw_updates["labels"].map do |l|
|
|
|
|
deleted = l.start_with?("-")
|
2015-04-03 09:29:27 -04:00
|
|
|
l = l[1..-1] if deleted
|
|
|
|
l = nice_label_name(l)
|
|
|
|
l = "~~#{l}~~" if deleted
|
|
|
|
l
|
|
|
|
end
|
|
|
|
|
|
|
|
updates << "*Labels: #{labels.join(", ")}*"
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("mergedInto")
|
2015-04-03 09:29:27 -04:00
|
|
|
updates << "*Merged into: ##{raw_updates["mergedInto"]}*"
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("blockedOn")
|
2015-04-03 09:29:27 -04:00
|
|
|
blocked_ons = raw_updates["blockedOn"].map do |raw_blocked_on|
|
2015-11-12 18:55:20 -05:00
|
|
|
format_blocking_updates(raw_blocked_on)
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
2015-11-12 18:55:20 -05:00
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
updates << "*Blocked on: #{blocked_ons.join(", ")}*"
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
if raw_updates.key?("blocking")
|
2015-04-03 09:29:27 -04:00
|
|
|
blockings = raw_updates["blocking"].map do |raw_blocked_on|
|
2015-11-12 18:55:20 -05:00
|
|
|
format_blocking_updates(raw_blocked_on)
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
2015-11-12 18:55:20 -05:00
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
updates << "*Blocking: #{blockings.join(", ")}*"
|
|
|
|
end
|
|
|
|
|
|
|
|
updates
|
|
|
|
end
|
|
|
|
|
2015-11-12 18:55:20 -05:00
|
|
|
def format_blocking_updates(raw_blocked_on)
|
|
|
|
name, id = raw_blocked_on.split(":", 2)
|
|
|
|
|
|
|
|
deleted = name.start_with?("-")
|
|
|
|
name = name[1..-1] if deleted
|
|
|
|
|
|
|
|
text =
|
|
|
|
if name == project.import_source
|
|
|
|
"##{id}"
|
|
|
|
else
|
2017-02-14 09:03:24 -05:00
|
|
|
"#{project.namespace.full_path}/#{name}##{id}"
|
2015-11-12 18:55:20 -05:00
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2015-11-12 18:55:20 -05:00
|
|
|
text = "~~#{text}~~" if deleted
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2015-04-03 09:29:27 -04:00
|
|
|
def format_attachments(issue_id, comment_id, raw_attachments)
|
|
|
|
return [] unless raw_attachments
|
|
|
|
|
|
|
|
raw_attachments.map do |attachment|
|
|
|
|
next if attachment["isDeleted"]
|
|
|
|
|
2015-04-04 08:19:05 -04:00
|
|
|
filename = attachment["fileName"]
|
|
|
|
link = "https://storage.googleapis.com/google-code-attachments/#{@repo.name}/issue-#{issue_id}/comment-#{comment_id}/#{filename}"
|
2015-11-12 18:55:20 -05:00
|
|
|
|
2015-04-04 08:19:05 -04:00
|
|
|
text = "[#{filename}](#{link})"
|
2015-07-19 18:53:43 -04:00
|
|
|
text = "!#{text}" if filename =~ /\.(png|jpg|jpeg|gif|bmp|tiff)\z/i
|
2015-04-04 08:19:05 -04:00
|
|
|
text
|
2015-04-03 09:29:27 -04:00
|
|
|
end.compact
|
|
|
|
end
|
2015-04-14 17:13:48 -04:00
|
|
|
|
|
|
|
def format_issue_comment_body(id, author, date, content, updates, attachments)
|
|
|
|
body = []
|
|
|
|
body << "*Comment #{id} by #{author} on #{date}*"
|
|
|
|
body << "---"
|
|
|
|
|
|
|
|
if content.blank?
|
|
|
|
content = "*(No comment has been entered for this change)*"
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2015-04-14 17:13:48 -04:00
|
|
|
body << content
|
|
|
|
|
|
|
|
if updates.any?
|
|
|
|
body << "---"
|
|
|
|
body += updates
|
|
|
|
end
|
|
|
|
|
|
|
|
if attachments.any?
|
|
|
|
body << "---"
|
|
|
|
body += attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
body.join("\n\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_issue_body(author, date, content, attachments)
|
|
|
|
body = []
|
2015-04-21 04:30:23 -04:00
|
|
|
body << "*By #{author} on #{date} (imported from Google Code)*"
|
2015-04-14 17:13:48 -04:00
|
|
|
body << "---"
|
|
|
|
|
|
|
|
if content.blank?
|
|
|
|
content = "*(No description has been entered for this issue)*"
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2015-04-14 17:13:48 -04:00
|
|
|
body << content
|
|
|
|
|
|
|
|
if attachments.any?
|
|
|
|
body << "---"
|
|
|
|
body += attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
body.join("\n\n")
|
|
|
|
end
|
2015-04-03 09:29:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|