Skip to content

Commit 79d43c1

Browse files
committed
feat: use GitHub API for verified+signoff commits
Signed-off-by: sandert-k8s <sandert98@gmail.com>
1 parent ae53c40 commit 79d43c1

1 file changed

Lines changed: 54 additions & 19 deletions

File tree

.github/workflows/sync-version-switcher.yml

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ jobs:
6262
URL: ${{ steps.ver.outputs.url }}
6363
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6464
run: |
65-
git config user.name "github-actions[bot]"
66-
git config user.email "github-actions[bot]@users.noreply.github.com"
67-
68-
# Sanitize version for use in a branch name (e.g. v0.14 -> 0-14)
6965
VERSION_SLUG=$(echo "$VERSION" | sed 's/^v//;s/\./-/g')
66+
REPO="${GITHUB_REPOSITORY}"
7067
7168
# Find all versioned release branches (release/0.x), skip preview branches
7269
for branch in $(git branch -r | grep -E 'origin/release/[0-9]' | sed 's|.*origin/||'); do
@@ -82,9 +79,6 @@ jobs:
8279
PR_ACTION="create"
8380
fi
8481
85-
# Create or reset the work branch to the current release branch HEAD
86-
git checkout -B "$WORK_BRANCH"
87-
8882
python3 - <<'PYEOF'
8983
import os, re
9084
@@ -122,28 +116,69 @@ jobs:
122116
f.write(content)
123117
PYEOF
124118
125-
git add config/_default/params.yaml
126-
if git diff --cached --quiet; then
127-
echo "Already in desired state, skipping"
128-
git checkout "$branch"
119+
# Check if there are actual changes vs the release branch HEAD
120+
if git diff --quiet HEAD -- config/_default/params.yaml; then
121+
echo "Already in desired state, skipping ${branch}"
122+
git restore config/_default/params.yaml
129123
echo "::endgroup::"
130124
continue
131125
fi
132126
133-
git commit -m "chore: add ${VERSION} to version switcher"
134-
git push --force-with-lease origin "$WORK_BRANCH"
135-
136-
if [ "$PR_ACTION" = "create" ]; then
127+
# Save desired content to a temp file (preserves trailing newline)
128+
DESIRED_FILE=$(mktemp)
129+
cat config/_default/params.yaml > "$DESIRED_FILE"
130+
git restore config/_default/params.yaml
131+
132+
# --- API-based commit (GitHub auto-verifies; no GPG key needed) ---
133+
134+
# Get the release branch HEAD and its tree
135+
BASE_SHA=$(gh api "repos/${REPO}/branches/${branch}" --jq '.commit.sha')
136+
BASE_TREE_SHA=$(gh api "repos/${REPO}/git/commits/${BASE_SHA}" --jq '.tree.sha')
137+
138+
# Upload the modified file as a blob
139+
CONTENT_B64=$(base64 -w 0 < "$DESIRED_FILE")
140+
rm -f "$DESIRED_FILE"
141+
BLOB_SHA=$(gh api "repos/${REPO}/git/blobs" \
142+
-f content="$CONTENT_B64" \
143+
-f encoding="base64" \
144+
--jq '.sha')
145+
146+
# Create a new tree with the updated file
147+
NEW_TREE_SHA=$(jq -n \
148+
--arg base_tree "$BASE_TREE_SHA" \
149+
--arg blob "$BLOB_SHA" \
150+
'{base_tree:$base_tree,tree:[{path:"config/_default/params.yaml",mode:"100644",type:"blob",sha:$blob}]}' | \
151+
gh api "repos/${REPO}/git/trees" --input - --jq '.sha')
152+
153+
# Build commit message with Signed-off-by for DCO
154+
COMMIT_MSG=$(printf 'chore: add %s to version switcher in release %s\n\nSigned-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>' \
155+
"$VERSION" "$branch")
156+
157+
# Create the commit (GitHub signs API commits automatically → Verified)
158+
NEW_COMMIT_SHA=$(jq -n \
159+
--arg msg "$COMMIT_MSG" \
160+
--arg tree "$NEW_TREE_SHA" \
161+
--arg parent "$BASE_SHA" \
162+
'{message:$msg,tree:$tree,parents:[$parent]}' | \
163+
gh api "repos/${REPO}/git/commits" --input - --jq '.sha')
164+
165+
# Create or force-update the work branch ref
166+
if [ "$PR_ACTION" = "update" ]; then
167+
gh api "repos/${REPO}/git/refs/heads/${WORK_BRANCH}" \
168+
-X PATCH -f sha="$NEW_COMMIT_SHA" -F force=true
169+
echo "Updated PR branch: ${WORK_BRANCH}"
170+
else
171+
gh api "repos/${REPO}/git/refs" \
172+
-f ref="refs/heads/${WORK_BRANCH}" \
173+
-f sha="$NEW_COMMIT_SHA"
137174
gh pr create \
175+
--repo "$REPO" \
138176
--base "$branch" \
139177
--head "$WORK_BRANCH" \
140-
--title "chore: add ${VERSION} to version switcher" \
178+
--title "chore: add ${VERSION} to version switcher in release ${branch}" \
141179
--body "Automated PR: adds \`${VERSION}\` to the version switcher in the \`${branch}\` release docs. Triggered by: ${{ github.event_name }} (${{ github.sha }})"
142180
echo "Opened PR: ${WORK_BRANCH} -> ${branch}"
143-
else
144-
echo "Updated PR branch: ${WORK_BRANCH}"
145181
fi
146182
147-
git checkout "$branch"
148183
echo "::endgroup::"
149184
done

0 commit comments

Comments
 (0)