Skip to content
Closed
Show file tree
Hide file tree
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
107 changes: 107 additions & 0 deletions .github/scripts/calculate-release-version.mjs
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);
Comment thread
lrfsdevs marked this conversation as resolved.
Resolved
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}`);
33 changes: 33 additions & 0 deletions .github/workflows/commitlint.yml
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
Comment thread
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
90 changes: 90 additions & 0 deletions .github/workflows/release-pr.yml
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
Comment thread
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\\`.
Comment thread
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"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ skills-lock.json
pokemon_metadata.csv
pokemon_metadata.json
pokemon_metadata.sqlite

.commandcode/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"db:seed": "npm run db:seed -w @holdmetoit/database",
"db:seed:flags": "npm run db:seed:flags -w @holdmetoit/database",
"db:seed:pokemon": "npm run db:seed:pokemon -w @holdmetoit/web",
"db:copy": "bash scripts/copy-db.sh",
"postinstall": "npm run db:generate",
"format": "prettier --write .",
"format:check": "prettier --check .",
Expand Down
Loading
Loading