Skip to content

Commit

Permalink
Encode chart_entry to base64 (openshift-helm-charts#297)
Browse files Browse the repository at this point in the history
This commit gets rid of the json.loads() call as the argparse type for
chart_entry in update-index. Instead we now encode the index entry to
base64 in chart-repo-manager before adding it to the GITHUB_OUTPUT and
decode it in update-index back to a dict.

This allows the use of double quotes when passing the "--chart-entry"
argument to update-index, which enables the possibility for variable
evaluation. This paves the way for using environment variables instead
of using step's output directly.

Signed-off-by: Matthias Goerens <mgoerens@redhat.com>
  • Loading branch information
mgoerens authored Nov 15, 2023
1 parent e94a6ab commit 9ab2526
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ jobs:
--index-branch=${INDEX_BRANCH} \
--index-file=${INDEX_FILE} \
--repository=${{ github.repository }} \
--chart-entry='${{ steps.prepare-chart-release.outputs.chart_entry }}' \
--chart-entry="${{ steps.prepare-chart-release.outputs.chart_entry }}" \
--chart-url="${{ steps.prepare-chart-release.outputs.chart_url }}" \
--version="${{ steps.prepare-chart-release.outputs.version }}"
Expand Down
21 changes: 20 additions & 1 deletion scripts/src/chartrepomanager/chartrepomanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"""

import argparse
import base64
import json
import shutil
import os
Expand Down Expand Up @@ -47,6 +48,24 @@
from tools import gitutils


def _encode_chart_entry(chart_entry):
"""Encode the chart_entry to base64. This is needed to pass it as an argument to
the update index step.
Args:
chart_entry (dict): the index entry for this chart to encode
Returns:
str: The encoded base64 string equivalent.
"""
chart_entry_str = json.dumps(chart_entry)
chart_entry_bytes = chart_entry_str.encode()

# Decoding to string for the GITHUB_OUTPUT
return base64.b64encode(chart_entry_bytes).decode()


def get_modified_charts(api_url):
"""Get the category, organization, chart name, and new version corresponding to
the chart being added or modified by this PR.
Expand Down Expand Up @@ -491,7 +510,7 @@ def main():
print(f"[INFO] Add key file for release : {current_dir}/{public_key_file}")
gitutils.add_output("public_key_file", f"{current_dir}/{public_key_file}")

gitutils.add_output("chart_entry", json.dumps(chart_entry))
gitutils.add_output("chart_entry", _encode_chart_entry(chart_entry))
gitutils.add_output("chart_url", chart_url)
gitutils.add_output("version", version)

Expand Down
25 changes: 22 additions & 3 deletions scripts/src/updateindex/updateindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

import argparse
import base64
import hashlib
import json
import os
Expand All @@ -18,6 +19,22 @@
from yaml import Loader, Dumper


def _decode_chart_entry(chart_entry_encoded):
"""Decode the base64 encoded index entry to add.
Args:
chart_entry_encoded (str): base64 encode index entry for this chart
Returns:
dict: Decoded index entry
"""
chart_entry_bytes = base64.b64decode(chart_entry_encoded)
chart_entry_str = chart_entry_bytes.decode()

return json.loads(chart_entry_str)


def download_index(index_file, repository, branch):
"""Download the index file to disk and retrieve its content.
Expand Down Expand Up @@ -181,8 +198,8 @@ def main():
parser.add_argument(
"-e",
"--chart-entry",
dest="chart_entry",
type=json.loads,
dest="chart_entry_encoded",
type=str,
required=True,
help="Index entry to add",
)
Expand All @@ -196,6 +213,8 @@ def main():
)
args = parser.parse_args()

chart_entry = _decode_chart_entry(args.chart_entry_encoded)

env = Env()
web_catalog_only = env.bool("WEB_CATALOG_ONLY", False)

Expand All @@ -204,7 +223,7 @@ def main():
index_data,
args.version,
args.chart_url,
args.chart_entry,
chart_entry,
web_catalog_only,
)
write_index_file(index_data, args.index_file)

0 comments on commit 9ab2526

Please sign in to comment.