From 26979fa23f9c7233fa541fae9c13849ff7799327 Mon Sep 17 00:00:00 2001 From: Artem Rys Date: Fri, 19 Jul 2024 16:43:39 +0200 Subject: [PATCH 01/18] chore: raise for status after requests.get --- splunk_matrix_update.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index e198c00..9606597 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -10,6 +10,7 @@ def get_token(): splunk_token_url = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:splunk/splunk:pull" response = requests.get(splunk_token_url) + response.raise_for_status() response_json = json.loads(response.text) token = response_json["token"] return token @@ -19,6 +20,7 @@ def get_images_list(token): headers = {"Authorization": f"Bearer {token}"} splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" response = requests.get(splunk_image_list_url, headers=headers) + response.raise_for_status() response_json = json.loads(response.text) return response_json["tags"] @@ -57,6 +59,7 @@ def get_image_digest(token, image): f"https://registry.hub.docker.com/v2/splunk/splunk/manifests/{image}" ) response = requests.get(image_digest_url, headers=headers) + response.raise_for_status() if response.headers["Docker-Content-Digest"]: return response.headers["Docker-Content-Digest"] else: @@ -71,6 +74,7 @@ def get_image_digest(token, image): ) ) response = requests.get(image_digest_url, headers=headers) + response.raise_for_status() return response.headers["Docker-Content-Digest"] From b07fa2b6f7d23ee1dadc387a4f6a97104b43a2cc Mon Sep 17 00:00:00 2001 From: Artem Rys Date: Fri, 19 Jul 2024 16:50:14 +0200 Subject: [PATCH 02/18] refactor: get rid of intermidiate bash script --- .github/workflows/update_deps.yml | 9 ++++++++- splunk_sync.sh | 7 ------- 2 files changed, 8 insertions(+), 8 deletions(-) delete mode 100755 splunk_sync.sh diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index a098d3a..1a4cbad 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -13,6 +13,8 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 - run: ./sc4s_sync.sh + with: + python-version: 3.12 - uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.SA_GPG_PRIVATE_KEY }} @@ -45,7 +47,12 @@ jobs: steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 - - run: ./splunk_sync.sh + with: + python-version: 3.12 + - run: | + pip install -r requirements.txt + splunk_version=$(python splunk_matrix_update.py) + echo $splunk_version - uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.SA_GPG_PRIVATE_KEY }} diff --git a/splunk_sync.sh b/splunk_sync.sh deleted file mode 100755 index 75934e3..0000000 --- a/splunk_sync.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -# echo all the commands -set -x - -pip install -r requirements.txt -splunk_version=$(python splunk_matrix_update.py) -echo $splunk_version From 9c5af744f751381d0b9b800411ae114ea91d98a9 Mon Sep 17 00:00:00 2001 From: Artem Rys Date: Fri, 19 Jul 2024 16:52:39 +0200 Subject: [PATCH 03/18] typo --- .github/workflows/update_deps.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index 1a4cbad..f923a89 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -12,9 +12,9 @@ jobs: steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 - - run: ./sc4s_sync.sh with: python-version: 3.12 + - run: ./sc4s_sync.sh - uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.SA_GPG_PRIVATE_KEY }} From a7481d08bd919949c8fb6e1fff2b872a2d7c7755 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 14:20:49 +0530 Subject: [PATCH 04/18] ci: enhancing script --- splunk_matrix_update.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 9606597..b36fccd 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -17,12 +17,15 @@ def get_token(): def get_images_list(token): - headers = {"Authorization": f"Bearer {token}"} - splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" - response = requests.get(splunk_image_list_url, headers=headers) - response.raise_for_status() - response_json = json.loads(response.text) - return response_json["tags"] + # headers = {"Authorization": f"Bearer {token}"} + # splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" + # response = requests.get(splunk_image_list_url, headers=headers) + # response.raise_for_status() + # response_json = json.loads(response.text) + all_details = "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=100" + response = requests.get(all_details) + all_details = json.loads(response.content) + return all_details["results"] def get_latest_image(stanza, images): @@ -49,6 +52,10 @@ def filter_image_list(images_list): filter_images[i] = filter_images[i].replace("'", "") return filter_images +def get_build_number_1(token, latest_image_digest): + image_lists = get_images_list(token) + match_and_return_name = next((d['name'] for d in image_lists for image in d.get('images', []) if image['digest'] == latest_image_digest), None) + return match_and_return_name def get_image_digest(token, image): headers = { @@ -90,8 +97,9 @@ def update_splunk_version(token): config.optionxform = str config.read("config/splunk_matrix.conf") update_file = False - images_list = get_images_list(token) - filter_images = filter_image_list(images_list) + all_images = get_images_list(token) + # filter_images = filter_image_list(images_list) + images_list = [d['name'] for d in all_images] for stanza in config.sections(): if stanza != "GENERAL": latest_image_version = get_latest_image(stanza, images_list) @@ -100,9 +108,10 @@ def update_splunk_version(token): if check_image_version(latest_image_version, stanza_image_version): config.set(stanza, "VERSION", latest_image_version) latest_image_digest = get_image_digest(token, latest_image_version) - build_number = get_build_number( - token, filter_images, latest_image_digest - ) + # build_number = get_build_number( + # token, filter_images, latest_image_digest + # ) + build_number = get_build_number_1(token,latest_image_digest) config.set(stanza, "BUILD", build_number) update_file = True From 9180fbe629c3c682d5d2c6242637c59a576e7b4a Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 14:25:29 +0530 Subject: [PATCH 05/18] ci: debugging --- splunk_matrix_update.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index b36fccd..730e18e 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -22,7 +22,9 @@ def get_images_list(token): # response = requests.get(splunk_image_list_url, headers=headers) # response.raise_for_status() # response_json = json.loads(response.text) - all_details = "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=100" + all_details = ( + "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=100" + ) response = requests.get(all_details) all_details = json.loads(response.content) return all_details["results"] @@ -52,11 +54,21 @@ def filter_image_list(images_list): filter_images[i] = filter_images[i].replace("'", "") return filter_images + def get_build_number_1(token, latest_image_digest): image_lists = get_images_list(token) - match_and_return_name = next((d['name'] for d in image_lists for image in d.get('images', []) if image['digest'] == latest_image_digest), None) + match_and_return_name = next( + ( + d["name"] + for d in image_lists + for image in d.get("images", []) + if image["digest"] == latest_image_digest + ), + None, + ) return match_and_return_name + def get_image_digest(token, image): headers = { "Authorization": f"Bearer {token}", @@ -99,19 +111,20 @@ def update_splunk_version(token): update_file = False all_images = get_images_list(token) # filter_images = filter_image_list(images_list) - images_list = [d['name'] for d in all_images] + images_list = [d["name"] for d in all_images] for stanza in config.sections(): if stanza != "GENERAL": + print(images_list) latest_image_version = get_latest_image(stanza, images_list) stanza_image_version = config.get(stanza, "VERSION") - + print(latest_image_version,stanza_image_version) if check_image_version(latest_image_version, stanza_image_version): config.set(stanza, "VERSION", latest_image_version) latest_image_digest = get_image_digest(token, latest_image_version) # build_number = get_build_number( # token, filter_images, latest_image_digest # ) - build_number = get_build_number_1(token,latest_image_digest) + build_number = get_build_number_1(token, latest_image_digest) config.set(stanza, "BUILD", build_number) update_file = True From a8a1a68dbaeb6502474bc3f530a158872b02ed49 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 14:28:34 +0530 Subject: [PATCH 06/18] ci: debugging --- .github/workflows/update_deps.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index f923a89..eb813be 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -51,7 +51,7 @@ jobs: python-version: 3.12 - run: | pip install -r requirements.txt - splunk_version=$(python splunk_matrix_update.py) + python splunk_matrix_update.py echo $splunk_version - uses: crazy-max/ghaction-import-gpg@v6 with: From e4e5c40c71a67ff04a4827f2319f117512d10e1a Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 14:33:01 +0530 Subject: [PATCH 07/18] ci: debugging --- splunk_matrix_update.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 730e18e..67f57ce 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -17,17 +17,17 @@ def get_token(): def get_images_list(token): - # headers = {"Authorization": f"Bearer {token}"} - # splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" - # response = requests.get(splunk_image_list_url, headers=headers) - # response.raise_for_status() - # response_json = json.loads(response.text) + headers = {"Authorization": f"Bearer {token}"} + splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" + response = requests.get(splunk_image_list_url, headers=headers) + response.raise_for_status() + response_json = json.loads(response.text) all_details = ( "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=100" ) response = requests.get(all_details) all_details = json.loads(response.content) - return all_details["results"] + return response_json["tags"], all_details["results"] def get_latest_image(stanza, images): @@ -56,7 +56,7 @@ def filter_image_list(images_list): def get_build_number_1(token, latest_image_digest): - image_lists = get_images_list(token) + _, image_lists = get_images_list(token) match_and_return_name = next( ( d["name"] @@ -109,9 +109,9 @@ def update_splunk_version(token): config.optionxform = str config.read("config/splunk_matrix.conf") update_file = False - all_images = get_images_list(token) + images_list, all_images = get_images_list(token) # filter_images = filter_image_list(images_list) - images_list = [d["name"] for d in all_images] + # images_list = [d["name"] for d in all_images] for stanza in config.sections(): if stanza != "GENERAL": print(images_list) From 28e9f5ce69dc902f202906a6348b701b7db2050d Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 14:57:47 +0530 Subject: [PATCH 08/18] ci: debugging --- splunk_matrix_update.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 67f57ce..0028fc2 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -57,12 +57,14 @@ def filter_image_list(images_list): def get_build_number_1(token, latest_image_digest): _, image_lists = get_images_list(token) + # images_list = [d["name"] for d in image_lists] + # images_list = filter_image_list(images_list) match_and_return_name = next( ( d["name"] for d in image_lists for image in d.get("images", []) - if image["digest"] == latest_image_digest + if image["digest"] == latest_image_digest and re.match(r"'[0-9a-z]{12}'", d["name"]) ), None, ) From 30cf5504e9915e05d991937fcb2edd4f4c3192ba Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 15:03:33 +0530 Subject: [PATCH 09/18] ci: debugging --- splunk_matrix_update.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 0028fc2..b28dcc7 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -126,7 +126,9 @@ def update_splunk_version(token): # build_number = get_build_number( # token, filter_images, latest_image_digest # ) + print(latest_image_digest) build_number = get_build_number_1(token, latest_image_digest) + print(build_number) config.set(stanza, "BUILD", build_number) update_file = True From b46aef575358a217ffb6254c3bfc04c3694eda7a Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 15:17:22 +0530 Subject: [PATCH 10/18] ci: debugging --- splunk_matrix_update.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index b28dcc7..1140a7c 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -64,7 +64,7 @@ def get_build_number_1(token, latest_image_digest): d["name"] for d in image_lists for image in d.get("images", []) - if image["digest"] == latest_image_digest and re.match(r"'[0-9a-z]{12}'", d["name"]) + if image["digest"] == latest_image_digest and re.match(r"[0-9a-z]{12}", d["name"]) ), None, ) From d7ef48c7da07654702e92faf501d8251c83a6257 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 9 Aug 2024 18:20:43 +0530 Subject: [PATCH 11/18] ci: debugging --- splunk_matrix_update.py | 56 +++++++++-------------------------------- 1 file changed, 12 insertions(+), 44 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 1140a7c..4c8672b 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -55,10 +55,8 @@ def filter_image_list(images_list): return filter_images -def get_build_number_1(token, latest_image_digest): +def get_build_number(token, latest_image_digest): _, image_lists = get_images_list(token) - # images_list = [d["name"] for d in image_lists] - # images_list = filter_image_list(images_list) match_and_return_name = next( ( d["name"] @@ -71,39 +69,16 @@ def get_build_number_1(token, latest_image_digest): return match_and_return_name -def get_image_digest(token, image): - headers = { - "Authorization": f"Bearer {token}", - "Accept": "application/vnd.docker.distribution.manifest.v2+json", - } - image_digest_url = ( - f"https://registry.hub.docker.com/v2/splunk/splunk/manifests/{image}" +def get_image_digest(token, image, image_list): + return next( + ( + image_data['digest'] + for d in image_list + if d['name'] == image + for image_data in d.get('images', []) + ), + None ) - response = requests.get(image_digest_url, headers=headers) - response.raise_for_status() - if response.headers["Docker-Content-Digest"]: - return response.headers["Docker-Content-Digest"] - else: - token = get_token() - headers = { - "Authorization": f"Bearer {token}", - "Accept": "application/vnd.docker.distribution.manifest.v2+json", - } - image_digest_url = ( - "https://registry.hub.docker.com/v2/splunk/splunk/manifests/{}".format( - image - ) - ) - response = requests.get(image_digest_url, headers=headers) - response.raise_for_status() - return response.headers["Docker-Content-Digest"] - - -def get_build_number(token, filter_images, latest_image_digest): - for image in filter_images: - if get_image_digest(token, image) == latest_image_digest: - return image - def update_splunk_version(token): if os.path.isfile("config/splunk_matrix.conf"): @@ -116,19 +91,12 @@ def update_splunk_version(token): # images_list = [d["name"] for d in all_images] for stanza in config.sections(): if stanza != "GENERAL": - print(images_list) latest_image_version = get_latest_image(stanza, images_list) stanza_image_version = config.get(stanza, "VERSION") - print(latest_image_version,stanza_image_version) if check_image_version(latest_image_version, stanza_image_version): config.set(stanza, "VERSION", latest_image_version) - latest_image_digest = get_image_digest(token, latest_image_version) - # build_number = get_build_number( - # token, filter_images, latest_image_digest - # ) - print(latest_image_digest) - build_number = get_build_number_1(token, latest_image_digest) - print(build_number) + latest_image_digest = get_image_digest(token, latest_image_version, all_images) + build_number = get_build_number(token, latest_image_digest) config.set(stanza, "BUILD", build_number) update_file = True From 21144eec0db1b7faa793971257855985c73ef068 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Mon, 12 Aug 2024 11:48:15 +0530 Subject: [PATCH 12/18] ci: debugging --- .github/workflows/update_deps.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index eb813be..0677a3e 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -49,10 +49,13 @@ jobs: - uses: actions/setup-python@v5 with: python-version: 3.12 + - name: check for latest version + - id: check-latest-splunk - run: | pip install -r requirements.txt - python splunk_matrix_update.py - echo $splunk_version + splunk_version=$(python splunk_matrix_update.py) + echo "splunk_version=splunk_version" >> "$GITHUB_OUTPUT" +# echo $splunk_version - uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.SA_GPG_PRIVATE_KEY }} @@ -60,6 +63,7 @@ jobs: git_user_signingkey: true git_commit_gpgsign: true - name: Create PR with new version + if: if [[ "${{ steps.check-latest-splunk.outputs.splunk_version }}" == "true" ]] run: | git config --global user.email ${{ secrets.GH_USER_EMAIL }} git config --global user.name ${{ secrets.GH_USER_ADMIN }} From 7fd2cdd39c25268b60359ee523a0d6bb2a5b00f2 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Mon, 12 Aug 2024 11:51:44 +0530 Subject: [PATCH 13/18] ci: debugging --- .github/workflows/update_deps.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index 0677a3e..a682d38 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -49,7 +49,6 @@ jobs: - uses: actions/setup-python@v5 with: python-version: 3.12 - - name: check for latest version - id: check-latest-splunk - run: | pip install -r requirements.txt From dab1bbb489a25bc60ca638a1f19cadd99f31ad41 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Mon, 12 Aug 2024 11:54:11 +0530 Subject: [PATCH 14/18] ci: debugging --- .github/workflows/update_deps.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index a682d38..aa8affc 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -49,8 +49,9 @@ jobs: - uses: actions/setup-python@v5 with: python-version: 3.12 - - id: check-latest-splunk - - run: | + - name: check for latest version + id: check-latest-splunk + run: | pip install -r requirements.txt splunk_version=$(python splunk_matrix_update.py) echo "splunk_version=splunk_version" >> "$GITHUB_OUTPUT" From 3a9dc000782aa7882826d0d442227450ccf32ccd Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Wed, 14 Aug 2024 17:56:30 +0530 Subject: [PATCH 15/18] ci: adding doc strings --- splunk_matrix_update.py | 152 ++++++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 59 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index 4c8672b..ddce27a 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -2,74 +2,95 @@ import json import os import re - -import requests from packaging import version +import requests +from typing import List, Dict, Optional -def get_token(): - splunk_token_url = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:splunk/splunk:pull" - response = requests.get(splunk_token_url) - response.raise_for_status() - response_json = json.loads(response.text) - token = response_json["token"] - return token +def get_images_details() -> List[Dict]: + """ + Fetches the details of images from the Docker Hub Splunk repository. + Returns: + List[Dict]: A list of dictionaries containing details about each image tag. + """ + get_images_details_endpoint = "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=1000&ordering=last_updated" + image_details_response = requests.get(get_images_details_endpoint) + image_details_response.raise_for_status() + image_details = image_details_response.json()["results"] + return image_details -def get_images_list(token): - headers = {"Authorization": f"Bearer {token}"} - splunk_image_list_url = "https://registry.hub.docker.com/v2/splunk/splunk/tags/list" - response = requests.get(splunk_image_list_url, headers=headers) - response.raise_for_status() - response_json = json.loads(response.text) - all_details = ( - "https://hub.docker.com/v2/repositories/splunk/splunk/tags?page_size=100" - ) - response = requests.get(all_details) - all_details = json.loads(response.content) - return response_json["tags"], all_details["results"] +def get_latest_image(stanza: str, images: List[Dict]) -> Optional[str]: + """ + Finds the latest image version that matches the given stanza version. + + Args: + stanza (str): The version string from the config file. + images (List[Dict]): A list of dictionaries containing image details. + + Returns: + Optional[str]: The latest image version that matches the stanza pattern, or None if no match is found. + """ + stanza_regex = r"\.".join(re.escape(part) for part in stanza.split(".")) + regex_image = rf"{stanza_regex}\.\d+|{stanza_regex}\.\d+\.\d+" + versions = [image['name'] for image in images] + filtered_images = re.findall(regex_image, str(versions)) -def get_latest_image(stanza, images): - stanza = stanza.split(".") - stanza = r"\.".join(stanza) - regex_image = rf"'{stanza}\.\d+'|'{stanza}\.\d+\.\d+'" - filtered_images = re.findall(regex_image, str(images)) if filtered_images: - for i in range(len(filtered_images)): - filtered_images[i] = filtered_images[i].replace("'", "") + filtered_images = [image.replace("'", "") for image in filtered_images] filtered_images.sort(key=lambda s: list(map(int, s.split(".")))) return filtered_images[-1] + return None + + +def check_image_version(latest_image: str, stanza_image: str) -> bool: + """ + Compares two version strings to determine if the latest image version is newer. + Args: + latest_image (str): The latest image version. + stanza_image (str): The current image version from the config file. -def check_image_version(latest_image, stanza_image): + Returns: + bool: True if the latest image version is newer, False otherwise. + """ return version.parse(latest_image) > version.parse(stanza_image) -def filter_image_list(images_list): - regex_filter_images = r"\'[0-9a-z]{12}\'" - filter_images = re.findall(regex_filter_images, str(images_list)) - if filter_images: - for i in range(len(filter_images)): - filter_images[i] = filter_images[i].replace("'", "") - return filter_images +def get_build_number(latest_image_digest: str, image_list: List[Dict]) -> Optional[str]: + """ + Retrieves the build number which is SHA corresponding to the latest image digest. + Args: + latest_image_digest (str): The digest of the latest image. + image_list (List[Dict]): A list of dictionaries containing image details. -def get_build_number(token, latest_image_digest): - _, image_lists = get_images_list(token) - match_and_return_name = next( + Returns: + Optional[str]: The build number if found, None otherwise. + """ + return next( ( d["name"] - for d in image_lists + for d in image_list for image in d.get("images", []) if image["digest"] == latest_image_digest and re.match(r"[0-9a-z]{12}", d["name"]) ), None, ) - return match_and_return_name -def get_image_digest(token, image, image_list): +def get_image_digest(image: str, image_list: List[Dict]) -> Optional[str]: + """ + Retrieves the digest for a specific image version. + + Args: + image (str): The name of the image version. + image_list (List[Dict]): A list of dictionaries containing image details. + + Returns: + Optional[str]: The digest of the image if found, None otherwise. + """ return next( ( image_data['digest'] @@ -80,34 +101,47 @@ def get_image_digest(token, image, image_list): None ) -def update_splunk_version(token): - if os.path.isfile("config/splunk_matrix.conf"): + +def update_splunk_version() -> str: + """ + Updates the Splunk version in the config file if a newer version is available. + + Returns: + str: "True" if the config file was updated, "False" otherwise. + """ + config_path = "config/splunk_matrix.conf" + + if os.path.isfile(config_path): config = configparser.ConfigParser() config.optionxform = str - config.read("config/splunk_matrix.conf") + config.read(config_path) update_file = False - images_list, all_images = get_images_list(token) - # filter_images = filter_image_list(images_list) - # images_list = [d["name"] for d in all_images] + all_images_list = get_images_details() + for stanza in config.sections(): if stanza != "GENERAL": - latest_image_version = get_latest_image(stanza, images_list) - stanza_image_version = config.get(stanza, "VERSION") - if check_image_version(latest_image_version, stanza_image_version): - config.set(stanza, "VERSION", latest_image_version) - latest_image_digest = get_image_digest(token, latest_image_version, all_images) - build_number = get_build_number(token, latest_image_digest) - config.set(stanza, "BUILD", build_number) - update_file = True + latest_image_version = get_latest_image(stanza, all_images_list) + + if latest_image_version: + stanza_image_version = config.get(stanza, "VERSION") + + if check_image_version(latest_image_version, stanza_image_version): + latest_image_digest = get_image_digest(latest_image_version, all_images_list) + build_number = get_build_number(latest_image_digest, all_images_list) + + config.set(stanza, "VERSION", latest_image_version) + if build_number: + config.set(stanza, "BUILD", build_number) + update_file = True if update_file: - with open("config/splunk_matrix.conf", "w") as configfile: + with open(config_path, "w") as configfile: config.write(configfile) return "True" + return "False" if __name__ == "__main__": - token = get_token() - update_file = update_splunk_version(token) + update_file = update_splunk_version() print(update_file) From 454fd42ae70517fa083ca2ae73af1d80e4b8e4ae Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Wed, 14 Aug 2024 17:59:41 +0530 Subject: [PATCH 16/18] ci: updated variable name --- .github/workflows/update_deps.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/update_deps.yml b/.github/workflows/update_deps.yml index aa8affc..7f9491c 100644 --- a/.github/workflows/update_deps.yml +++ b/.github/workflows/update_deps.yml @@ -53,9 +53,8 @@ jobs: id: check-latest-splunk run: | pip install -r requirements.txt - splunk_version=$(python splunk_matrix_update.py) - echo "splunk_version=splunk_version" >> "$GITHUB_OUTPUT" -# echo $splunk_version + is_splunk_config_file_updated=$(python splunk_matrix_update.py) + echo $is_splunk_config_file_updated - uses: crazy-max/ghaction-import-gpg@v6 with: gpg_private_key: ${{ secrets.SA_GPG_PRIVATE_KEY }} @@ -63,7 +62,6 @@ jobs: git_user_signingkey: true git_commit_gpgsign: true - name: Create PR with new version - if: if [[ "${{ steps.check-latest-splunk.outputs.splunk_version }}" == "true" ]] run: | git config --global user.email ${{ secrets.GH_USER_EMAIL }} git config --global user.name ${{ secrets.GH_USER_ADMIN }} From 8b6b0b0a473f0479125d57b44cd2612af7d98f74 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Wed, 14 Aug 2024 18:11:34 +0530 Subject: [PATCH 17/18] ci: lint changes --- splunk_matrix_update.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index ddce27a..c684491 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -34,7 +34,7 @@ def get_latest_image(stanza: str, images: List[Dict]) -> Optional[str]: """ stanza_regex = r"\.".join(re.escape(part) for part in stanza.split(".")) regex_image = rf"{stanza_regex}\.\d+|{stanza_regex}\.\d+\.\d+" - versions = [image['name'] for image in images] + versions = [image["name"] for image in images] filtered_images = re.findall(regex_image, str(versions)) if filtered_images: @@ -74,7 +74,8 @@ def get_build_number(latest_image_digest: str, image_list: List[Dict]) -> Option d["name"] for d in image_list for image in d.get("images", []) - if image["digest"] == latest_image_digest and re.match(r"[0-9a-z]{12}", d["name"]) + if image["digest"] == latest_image_digest + and re.match(r"[0-9a-z]{12}", d["name"]) ), None, ) @@ -93,12 +94,12 @@ def get_image_digest(image: str, image_list: List[Dict]) -> Optional[str]: """ return next( ( - image_data['digest'] + image_data["digest"] for d in image_list - if d['name'] == image - for image_data in d.get('images', []) + if d["name"] == image + for image_data in d.get("images", []) ), - None + None, ) @@ -126,8 +127,12 @@ def update_splunk_version() -> str: stanza_image_version = config.get(stanza, "VERSION") if check_image_version(latest_image_version, stanza_image_version): - latest_image_digest = get_image_digest(latest_image_version, all_images_list) - build_number = get_build_number(latest_image_digest, all_images_list) + latest_image_digest = get_image_digest( + latest_image_version, all_images_list + ) + build_number = get_build_number( + latest_image_digest, all_images_list + ) config.set(stanza, "VERSION", latest_image_version) if build_number: From 6e84168f86b73fa93f33ee66ef1a22c1d53ede15 Mon Sep 17 00:00:00 2001 From: Harshil Gajera Date: Fri, 16 Aug 2024 11:27:56 +0530 Subject: [PATCH 18/18] ci: renaming --- splunk_matrix_update.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/splunk_matrix_update.py b/splunk_matrix_update.py index c684491..69f19c0 100644 --- a/splunk_matrix_update.py +++ b/splunk_matrix_update.py @@ -44,7 +44,7 @@ def get_latest_image(stanza: str, images: List[Dict]) -> Optional[str]: return None -def check_image_version(latest_image: str, stanza_image: str) -> bool: +def is_latest_image(latest_image: str, stanza_image: str) -> bool: """ Compares two version strings to determine if the latest image version is newer. @@ -126,7 +126,7 @@ def update_splunk_version() -> str: if latest_image_version: stanza_image_version = config.get(stanza, "VERSION") - if check_image_version(latest_image_version, stanza_image_version): + if is_latest_image(latest_image_version, stanza_image_version): latest_image_digest = get_image_digest( latest_image_version, all_images_list )