Importing Your Project from GitHub to GitLab
local graph
Chapter 5 — Importing Your Project from GitHub to GitLab
Section titled “Chapter 5 — Importing Your Project from GitHub to GitLab”In the first section, we explained the GitLab architecture and how to install and configure GitLab in several ways. In this part of the book, we will look at several ways of migrating source projects from third parties to GitLab. We will start with GitHub, which resembles GitLab a lot and which was the inspiration to develop it. After years of development, the two products still share the same basic functionality, but have diverged in additional features. There are several ways you can import projects from GitHub.
In this chapter, the following points will be covered:
- Using the GitHub integration feature in GitLab (also known as the GitHub importer)
- Using a GitHub token—create this in GitHub for third party application integration
- Using a GitLab rake task—you need access to a GitLab instance that can run rake tasks, and when you possess administrator rights
For each method, we will present what you have to configure and prepare in GitHub and GitLab. Finally, we will run the import for each method.
In order to manage omnibus installs, you need to use a central configuration file called gitlab.rb. You need to create it or copy an example of one. Fortunately, a template is available, which you can find at https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template. It isn’t updated after upgrades. Later on in this chapter, I will quote and discuss parts of this file.
To follow along with the instructions in this chapter, please download the GitHub repository, along with the examples available on the GitHub repository, at https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter05.
You will also need a GitHub account, as well as a GitLab account in the cloud or on premise.
In regards to user matching, you want to make sure that the GitHub users associated with the repository have the following:
- A GitLab account that uses OAuth-based login using the GitHub icon
- A GitLab account with an email address that is identical to their public email address in GitHub
To export things from GitHub, the proper authorizations must be set. This can be done using GitHub integration with OAuth registration, where GitLab authenticates with GitHub to access a list of projects for the user.
The best result of mapping authors and assignees from GitHub to GitLab is achieved when using the GitHub integration features that are available in GitLab instead of using a single personal access token from GitHub.
The integration covers all the import items we just mentioned and tries to keep all the references intact. For instance, the importer will try to find the GitHub authors and assignees of issues and pull requests. This means that GitLab integration has to be enabled in your GitLab instance.
To register GitLab as an application that can connect to GitHub, you have to log in to your GitHub account. To do this, go to https://github.com/settings/developers and register GitLab as an OAuth application:

Fill in the form using the following data:
- Application name (the name of your application)
- Homepage URL (the full URL to your application)
- Application description (general description)
- Authorization callback URL (the most important setting, the URL to send to after authentication):

After the registration has succeeded, an overview is presented with two very important pieces of information: the client ID and client secret are shown, and also how many users have used the OAuth link to connect GitHub repositories. The client ID and client secret are needed later to configure GitLab for integration.
The following screen appears after a successful registration:

You now have GitHub configured and can move on to the Preparing GitLab for import section.
To finish the setup for the GitHub integration, we have to set up our GitLab instance so that we can connect as a registered OAuth application to GitHub. We do this by adding an omniauth_provider to the GitLab configuration. Remember the client ID and the client secret? We will need these here.
For GitLab omnibus installations, we need to create a section in /etc/gitlab/gitlab.rb like the following one. app_id is the client ID, while app_secret is the client secret. The name that is provided isn’t important. After the file has been saved, you have to use gitlab-ctl reconfigure:
sidekiq[‘concurrency’] = 25
Section titled “sidekiq[‘concurrency’] = 25”gitlab_rails['omniauth_providers'] = [ { "name" => "github", "app_id" => "dd1c6d6aed110b2cce8e", "app_secret" => "f6ddd6059c694ecfc1a96f962fa20b6c3f7c8c4a", "args" => { "scope" => "user:email" } } ]For GitHub Enterprise, you can specify the URL of your GitHub instance as well:
"url" => "https://github.example.com/",For GitLab installations from source, we edit the config.yml file, as follows. and restart GitLab after saving it:
- { name: 'github', app_id: 'dd1c6d6aed110b2cce8e', app_secret: 'f6ddd6059c694ecfc1a96f962fa20b6c3f7c8c4a', url: "https://github.com/", verify_ssl: true, args: { scope: 'user:email' } }See https://github.com/gitlabhq/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template for more information on settings you can set in the gitlab.rb file. Also see Chapter 2, Installing GitLab about this subject.
Follow these steps to learn how to run the import:
- In GitLab, choose to create a new project and use the Import project tab to find the GitHub icon:

- After clicking on the GitHub icon, you will find a page where you can click List your GitHub repositories:

- When GitHub integration is enabled, the next step will be to authorize GitLab to access your projects:

- If authorization succeeds, a list of projects that can be migrated is shown. Select the projects to be migrated, and click Import:

Make sure that you don’t have projects with the same name already in GitLab in your namespace, otherwise you will receive a 422 error, as shown in the following screenshot:

When the import is finished, you will receive a message:

The project has been successfully imported!
When using a GitHub token to import, there’s nothing special to configure in GitLab – you can enter it immediately, as you will see later.
A personal token can be used as delegated authorization from your GitHub account. Let’s get started:
- You create one via https://github.com/settings/tokens:

- Make sure you set a meaningful token description and choose the repo scope only. We want the GitLab importer to access these objects:

- Once the token has been created, make sure to record the token somewhere because it won’t be shown again, and losing it means that you have to recreate it:

You now have a GitHub token and can choose to use it directly in the GitLab web interface.
Let’s get started and learn how to prepare GitLab for import:
- In GitLab, create a new project and use the import tab to find the GitHub icon, as shown in the following screenshot:

- If GitHub integration is disabled, the next screen won’t have an option for you to list your GitHub repositories. To choose a project to migrate, you have to enter the personal token you created on https://github.com/:

- Again, if authorization succeeds, a list of projects that can be migrated is shown. Select the projects to be migrated and click Import:

- When the import is finished, you will receive a message:

The project has successfully been imported using the token.
In order to retrieve and import GitHub repositories, you will need a GitHub personal access token, as demonstrated earlier.
For importing projects using a rake task, there is only one option to configure in GitLab beforehand: the number of Sidekiq resources. You will notice that for big projects, it can take quite some time to import all the data. To speed up this process, assign more Sidekiq workers to the following queues:
- github_importer
- github_importer_advance_stage
For GitLab omnibus installations, these queues are part of the default Sidekiq process. Giving more threads to Sidekiq will mean that there will be more workers:
See https://github.com/gitlabhq/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template for more information on settings you can set in the gitlab.rb file. Also, see Chapter 2, Installing GitLab about this subject.
The GitLab rake task can import a single project or multiple projects at once. It is invoked from the command line, so you need administrator access to the machine where the GitLab application is running.
When specifying a GitHub repository as the fourth argument to the rake task, you can directly import it.
To import a specific GitHub project (named joustie/github_repo here), do the following:
- Omnibus installations:
sudo gitlab-rake "import:github[<personal_access_token>,<gitlab user>,<namespace/project>,<source_namespace/github_repo>]"- Installations from source:
bundle exec rake "import:github[<personal_access_token>,<gitlab user>,<namespace/project>,<source_namespace/github_repo>]" RAILS_ENV=productionTo import a project from the list of your available GitHub projects, do the following:
- Omnibus installations:
sudo gitlab-rake "import:github[<personal_access_token>,<gitlab user>,<namespace/project>]"- Installations from source:
bundle exec rake "import:github[<personal_access_token>,<gitlab user>,<namespace/project>]" RAILS_ENV=productionIt is also possible to specify subgroups such as <groupname/groupname/project>.
For my example project, you will see the following results:

The GitHub importer obtained a list of projects with your GitHub personal access token. When you want to specify one particular project, use the ID:

The importer will ask for confirmation after that:

The rake tasks will run synchronously and perform the import. After the import operation, a summary of the timings is given:

When you log in to your GitLab instance, you will find the imported project.
We started this chapter by explaining what items can be migrated from GitHub to GitLab, and what the possibilities are. There are three options regarding migrating your data, and which ones you can use depend on how your GitLab instance is configured. The recommended way is to use the GitHub importer. This mechanism presents the most user-friendly and complete migration path.
GitHub and GitLab are very comparable, but in the next chapter, we will take a look at CVS and how it compares to Git as a versioning tool, and how to migrate it.
- What is the equivalent of a pull request in GitLab?
- Which ways exist to import GitHub projects into GitLab?
- What is needed to display the authors correctly?
- Which authentication mechanism is used to connect to GitHub?
- What is the secret called that’s needed to connect to GitHub?
- What scope do you need for a token to import as much as possible from GitHub?
- What is the name of the two queues that process the import?
- Where can you find the import button for GitHub in GitLab?
- What is the name of the rake task to import from GitHub?
- Is it possible to import one particular project only?
- GitHub Essentials – Second Edition, by Achilleas Pipinellis: