diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-05-10 01:45:37 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-05-14 01:55:42 +0300 |
commit | 795a7ca8f171a1634134e8b8ce5cf80a65381903 (patch) | |
tree | 0c012e7c3d7eabcf0f4284cee91be8571564d665 /lib/gitlab | |
parent | e001bd5e3d813fa72f724fc0e661b36099f678ea (diff) |
Extract GitHub branch formatter
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/github_import/branch_formatter.rb | 25 | ||||
-rw-r--r-- | lib/gitlab/github_import/importer.rb | 30 | ||||
-rw-r--r-- | lib/gitlab/github_import/pull_request_formatter.rb | 74 |
3 files changed, 52 insertions, 77 deletions
diff --git a/lib/gitlab/github_import/branch_formatter.rb b/lib/gitlab/github_import/branch_formatter.rb new file mode 100644 index 00000000000..f968ec4b102 --- /dev/null +++ b/lib/gitlab/github_import/branch_formatter.rb @@ -0,0 +1,25 @@ +module Gitlab + module GithubImport + class BranchFormatter < BaseFormatter + delegate :repo, :sha, :ref, to: :raw_data + + def exists? + project.repository.branch_exists?(ref) + end + + def name + @name ||= exists? ? ref : "#{ref}-#{short_id}" + end + + def valid? + repo.present? + end + + private + + def short_id + sha.to_s[0..7] + end + end + end +end diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index 9aea947a812..1f160837c5d 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -6,8 +6,8 @@ module Gitlab attr_reader :client, :project, :repo, :repo_url def initialize(project) - @project = project - @repo = project.import_source + @project = project + @repo = project.import_source @repo_url = project.import_url if credentials @@ -30,7 +30,7 @@ module Gitlab end def import_labels - client.labels(project.import_source).each do |raw_data| + client.labels(repo).each do |raw_data| Label.create!(LabelFormatter.new(project, raw_data).attributes) end @@ -40,7 +40,7 @@ module Gitlab end def import_milestones - client.list_milestones(project.import_source, state: :all).each do |raw_data| + client.list_milestones(repo, state: :all).each do |raw_data| Milestone.create!(MilestoneFormatter.new(project, raw_data).attributes) end @@ -50,9 +50,7 @@ module Gitlab end def import_issues - client.list_issues(project.import_source, state: :all, - sort: :created, - direction: :asc).each do |raw_data| + client.list_issues(repo, state: :all, sort: :created, direction: :asc).each do |raw_data| gh_issue = IssueFormatter.new(project, raw_data) if gh_issue.valid? @@ -75,8 +73,8 @@ module Gitlab .map { |raw| PullRequestFormatter.new(project, raw) } .select(&:valid?) - source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch, pr.source_sha] } - target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch, pr.target_sha] } + source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch_name, pr.source_branch_sha] } + target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch_name, pr.target_branch_sha] } branches_removed = source_branches_removed | target_branches_removed create_refs(branches_removed) @@ -101,19 +99,19 @@ module Gitlab end def create_refs(branches) - branches.each do |branch| - client.create_ref(repo, "refs/heads/#{branch.first}", branch.last) + branches.each do |name, sha| + client.create_ref(repo, "refs/heads/#{name}", sha) end end def delete_refs(branches) - branches.each do |branch| - client.delete_ref(repo, "heads/#{branch.first}") + branches.each do |name, _| + client.delete_ref(repo, "heads/#{name}") end end def apply_labels(number, issuable) - issue = client.issue(project.import_source, number) + issue = client.issue(repo, number) if issue.labels.count > 0 label_ids = issue.labels.map do |raw| @@ -125,12 +123,12 @@ module Gitlab end def import_comments(issue_number, noteable) - comments = client.issue_comments(project.import_source, issue_number) + comments = client.issue_comments(repo, issue_number) create_comments(comments, noteable) end def import_comments_on_diff(pull_request_number, merge_request) - comments = client.pull_request_comments(project.import_source, pull_request_number) + comments = client.pull_request_comments(repo, pull_request_number) create_comments(comments, merge_request) end diff --git a/lib/gitlab/github_import/pull_request_formatter.rb b/lib/gitlab/github_import/pull_request_formatter.rb index 9f8182f643e..361d15d991c 100644 --- a/lib/gitlab/github_import/pull_request_formatter.rb +++ b/lib/gitlab/github_import/pull_request_formatter.rb @@ -1,17 +1,20 @@ module Gitlab module GithubImport class PullRequestFormatter < BaseFormatter + delegate :exists?, :name, :project, :repo, :sha, to: :source_branch, prefix: true + delegate :exists?, :name, :project, :repo, :sha, to: :target_branch, prefix: true + def attributes { iid: number, title: raw_data.title, description: description, - source_project: source_project, - source_branch: source_branch, - head_source_sha: source_sha, - target_project: target_project, - target_branch: target_branch, - base_target_sha: target_sha, + source_project: source_branch_project, + source_branch: source_branch_name, + head_source_sha: source_branch_sha, + target_project: target_branch_project, + target_branch: target_branch_name, + base_target_sha: target_branch_sha, state: state, milestone: milestone, author_id: author_id, @@ -29,40 +32,12 @@ module Gitlab !cross_project? end - def source_branch_exists? - source_project.repository.branch_exists?(source_ref) - end - def source_branch - @source_branch ||= if source_branch_exists? - source_ref - else - "#{source_ref}-#{short_id(source_sha)}" - end - end - - def short_id(sha, length = 7) - sha.to_s[0..length] - end - - def source_sha - raw_data.head.sha - end - - def target_branch_exists? - target_project.repository.branch_exists?(target_ref) + @source_branch ||= BranchFormatter.new(project, raw_data.head) end def target_branch - @target_branch ||= if target_branch_exists? - target_ref - else - "#{target_ref}-#{short_id(target_sha)}" - end - end - - def target_sha - raw_data.base.sha + @target_branch ||= BranchFormatter.new(project, raw_data.base) end private @@ -90,7 +65,8 @@ module Gitlab end def cross_project? - source_repo.present? && target_repo.present? && source_repo.id != target_repo.id + source_branch_repo.present? && target_branch_repo.present? && + source_branch_repo.id != target_branch_repo.id end def description @@ -103,30 +79,6 @@ module Gitlab end end - def source_project - project - end - - def source_repo - raw_data.head.repo - end - - def source_ref - raw_data.head.ref - end - - def target_project - project - end - - def target_repo - raw_data.base.repo - end - - def target_ref - raw_data.base.ref - end - def state @state ||= case true when raw_data.state == 'closed' && raw_data.merged_at.present? |