Copy a github pull request to gitlab

A mirror of a github repository is setup and contains two remotes:

gitlab	 git@workbench.dachary.org:tests/testrepo.git (push)
origin	 https://github.com/loic-bot/testrepo (push)

The github2gitlab command of gh (run from ~gitmirrors/repositories/Tests/testrepo) creates a merge request in gitlab by copying the designated pull request from github:

$ gh gg --user loic-bot --repo testrepo --number 3

Original github pull request

Matching gitlab merge request


For each pull request github creates a refs/pull/N/head reference to the base of the pull request (i.e. the commit hash the pull request wants to merge). github2gitlab fetches all the pull requests:

$ git fetch origin +refs/pull/*:refs/pull/*
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 1), reused 4 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://github.com/loic-bot/testrepo
 * [new ref]         refs/pull/1/head -> refs/pull/1/head
 * [new ref]         refs/pull/1/merge -> refs/pull/1/merge
 * [new ref]         refs/pull/2/head -> refs/pull/2/head
 * [new ref]         refs/pull/2/merge -> refs/pull/2/merge

The github JSON description of a pull request contains the name of the branch to merge in pull.head.ref. It is used to create a new branch in gitlab with:

$ git push gitlab +refs/pull/2/head:refs/heads/dacharypr
Counting objects: 3, done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To gitlab
 * [new branch]      refs/pull/3/head -> dacharypr

The gitlab pull request is created against the head found in pull.head.ref and assuming the gitlab user/project exactly matches the github user/repo found in pull.base.repo.full_name. The pull request created in gitlab always points to a base reference within the same project as the head reference.
The creation of the merge request can be done manually using the gitlab command line utility which wraps the node-gitlab library used by github2gitlab:

$ DEBUG='*' gitlab addMergeRequest loic-bot/testrepo dacharypr master null 'dacharypr from github '
  gitlab:ApiBase handleOptions() +0ms
  gitlab:ApiBaseHTTP handleOptions() +1ms
  gitlab:ApiV3 handleOptions() +0ms
  gitlab:ApiBase init() +0ms
  slumber:Serializer constructor +0ms @default=json serializers=null
  slumber:JsonSerializer constructor +0ms
  slumber:YamlSerializer constructor +0ms
  gitlab:ApiBase constructor() +6ms
  gitlab:Models:ProjectMergeRequests Projects::addMergeRequest() +0ms
  slumber:api POST +0ms http://workbench.dachary.org/api/v3/projects/loic-bot%2Ftestrepo/merge_requests?private_token=hN4Qssygijhkxs61WL

The implementation of the gitlab API complements the documentation.