]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
ci: Introduce a util module
authorErik Skultety <eskultet@redhat.com>
Wed, 10 Feb 2021 16:42:30 +0000 (17:42 +0100)
committerErik Skultety <eskultet@redhat.com>
Fri, 19 Mar 2021 10:47:06 +0000 (11:47 +0100)
With the gradual rewrite of the Makefile to the 'helper' script will
require helper functions that would better live in a separate util
module.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
ci/util.py [new file with mode: 0644]

diff --git a/ci/util.py b/ci/util.py
new file mode 100644 (file)
index 0000000..f9f8320
--- /dev/null
@@ -0,0 +1,40 @@
+import json
+import urllib.request
+import urllib.parse
+
+from typing import Dict, List
+
+
+def get_registry_uri(namespace: str,
+                     gitlab_uri: str = "https://gitlab.com") -> str:
+    """
+    Construct a v4 API URI pointing the namespaced project's image registry.
+
+    :param namespace: GitLab project namespace, e.g. "libvirt/libvirt"
+    :param gitlab_uri: GitLab base URI, can be a private deployment
+    :param api_version: GitLab REST API version number
+    :return: URI pointing to a namespaced project's image registry
+    """
+
+    # this converts something like "libvirt/libvirt" to "libvirt%2Flibvirt"
+    namespace_urlenc = urllib.parse.quote_plus(namespace)
+
+    project_uri = f"{gitlab_uri}/api/v4/projects/{namespace_urlenc}"
+
+    uri = project_uri + "/registry/repositories"
+    return uri
+
+
+def get_registry_images(uri: str) -> List[Dict]:
+    """
+    List all container images that are currently available in the given GitLab
+    project.
+
+    :param uri: URI pointing to a GitLab instance's image registry
+    :return: list of container image names
+    """
+
+    r = urllib.request.urlopen(uri + "?per_page=100")
+
+    # read the HTTP response and load the JSON part of it
+    return json.loads(r.read().decode())