blob: 115b4672b1b7ba52f478f20fde5ab4b5a579a9ec [file] [log] [blame]
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""Create configuration for code search."""
5
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +01006import argparse
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01007import json
8import urllib.request
9import sys
10
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010011DEFAULT_GERRIT = "gerrit.onap.org"
12API_PREFIX = "/r"
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010013API_PROJECTS = "/projects/"
14
15MAGIC_PREFIX = ")]}'"
16
Pawel Wieczorek5af76842020-01-07 15:28:53 +010017GITWEB_ANCHOR = "#l{line}"
18GIT_ANCHOR = "#n{line}"
19
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010020
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010021def get_projects_list(gerrit):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010022 """Request list of all available projects from ONAP Gerrit."""
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010023 resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS))
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010024 resp_body = resp.read()
25
26 no_magic = resp_body[len(MAGIC_PREFIX):]
27 decoded = no_magic.decode("utf-8")
28 projects = json.loads(decoded)
29
30 return projects.keys()
31
32
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010033def create_repos_list(projects, gerrit, ssh, git):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010034 """Create a map of all projects to their repositories' URLs."""
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010035 gerrit_url = "https://{}{}".format(gerrit, API_PREFIX)
36 gerrit_project_url = "{}/{{}}.git".format(gerrit_url)
37 gitweb_code_url = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f={{path}}{{anchor}}".format(gerrit_url)
Pawel Wieczorek5af76842020-01-07 15:28:53 +010038
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010039 repos_list = {}
40 for project in projects:
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010041 project_url = gerrit_project_url.format(project)
42 code_url = gitweb_code_url.format(project)
43 anchor = GITWEB_ANCHOR
44
45 if ssh and len(ssh) == 2:
46 user, port = ssh[0], ssh[1]
47 project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project)
48 if git:
49 code_url = "https://{}/{}/tree/{{path}}{{anchor}}".format(git, project)
Pawel Wieczorek5af76842020-01-07 15:28:53 +010050 anchor = GIT_ANCHOR
51
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010052 repos_list[project] = {
Pawel Wieczorek5af76842020-01-07 15:28:53 +010053 "url": project_url,
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010054 "url-pattern": {
Pawel Wieczorek5af76842020-01-07 15:28:53 +010055 "base-url": code_url,
56 "anchor": anchor
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010057 }
58 }
59
60 return repos_list
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010061
62
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010063def parse_arguments():
64 """Return parsed command-line arguments."""
65 parser = argparse.ArgumentParser(description=__doc__)
66 parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010067 parser.add_argument('--ssh', help='SSH information: user, port', nargs=2)
68 parser.add_argument('--git', help='external git address')
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010069
70 return parser.parse_args()
71
72
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010073def main():
74 """Main entry point for the script."""
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010075 arguments = parse_arguments()
76
Pawel Wieczorek5af76842020-01-07 15:28:53 +010077 projects = get_projects_list(arguments.gerrit)
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010078 repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git)
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010079 config = {
80 "max-concurrent-indexers": 2,
81 "dbpath": "data",
82 "health-check-uri": "/healthz",
83 "repos": repos
84 }
85 print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
86
87
88if __name__ == '__main__':
89 sys.exit(main())