Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions scripts/generate_gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python

import argparse
import os
import sys

import requests
import yaml

try:
gitlab_token = os.environ["GITLAB_TOKEN"]
except KeyError:
print("Please provide the environment variable GITLAB_TOKEN")
sys.exit(1)

parser = argparse.ArgumentParser(
description="Script to generate vcsconfig for all repositories \
under the given namespace (needs Gitlab >= 10.3)"
)
parser.add_argument("gitlab_host", type=str, help="url to the gitlab instance")
parser.add_argument(
"gitlab_namespace",
type=str,
help="namespace/group in gitlab to generate vcsconfig for",
)
parser.add_argument(
"-c",
type=str,
help="path to the target config file (default: ./vcspull.yaml)",
dest="config_file_name",
required=False,
default="./vcspull.yaml",
)

args = vars(parser.parse_args())
gitlab_host = args["gitlab_host"]
gitlab_namespace = args["gitlab_namespace"]
config_filename = args["config_file_name"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might just want to create a pathlib.Path here, as it has easy object based handling (e.g. Path.is_file())

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvzrv @aschleifer would either of you like to make a follow up PR improving upon this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tony I will do a follow up PR soon to address this point.


try:
if os.path.isfile(config_filename):
result = input(
"The target config file (%s) already exists, \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this codebase is for python >= 3.7, you can just use f-strings PEP0498. E.g. f"foo {my_variable}"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tony how do you stand on the topic of injecting variables into strings? is there one formatting / style you would like to see used in the python code?

do you want to overwrite it? [y/N] "
% (config_filename)
)

if result != "y":
print(
"Aborting per user request as existing config file (%s) \
should not be overwritten!"
% (config_filename)
)
sys.exit(0)

config_file = open(config_filename, "w")
except IOError:
print("File %s not accesible" % (config_filename))
sys.exit(1)

result = requests.get(
"%s/api/v4/groups/%s/projects" % (gitlab_host, gitlab_namespace),
params={"include_subgroups": "true", "per_page": "100"},
headers={"Authorization": "Bearer %s" % (gitlab_token)},
)

if 200 != result.status_code:
print("Error: ", result)
sys.exit(1)

path_prefix = os.getcwd()
config = {}

for group in result.json():
url_to_repo = group["ssh_url_to_repo"].replace(":", "/")
namespace_path = group["namespace"]["full_path"]
reponame = group["path"]

path = "%s/%s" % (path_prefix, namespace_path)

if path not in config:
config[path] = {}

# simplified config not working - https://github.com/vcs-python/vcspull/issues/332
# config[path][reponame] = 'git+ssh://%s' % (url_to_repo)

config[path][reponame] = {
"url": "git+ssh://%s" % (url_to_repo),
"remotes": {"origin": "ssh://%s" % (url_to_repo)},
}

config_yaml = yaml.dump(config)

print(config_yaml)

config_file.write(config_yaml)
config_file.close()