Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions .github/workflows/bump-kmp-submodule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: Bump KMP Submodule

on:
workflow_call:
inputs:
target_repository:
description: Repository whose OneSignal-KMP-SDK submodule should be updated
required: true
type: string
kmp_version:
description: "OneSignal-KMP-SDK tag to pin the submodule to (e.g. v0.2.0)"
required: true
type: string
secrets:
GH_PUSH_TOKEN:
required: false
description: Cross-repository token with contents and pull-request write access
workflow_dispatch:
inputs:
target_repository:
description: Repository whose OneSignal-KMP-SDK submodule should be updated
required: true
type: string
kmp_version:
description: "OneSignal-KMP-SDK tag to pin the submodule to (e.g. v0.2.0)"
required: true
type: string

permissions:
contents: write
pull-requests: write

jobs:
bump:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GH_PUSH_TOKEN || github.token }}
SUBMODULE_PATH: OneSignal-KMP-SDK
TARGET_REPOSITORY: ${{ inputs.target_repository }}
VERSION: ${{ inputs.kmp_version }}
steps:
- name: "[Checkout] Target SDK"
uses: actions/checkout@v6
with:
repository: ${{ env.TARGET_REPOSITORY }}
ref: main
fetch-depth: 0
token: ${{ env.GH_TOKEN }}

- name: "[Setup] Git user"
uses: OneSignal/sdk-shared/.github/actions/setup-git-user@main

- name: "[Resolve] KMP release tag"
env:
KMP_REMOTE: https://github.com/OneSignal/OneSignal-KMP-SDK.git
run: |
set -euo pipefail

if [[ ! "$VERSION" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then
echo "::error::Invalid KMP version '$VERSION'. Expected a release tag such as v0.2.0."
exit 1
fi

if ! git ls-remote --exit-code --refs "$KMP_REMOTE" "refs/tags/${VERSION}" >/dev/null; then
echo "::error::KMP release tag '$VERSION' does not exist or could not be resolved."
exit 1
fi

tag_repo="$(mktemp -d)"
trap 'rm -rf "$tag_repo"' EXIT
git -C "$tag_repo" init --bare --quiet
if ! git -C "$tag_repo" fetch --quiet --depth=1 "$KMP_REMOTE" "refs/tags/${VERSION}"; then
echo "::error::Failed to fetch KMP release tag '$VERSION'."
exit 1
fi

new_sha="$(git -C "$tag_repo" rev-parse 'FETCH_HEAD^{commit}')"
if ! git -C "$tag_repo" cat-file -e "${new_sha}^{commit}"; then
echo "::error::KMP release tag '$VERSION' does not resolve to a commit."
exit 1
fi

echo "Resolved $VERSION to $new_sha."
echo "NEW_SHA=$new_sha" >> "$GITHUB_ENV"

- name: "[Bump] Re-point submodule gitlink"
id: bump
run: |
set -euo pipefail
branch="chore/bump-kmp-${VERSION}"
echo "branch=$branch" >> "$GITHUB_OUTPUT"

git checkout -b "$branch"
git update-index --add --cacheinfo "160000,${NEW_SHA},${SUBMODULE_PATH}"

if git diff --cached --quiet; then
echo "Submodule already at ${NEW_SHA}; nothing to bump."
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi

echo "changed=true" >> "$GITHUB_OUTPUT"
git commit -m "chore(logger): bump ${SUBMODULE_PATH} submodule to ${VERSION}"
git push origin "$branch" --force-with-lease

- name: "[PR] Open or update bump PR"
if: ${{ steps.bump.outputs.changed == 'true' }}
run: |
set -euo pipefail
branch="${{ steps.bump.outputs.branch }}"
notes_url="https://github.com/OneSignal/OneSignal-KMP-SDK/releases/tag/${VERSION}"
notes="$(gh release view "$VERSION" --repo OneSignal/OneSignal-KMP-SDK --json body --jq .body 2>/dev/null || true)"

body_file="$RUNNER_TEMP/bump_pr_body.md"
{
printf 'Automated bump of the `%s` submodule to **%s** (`%s`).\n\n' "$SUBMODULE_PATH" "$VERSION" "$NEW_SHA"
printf '## Release notes — %s\n\n' "$VERSION"
if [ -n "$notes" ]; then
printf '%s\n\n' "$notes"
printf '_Source: [%s](%s)_\n\n' "$VERSION" "$notes_url"
else
printf 'See [%s](%s).\n\n' "$VERSION" "$notes_url"
fi
printf 'After merge, run `git submodule update --init --recursive` locally to sync.\n'
} > "$body_file"

pr_number="$(gh pr list --base main --head "$branch" --state all --json number --jq '.[0].number // empty')"
if [ -n "$pr_number" ]; then
pr_state="$(gh pr view "$pr_number" --json state --jq .state)"
if [ "$pr_state" = "CLOSED" ]; then
echo "Reopening closed PR #${pr_number} for $branch."
if ! gh pr reopen "$pr_number"; then
echo "::warning::Could not reopen PR #${pr_number}; creating a replacement."
pr_number=""
fi
elif [ "$pr_state" = "MERGED" ]; then
pr_number=""
fi
fi

if [ -n "$pr_number" ]; then
gh pr edit "$pr_number" \
--title "chore(logger): bump shared KMP logger to ${VERSION}" \
--body-file "$body_file"
else
gh pr create \
--base main \
--head "$branch" \
--title "chore(logger): bump shared KMP logger to ${VERSION}" \
--body-file "$body_file"
fi