Class: Gitlab::GithubImport::Importer::IssueImporter

Inherits:
Object
  • Object
show all
Includes:
Import::DatabaseHelpers
Defined in:
lib/gitlab/github_import/importer/issue_importer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Import::DatabaseHelpers

#insert_and_return_id

Constructor Details

#initialize(issue, project, client) ⇒ IssueImporter

issue - An instance of `Gitlab::GithubImport::Representation::Issue`. project - An instance of `Project` client - An instance of `Gitlab::GithubImport::Client`


20
21
22
23
24
25
26
27
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 20

def initialize(issue, project, client)
  @issue = issue
  @project = project
  @client = client
  @user_finder = GithubImport::UserFinder.new(project, client)
  @milestone_finder = MilestoneFinder.new(project)
  @issuable_finder = GithubImport::IssuableFinder.new(project, issue)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def client
  @client
end

#issuable_finderObject (readonly)

Returns the value of attribute issuable_finder


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def issuable_finder
  @issuable_finder
end

#issueObject (readonly)

Returns the value of attribute issue


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def issue
  @issue
end

#milestone_finderObject (readonly)

Returns the value of attribute milestone_finder


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def milestone_finder
  @milestone_finder
end

#projectObject (readonly)

Returns the value of attribute project


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def project
  @project
end

#user_finderObject (readonly)

Returns the value of attribute user_finder


9
10
11
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 9

def user_finder
  @user_finder
end

Class Method Details

.import_if_issue(issue, project, client) ⇒ Object

Imports an issue if it's a regular issue and not a pull request.


13
14
15
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 13

def self.import_if_issue(issue, project, client)
  new(issue, project, client).execute unless issue.pull_request?
end

Instance Method Details

#create_assignees(issue_id) ⇒ Object

Stores all issue assignees in the database.

issue_id - The ID of the created issue.


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 69

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) # rubocop:disable Gitlab/BulkInsert
end

#create_issueObject

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.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 42

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_id: ::Issue.available_states[issue.state],
    created_at: issue.created_at,
    updated_at: issue.updated_at
  }

  insert_and_return_id(attributes, project.issues)
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

#executeObject


29
30
31
32
33
34
35
36
# File 'lib/gitlab/github_import/importer/issue_importer.rb', line 29

def execute
  Issue.transaction do
    if (issue_id = create_issue)
      create_assignees(issue_id)
      issuable_finder.cache_database_id(issue_id)
    end
  end
end