Skip to content

Commit b88b34c

Browse files
committed
add formatter overrides in action
1 parent b82625a commit b88b34c

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

.github/workflows/online_test_deck.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
sudo podman pull ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }}
3939
- name: Run Rechunker
4040
id: rechunk
41-
uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/[email protected].2"
41+
uses: ./. # IMPORTANT: Use a tag, e.g.: "hhd-dev/[email protected].4"
4242
with:
4343
ref: 'ghcr.io/ublue-os/bazzite-deck:${{ github.event.inputs.ref }}'
4444
prev-ref: ghcr.io/hhd-dev/bazzite-automated-deck:stable
@@ -92,6 +92,14 @@ jobs:
9292
Package Changes:
9393
<pkgupd>
9494
95+
formatters: |
96+
commits.none=No changes\n
97+
commits.commit=- **[<short>](https://github.com/hhd-dev/rechunk/commit/<hash>)** <subject>\n
98+
pkgupd.none=No package updates\n
99+
pkgupd.add=- **<package>** Added at <new>\n
100+
pkgupd.update=- **<package>** <old> → <new>\n
101+
pkgupd.remove=- **<package>** <old> → Removed\n
102+
95103
- name: Upload Changelog
96104
uses: actions/upload-artifact@v4
97105
with:

3_chunk.sh

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,41 @@ if [ -n "$CLEAR_PLAN" ]; then
8585
PREV_ARG+=("--clear-plan")
8686
fi
8787

88-
LABEL_ARR=()
88+
ARG_ARR=()
8989
if [ -n "$LABELS" ]; then
9090
IFS=$'\n'
9191
for label in $LABELS; do
9292
if [ -z "$label" ]; then
9393
continue
9494
fi
95-
LABEL_ARR+=("--label" "$label")
95+
ARG_ARR+=("--label" "$label")
9696
done
9797
unset IFS
9898
fi
99+
if [ -n "$FORMATTERS" ]; then
100+
IFS=$'\n'
101+
for form in $FORMATTERS; do
102+
if [ -z "$form" ]; then
103+
continue
104+
fi
105+
ARG_ARR+=("--formatter" "$(echo $form)")
106+
done
107+
unset IFS
108+
fi
109+
99110
if [ -n "$DESCRIPTION" ]; then
100111
echo "Writing description to 'org.opencontainers.image.description'"
101-
LABEL_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION")
112+
ARG_ARR+=("--label" "org.opencontainers.image.description=$DESCRIPTION")
102113
fi
103114

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

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

113124

114125
PREV_ARG=""

action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ inputs:
7979
tag along with the git path.
8080
If <commit> is not used, providing it as part of a "org.opencontainers.image.revision"
8181
is the same.
82+
formatters:
83+
description: |
84+
The formatters to use for the changelog. The format is as follows:
85+
```
86+
formatter=<format>
87+
```
88+
Formatter is a string up to the first = and defines the formatter name.
89+
After that and until the end of the line is the format.
90+
Use \n for newlines. Different formatters use different
91+
substitutions (e.g., <name>), refer to the examples for specifics.
92+
93+
You can use the characters `<`, `>`, `=` freely. Substitution is only
94+
performed on exact tag matches.
8295
8396
outputs:
8497
ref:
@@ -165,6 +178,7 @@ runs:
165178
-e PREV_REF="${{ inputs.prev-ref }}" \
166179
-e OUT_NAME="$OUT_NAME" \
167180
-e LABELS="${{ inputs.labels }}" \
181+
-e FORMATTERS="${{ inputs.formatters }}" \
168182
-e VERSION="${{ inputs.version }}" \
169183
-e VERSION_FN="/workspace/version.txt" \
170184
-e PRETTY="${{ inputs.pretty }}" \

src/rechunk/__main__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ def argparse_func():
6262
default=None,
6363
)
6464
parser.add_argument(
65-
"-l", "--label", help="Add labels to the output image.", action="append"
65+
"-l",
66+
"--label",
67+
help="Add labels to the output image (`label=var`).",
68+
action="append",
69+
)
70+
parser.add_argument(
71+
"--formatter",
72+
help="Override string formatters for tags for i18n (`formatter=var`).",
73+
action="append",
6674
)
6775
parser.add_argument("--pretty", help="Pretty version string.", default=None)
6876
parser.add_argument(
@@ -126,6 +134,15 @@ def argparse_func():
126134
)
127135

128136
args = parser.parse_args()
137+
138+
# Parse formatters
139+
formatters = {}
140+
for line in args.formatter:
141+
if "=" not in line:
142+
continue
143+
idx = line.index("=")
144+
formatters[line[:idx]] = line[idx + 1 :]
145+
129146
alg_main(
130147
repo=args.repo,
131148
ref=args.ref,
@@ -145,6 +162,7 @@ def argparse_func():
145162
changelog=args.changelog,
146163
changelog_fn=args.changelog_fn,
147164
clear_plan=args.clear_plan,
165+
formatters=formatters,
148166
)
149167

150168

src/rechunk/alg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ def main(
547547
changelog: str | None = None,
548548
changelog_fn: str | None = None,
549549
clear_plan: bool = False,
550+
formatters: dict[str, str] = {},
550551
):
551552
if not meta_fn:
552553
meta_fn = get_default_meta_yaml()
@@ -655,6 +656,7 @@ def main(
655656
changelog_template=changelog,
656657
changelog_fn=changelog_fn,
657658
info=info,
659+
formatters=formatters,
658660
)
659661

660662
if contentmeta_fn:

src/rechunk/utils.py

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
VERSION_TAG = "org.opencontainers.image.version"
1919
REVISION_TAG = "org.opencontainers.image.revision"
2020

21+
DEFAULT_FORMATTERS = {
22+
"commits.none": "-\n",
23+
"commits.commit": "- **<short>** <subject>\n",
24+
"pkgupd.none": "-\n",
25+
"pkgupd.update": " - **<package>**: <old> → <new>\n",
26+
"pkgupd.add": " - **<package>**: x → <new>\n",
27+
"pkgupd.remove": " - **<package>**: <old> → x\n",
28+
}
29+
2130

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

138147

139-
def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None):
148+
def get_commits(
149+
git_dir: str | None,
150+
revision: str | None,
151+
prev_rev: str | None,
152+
formatters: dict[str, str],
153+
):
140154
logger.info(f"Getting commits from '{prev_rev}' to '{revision}' in '{git_dir}'")
141155
if not git_dir or not revision or not prev_rev:
142156
return ""
143157

144158
out = ""
145159
try:
146-
cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%s\" --no-merges {prev_rev}..{revision}"
160+
cmd = f"git --git-dir='{git_dir}/.git' log --format=\"%t/%T/%s\" --no-merges {prev_rev}..{revision}"
147161
for commit in (
148162
subprocess.run(cmd, stdout=subprocess.PIPE, shell=True)
149163
.stdout.decode("utf-8")
@@ -152,12 +166,27 @@ def get_commits(git_dir: str | None, revision: str | None, prev_rev: str | None)
152166
if not "/" in commit:
153167
continue
154168
idx = commit.index("/")
155-
out += f" - **{commit[:idx]}** {commit[idx+1:]}\n"
169+
short = commit[:idx]
170+
rest = commit[idx + 1 :]
171+
idx = rest.index("/")
172+
hash = rest[:idx]
173+
commit = rest[idx + 1 :]
174+
out += (
175+
formatters["commits.commit"]
176+
.replace("<short>", short)
177+
.replace("<subject>", commit)
178+
.replace("<hash>", hash)
179+
)
156180
except Exception as e:
157181
logger.error(f"Failed to get commits: {e}")
158182
return out
159183

160-
def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo | None):
184+
185+
def get_package_update_str(
186+
base_pkg: Sequence[Package] | None,
187+
info: ExportInfo | None,
188+
formatters: dict[str, str],
189+
):
161190
if not base_pkg or not info or not info.get("packages", None):
162191
return ""
163192

@@ -169,33 +198,51 @@ def get_package_update_str(base_pkg: Sequence[Package] | None, info: ExportInfo
169198
if p.name in seen:
170199
continue
171200
seen.add(p.name)
172-
201+
173202
if p.name not in previous:
174-
out += f" - {p.name}: x → {p.version}\n"
203+
out += (
204+
formatters["pkgupd.add"]
205+
.replace("<new>", p.version)
206+
.replace("<package>", p.name)
207+
)
175208
else:
176209
pv = previous[p.name]
177210
# Skip release for package version updates
178211
if "-" in pv:
179212
prel = pv[pv.rindex("-") + 1 :]
180-
pv = pv[:pv.rindex("-")]
213+
pv = pv[: pv.rindex("-")]
181214
else:
182215
prel = None
183216

184217
if p.version != pv:
185-
out += f" - {p.name}: {pv}{p.version}\n"
218+
prev = pv
219+
newv = p.version
186220
elif prel and p.release != prel:
187-
out += f" - {p.name}: {pv}-{prel}{p.version}-{p.release}\n"
188-
221+
prev = f"{pv}-{prel}"
222+
newv = f"{p.version}-{p.release}"
223+
else:
224+
continue
225+
226+
out += (
227+
formatters["pkgupd.update"]
228+
.replace("<old>", prev)
229+
.replace("<new>", newv)
230+
.replace("<package>", p.name)
231+
)
232+
189233
for p in previous:
190234
if p not in seen:
191235
pv = previous[p]
192236
if "-" in pv:
193-
pv = pv[:pv.rindex("-")]
194-
out += f" - {p}: {pv} → x\n"
237+
pv = pv[: pv.rindex("-")]
238+
out += (
239+
formatters["pkgupd.remove"].replace("<old>", pv).replace("<package>", p)
240+
)
195241
seen.add(p)
196242

197243
return out
198244

245+
199246
def get_labels(
200247
labels: Sequence[str],
201248
version: str | None,
@@ -209,13 +256,16 @@ def get_labels(
209256
changelog_template: str | None,
210257
changelog_fn: str | None,
211258
info: ExportInfo | None,
259+
formatters: dict[str, str] = {},
212260
) -> tuple[dict[str, str], str]:
261+
formatters = {**DEFAULT_FORMATTERS, **formatters}
262+
213263
# Date format is YYMMDD
214264
# Timestamp format is YYYY-MM-DDTHH:MM:SSZ
215265
now = datetime.now()
216266
date = now.strftime("%y%m%d")
217267
timestamp = now.strftime("%Y-%m-%dT%H:%M:%SZ")
218-
pkgupd = get_package_update_str(base_pkg, info)
268+
pkgupd = get_package_update_str(base_pkg, info, formatters)
219269

220270
prev_labels = prev_manifest.get("Labels", {}) if prev_manifest else {}
221271
prev_version = prev_labels.get(VERSION_TAG, None) if prev_labels else None
@@ -266,6 +316,7 @@ def get_labels(
266316
git_dir,
267317
revision,
268318
(info or {}).get("revision", None) or prev_labels.get(REVISION_TAG, None),
319+
formatters=formatters,
269320
)
270321

271322
def process_label(key: str, value: str):
@@ -285,9 +336,9 @@ def process_label(key: str, value: str):
285336
value = value.replace("<imginfo>", imginfo)
286337
blacklist[key] = BLACKLIST_KEY
287338
if "<commits>" in value:
288-
value = value.replace("<commits>", commit_str or "-")
339+
value = value.replace("<commits>", commit_str or formatters["commits.none"])
289340
if "<pkgupd>" in value:
290-
value = value.replace("<pkgupd>", pkgupd or "-")
341+
value = value.replace("<pkgupd>", pkgupd or formatters["pkgupd.none"])
291342

292343
if base_pkg:
293344
for pkg in base_pkg:

0 commit comments

Comments
 (0)