Skip to content

Commit 50b6382

Browse files
author
Andreas Schleifer
authored
docs(example): add python script to generate config based on gitlab group (#337)
1 parent e0bfdbe commit 50b6382

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

scripts/generate_gitlab.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import os
5+
import sys
6+
7+
import requests
8+
import yaml
9+
10+
try:
11+
gitlab_token = os.environ["GITLAB_TOKEN"]
12+
except KeyError:
13+
print("Please provide the environment variable GITLAB_TOKEN")
14+
sys.exit(1)
15+
16+
parser = argparse.ArgumentParser(
17+
description="Script to generate vcsconfig for all repositories \
18+
under the given namespace (needs Gitlab >= 10.3)"
19+
)
20+
parser.add_argument("gitlab_host", type=str, help="url to the gitlab instance")
21+
parser.add_argument(
22+
"gitlab_namespace",
23+
type=str,
24+
help="namespace/group in gitlab to generate vcsconfig for",
25+
)
26+
parser.add_argument(
27+
"-c",
28+
type=str,
29+
help="path to the target config file (default: ./vcspull.yaml)",
30+
dest="config_file_name",
31+
required=False,
32+
default="./vcspull.yaml",
33+
)
34+
35+
args = vars(parser.parse_args())
36+
gitlab_host = args["gitlab_host"]
37+
gitlab_namespace = args["gitlab_namespace"]
38+
config_filename = args["config_file_name"]
39+
40+
try:
41+
if os.path.isfile(config_filename):
42+
result = input(
43+
"The target config file (%s) already exists, \
44+
do you want to overwrite it? [y/N] "
45+
% (config_filename)
46+
)
47+
48+
if result != "y":
49+
print(
50+
"Aborting per user request as existing config file (%s) \
51+
should not be overwritten!"
52+
% (config_filename)
53+
)
54+
sys.exit(0)
55+
56+
config_file = open(config_filename, "w")
57+
except IOError:
58+
print("File %s not accesible" % (config_filename))
59+
sys.exit(1)
60+
61+
result = requests.get(
62+
"%s/api/v4/groups/%s/projects" % (gitlab_host, gitlab_namespace),
63+
params={"include_subgroups": "true", "per_page": "100"},
64+
headers={"Authorization": "Bearer %s" % (gitlab_token)},
65+
)
66+
67+
if 200 != result.status_code:
68+
print("Error: ", result)
69+
sys.exit(1)
70+
71+
path_prefix = os.getcwd()
72+
config = {}
73+
74+
for group in result.json():
75+
url_to_repo = group["ssh_url_to_repo"].replace(":", "/")
76+
namespace_path = group["namespace"]["full_path"]
77+
reponame = group["path"]
78+
79+
path = "%s/%s" % (path_prefix, namespace_path)
80+
81+
if path not in config:
82+
config[path] = {}
83+
84+
# simplified config not working - https://github.com/vcs-python/vcspull/issues/332
85+
# config[path][reponame] = 'git+ssh://%s' % (url_to_repo)
86+
87+
config[path][reponame] = {
88+
"url": "git+ssh://%s" % (url_to_repo),
89+
"remotes": {"origin": "ssh://%s" % (url_to_repo)},
90+
}
91+
92+
config_yaml = yaml.dump(config)
93+
94+
print(config_yaml)
95+
96+
config_file.write(config_yaml)
97+
config_file.close()

0 commit comments

Comments
 (0)