From 6608efb2c44ef116266475c33b25add874a15ac6 Mon Sep 17 00:00:00 2001
From: Max Gautier <mg@max.gautier.name>
Date: Sat, 21 Dec 2024 16:43:56 +0100
Subject: [PATCH] download: compute version from Github tags for gvisor

Gvisor is the only one of our deployed components which use tags instead
of proper releases. So the tags scraping support will, for now, cater to
gvisor particularities, notably in the tag name format and the fact that
some older releases don't have the same URL scheme.
---
 scripts/download_hash.py | 37 ++++++++++++++++++++++++++++++-------
 scripts/get_node_ids.sh  |  3 +++
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/scripts/download_hash.py b/scripts/download_hash.py
index 937916097..b1f7656b1 100644
--- a/scripts/download_hash.py
+++ b/scripts/download_hash.py
@@ -7,7 +7,8 @@
 import sys
 import os
 
-from itertools import groupby
+from itertools import groupby, chain
+from more_itertools import partition
 from collections import defaultdict
 from functools import cache
 import argparse
@@ -70,6 +71,18 @@ downloads = {
         'url': "https://github.com/etcd-io/etcd/releases/download/v{version}/SHA256SUMS",
         'graphql_id': "R_kgDOAKtHtg",
         },
+    "gvisor_containerd_shim_binary": {
+        'url': "https://storage.googleapis.com/gvisor/releases/release/{version}/{alt_arch}/containerd-shim-runsc-v1.sha512",
+        'hashtype': "sha512",
+        'tags': True,
+        'graphql_id': "R_kgDOB9IlXg",
+        },
+    "gvisor_runsc_binary": {
+        'url': "https://storage.googleapis.com/gvisor/releases/release/{version}/{alt_arch}/runsc.sha512",
+        'hashtype': "sha512",
+        'tags': True,
+        'graphql_id': "R_kgDOB9IlXg",
+        },
     "kata_containers_binary": {
         'url': "https://github.com/kata-containers/kata-containers/releases/download/{version}/kata-static-{version}-{arch}.tar.xz",
         'binary': True,
@@ -179,10 +192,14 @@ def download_hash(only_downloads: [str]) -> None:
         hash_file.raise_for_status()
         return download_hash_extract[download](hash_file.content.decode())
 
-    nodes_ids = [x['graphql_id'] for x in downloads.values()]
+
+    releases, tags = map(dict,
+                         partition(lambda r: r[1].get('tags', False),
+                                    {k: downloads[k] for k in (downloads.keys() & only_downloads)}.items()
+                                   ))
     ql_params = {
-            'repoWithReleases': nodes_ids,
-            'repoWithTags': [],
+        'repoWithReleases': [r['graphql_id'] for r in releases.values()],
+        'repoWithTags': [t['graphql_id'] for t in tags.values()],
     }
     with open("list_releases.graphql") as query:
         response = s.post("https://api.github.com/graphql",
@@ -197,15 +214,21 @@ def download_hash(only_downloads: [str]) -> None:
             return Version(possible_version)
         except InvalidVersion:
             return None
-
-    github_versions = dict(zip(downloads.keys(),
+    rep = response.json()["data"]
+    github_versions = dict(zip(chain(releases.keys(), tags.keys()),
                                [
                                    {
                                        v for r in repo["releases"]["nodes"]
                                        if not r["isPrerelease"]
                                           and (v := valid_version(r["tagName"])) is not None
                                    }
-                                   for repo in response.json()["data"]["with_releases"]
+                                   for repo in rep["with_releases"]
+                               ] +
+                               [
+                                   { v for t in repo["refs"]["nodes"]
+                                    if (v := valid_version(t["name"].removeprefix('release-'))) is not None
+                                   }
+                                   for repo in rep["with_tags"]
                                ],
                                strict=True))
 
diff --git a/scripts/get_node_ids.sh b/scripts/get_node_ids.sh
index 8f2f3f9ba..304d44b8a 100755
--- a/scripts/get_node_ids.sh
+++ b/scripts/get_node_ids.sh
@@ -45,4 +45,7 @@ gh api graphql -H "X-Github-Next-Global-ID: 1" -f query='{
     crun: repository(owner: "containers", name: "crun") {
     id
     }
+    gvisor: repository(owner: "google", name: "gvisor") {
+    id
+    }
 }'
-- 
GitLab