Repository navigation
ci(release): add release PR automation and title checks #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59ff91b
ci(release): add release PR automation and title checks
HyperAfnan 8356e0a
feat(tooling): add copy-db full database cloning script
HyperAfnan f4a3f30
fix(tooling): harden copy-db connection env and restore error detection
HyperAfnan e2b2ac9
fix(tooling): make copy-db Docker mode work with hosted sources
HyperAfnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { execFileSync } from "node:child_process"; | ||
| import { appendFileSync, readFileSync } from "node:fs"; | ||
|
|
||
| function git(args) { | ||
| return execFileSync("git", args, { encoding: "utf8" }).trim(); | ||
| } | ||
|
|
||
| function setOutput(name, value) { | ||
| const line = `${name}=${value}\n`; | ||
| if (process.env.GITHUB_OUTPUT) | ||
| appendFileSync(process.env.GITHUB_OUTPUT, line); | ||
| else process.stdout.write(line); | ||
| } | ||
|
|
||
| function parseVersion(tag) { | ||
| const match = /^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(tag); | ||
| return match ? match.slice(1).map(Number) : null; | ||
| } | ||
|
|
||
| function compareVersions(a, b) { | ||
| for (let i = 0; i < 3; i += 1) { | ||
| if (a[i] !== b[i]) return b[i] - a[i]; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| function bumpVersion(version, level) { | ||
| const next = [...version]; | ||
| if (level === "major") return [next[0] + 1, 0, 0]; | ||
| if (level === "minor") return [next[0], next[1] + 1, 0]; | ||
| return [next[0], next[1], next[2] + 1]; | ||
| } | ||
|
|
||
| function commitBump(subject, body) { | ||
| const match = /^([a-z][a-z0-9-]*)(?:\([^)]+\))?(!)?: .+/.exec(subject); | ||
| if (!match) return null; | ||
| const type = match[1]; | ||
| const breaking = | ||
| Boolean(match[2]) || /(?:^|\n)BREAKING CHANGE:\s*\S/m.test(body); | ||
| if (breaking) return "major"; | ||
| if (type === "feat") return "minor"; | ||
| if (["fix", "perf", "revert"].includes(type)) return "patch"; | ||
| return null; | ||
| } | ||
|
|
||
| const tags = git(["tag", "--list", "v[0-9]*"]) | ||
| .split("\n") | ||
| .filter((tag) => parseVersion(tag)) | ||
| .sort((a, b) => compareVersions(parseVersion(a), parseVersion(b))); | ||
| const latestTag = tags[0] || null; | ||
| const range = latestTag ? `${latestTag}..HEAD` : "HEAD"; | ||
| const rawLog = execFileSync("git", ["log", range, "--format=%s%x1f%b%x1e"], { | ||
| encoding: "utf8", | ||
| }); | ||
| const commits = rawLog | ||
| .split("\x1e") | ||
| .filter((entry) => entry.trim()) | ||
| .map((entry) => { | ||
| const separator = entry.indexOf("\x1f"); | ||
| return { | ||
| subject: entry.slice(0, separator).trim(), | ||
| body: entry.slice(separator + 1).trim(), | ||
| }; | ||
| }); | ||
|
|
||
| const ranks = { patch: 1, minor: 2, major: 3 }; | ||
| let bump = null; | ||
| for (const commit of commits) { | ||
| const candidate = commitBump(commit.subject, commit.body); | ||
| if (candidate && (!bump || ranks[candidate] > ranks[bump])) bump = candidate; | ||
| } | ||
|
|
||
| if (!bump) { | ||
| setOutput("release", "false"); | ||
| setOutput("version", ""); | ||
| setOutput("target_sha", git(["rev-parse", "HEAD"])); | ||
| setOutput( | ||
| "reason", | ||
| "No release-worthy Conventional Commits since the previous release." | ||
| ); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| let next; | ||
| if (!latestTag) { | ||
| const pkg = JSON.parse(readFileSync("package.json", "utf8")); | ||
| const base = parseVersion(`v${pkg.version}`); | ||
| if (!base) | ||
| throw new Error( | ||
| "package.json version must be a stable SemVer version, such as 0.1.0." | ||
| ); | ||
| next = base; | ||
| } else { | ||
| next = bumpVersion(parseVersion(latestTag), bump); | ||
| } | ||
|
|
||
| const version = `v${next.join(".")}`; | ||
| if (tags.includes(version)) | ||
| throw new Error(`Refusing to overwrite existing tag ${version}.`); | ||
| setOutput("release", "true"); | ||
| setOutput("version", version); | ||
| setOutput("target_sha", git(["rev-parse", "HEAD"])); | ||
| setOutput( | ||
| "reason", | ||
| `Detected ${bump} changes since ${latestTag || "the beginning of history"}.` | ||
| ); | ||
| console.log(`Release candidate: ${version}`); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| name: Pull Request Title | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [dev, main] | ||
| types: [opened, edited, synchronize, reopened] | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: read | ||
|
|
||
| jobs: | ||
| conventional-title: | ||
| name: Validate Conventional Commit title | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check pull request title | ||
| uses: amannn/action-semantic-pull-request@v5 | ||
|
lrfsdevs marked this conversation as resolved.
Resolved
|
||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| with: | ||
| types: | | ||
| feat | ||
| fix | ||
| perf | ||
| refactor | ||
| docs | ||
| test | ||
| build | ||
| ci | ||
| chore | ||
| revert | ||
| requireScope: false | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| name: Release PR | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: "0 */12 * * *" | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| concurrency: | ||
| group: holdmetoit-release-pr | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| create-release-pr: | ||
| name: Open or refresh dev → main PR | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check out dev and fetch full history | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: dev | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Check for changes to release | ||
| id: changes | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| git fetch origin main dev --quiet | ||
| count="$(git rev-list --count origin/main..origin/dev)" | ||
| echo "count=$count" >> "$GITHUB_OUTPUT" | ||
| if [[ "$count" -gt 0 ]]; then | ||
| { | ||
| echo 'summary<<EOF' | ||
| git log origin/main..origin/dev --pretty=format:'- %s (%h)' -n 50 | ||
| echo | ||
| echo 'EOF' | ||
| } >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Skip when dev has no unreleased commits | ||
| if: steps.changes.outputs.count == '0' | ||
| run: echo 'dev has no commits ahead of main; no release PR needed.' | ||
|
|
||
| - name: Create release PR if one does not exist | ||
| if: steps.changes.outputs.count != '0' | ||
| env: | ||
| GH_TOKEN: ${{ secrets.RELEASE_PR_TOKEN }} | ||
| CHANGE_SUMMARY: ${{ steps.changes.outputs.summary }} | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| if [[ -z "${GH_TOKEN:-}" ]]; then | ||
| echo 'Missing RELEASE_PR_TOKEN secret. Configure a fine-grained PAT or GitHub App token with repository Contents: read and Pull requests: write permissions.' >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| existing_pr="$(gh pr list --repo "$GITHUB_REPOSITORY" --head dev --base main --state open --json number --jq '.[0].number // empty')" | ||
| if [[ -n "$existing_pr" ]]; then | ||
| echo "Release PR #$existing_pr already exists. New commits on dev will update its diff automatically." | ||
| exit 0 | ||
| fi | ||
|
lrfsdevs marked this conversation as resolved.
Resolved
|
||
|
|
||
| body_file="$(mktemp)" | ||
| trap 'rm -f "$body_file"' EXIT | ||
| cat > "$body_file" <<BODY | ||
| ## Scheduled release review | ||
|
|
||
| This PR proposes promoting the current \\`dev\\` branch to \\`main\\`. It is opened automatically every 12 hours when \\`dev\\` is ahead of \\`main\\`. | ||
|
lrfsdevs marked this conversation as resolved.
Resolved
|
||
|
|
||
| ### Included commits | ||
| ${CHANGE_SUMMARY} | ||
|
|
||
| ### Before merging | ||
| - [ ] Review the complete diff and included commits. | ||
| - [ ] Confirm required CI checks pass. | ||
| - [ ] Confirm every release-worthy change uses a Conventional Commit / PR title. | ||
|
|
||
| After this PR is merged, the Release workflow calculates the next SemVer version from merged commits and creates the Git tag and GitHub Release if eligible changes exist. | ||
| BODY | ||
|
|
||
| gh pr create \ | ||
| --repo "$GITHUB_REPOSITORY" \ | ||
| --base main \ | ||
| --head dev \ | ||
| --title 'chore(release): promote dev to main' \ | ||
| --body-file "$body_file" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,3 +41,5 @@ skills-lock.json | |
| pokemon_metadata.csv | ||
| pokemon_metadata.json | ||
| pokemon_metadata.sqlite | ||
|
|
||
| .commandcode/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.