How to create a GitLab merge request via command line

后端 未结 9 1114
清酒与你
清酒与你 2020-12-13 23:49

We are working on integrating GitLab (enterprise edition) in our tooling, but one thing that is still on our wishlist is to create a merge request in GitLab via a command li

相关标签:
9条回答
  • 2020-12-14 00:11

    I use https://github.com/mdsb100/cli-gitlab

    I am creating the MR from inside of a gitlab CI docker container based on alpine linux, so I include the install command in before-script (that could also be included in your image). All commands in the following .gitlab-ci.yml file, are also relevant for normal command line usage (as long as you have the cli-gitlab npm installed).

    variables:
        TARGET_BRANCH: 'live'
        GITLAB_URL: 'https://your.gitlab.net'
        GITLAB_TOKEN: $PRIVATE_TOKEN #created in user profile & added in project settings
    before-script:
        -apk update && apk add nodejs && npm install cli-gitlab -g
    script:
        - gitlab url $GITLAB_URL && gitlab token $GITLAB_TOKEN
        - 'echo "gitlab addMergeRequest $CI_PROJECT_ID $CI_COMMIT_REF_NAME \"$TARGET_BRANCH\" 13 `date +%Y%m%d%H%M%S`"'
        - 'gitlab addMergeRequest $CI_PROJECT_ID $CI_COMMIT_REF_NAME "$TARGET_BRANCH" 13 `date +%Y%m%d%H%M%S` 2> ./mr.json'
        - cat ./mr.json
    

    This will echo true if the merge request already exists, and echo the json result of the new MR if it succeeds to create one (also saving to a mr.json file).

    0 讨论(0)
  • 2020-12-14 00:11

    You can write a local git alias to open a Gitlab Merge Request creation page in the default browser for the currently checked-out branch.

    [alias]
        lab = "!start https://gitlab.com/path/to/repo/-/merge_requests/new?merge_request%5Bsource_branch%5D=\"$(git rev-parse --abbrev-ref HEAD)\""
    

    (this is a very simple alias for windows; I guess there are equivalent replacements for "start" on linux and fancier aliases that work with github and bitbucket too)

    As well as being able to immediately see&modify the details of the MR, the advantage of this over using the merge_request.create push option is that you don't need your local branch to be behind the remote for it to work.

    You might additionally want to store the alias in the repo itself.

    0 讨论(0)
  • 2020-12-14 00:20

    If you push your branch before this command (git push -o merge_request.create) it will not work. Git will response with Everything up-to-date and merge request will not be created (gitlab 12.3).

    When I tried to remove my branch from a server (do not remove your local branch!!!) then it worked for me in this form.

    git push --set-upstream origin your-branch-name -o merge_request.create

    0 讨论(0)
  • 2020-12-14 00:20

    As of now, GitLab sadly does not support this, however I recently saw it on their issue tracker. It appears one can expect a 'native tool' in the upcoming months.

    GitLab tweeted out about numa08/git-gitlab some time ago, so I guess this would be worth a try.

    0 讨论(0)
  • 2020-12-14 00:25

    In addition to answering of @AhmadSherif, You can use merge_request.target=<branch_name> for declaring the target branch.

    sample usage:

    git push -o merge_request.create -o merge_request.target=develop origin feature
    

    Simple This:

    According to the Gitlab documents, you can define an alias for this command to simpler usage.

    git config --global alias.mwps "push -o merge_request.create -o 
    merge_request.target=master -o merge_request.merge_when_pipeline_succeeds"
    
    0 讨论(0)
  • 2020-12-14 00:27

    You can use following utility.

    Disclosure : I developed it.

    https://github.com/vishwanatharondekar/gitlab-cli

    You can create merge request using this.

    Some of the features it has are.

    1. Base branch is optional. If base branch is not provided. Current branch is used as base branch.
    2. target branch is optional. If target branch is not provided, default branch of the repo in gitlab will be used.
    3. Created pull request page will be opened automatically after successful creation.
    4. If title is not supported with -m option value. It will be taken from in place editor opened. First line is taken as title.
    5. In the editor opened third line onwards takes as description.
    6. Comma separated list of labels can be provided with its option.
    7. Supports CI.
    8. Repository specific configs can be given.
    9. squash option is available.
    10. remove source branch option is available.
    0 讨论(0)
提交回复
热议问题