2017-10-13 16:50:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
module Importer
|
|
|
|
class IssueImporter
|
2018-08-13 22:36:15 +00:00
|
|
|
include Gitlab::Import::DatabaseHelpers
|
|
|
|
|
2017-10-13 16:50:36 +00:00
|
|
|
attr_reader :project, :issue, :client, :user_finder, :milestone_finder,
|
|
|
|
:issuable_finder
|
|
|
|
|
|
|
|
# Imports an issue if it's a regular issue and not a pull request.
|
|
|
|
def self.import_if_issue(issue, project, client)
|
|
|
|
new(issue, project, client).execute unless issue.pull_request?
|
|
|
|
end
|
|
|
|
|
|
|
|
# issue - An instance of `Gitlab::GithubImport::Representation::Issue`.
|
|
|
|
# project - An instance of `Project`
|
|
|
|
# client - An instance of `Gitlab::GithubImport::Client`
|
|
|
|
def initialize(issue, project, client)
|
|
|
|
@issue = issue
|
|
|
|
@project = project
|
|
|
|
@client = client
|
2018-07-30 15:45:49 +00:00
|
|
|
@user_finder = GithubImport::UserFinder.new(project, client)
|
2017-10-13 16:50:36 +00:00
|
|
|
@milestone_finder = MilestoneFinder.new(project)
|
|
|
|
@issuable_finder = GithubImport::IssuableFinder.new(project, issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
Issue.transaction do
|
|
|
|
if (issue_id = create_issue)
|
|
|
|
create_assignees(issue_id)
|
|
|
|
issuable_finder.cache_database_id(issue_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a new GitLab issue for the current GitHub issue.
|
|
|
|
#
|
|
|
|
# Returns the ID of the created issue as an Integer. If the issue
|
|
|
|
# couldn't be created this method will return `nil` instead.
|
|
|
|
def create_issue
|
|
|
|
author_id, author_found = user_finder.author_id_for(issue)
|
|
|
|
|
|
|
|
description =
|
|
|
|
MarkdownText.format(issue.description, issue.author, author_found)
|
|
|
|
|
|
|
|
attributes = {
|
|
|
|
iid: issue.iid,
|
|
|
|
title: issue.truncated_title,
|
|
|
|
author_id: author_id,
|
|
|
|
project_id: project.id,
|
|
|
|
description: description,
|
|
|
|
milestone_id: milestone_finder.id_for(issue),
|
|
|
|
state: issue.state,
|
|
|
|
created_at: issue.created_at,
|
|
|
|
updated_at: issue.updated_at
|
|
|
|
}
|
|
|
|
|
2018-08-13 22:36:15 +00:00
|
|
|
insert_and_return_id(attributes, project.issues).tap do |id|
|
2018-07-31 14:13:32 +00:00
|
|
|
# We use .insert_and_return_id which effectively disables all callbacks.
|
|
|
|
# Trigger iid logic here to make sure we track internal id values consistently.
|
|
|
|
project.issues.find(id).ensure_project_iid!
|
|
|
|
end
|
2017-10-13 16:50:36 +00:00
|
|
|
rescue ActiveRecord::InvalidForeignKey
|
|
|
|
# It's possible the project has been deleted since scheduling this
|
|
|
|
# job. In this case we'll just skip creating the issue.
|
|
|
|
end
|
|
|
|
|
|
|
|
# Stores all issue assignees in the database.
|
|
|
|
#
|
|
|
|
# issue_id - The ID of the created issue.
|
|
|
|
def create_assignees(issue_id)
|
|
|
|
assignees = []
|
|
|
|
|
|
|
|
issue.assignees.each do |assignee|
|
|
|
|
if (user_id = user_finder.user_id_for(assignee))
|
|
|
|
assignees << { issue_id: issue_id, user_id: user_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Database.bulk_insert(IssueAssignee.table_name, assignees)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|