From 538e8b2c68fb1aebb84987d21dd04f627e77aaa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 19 Jan 2015 19:46:02 +0500 Subject: [PATCH 1/4] Github import script: now can import only specified repository --- import/github/import_all.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/import/github/import_all.rb b/import/github/import_all.rb index b012549..5086367 100644 --- a/import/github/import_all.rb +++ b/import/github/import_all.rb @@ -19,6 +19,7 @@ :group => nil, :ssh => false, :private => false, + :repository => nil, :gitlab_api => 'http://gitlab.example.com/api/v3', :gitlab_token => 'secret' } @@ -54,6 +55,9 @@ opts.on('-g', '--group GROUP', 'The GitLab group to import projects to') do |g| options[:group] = g end + opts.on('--repository REPOSITORY', String, 'Imoprt only specified repository') do |r| + options[:repository] = r + end opts.on('-h', '--help', 'Display this screen') do puts opts exit @@ -89,8 +93,12 @@ #setup the clients gh_client = Octokit::Client.new(:login => options[:usr], :password => options[:pw]) gl_client = Gitlab.client() -#get all of the repos that are in the specified space (user or org) -gh_repos = gh_client.repositories(options[:space], {:type => options[:private] ? 'private' : 'all'}) +if options[:repository].nil? + #get all of the repos that are in the specified space (user or org) + gh_repos = gh_client.repositories(options[:space], {:type => options[:private] ? 'private' : 'all'}) +else + gh_repos = [gh_client.repository(options[:repository])] +end gh_repos.each do |gh_r| # ## clone the repo from the github server @@ -129,7 +137,7 @@ name = "gh-#{gh_r.name}" end - puts gh_r.name + puts "Importing repository '#{gh_r.name}'" #create and push the project to GitLab new_project = gl_client.create_project(name) git_repo.add_remote("gitlab", new_project.ssh_url_to_repo) @@ -186,7 +194,7 @@ gl_client.close_issue(new_project.id, gl_issue.id) end - pp i.number.to_s + ' ' + i.title + ' ' + i.state + ' ' + labels + puts "Importing issue \##{i.number} '#{i.title}' \{#{i.state}\} [#{labels}]" end end From 2f5ccdc060c48994b1a6933780c0619f409b187b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 19 Jan 2015 20:36:05 +0500 Subject: [PATCH 2/4] Github import script: now imports milestones. --- import/github/import_all.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/import/github/import_all.rb b/import/github/import_all.rb index 5086367..66a4807 100644 --- a/import/github/import_all.rb +++ b/import/github/import_all.rb @@ -148,7 +148,16 @@ labels.each do |l| gl_client.create_label(new_project.id, l.name, '#'+l.color) end - + + # Copy milestones for this project + milestone_hash = {} + milestones = gh_client.milestones(gh_r.full_name) + milestones.each do |m| + gl_milestone = gl_client.create_milestone(new_project.id, m.title, {:description => m.description, :due_date => m.due_on }) + milestone_hash[gl_milestone.title] = gl_milestone.id + puts "Imported milestone #{gl_milestone.title}" + end + # ## Look for issues in GitHub for this project and push them to GitLab ## I wish the GitLab API let me create comments for issues. Oh well, smashing it all into the body of the issue. @@ -188,13 +197,17 @@ labels = i.labels.map {|l| l.name }.join(sep=',') - gl_issue = gl_client.create_issue(new_project.id, i.title, :description => body, :labels => labels) + unless i.milestone.nil? or i.milestone.title.nil? or milestone_hash[i.milestone.title].nil? + milestone_id = milestone_hash[i.milestone.title] + end + + gl_issue = gl_client.create_issue(new_project.id, i.title, :description => body, :labels => labels, :milestone_id => milestone_id) if i.state == 'closed' gl_client.close_issue(new_project.id, gl_issue.id) end - puts "Importing issue \##{i.number} '#{i.title}' \{#{i.state}\} [#{labels}]" + puts "Imported issue \##{i.number} '#{i.title}' \{#{i.state}\} [#{labels}]" end end From 4d309aee6f4f4d6bd83b2a7be3de3d4c339d8faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 20 Jan 2015 16:12:20 +0500 Subject: [PATCH 3/4] Github import script: add CLI parameters --no-issues, --no-milestones and --all. --- import/github/import_all.rb | 38 +++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/import/github/import_all.rb b/import/github/import_all.rb index 66a4807..d6fde98 100644 --- a/import/github/import_all.rb +++ b/import/github/import_all.rb @@ -20,6 +20,8 @@ :ssh => false, :private => false, :repository => nil, + :import_issues => true, + :import_milestones => true, :gitlab_api => 'http://gitlab.example.com/api/v3', :gitlab_token => 'secret' } @@ -49,15 +51,25 @@ options[:private] = p options[:ssh] = true end + opts.on('--all', 'Import all GitHub repositories (enables ssh)') do |a| + options[:all] = a + options[:ssh] = true + end opts.on('-s', '--space SPACE', 'The space to import repositories from (User or Organization)') do |s| options[:space] = s end opts.on('-g', '--group GROUP', 'The GitLab group to import projects to') do |g| options[:group] = g end - opts.on('--repository REPOSITORY', String, 'Imoprt only specified repository') do |r| + opts.on('--repository REPOSITORY', String, 'Import only specified repository') do |r| options[:repository] = r end + opts.on('--[no-]issues', '[do not] import issues') do |b| + options[:import_issues] = b + end + opts.on('--[no-]milestones', '[do not] import milestones') do |b| + options[:import_milestones] = b + end opts.on('-h', '--help', 'Display this screen') do puts opts exit @@ -71,6 +83,14 @@ exit end +if options[:private] + options[:space] = nil +end + +if not options[:import_issues] + options[:import_milestones] = false +end + if options[:group].nil? if options[:space].nil? raise 'Both group and space can\'t be empty!' @@ -95,7 +115,7 @@ gl_client = Gitlab.client() if options[:repository].nil? #get all of the repos that are in the specified space (user or org) - gh_repos = gh_client.repositories(options[:space], {:type => options[:private] ? 'private' : 'all'}) + gh_repos = gh_client.repositories(options[:space], {:type => options[:private] ? 'private' : (options[:all] ? 'all' : 'public')}) else gh_repos = [gh_client.repository(options[:repository])] end @@ -151,18 +171,20 @@ # Copy milestones for this project milestone_hash = {} - milestones = gh_client.milestones(gh_r.full_name) - milestones.each do |m| - gl_milestone = gl_client.create_milestone(new_project.id, m.title, {:description => m.description, :due_date => m.due_on }) - milestone_hash[gl_milestone.title] = gl_milestone.id - puts "Imported milestone #{gl_milestone.title}" + if options[:import_milestones] + milestones = gh_client.milestones(gh_r.full_name) + milestones.each do |m| + gl_milestone = gl_client.create_milestone(new_project.id, m.title, {:description => m.description, :due_date => m.due_on }) + milestone_hash[gl_milestone.title] = gl_milestone.id + puts "Imported milestone #{gl_milestone.title}" + end end # ## Look for issues in GitHub for this project and push them to GitLab ## I wish the GitLab API let me create comments for issues. Oh well, smashing it all into the body of the issue. # - if gh_r.has_issues + if gh_r.has_issues and options[:import_issues] issues = [] # Get opened issues From 170c4bf5db5aa4d855f917c1f3518a01d32f0e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=91=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 20 Jan 2015 17:08:19 +0500 Subject: [PATCH 4/4] Github import script: add README.md --- import/github/README.md | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 import/github/README.md diff --git a/import/github/README.md b/import/github/README.md new file mode 100644 index 0000000..d1c35d1 --- /dev/null +++ b/import/github/README.md @@ -0,0 +1,53 @@ +import_all.rb is a ruby script that can import repositories from GitHub to GitLab. + +## Simple usage: +```bash +ruby import_all.rb -u -p \ +-s -t \ +--gitlab-api http:///api/v3 +``` +This command will import all public projects from specified space. Take in mind, that you should specify valid GitHub user login and password (2-factor authentication is not supported). Also, script uses directory /tmp/clones, it should be **created** and **empty**. + +## CLI options +* **-u, --user USER** - user to connect to *GitHub* with. Valid *GitHub* user login. +* **-p, --pw PASSWORD** - password for user to connect to *GitHub* with. Valid *GitHub* user password. +* **--api API** - API endpoint for *GitHub*. Default is "https://api.github.com". Change it to import from the *GitHub* Enterprise. +* **--web** - Web endpoint for *GitHub*. Default is "https://github.com/". Change it to import from the *GitHub* Enterprise. +* **--gitlab-api API** - API endpoint for *GitLab*. Set it to API endpoint of the destination *GitLab* instance. +* **-t, --gitlab-token TOKEN** - Private token for *GitLab*. Set it to the token from your *GitLab* account (http://my.gitlab.host/profile/account). +* **--ssh** - Use ssh for *GitHub*. By default, script uses **https** protocol to access *GitHub*. Use this option to change the protocol to the **ssh**. +* **-s, --space SPACE** - The space to import repositories from (User or Organization). If **--group** is not set, **SPACE** will also be used to determine destination projects **group** in *GitLab*. If **SPACE** is not set, the default space of the authenticated user will be used. +* **-g, --group GROUP** - The *GitLab* group to import projects to. Determines destination projects **group** in *GitLab*. +* **--private** - Import only private *GitHub* repositories (enables ssh). By default, script will try to import only public projects from specified space. Use this option to import only private projects of authenticated user (automatically enables **ssh** protocol, ignores **SPACE**). +* **--all** - Import both private and public *GitHub* repositories (enables ssh). By default, script will try to import only public projects from specified space. Use this option to import both public and private projects of authenticated user (automatically enables **ssh** protocol, ignores **SPACE**). +* **--repository REPOSITORY** -- Import only specified repository. By default, script will try to import all available repositories from selected **SPACE**. This option allows to import only one specified repository. +* **--[no-]issues** - [do not] import issues. Use this switch to enable or disable import of issues. Enabled by default. +* **--[no-]milestones** - [do not] import milestones. Use this switch to enable or disable import of milestones. Enabled by default. +* **-h, --help** - Display help. + +## Examples +1. This example will import all private repositories of user1 from user1 **SPACE** from *GitHub* (https://github.com/user1/*) to user1 project group in *GitLab* (http://gitlab.example.com/user1/*). +```bash +rm -rf /tmp/clones/ && # remove /tmp/clones/ directory +mkdir /tmp/clones/ && # and recreate it +ruby import_all.rb -u user1 -p password_of_user1 -s user1 --private \ +--gitlab-api http://gitlab.example.com/api/v3 -t some_private_token_from_gitalb +``` + +2. This example will import all public repositories from gitlabhq **SPACE** from *GitHub* (https://github.com/gitlabhq/*) to group1 project group in *GitLab* (http://gitlab.example.com/group1/*) without issues. +```bash +rm -rf /tmp/clones/ && +mkdir /tmp/clones/ && +ruby import_all.rb -u user1 -p password_of_user1 --group group1 \ +-s gitlabhq --gitlab-api http://gitlab.example.com/api/v3 --no-issues \ +-t some_private_token_from_gitalb +``` + +3. This example will import repository gitlabhq/gitlab-ci from *GitHub* (https://github.com/gitlabhq/gitlab-ci) to group2 project group in *GitLab* (http://gitlab.example.com/group2/gitlab-ci) with issues. +```bash +rm -rf /tmp/clones/ && +mkdir /tmp/clones/ && +ruby import_all.rb -u user1 -p password_of_user1 \ +--gitlab-api http://gitlab.example.com/api/v3 --group group2 \ +-t some_private_token_from_gitalb --repository gitlabhq/gitlab-ci +```