Script to Approve Multiple MRs with the Same Branch Name Across Multiple Gitlab Projects
Script to Approve Multiple MRs with the Same Branch Name Across Multiple Gitlab Projects
Evergreen
Created: 2023-12-29 Updated: 2023-12-29
python gitlab click merge-requests automation
import click import gitlab
@click.command() @click.option('--token', prompt=True, help='Your GitLab personal access token.') @click.option('--gitlab_url', default='https://gitlab.com', help='Your GitLab instance URL.') @click.option('--mr_name', prompt=True, help='The name of the merge requests to approve.') def approve_merge_requests(token, gitlab_url, mr_name):
Initialize GitLab client
gl = gitlab.Gitlab(url=gitlab_url, private_token=token)
List all projects
projects = gl.projects.list(all=True)
for project in projects:
List merge requests for each project
merge_requests = project.mergerequests.list(state='opened') click.echo(f"Merge requests for project {project.name}:") click.echo(f"--------------------------------") for mr in merge_requests: click.echo(f"MR: {mr}") click.echo(f"Title: {mr.source_branch}")
Check if the merge request title matches the specified name
if mr.source_branch == mr_name and project.name != "lrs-service-amdocs":
Approve the merge request
mr.approve() click.echo(f"Approved MR: {mr.title} in project {project.name}")
if name == 'main': approve_merge_requests()
Subscribe to my newsletter
Read articles from Joshua Oliphant directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by