Skip to content

Commit 8b8e8fd

Browse files
committed
[ADD] manage gitlab requests
1 parent fa5a0fb commit 8b8e8fd

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

bin/manage-gitlab-merge-reqs.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# pip install requests
2+
# pip install yaml
3+
# pip install ruamel.yaml
4+
5+
import requests
6+
import yaml
7+
from ruamel.yaml import YAML
8+
import argparse
9+
10+
11+
# How to use it:
12+
# python gitlab-script.py glpat-dummyToken789000000 https://gitlab.test.com/api/v4 155 repos.yaml OCA/test
13+
14+
15+
def fetch_and_update_yaml(GITLAB_TOKEN, GITLAB_API_ENDPOINT, PROJECT_IDS,
16+
YAML_FILE, REPO_SECTIONS):
17+
headers = {'Private-Token': GITLAB_TOKEN}
18+
s_yaml = YAML()
19+
# print(f"GITLAB TOKEN: {GITLAB_TOKEN} "
20+
# f"URL ENDPOINT: {GITLAB_API_ENDPOINT} "
21+
# f"PROJECT IDS: {PROJECT_IDS.split(',')} "
22+
# f"REPO SECTIONS: {REPO_SECTIONS.split(',')}")
23+
for PROJECT_ID in PROJECT_IDS.split(','):
24+
# 1. Fetch merged branches using GitLab API
25+
try:
26+
response = requests.get(
27+
f'{GITLAB_API_ENDPOINT}/projects/{PROJECT_ID}/merge_requests?state=merged',
28+
headers=headers)
29+
response.raise_for_status()
30+
except requests.HTTPError as err:
31+
print(f"HTTP error occurred: {err}")
32+
else:
33+
branches_data = response.json()
34+
35+
merged_branches = [b['source_branch'] for b in branches_data]
36+
merged_lst_branches = '\n'.join(merged_branches)
37+
print(f"LIST OF MERGED BRANCHES:\n============================\n"
38+
f" {merged_lst_branches}\n\n")
39+
40+
# 2. Load YAML file
41+
with open(YAML_FILE, 'r') as stream:
42+
# data = yaml.safe_load(stream)
43+
data = s_yaml.load(stream)
44+
45+
for REPO_SECTION in REPO_SECTIONS.split(','):
46+
# 3. Remove merged branches from YAML for each repo section
47+
branches_in_yaml = data[REPO_SECTION]['merges']
48+
found_mr_yaml = ''.join(branches_in_yaml)
49+
print(f'FOUND MERGE REQUESTS ON {YAML_FILE} UNDER '
50+
f'{REPO_SECTION}:'
51+
f'\n===================================================\n'
52+
f' {found_mr_yaml}\n')
53+
54+
for branch in branches_in_yaml.copy():
55+
branch_name = branch.split()[-1]
56+
if branch_name in merged_branches:
57+
print(f"Removing Branch: {branch_name} its already "
58+
f"merged")
59+
branches_in_yaml.remove(branch)
60+
61+
# 4. Save updated YAML
62+
with open(YAML_FILE, 'w') as stream:
63+
s_yaml.dump(data, stream)
64+
65+
66+
if __name__ == '__main__':
67+
parser = argparse.ArgumentParser(
68+
description='Process branches with GitLab API and update a YAML file.')
69+
parser.add_argument('GITLAB_TOKEN', type=str,
70+
help='Your GitLab private token')
71+
parser.add_argument('GITLAB_API_ENDPOINT', type=str,
72+
help='GitLab API endpoint, e.g., https://gitlab.com/api/v4')
73+
parser.add_argument('PROJECT_IDS', type=str,
74+
help='Comma-separated list of project IDs or URL-encoded namespace/project names')
75+
parser.add_argument('YAML_FILE', type=str, help='Path to your YAML file')
76+
parser.add_argument('REPO_SECTIONS', type=str,
77+
help='Comma-separated list of repo sections in the '
78+
'YAML to process, e.g., OCA/web,'
79+
'ThinkTankOdoo/main')
80+
81+
args = parser.parse_args()
82+
83+
fetch_and_update_yaml(args.GITLAB_TOKEN, args.GITLAB_API_ENDPOINT,
84+
args.PROJECT_IDS, args.YAML_FILE, args.REPO_SECTIONS)

0 commit comments

Comments
 (0)