How to create a GitLab merge request via command line

别说谁变了你拦得住时间么 提交于 2019-12-17 23:16:35

问题


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 line (or batchfile or similar, for that matter). We would like to integrate this in our tooling. Searching here and on the web lead me to believe that this is not possible with native GitLab, but that we need additional tooling for that.

Am I correct? And what kind of tooling would I want to use for this?


回答1:


It's not natively supported, but it's not hard to throw together. The gitlab API has support for opening MR: https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/merge_requests.md#create-mr




回答2:


As of GitLab 11.10, if you're using git 2.10 or newer, you can automatically create a merge request from the command line like this:

git push -o merge_request.create

More information can be found in the docs.




回答3:


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.



回答4:


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.




回答5:


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




回答6:


In our build script we just pop up the browser with the correct URL and let the developer write his comments in the form hit save to create the merge request. You get this url with the correct parameters by creating a merge request manually and copying the url of the form.

#!/bin/bash
set -e
set -o pipefail

BRANCH=${2}

....

git push -f origin-gitlab $BRANCH
open "https://gitlab.com/**username**/**project-name**/merge_requests/new?merge_request%5Bsource_branch%5D=$BRANCH&merge_request%5Bsource_project_id%5D=99999&merge_request%5Btarget_branch%5D=master&merge_request%5Btarget_project_id%5D=99999"



回答7:


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).



来源:https://stackoverflow.com/questions/37410262/how-to-create-a-gitlab-merge-request-via-command-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!