Skip to content

Commit

Permalink
add formatter overrides in action
Browse files Browse the repository at this point in the history
  • Loading branch information
antheas committed Aug 4, 2024
1 parent b82625a commit b88b34c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 22 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/online_test_deck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
sudo podman pull ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }}
- name: Run Rechunker
id: rechunk
uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/[email protected].2"
uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/[email protected].4"
with:
ref: 'ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }}'
prev-ref: ghcr.io/hhd-dev/bazzite-automated-deck:stable
Expand Down Expand Up @@ -92,6 +92,14 @@ jobs:
Package Changes:
<pkgupd>
formatters: |
commits.none=No changes\n
commits.commit=- **[<short>](https://github.com/hhd-dev/rechunk/commit/<hash>)** <subject>\n
pkgupd.none=No package updates\n
pkgupd.add=- **<package>** Added at <new>\n
pkgupd.update=- **<package>** <old> → <new>\n
pkgupd.remove=- **<package>** <old> → Removed\n
- name: Upload Changelog
uses: actions/upload-artifact@v4
with:
Expand Down
21 changes: 16 additions & 5 deletions 3_chunk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,41 @@ if [ -n "$CLEAR_PLAN" ]; then
PREV_ARG+=("--clear-plan")
fi

LABEL_ARR=()
ARG_ARR=()
if [ -n "$LABELS" ]; then
IFS=$'\n'
for label in $LABELS; do
if [ -z "$label" ]; then
continue
fi
LABEL_ARR+=("--label" "$label")
ARG_ARR+=("--label" "$label")
done
unset IFS
fi
if [ -n "$FORMATTERS" ]; then
IFS=$'\n'
for form in $FORMATTERS; do
if [ -z "$form" ]; then
continue
fi
ARG_ARR+=("--formatter" "$(echo $form)")
done
unset IFS
fi

if [ -n "$DESCRIPTION" ]; then
echo "Writing description to 'org.opencontainers.image.description'"
LABEL_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION")
ARG_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION")
fi

echo Executing command:
echo $RECHUNK -r "$REPO" -b "$OUT_TAG" -c "$CONTENT_META" \
--changelog-fn "${OUT_NAME}.changelog.txt" \
"${PREV_ARG[@]}" "${LABEL_ARR[@]}"
"${PREV_ARG[@]}" "${ARG_ARR[@]}"

$RECHUNK -r "$REPO" -b "$OUT_TAG" -c "$CONTENT_META" \
--changelog-fn "${OUT_NAME}.changelog.txt" \
"${PREV_ARG[@]}" "${LABEL_ARR[@]}"
"${PREV_ARG[@]}" "${ARG_ARR[@]}"


PREV_ARG=""
Expand Down
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ inputs:
tag along with the git path.
If <commit> is not used, providing it as part of a "org.opencontainers.image.revision"
is the same.
formatters:
description: |
The formatters to use for the changelog. The format is as follows:
```
formatter=<format>
```
Formatter is a string up to the first = and defines the formatter name.
After that and until the end of the line is the format.
Use \n for newlines. Different formatters use different
substitutions (e.g., <name>), refer to the examples for specifics.
You can use the characters `<`, `>`, `=` freely. Substitution is only
performed on exact tag matches.
outputs:
ref:
Expand Down Expand Up @@ -165,6 +178,7 @@ runs:
-e PREV_REF="${{ inputs.prev-ref }}" \
-e OUT_NAME="$OUT_NAME" \
-e LABELS="${{ inputs.labels }}" \
-e FORMATTERS="${{ inputs.formatters }}" \
-e VERSION="${{ inputs.version }}" \
-e VERSION_FN="/workspace/version.txt" \
-e PRETTY="${{ inputs.pretty }}" \
Expand Down
20 changes: 19 additions & 1 deletion src/rechunk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ def argparse_func():
default=None,
)
parser.add_argument(
"-l", "--label", help="Add labels to the output image.", action="append"
"-l",
"--label",
help="Add labels to the output image (`label=var`).",
action="append",
)
parser.add_argument(
"--formatter",
help="Override string formatters for tags for i18n (`formatter=var`).",
action="append",
)
parser.add_argument("--pretty", help="Pretty version string.", default=None)
parser.add_argument(
Expand Down Expand Up @@ -126,6 +134,15 @@ def argparse_func():
)

args = parser.parse_args()

# Parse formatters
formatters = {}
for line in args.formatter:
if "=" not in line:
continue
idx = line.index("=")
formatters[line[:idx]] = line[idx + 1 :]

alg_main(
repo=args.repo,
ref=args.ref,
Expand All @@ -145,6 +162,7 @@ def argparse_func():
changelog=args.changelog,
changelog_fn=args.changelog_fn,
clear_plan=args.clear_plan,
formatters=formatters,
)


Expand Down
2 changes: 2 additions & 0 deletions src/rechunk/alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ def main(
changelog: str | None = None,
changelog_fn: str | None = None,
clear_plan: bool = False,
formatters: dict[str, str] = {},
):
if not meta_fn:
meta_fn = get_default_meta_yaml()
Expand Down Expand Up @@ -655,6 +656,7 @@ def main(
changelog_template=changelog,
changelog_fn=changelog_fn,
info=info,
formatters=formatters,
)

if contentmeta_fn:
Expand Down
81 changes: 66 additions & 15 deletions src/rechunk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
VERSION_TAG = "org.opencontainers.image.version"
REVISION_TAG = "org.opencontainers.image.revision"

DEFAULT_FORMATTERS = {
"commits.none": "-\n",
"commits.commit": "- **<short>** <subject>\n",
"pkgupd.none": "-\n",
"pkgupd.update": " - **<package>**: <old> → <new>\n",
"pkgupd.add": " - **<package>**: x → <new>\n",
"pkgupd.remove": " - **<package>**: <old> → x\n",
}


class tqdm(tqdm_orig):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -136,14 +145,19 @@ def get_update_matrix(packages: list[MetaPackage], biweekly: bool = True):
return p_upd


def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None):
def get_commits(
git_dir: str | None,
revision: str | None,
prev_rev: str | None,
formatters: dict[str, str],
):
logger.info(f"Getting commits from '{prev_rev}' to '{revision}' in '{git_dir}'")
if not git_dir or not revision or not prev_rev:
return ""

out = ""
try:
cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%s\" --no-merges {prev_rev}..{revision}"
cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%T/%s\" --no-merges {prev_rev}..{revision}"
for commit in (
subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
.stdout.decode("utf-8")
Expand All @@ -152,12 +166,27 @@ def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None)
if not "/" in commit:
continue
idx = commit.index("/")
out += f" - **{commit[:idx]}** {commit[idx+1:]}\n"
short = commit[:idx]
rest = commit[idx + 1 :]
idx = rest.index("/")
hash = rest[:idx]
commit = rest[idx + 1 :]
out += (
formatters["commits.commit"]
.replace("<short>", short)
.replace("<subject>", commit)
.replace("<hash>", hash)
)
except Exception as e:
logger.error(f"Failed to get commits: {e}")
return out

def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo | None):

def get_package_update_str(
base_pkg: Sequence[Package] | None,
info: ExportInfo | None,
formatters: dict[str, str],
):
if not base_pkg or not info or not info.get("packages", None):
return ""

Expand All @@ -169,33 +198,51 @@ def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo
if p.name in seen:
continue
seen.add(p.name)

if p.name not in previous:
out += f" - {p.name}: x → {p.version}\n"
out += (
formatters["pkgupd.add"]
.replace("<new>", p.version)
.replace("<package>", p.name)
)
else:
pv = previous[p.name]
# Skip release for package version updates
if "-" in pv:
prel = pv[pv.rindex("-") + 1 :]
pv = pv[:pv.rindex("-")]
pv = pv[: pv.rindex("-")]
else:
prel = None

if p.version != pv:
out += f" - {p.name}: {pv}{p.version}\n"
prev = pv
newv = p.version
elif prel and p.release != prel:
out += f" - {p.name}: {pv}-{prel}{p.version}-{p.release}\n"

prev = f"{pv}-{prel}"
newv = f"{p.version}-{p.release}"
else:
continue

out += (
formatters["pkgupd.update"]
.replace("<old>", prev)
.replace("<new>", newv)
.replace("<package>", p.name)
)

for p in previous:
if p not in seen:
pv = previous[p]
if "-" in pv:
pv = pv[:pv.rindex("-")]
out += f" - {p}: {pv} → x\n"
pv = pv[: pv.rindex("-")]
out += (
formatters["pkgupd.remove"].replace("<old>", pv).replace("<package>", p)
)
seen.add(p)

return out


def get_labels(
labels: Sequence[str],
version: str | None,
Expand All @@ -209,13 +256,16 @@ def get_labels(
changelog_template: str | None,
changelog_fn: str | None,
info: ExportInfo | None,
formatters: dict[str, str] = {},
) -> tuple[dict[str, str], str]:
formatters = {**DEFAULT_FORMATTERS, **formatters}

# Date format is YYMMDD
# Timestamp format is YYYY-MM-DDTHH:MM:SSZ
now = datetime.now()
date = now.strftime("%y%m%d")
timestamp = now.strftime("%Y-%m-%dT%H:%M:%SZ")
pkgupd = get_package_update_str(base_pkg, info)
pkgupd = get_package_update_str(base_pkg, info, formatters)

prev_labels = prev_manifest.get("Labels", {}) if prev_manifest else {}
prev_version = prev_labels.get(VERSION_TAG, None) if prev_labels else None
Expand Down Expand Up @@ -266,6 +316,7 @@ def get_labels(
git_dir,
revision,
(info or {}).get("revision", None) or prev_labels.get(REVISION_TAG, None),
formatters=formatters,
)

def process_label(key: str, value: str):
Expand All @@ -285,9 +336,9 @@ def process_label(key: str, value: str):
value = value.replace("<imginfo>", imginfo)
blacklist[key] = BLACKLIST_KEY
if "<commits>" in value:
value = value.replace("<commits>", commit_str or "-")
value = value.replace("<commits>", commit_str or formatters["commits.none"])
if "<pkgupd>" in value:
value = value.replace("<pkgupd>", pkgupd or "-")
value = value.replace("<pkgupd>", pkgupd or formatters["pkgupd.none"])

if base_pkg:
for pkg in base_pkg:
Expand Down

0 comments on commit b88b34c

Please sign in to comment.