Class: Gitlab::GithubImport::Importer::PullRequestImporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Import::MergeRequestHelpers

#create_merge_request_without_hooks, #insert_merge_request_reviewers, #insert_or_replace_git_data

Methods included from Import::DatabaseHelpers

#insert_and_return_id

Constructor Details

#initialize(pull_request, project, client) ⇒ PullRequestImporter

pull_request - An instance of

`Gitlab::GithubImport::Representation::PullRequest`.

project - An instance of ‘Project` client - An instance of `Gitlab::GithubImport::Client`



16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/github_import/importer/pull_request_importer.rb', line 16

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/gitlab/github_import/importer/pull_request_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/pull_request_importer.rb', line 9

def issuable_finder
  @issuable_finder
end

#milestone_finderObject (readonly)

Returns the value of attribute milestone_finder.



9
10
11
# File 'lib/gitlab/github_import/importer/pull_request_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/pull_request_importer.rb', line 9

def project
  @project
end

#pull_requestObject (readonly)

Returns the value of attribute pull_request.



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

def pull_request
  @pull_request
end

#user_finderObject (readonly)

Returns the value of attribute user_finder.



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

def user_finder
  @user_finder
end

Instance Method Details

#create_merge_requestObject

Creates the merge request and returns its ID.

This method will return ‘nil` if the merge request could not be created, otherwise it will return an Array containing the following values:

  1. A MergeRequest instance.

  2. A boolean indicating if the MR already exists.



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

def create_merge_request
  author_id, author_found = user_finder.author_id_for(pull_request)

  description = MarkdownText.format(pull_request.description, pull_request.author, author_found)

  attributes = {
    iid: pull_request.iid,
    title: pull_request.truncated_title,
    description: description,
    source_project_id: project.id,
    target_project_id: project.id,
    source_branch: pull_request.formatted_source_branch,
    target_branch: pull_request.target_branch,
    state_id: ::MergeRequest.available_states[pull_request.state],
    milestone_id: milestone_finder.id_for(pull_request),
    author_id: author_id,
    created_at: pull_request.created_at,
    updated_at: pull_request.updated_at
  }

  mr = project.merge_requests.new(attributes.merge(importing: true))
  mr.validate!

  create_merge_request_without_hooks(project, attributes, pull_request.iid)
end

#create_source_branch_if_not_exists(merge_request) ⇒ Object

An imported merge request will not be mergeable unless the source branch exists. For pull requests from forks, the source branch will be in the form of “github/fork/project-name/source_branch”. This branch will never exist, so we create it here.

Note that we only create the branch if the merge request is still open. For projects that have many pull requests, we assume that if it’s closed the branch has already been deleted.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gitlab/github_import/importer/pull_request_importer.rb', line 91

def create_source_branch_if_not_exists(merge_request)
  return unless merge_request.open?

  source_branch = pull_request.formatted_source_branch

  return if project.repository.branch_exists?(source_branch)

  project.repository.add_branch(project.creator, source_branch, pull_request.source_branch_sha)
rescue Gitlab::Git::PreReceiveError, Gitlab::Git::CommandError => e
  Gitlab::ErrorTracking.track_exception(e,
    source_branch: source_branch,
    project_id: merge_request.project.id,
    merge_request_id: merge_request.id)
end

#executeObject



26
27
28
29
30
31
32
33
34
# File 'lib/gitlab/github_import/importer/pull_request_importer.rb', line 26

def execute
  mr, already_exists = create_merge_request

  if mr
    set_merge_request_assignees(mr)
    insert_git_data(mr, already_exists)
    issuable_finder.cache_database_id(mr.id)
  end
end

#insert_git_data(merge_request, already_exists) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/gitlab/github_import/importer/pull_request_importer.rb', line 74

def insert_git_data(merge_request, already_exists)
  insert_or_replace_git_data(merge_request, pull_request.source_branch_sha, pull_request.target_branch_sha, already_exists)
  # We need to create the branch after the merge request is
  # populated to ensure the merge request is in the right state
  # when the branch is created.
  create_source_branch_if_not_exists(merge_request)
end

#set_merge_request_assignees(merge_request) ⇒ Object



70
71
72
# File 'lib/gitlab/github_import/importer/pull_request_importer.rb', line 70

def set_merge_request_assignees(merge_request)
  merge_request.assignee_ids = [user_finder.assignee_id_for(pull_request)]
end