Skip to content

Commit 3dc04b2

Browse files
authored
Fix: keep target action references on sync (#9)
Synced files overwrote the action references of target repositories, so Dependabot pull requests opened there conflicted with the sync pull request or were reverted by it. The sync now keeps the references already set in each target file, letting the target Dependabot manage them on every branch. References set here only apply to targets missing the action; this repository's Dependabot now scans sync/files and sync/files-public to keep them up to date. Also bumps korthout/backport-action to v5.0.0 in the forward-port workflow.
1 parent e3d5fdc commit 3dc04b2

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎.github/dependabot.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ version: 2
22
updates:
33
# Ensure GitHub Actions are used in their latest version
44
- package-ecosystem: "github-actions"
5-
directory: "/"
5+
directories:
6+
- "/"
7+
- "/sync/files/.github/workflows"
8+
- "/sync/files-public/.github/workflows"
69
schedule:
710
interval: "monthly"

‎.github/scripts/sync.sh‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ while read -r repo; do
170170
fi
171171
# Keep the plugin specific inputs and secrets of the ci job, written after its last common input
172172
ci_extra="$(sed '1,/db-image: "\${{ matrix.db-image }}"/d' "$repo_dir/$ci_file" 2>/dev/null || true)"
173+
# Keep the action references of the repository, its Dependabot updates them on every branch without conflicting with this pull request
174+
pins_dir="$work_dir/$name-pins"
175+
for file in "${synced_files[@]}" "${public_files[@]}"; do
176+
if [[ -s "$repo_dir/$file" ]]; then
177+
mkdir -p "$pins_dir/$(dirname "$file")"
178+
cp "$repo_dir/$file" "$pins_dir/$file"
179+
fi
180+
done
173181
cp -r "$files_dir/." "$repo_dir/"
174182
repo_files=("${synced_files[@]}")
175183
repo_deleted_paths=("${deleted_paths[@]}")
@@ -181,6 +189,42 @@ while read -r repo; do
181189
fi
182190
for file in "${repo_files[@]}"; do
183191
sed -i -e "s/{PLUGIN}/${name}/g" -e "s/{GLPI_VERSION}/${glpi_version:-$default_glpi_version}/g" "$repo_dir/$file"
192+
if [[ -f "$pins_dir/$file" ]]; then
193+
# Replaces everything after "action@" with the repository value, version comment included, unless the template version is higher
194+
awk '
195+
# Version from the "# vX.Y.Z" comment, else from a "@vN" tag, empty when unknown
196+
function version(ref, v) {
197+
if (match(ref, /# v?[0-9]+(\.[0-9]+)*/)) {
198+
v = substr(ref, RSTART + 2, RLENGTH - 2)
199+
} else if (match(ref, /^v?[0-9]+(\.[0-9]+)*"/)) {
200+
v = substr(ref, RSTART, RLENGTH - 1)
201+
}
202+
sub(/^v/, "", v)
203+
return v
204+
}
205+
function at_least(a, b, x, y, n, i) {
206+
n = split(a, x, ".")
207+
if (split(b, y, ".") > n) { n = split(b, y, ".") }
208+
for (i = 1; i <= n; i++) {
209+
if (x[i] + 0 != y[i] + 0) { return x[i] + 0 > y[i] + 0 }
210+
}
211+
return 1
212+
}
213+
match($0, /uses: "[^"@]+@/) {
214+
prefix = substr($0, 1, RSTART + RLENGTH - 1)
215+
action = substr($0, RSTART, RLENGTH)
216+
ref = substr($0, RSTART + RLENGTH)
217+
if (FNR == NR) { pins[action] = ref; next }
218+
if (action in pins) {
219+
pinned = version(pins[action])
220+
template = version(ref)
221+
if (pinned != "" && template != "" && at_least(pinned, template)) { $0 = prefix pins[action] }
222+
}
223+
}
224+
FNR != NR { print }
225+
' "$pins_dir/$file" "$repo_dir/$file" > "$work_dir/pinned"
226+
cat "$work_dir/pinned" > "$repo_dir/$file"
227+
fi
184228
done
185229
if [[ -n "$ci_extra" ]]; then
186230
printf '%s\n' "$ci_extra" >> "$repo_dir/$ci_file"

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The repository must also be granted to the sync GitHub App installation, otherwi
2727
- `{PLUGIN}` in a synced file is replaced by the target repository name.
2828
- `{GLPI_VERSION}` is replaced by the `glpi-version` currently set in the target continuous integration workflow (`12.0.x` when missing or not in the `X.Y.x` or `X.Y.Z` format).
2929
- Lines following `db-image` in the target continuous integration workflow (plugin specific inputs and secrets of the `ci` job) are kept.
30+
- Action references (`uses: "owner/action@ref" # version`) already set in a target file are kept when their version is not lower than the one set here, so the target Dependabot updates them without conflicting with the sync pull request; otherwise, or when either version is unknown, the reference set here applies, and this repository's Dependabot keeps it up to date.
3031
- The npm entry of `dependabot.yml` is dropped when the target has no `package.json`.
3132
- Every `dependabot.yml` entry is duplicated with a `target-branch` for each branch of `sync/maintained-branches.txt` existing in the target: add a new `X.Y/bugfixes` branch there when it opens, remove it at its end of life.
3233
- Pull requests merged into a branch are forward-ported to the next one, the protected `X.Y/bugfixes` branches sorted by version then the default branch: the chain follows new branches without any change here. Pull requests labelled `no forward-port` or opened by bots (except forward-port pull requests) are skipped.

‎sync/files/.github/workflows/forward-port.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585

8686
- name: "Create forward-port pull request"
8787
if: steps.target.outputs.branch != ''
88-
uses: "korthout/backport-action@8560fb503c275d433c56f05a2f64850093baa9f7" # v4.7.0
88+
uses: "korthout/backport-action@ad272ff24e4a8593bf18a951a154ae6af93b26b8" # v5.0.0
8989
with:
9090
github_token: "${{ steps.app-token.outputs.token }}"
9191
target_branches: ${{ steps.target.outputs.branch }}

0 commit comments

Comments
 (0)