otc-metadata-swiss/tools/generate_doc_gitcontrol_repos.py
Hasko, Vladimir 00fefac3dd
Some checks failed
gl/check check status: failure (00fefac3ddd0e26193ebd276aa057708a0f9e2c2)
gitcontrol fix for repositories
2023-06-08 18:22:17 +00:00

187 lines
6.0 KiB
Python

#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import copy
import logging
import yaml
import otc_metadata.services
data = otc_metadata.services.Services()
def process_services(args, services):
"""Process services"""
gitea_bp = dict(
branch_name="main",
enable_approvals_whitelist=True,
approvals_whitelist_teams=["docs-infra-team"],
block_on_rejected_reviews=True,
dismiss_stale_approvals=True,
enable_push=False,
enable_status_check=True,
status_check_contexts=["gl/check"],
enable_merge_whitelist=True,
merge_whitelist_usernames=["zuul"],
required_approvals=1,
)
gitea_repo_template = dict(
default_branch="main",
description="Open Telekom Cloud Service docs",
homepage=None,
archived=False,
has_issues=True,
has_projects=False,
has_wiki=False,
default_delete_branch_after_merge=True,
allow_merge_commit=False,
allow_squash_merge=True,
allow_rebase_merge=False,
default_merge_style="squash",
branch_protections=[],
)
github_bp = dict(
branch_name="main",
enable_approvals_whitelist=True,
approvals_whitelist_teams=["docs-infra-team"],
block_on_rejected_reviews=True,
dismiss_stale_approvals=True,
enable_push=False,
status_check_contexts=["gl/check"],
enable_merge_whitelist=True,
merge_whitelist_usernames=["zuul"],
)
github_repo_template = dict(
default_branch="main",
description="Open Telekom Cloud Service docs",
homepage=None,
archived=False,
has_issues=True,
has_projects=False,
has_wiki=False,
delete_branch_on_merge=True,
allow_merge_commit=False,
allow_squash_merge=True,
allow_rebase_merge=False,
allow_update_branch=True,
branch_protections=[],
)
for service in services:
logging.debug(f"Processing service {service}")
config = None
repo_name = None
for repo in service["repositories"]:
logging.debug(f"Processing repository {repo}")
if repo["environment"] != args.target_environment:
continue
if repo["type"] == "gitea":
teams = []
branch_protections_main = copy.deepcopy(gitea_bp)
if "teams" in repo:
teams_def = repo["teams"]
if "teams" in service:
teams_def = service["teams"]
if teams_def:
for team in teams_def:
branch_protections_main[
"approvals_whitelist_teams"
].append(team["name"])
teams.append(team["name"])
data = copy.deepcopy(gitea_repo_template)
data["description"] = (
f"Open Telekom Cloud {service['service_title']} "
f"Service docs"
)
data["branch_protections"].append(branch_protections_main)
data["teams"] = teams
repo_name = repo["repo"].split("/")[1]
config = {repo_name: data}
elif repo["type"] == "github":
teams = []
branch_protections_main = copy.deepcopy(github_bp)
if "teams" in repo:
teams_def = repo["teams"]
if "teams" in service:
teams_def = service["teams"]
if teams_def:
for team in teams_def:
teams.append(
{
"slug": team["name"],
"permission": "push"
if team["permission"] == "write"
else "pull",
}
)
data = copy.deepcopy(github_repo_template)
data["description"] = (
f"Open Telekom Cloud {service['service_title']} " f"docs"
)
data["branch_protections"].append(
{"branch": "main", "template": "zuul"}
)
data["teams"] = teams
repo_name = repo["repo"].split("/")[1]
config = {repo_name: data}
else:
logging.error(
"Repository type %s is not supported", repo["type"]
)
exit(1)
if data:
with open(
f"{args.work_dir}/{repo_name}.yml",
"w",
encoding="utf-8",
newline="",
) as out:
logging.debug(
"Generating %s config from template...",
service["service_type"],
)
yaml.dump(config, out)
def main():
parser = argparse.ArgumentParser(
description="Update conf.py file in repositories."
)
parser.add_argument(
"--target-environment",
required=True,
help="Environment to be used as a source",
)
parser.add_argument(
"--work-dir",
required=True,
help="Working directory to use for repository checkout.",
)
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
process_services(args, data.all_services)
if __name__ == "__main__":
main()