BigW Consortium Gitlab

import.rake 3.73 KB
Newer Older
James Lopez committed
1 2 3 4 5 6 7 8 9
require 'benchmark'
require 'rainbow/ext/string'

class GithubImport
  def self.run!(*args)
    new(*args).run!
  end

  def initialize(token, gitlab_username, project_path, extras)
10
    @options = { token: token }
James Lopez committed
11 12 13 14 15 16
    @project_path = project_path
    @current_user = User.find_by_username(gitlab_username)
    @github_repo = extras.empty? ? nil : extras.first
  end

  def run!
17 18 19
    @repo = GithubRepos
      .new(@options[:token], @current_user, @github_repo)
      .choose_one!
James Lopez committed
20 21 22 23 24 25 26 27 28 29 30 31 32

    raise 'No repo found!' unless @repo

    show_warning!

    @project = Project.find_by_full_path(@project_path) || new_project

    import!
  end

  private

  def show_warning!
33
    puts "This will import GitHub #{@repo.full_name.bright} into GitLab #{@project_path.bright} as #{@current_user.name}"
James Lopez committed
34 35 36 37
    puts "Permission checks are ignored. Press any key to continue.".color(:red)

    STDIN.getch

38
    puts 'Starting the import (this could take a while)'.color(:green)
James Lopez committed
39 40 41
  end

  def import!
42
    @project.force_import_start
James Lopez committed
43

44 45
    import_success = false

James Lopez committed
46
    timings = Benchmark.measure do
47 48 49
      import_success = Gitlab::GithubImport::SequentialImporter
        .new(@project, token: @options[:token])
        .execute
James Lopez committed
50 51
    end

52 53 54 55 56 57 58
    if import_success
      @project.import_finish
      puts "Import finished. Timings: #{timings}".color(:green)
    else
      puts "Import was not successful. Errors were as follows:"
      puts @project.import_error
    end
James Lopez committed
59 60 61 62 63 64 65
  end

  def new_project
    Project.transaction do
      namespace_path, _sep, name = @project_path.rpartition('/')
      namespace = find_or_create_namespace(namespace_path)

66
      project = Projects::CreateService.new(
67
        @current_user,
James Lopez committed
68 69
        name: name,
        path: name,
70
        description: @repo.description,
71
        namespace_id: namespace.id,
James Lopez committed
72
        visibility_level: visibility_level,
73
        skip_wiki: @repo.has_wiki
74
      ).execute
75 76 77

      project.update!(
        import_type: 'github',
78 79
        import_source: @repo.full_name,
        import_url: @repo.clone_url.sub('://', "://#{@options[:token]}@")
80 81 82
      )

      project
James Lopez committed
83 84 85 86 87 88 89
    end
  end

  def find_or_create_namespace(names)
    return @current_user.namespace if names == @current_user.namespace_path
    return @current_user.namespace unless @current_user.can_create_group?

90
    Groups::NestedCreateService.new(@current_user, group_path: names).execute
James Lopez committed
91 92 93 94 95 96 97
  end

  def full_path_namespace(names)
    @full_path_namespace ||= Namespace.find_by_full_path(names)
  end

  def visibility_level
98
    @repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::CurrentSettings.current_application_settings.default_project_visibility
James Lopez committed
99 100 101 102
  end
end

class GithubRepos
103
  def initialize(token, current_user, github_repo)
104 105
    @client = Gitlab::GithubImport::Client.new(token)
    @client.octokit.auto_paginate = true
106

James Lopez committed
107 108 109 110 111 112 113 114
    @current_user = current_user
    @github_repo = github_repo
  end

  def choose_one!
    return found_github_repo if @github_repo

    repos.each do |repo|
115 116
      print "ID: #{repo.id.to_s.bright}".color(:green)
      print "\tName: #{repo.full_name}\n".color(:green)
James Lopez committed
117 118 119 120
    end

    print 'ID? '.bright

121
    repos.find { |repo| repo.id == repo_id }
James Lopez committed
122 123 124
  end

  def found_github_repo
125
    repos.find { |repo| repo.full_name == @github_repo }
James Lopez committed
126 127 128 129 130 131 132
  end

  def repo_id
    @repo_id ||= STDIN.gets.chomp.to_i
  end

  def repos
133
    @client.octokit.list_repositories
James Lopez committed
134 135 136 137 138 139 140 141 142 143 144
  end
end

namespace :import do
  desc 'Import a GitHub project - Example: import:github[ToKeN,root,root/blah,my/github_repo] (optional my/github_repo)'
  task :github, [:token, :gitlab_username, :project_path] => :environment do |_t, args|
    abort 'Project path must be: namespace(s)/project_name'.color(:red) unless args.project_path.include?('/')

    GithubImport.run!(args.token, args.gitlab_username, args.project_path, args.extras)
  end
end