-
Tim Jaacks authored
Restore the "---" separator which was removed in a previous commit. The description simply must not start with this separator. If we include it only in between the blocks, all instances are rendered as a horizontal line, which makes the MR description more readable. Include a helper function for this purpose, which extracts the message body from a commit message.
62519790
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
create_merge_request.py 2.54 KiB
#!/usr/bin/env python3
import common
import argparse
import sys
from gitlab import Gitlab, GitlabGetError
from gitlab.v4.objects import Project
from get_merge_requests import get_merge_requests
def create_merge_request(project: Project, source_branch, target_branch):
"""Create a merge request"""
# Check if merge request already exists
mrs = get_merge_requests(
project,
source_branch=source_branch,
target_branch=target_branch,
state="opened",
)
if mrs:
return mrs[0], False
# Check if branches exist
try:
sb = project.branches.get(source_branch, retry_transient_errors=True)
except GitlabGetError:
sys.exit("ERROR: source branch '%s' does not exist." % source_branch)
try:
project.branches.get(target_branch, retry_transient_errors=True)
except GitlabGetError:
sys.exit("ERROR: target branch '%s' does not exist." % target_branch)
# Get commit of source branch
commit = project.commits.get(sb.attributes["commit"]["id"])
# Create new merge request
mr = project.mergerequests.create(
{
"source_branch": source_branch,
"target_branch": target_branch,
"remove_source_branch": True,
"title": commit.attributes["title"],
"description": common.extract_message_body(commit.attributes["message"]),
}
)
return mr, True
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--gitlab-url",
help="""URL to the GitLab instance""",
dest="gitlab_url",
required=True,
)
parser.add_argument(
"--token",
help="""GitLab REST API private access token""",
dest="token",
required=True,
)
parser.add_argument(
"--project",
help="""name of the GitLab project""",
dest="project",
required=True,
)
parser.add_argument(
"--source-branch",
help="""source branch of the merge request""",
dest="source_branch",
required=True,
)
parser.add_argument(
"--target-branch",
help="""target branch of the merge request""",
dest="target_branch",
required=True,
)
args, _ = parser.parse_known_args()
gitlab = Gitlab(args.gitlab_url, private_token=args.token)
project = common.get_project(gitlab, args.project)
create_merge_request(
project,
source_branch=args.source_branch,
target_branch=args.target_branch,
)
if __name__ == "__main__":
main()