Skip to content

release: v0.6.0

release: v0.6.0 #17

name: Dev->Main PR Metadata
on:
pull_request:
types: [opened, reopened, synchronize]
branches: [main]
jobs:
autofill:
name: PR / Dev->Main Metadata
if: github.event.pull_request.head.ref == 'dev' && github.event.pull_request.base.ref == 'main'
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Auto-set title and body
uses: actions/github-script@v7
with:
script: |
const fs = require("fs");
const path = ".github/PULL_REQUEST_TEMPLATE/dev-to-main.md";
const template = fs.readFileSync(path, "utf8");
const { owner, repo } = context.repo;
const pull_number = context.payload.pull_request.number;
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
const pkgVersion = String(pkg.version || "0.0.0");
function parseSemver(v) {
const m = String(v).match(/^v?(\d+)\.(\d+)\.(\d+)$/);
if (!m) return [0, 0, 0];
return [Number(m[1]), Number(m[2]), Number(m[3])];
}
function cmp(a, b) {
const pa = parseSemver(a);
const pb = parseSemver(b);
if (pa[0] !== pb[0]) return pa[0] - pb[0];
if (pa[1] !== pb[1]) return pa[1] - pb[1];
return pa[2] - pb[2];
}
const tagRefs = await github.paginate(github.rest.repos.listTags, {
owner,
repo,
per_page: 100,
});
const stableTags = tagRefs
.map((t) => t.name)
.filter((t) => /^v\d+\.\d+\.\d+$/.test(t));
stableTags.sort((a, b) => cmp(a, b));
const latestTag = stableTags.length ? stableTags[stableTags.length - 1].replace(/^v/, "") : "0.0.0";
const version = cmp(pkgVersion, latestTag) >= 0 ? pkgVersion : latestTag;
const title = `release: v${version}`;
const commits = await github.paginate(github.rest.pulls.listCommits, {
owner,
repo,
pull_number,
per_page: 100,
});
const commitLines = commits.map((c) => `- ${c.commit.message.split("\n")[0]} (${c.sha.slice(0, 7)})`);
const commitsText = commitLines.length ? commitLines.join("\n") : "- No commits found.";
const body = template
.replace("`v<package.json.version>`", `v${version}`)
.replace(
/<!-- AUTO-GENERATED:COMMITS -->[\s\S]*?<!-- \/AUTO-GENERATED:COMMITS -->/m,
`<!-- AUTO-GENERATED:COMMITS -->\n${commitsText}\n<!-- /AUTO-GENERATED:COMMITS -->`
);
const existingBody = context.payload.pull_request.body || "";
const preserveManual = /<!-- AUTO-GENERATED:COMMITS -->[\s\S]*?<!-- \/AUTO-GENERATED:COMMITS -->/m.test(existingBody);
const nextBody = preserveManual
? existingBody
.replace(/- Version: .*/m, `- Version: v${version}`)
.replace(
/<!-- AUTO-GENERATED:COMMITS -->[\s\S]*?<!-- \/AUTO-GENERATED:COMMITS -->/m,
`<!-- AUTO-GENERATED:COMMITS -->\n${commitsText}\n<!-- /AUTO-GENERATED:COMMITS -->`
)
: body;
await github.rest.pulls.update({
owner,
repo,
pull_number,
title,
body: nextBody,
});