aboutsummaryrefslogtreecommitdiffstats
path: root/bootstrap/codesearch/create_config.py
blob: def4b3ecd9e02c81536e282e6d38246559bab6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""Create configuration for code search."""

import json
import urllib.request
import sys

ONAP_GERRIT = "https://gerrit.onap.org/r"
ONAP_CGIT = "https://git.onap.org"
API_PROJECTS = "/projects/"

MAGIC_PREFIX = ")]}'"


def get_projects_list():
    """Request list of all available projects from ONAP Gerrit."""
    resp = urllib.request.urlopen(ONAP_GERRIT + API_PROJECTS)
    resp_body = resp.read()

    no_magic = resp_body[len(MAGIC_PREFIX):]
    decoded = no_magic.decode("utf-8")
    projects = json.loads(decoded)

    return projects.keys()


def create_repos_list(projects):
    """Create a map of all projects to their repositories' URLs."""
    repos_list = {}
    for project in projects:
        repos_list[project] = {
            "url": "{}/{}".format(ONAP_CGIT, project),
            "url-pattern": {
                "base-url": "{url}/tree/{path}{anchor}",
                "anchor": "#n{line}"
            }
        }

    return repos_list


def main():
    """Main entry point for the script."""
    repos = create_repos_list(get_projects_list())
    config = {
        "max-concurrent-indexers": 2,
        "dbpath": "data",
        "health-check-uri": "/healthz",
        "repos": repos
    }
    print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))


if __name__ == '__main__':
    sys.exit(main())