From 29a1516367f1e0988a7cf83512cf4acba4568f5e Mon Sep 17 00:00:00 2001
From: Pierre Smeyers <pierre.smeyers@gmail.com>
Date: Fri, 17 May 2024 15:17:55 +0200
Subject: [PATCH] refactor: add function to get latest package version from
 github

---
 templates/gitlab-ci-python.yml | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index ffc9250..d1dd8ec 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -312,6 +312,11 @@ variables:
       echo -e "[\\e[1;91mERROR\\e[0m] $*"
   }
 
+  function fail() {
+    log_error "$*"
+    exit 1
+  }
+
   function assert_defined() {
     if [[ -z "$1" ]]
     then
@@ -836,6 +841,18 @@ variables:
     fi
   }
 
+  function github_get_latest_version() {
+    if command -v curl &> /dev/null
+    then
+      curl -sSf -I "https://github.com/$1/releases/latest" | awk -F '/' '/location:/ {print $NF}'
+    elif command -v python3 &> /dev/null
+    then
+      python3 -c "import urllib.request;url='https://github.com/$1/releases/latest';opener=urllib.request.build_opener(type('NoRedirection', (urllib.request.HTTPErrorProcessor,), {'http_response': lambda self, req, resp: resp, 'https_response': lambda self, req, resp: resp})());req=urllib.request.Request(url, method='HEAD');print(opener.open(req).headers.get('Location').split('/')[-1])"
+    else
+      fail "curl or python3 required"
+    fi
+  }
+
   unscope_variables
   eval_all_secrets
 
-- 
GitLab