Last active
July 6, 2016 09:47
-
-
Save liger1978/aa6cce586dd018516dd44fb50c870c9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fisheye2gitlab.rb | |
# | |
# Migrate git repos from Atlassian FishEye git server (or a generic git server) | |
# to GitLab with JIRA integration enabled. | |
# | |
# Requires https://github.com/NARKOZ/gitlab | |
# | |
# Ensure 'git' user on GitLab server has git/ssh/http access to source repos for | |
# migration | |
require 'gitlab' | |
# Set these variables for your environment | |
gitlab_host = 'gitlab.my.domain' | |
gitlab_proto = 'https' | |
gitlab_port = '443' | |
gitlab_token = 'my_token' | |
gitlab_hook_regex = '\b[A-Z]{3}-[0-9]+\b|#\d{5}' | |
jira_host = 'jira.my.domain' | |
jira_proto = 'https' | |
jira_port = '443' | |
jira_username = 'svc-gitlab' # Ensure this service account exists in JIRA | |
jira_password = 'my_password' # with adequate permissions to update issues | |
# These are built from above variables and probably don't need to be altered | |
jira_root = "#{jira_proto}://#{jira_host}:#{jira_port}" | |
jira_new_issue_url = "#{jira_root}/secure/CreateIssue.jspa" | |
jira_project_url_root = "#{jira_root}/issues/?jql=project=" | |
jira_issues_url = "#{jira_root}/browse/:id" | |
jira_api_url = "#{jira_root}/rest/api/2" | |
# Supply an array of projects to be migrated. | |
projects = [{ name: 'webapp', | |
description: 'Our Web App', | |
jira_project_url: "#{jira_project_url_root}webapp", | |
import_url: "ssh://[email protected]/git/webapp.git" | |
}, | |
{ name: 'scripts', | |
description: 'Our scripts repo', | |
jira_project_url: "#{jira_project_url_root}scripts", | |
import_url: "ssh://[email protected]/git/scripts.git" | |
} | |
] | |
# Create GitLab client object | |
g = Gitlab.client(endpoint: "#{gitlab_proto}://#{gitlab_host}:#{gitlab_port}/api/v3", | |
private_token: gitlab_token, | |
httparty: {verify: false}) | |
# Create empty hash to hold existing project names and ids | |
existing_projects = {} | |
# Get existing projects object and add names and ids to hash | |
projects_obj = g.projects() | |
projects_obj.auto_paginate do |project| | |
proj_name = project.name | |
proj_id = project.id | |
existing_projects[proj_name] = proj_id | |
end | |
# Create projects on GitLab server if they don't already exist and update | |
# settings | |
projects.each do |project| | |
if existing_projects.keys.include?("#{project[:name]}") | |
puts "Project #{project[:name]} already exists" | |
id = existing_projects["#{project[:name]}"] | |
else | |
puts "Creating project #{project[:name]}..." | |
p = g.create_project("#{project[:name]}", | |
{description: "#{project[:description]}", | |
group_id: '4', | |
namespace_id: '4', | |
import_url: "#{project[:import_url]}"}) | |
id = p.id | |
end | |
g.change_service(id, :jira, { description: 'Jira issue tracker', | |
new_issue_url: "#{jira_new_issue_url}", | |
project_url: "#{project[:jira_project_url]}", | |
issues_url: "#{jira_issues_url}", | |
api_url: "#{jira_api_url}", | |
username: "#{jira_username}", | |
password: "#{jira_password}" }) | |
g.edit_git_hook(id, { deny_delete_tag: false, | |
commit_message_regex: "#{gitlab_hook_regex}" }) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment