我对gitlab和gitlab CI非常陌生,我已经建立了一条正在成功完成的管道。我的主分支和开发分支受到保护,因此需要合并请求,以便组中的另一个开发人员可以在合并之前检查代码和注释。我想知道是否有可能在这个管道的末尾生成这个合并请求。gitlab存储库中是否有此设置,还是必须创建脚本才能实现?
旁注:
在发布这篇文章之前,我偶然发现了gitlab的这一部分文档
我正在使用gitlab-runner 11.0.0在ubuntu 18.04上
发布于 2018-06-29 15:41:51
简短的回答:肯定--任何事情都是可能的。GitLab有一个很好的API (包括创建一个MR)。但我觉得走那条路是不对的。您应该根据GitLab的设计使用它。你开始合并请求太晚了。在开始任何工作之前启动它,合并请求将在整个分支期间保持打开。
长答案:--这是理想的GitLab工作流:
从根本上讲,这与GitHub的工作方式(我来自这里)是从根本上说的,因为您不必告诉人们您在做什么。
编辑:,听起来您对利用API很感兴趣。实际上,有一个名为‘python’的python包可以很好地运行对象/mrs.html。
import gitlab
import os
origin = "https://gitlab.example.com"
# Private token is set as an env var
gl = gitlab.Gitlab(origin, private_token, api_version='4')
gl.auth()
def create_merge_request(origin, private_token):
mr = project.mergerequests.create({'source_branch': 'cool_feature',
'target_branch': 'master',
'title': 'merge cool feature',
'labels': ['label1', 'label2']})
mr.assignee_id = gl.users.get(2).id # Assign it to coworker
def lookup_last_pipeline(origin, private_token):
current_pipeline_id = os.environ['CI_PIPELINE_ID']
pipelines = gl.projects.get(os.environ['CI_PROJECT_ID']).pipelines.list()
for pipeline in pipelines:
if pipeline.status == 'success' and pipeline.id == current_pipeline_id:
create_merge_request()当然,这是一个例子,您将不得不调整它以满足您的精确需求。
发布于 2018-07-03 20:36:32
为了满足我的简单需求,我只是在管道中添加了最后一个阶段,它本质上执行了一个从这个职位改编的bash脚本。
编辑:应@Yuva的要求
# Create a pull request on pipeline success
create_merge_request:
stage: createMR
tags:
- autoMR
script:
- 'echo Merge request opened by $GITLAB_USER_NAME '
- ~/commit.sh在commit.sh中
#!/bin/bash
# This script was adapted from:
# https://about.gitlab.com/2017/09/05/how-to-automatically-create-a-new-mr-on-gitlab-with-gitlab-ci/
# TODO determine URL from git repository URL
[[ $HOST =~ ^https?://[^/]+ ]] && HOST="${BASH_REMATCH[0]}/api/v4/projects/"
# The branch which we wish to merge into
TARGET_BRANCH=develop;
# The user's token name so that we can open the merge request as the user
TOKEN_NAME=`echo ${GITLAB_USER_LOGIN}_COMMIT_TOKEN | tr "[a-z]" "[A-Z]"`
# See: http://www.tldp.org/LDP/abs/html/parameter-substitution.html search ${!varprefix*}, ${!varprefix@} section
PRIVATE_TOKEN=`echo ${!TOKEN_NAME}`
# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"project_id\": ${CI_PROJECT_ID},
\"source_branch\": \"${CI_COMMIT_REF_NAME}\",
\"target_branch\": \"${TARGET_BRANCH}\",
\"remove_source_branch\": false,
\"force_remove_source_branch\": false,
\"allow_collaboration\": true,
\"subscribed\" : true,
\"title\": \"${GITLAB_USER_NAME} merge request for: ${CI_COMMIT_REF_SLUG}\"
}";
# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=`curl --silent "${HOST}${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${PRIVATE_TOKEN}"`;
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"${CI_COMMIT_REF_NAME}\"" | wc -l`;
# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
curl -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" \
--header "PRIVATE-TOKEN:${PRIVATE_TOKEN}" \
--header "Content-Type: application/json" \
--data "${BODY}";
echo "Opened a new merge request: WIP: ${CI_COMMIT_REF_SLUG} for user ${GITLAB_USER_LOGIN}";
exit;
fi
echo "No new merge request opened"发布于 2018-11-26 17:24:03
另一种办法是:
通过电子邮件使用修补程序打开合并请求 GitLab长期以来一直支持通过电子邮件打开合并请求,但是在发送电子邮件之前,分支必须已经存在于服务器上。现在,您可以通过附加一个或多个修补程序文件(
.patch)来打开仅使用电子邮件的合并请求。 补丁文件是系统之间共享和传输更改的标准。在GitLab的未来版本中,我们将在此基础上构建分布式合并请求,这将允许GitLab实例和其他Git宿主工具之间的合并请求。
https://stackoverflow.com/questions/51104622
复制相似问题