Skip to content
Merged
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
27 changes: 19 additions & 8 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ name: Publish Package to npmjs
# The version is never edited by hand. GitVersion works it out from git (GitVersion.yml) and
# scripts/version/updateVersion.sh stamps it into package.json:
# release / tag 0.2.0 -> 0.2.0 (the tag wins)
# run from main -> a prerelease of the next version, e.g. 0.1.1-3 (3 commits after tag
# 0.1.0), published under the `next` dist-tag; a commit message saying
# "+semver: minor" / "+semver: major" raises the base to 0.2.0 / 1.0.0
# run from main -> MAJOR.MINOR.(newest tag patch + pull requests merged since that tag),
# e.g. tag 0.1.0 + 4 merges -> 0.1.4 (scripts/version/versionRules.sh)
# `version` input -> exactly that
on:
release:
Expand Down Expand Up @@ -51,6 +50,23 @@ jobs:
with:
fetch-depth: 0

# Publishing, dry runs included, is only allowed from main, dev or master. A manual run must
# be started on one of those branches; a release's tag must point at a commit on one of them.
- name: Only main, dev or master may publish
run: |
allowed='^(main|dev|master)$'
if [[ "$GITHUB_EVENT_NAME" == "release" ]]; then
branches="$(git branch -r --contains "$GITHUB_SHA" --format='%(refname:short)' | sed 's|^origin/||')"
echo "Tag $GITHUB_REF_NAME is on: $(echo $branches)"
if ! echo "$branches" | grep -Eq "$allowed"; then
echo "::error::Release tag $GITHUB_REF_NAME is not on main, dev or master."
exit 1
fi
elif ! [[ "$GITHUB_REF_NAME" =~ $allowed ]]; then
echo "::error::Publishing only runs from main, dev or master (got '$GITHUB_REF_NAME')."
exit 1
fi

- uses: actions/setup-node@v4
with:
node-version: 24.x
Expand Down Expand Up @@ -87,11 +103,6 @@ jobs:
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "Publishing version: $version"

- name: Real publishes only come from a release or main
if: env.DRY_RUN != 'true' && github.event_name == 'workflow_dispatch' && github.ref != 'refs/heads/main'
run: |
echo "::error::Run this workflow from main (or tick dry_run). Got $GITHUB_REF."
exit 1

- run: npm run lint
- run: npm test
Expand Down
2 changes: 1 addition & 1 deletion GitVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# - tag it: git tag v0.2.0 && git push --tags
# - commit message: "... +semver: minor" (or +semver: major)
# (commit-message-incrementing is enabled)
# feature branch / PR -> <next version>-<label>.<n> 0.1.1-beta-my-thing.3, 0.1.1-pr7.2
# any other branch / PR -> that number, -<commits on the branch> 0.1.4-3
#
# Tags are 3-part (0.1.0); a leading v (v0.1.0) is accepted too.
# scripts/version/generateVersion.{sh,ps1} runs GitVersion and writes version-<semver>.txt;
Expand Down
15 changes: 13 additions & 2 deletions scripts/version/generateVersion.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generateVersion.ps1 - Generate version metadata using GitVersion.
# Writes version-<SEMVER>.txt in the repo root for updateVersion.ps1 to consume.
# generateVersion.ps1 - Generate version metadata using GitVersion, then the merge-count rules of versionRules.ps1.
# Writes version-<VERSION>.txt in the repo root for updateVersion.ps1 to consume.
#
# Windows twin of generateVersion.sh - same output file, same keys, so either script can feed
# either updateVersion. The .sh runs on the Linux CI runner (it needs jq to parse GitVersion's
Expand Down Expand Up @@ -55,6 +55,17 @@ try {
$GIT_TAG = $versionData.PreReleaseTag
$GIT_BRANCH = $versionData.BranchName
$COMMIT_COUNT = $versionData.CommitsSinceVersionSource

# The version string rules (versionRules.ps1, the twin of versionRules.sh): main 0.6.56, a branch 0.6.56-3. The branch is the pipeline's
# when there is one; $env:VERSION_BRANCH previews another.
. (Join-Path $PSScriptRoot 'versionRules.ps1')
$rulesBranch = @($env:VERSION_BRANCH, $env:CI_MERGE_REQUEST_SOURCE_BRANCH_NAME, $env:CI_COMMIT_BRANCH, $env:GITHUB_HEAD_REF, $env:GITHUB_REF_NAME, $GIT_BRANCH) |
Where-Object { $_ } | Select-Object -First 1
$rules = Get-VersionRules -Branch $rulesBranch -MajorMinorPatch $MAJOR_MINOR_PATCH -Version $VERSION -FullSemVer $FULL_SEMVER -CommitCount $COMMIT_COUNT
$MAJOR_MINOR_PATCH = $rules.MajorMinorPatch
$VERSION = $rules.Version
$FULL_SEMVER = $rules.FullSemVer
$COMMIT_COUNT = $rules.CommitCount
$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")

$versionFile = Join-Path $repo "version-$VERSION.txt"
Expand Down
12 changes: 10 additions & 2 deletions scripts/version/generateVersion.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

# generateVersion.sh - Generate version metadata using GitVersion.
# Writes version-<SEMVER>.txt in the repo root for updateVersion.sh to consume.
# generateVersion.sh - Generate version metadata using GitVersion, then the merge-count rules of versionRules.sh.
# Writes version-<VERSION>.txt in the repo root for updateVersion.sh to consume.
#
# Resolution order for the GitVersion binary:
# 1. $GITVERSION_BIN (set by CI after `dotnet tool install GitVersion.Tool`)
Expand Down Expand Up @@ -89,6 +89,14 @@ MAJOR_MINOR_PATCH=${MAJOR_MINOR_PATCH:-"0.0.0"}
# Replace 'PullRequest'/'pr' labels with 'Patch' in SemVer so PR builds still produce a usable version.
VERSION=${SEMVER//PullRequest/Patch}

# The version string rules (scripts/version/versionRules.sh): main is MAJOR.MINOR.(tag patch + merge requests since the tag), 0.6.56; any other
# branch is that number where it left main, -<its commits>, 0.6.56-3. The branch is the pipeline's (a merge request pipeline and a GitHub run
# check out a detached commit, which GitVersion may not name); to preview another branch's string:
# VERSION_BRANCH=main ./scripts/version/generateVersion.sh
RULES_BRANCH="${VERSION_BRANCH:-${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-${CI_COMMIT_BRANCH:-${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-$GIT_BRANCH}}}}}"
source "$REPO/scripts/version/versionRules.sh"
apply_version_rules "$RULES_BRANCH"

BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

# Remove any existing version-*.txt files in repo root
Expand Down
53 changes: 53 additions & 0 deletions scripts/version/versionRules.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# versionRules.ps1 - PowerShell twin of versionRules.sh (the version-string rules); dot-source it, don't run it.
#
# Get-VersionRules -Branch <name> -MajorMinorPatch <GitVersion's> -Version <...> -FullSemVer <...> -CommitCount <...>
#
# returns MajorMinorPatch, Version, FullSemVer and CommitCount after the rules (see versionRules.sh for them and the reasons):
# main/master MAJOR.MINOR.(tag patch + merge requests merged into main since the tag, min 1) 0.6.56; the tagged commit is the tag
# anything else <that number>-<commits on the branch since main's newest merge> 0.6.56-3 (no suffix for 0, or for main's tree)

# GitHub's merge commit for a pull request (doi matches GitLab's "into 'main'" here instead).
$MainMergeGrep = "^Merge pull request #"

function Get-NewestStableTag([string]$Ref) {
git tag --merged $Ref --sort=-v:refname | Where-Object { $_ -match '^v?\d+\.\d+\.\d+$' } | Select-Object -First 1
}

function Get-ReleaseLineVersion([string]$Ref) {
$tag = Get-NewestStableTag $Ref
if (-not $tag) { return $null }
if ((git rev-parse "$tag^{commit}") -eq (git rev-parse "$Ref^{commit}")) { return $tag.TrimStart('v') }
$parts = $tag.TrimStart('v').Split('.')
$merges = [int](git rev-list --count --merges "--grep=$MainMergeGrep" "$tag..$Ref")
if ($merges -lt 1) { $merges = 1 }
return "$($parts[0]).$($parts[1]).$([int]$parts[2] + $merges)"
}

function Get-BranchCommits([string]$Ref) {
$last = git rev-list -1 --merges "--grep=$MainMergeGrep" $Ref
if (-not $last) { $last = Get-NewestStableTag $Ref }
if (-not $last) { return 0 }
git diff --quiet $last $Ref -- 2>$null
if ($LASTEXITCODE -eq 0) { return 0 }
return [int](git rev-list --count --no-merges "$last..$Ref")
}

function Get-VersionRules([string]$Branch, [string]$MajorMinorPatch, [string]$Version, [string]$FullSemVer, $CommitCount) {
$result = [ordered]@{ MajorMinorPatch = $MajorMinorPatch; Version = $Version; FullSemVer = $FullSemVer; CommitCount = $CommitCount }
$base = Get-ReleaseLineVersion 'HEAD'
if (-not $base) { return $result }
$result.MajorMinorPatch = $base
# A commit that carries a stable tag IS that tag, whatever the branch is called (a tag run's branch is the tag's name).
$tagged = git tag --points-at HEAD | Where-Object { $_ -match '^v?\d+\.\d+\.\d+$' } | Select-Object -First 1
if ($Branch -in @('main', 'master') -or $tagged) {
$result.Version = $base
$result.FullSemVer = $base
$result.CommitCount = 0
}
else {
$ahead = Get-BranchCommits 'HEAD'
$result.Version = if ($ahead -eq 0) { $base } else { "$base-$ahead" }
$result.CommitCount = $ahead
}
return $result
}
78 changes: 78 additions & 0 deletions scripts/version/versionRules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash
# versionRules.sh - the one place the version-string rules live. Source it; don't run it.
#
# Sourced by generateVersion.sh (generateVersion.ps1 mirrors it), which every pipeline runs (GitLab's `test`, GitHub's `version` job), so a
# pipeline and a local run produce the same string. Modelled on cellhasher's scripts/version/versionRules.sh, whose `dev` line is our `main`.
#
# apply_version_rules <branch>
#
# Reads and updates MAJOR_MINOR_PATCH, VERSION, FULL_SEMVER and COMMIT_COUNT, which the caller has already set from GitVersion:
#
# main/master MAJOR.MINOR.(tag patch + merge requests merged since the tag, min 1) tag 0.6.0 + 56 merges -> 0.6.56
# (the tagged commit itself is the tag: 0.7.0)
# a tagged commit the tag itself (whatever the branch is called: a tag run's "branch" is the tag's name)
# anything else <that number where the branch left main>-<commits on the branch> 0.6.56, 3 commits -> 0.6.56-3
# (0 commits, or a tree identical to main's, drops the suffix: the build IS main's version)
#
# MAJOR and MINOR come from the newest X.Y.Z tag (tag 0.7.0 to start 0.7.x). "Merge requests merged" are the merge commits GitLab writes into
# main ("Merge branch '<name>' into 'main'"), counted wherever they are reachable rather than along the first parent: GitHub (lastlink/pocket_dai,
# a mirror kept by sync pull requests) has the same commits but its own first-parent history, and must print the same number for the same code.
# COMMIT_COUNT becomes the branch's commit count (0 on main), the fourth part of AssemblyVersion and the "-N" of InformationalVersion.
#
# Needs full history and the tags (GIT_DEPTH 0 on GitLab, fetch-depth 0 on GitHub; generateVersion.sh runs `git fetch --tags`). Without a tag
# the rules leave GitVersion's values alone.

# The merge commit GitHub writes when a pull request lands on main ("Merge pull request #9 from ..."). doi matches GitLab's "into 'main'" here instead.
MAIN_MERGE_GREP="^Merge pull request #"

# release_line_version <ref>: the main line's number at <ref>: the newest stable X.Y.Z tag reachable from it, patch + main merges since that tag
# (min 1); <ref> being the tagged commit prints the tag. Prints nothing without a tag.
release_line_version() {
local ref="$1" base_tag tag_major tag_minor tag_patch merges
base_tag=$(git tag --merged "$ref" --sort=-v:refname | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | head -n1 || true)
[ -n "$base_tag" ] || return 0
if [ "$(git rev-parse "$base_tag^{commit}")" = "$(git rev-parse "$ref^{commit}")" ]; then
echo "${base_tag#v}"
return 0
fi
IFS=. read -r tag_major tag_minor tag_patch <<< "${base_tag#v}"
merges=$(git rev-list --count --merges --grep="$MAIN_MERGE_GREP" "$base_tag..$ref")
if [ "$merges" -lt 1 ]; then merges=1; fi
echo "$tag_major.$tag_minor.$((tag_patch + merges))"
}

# branch_commits <ref>: commits on a branch since main's newest merge (or the tag) reachable from it, merges not counted; 0 when the tree is the
# same as at that merge (a GitHub sync branch: GitHub's own commits, same code).
branch_commits() {
local ref="$1" last
last=$(git rev-list -1 --merges --grep="$MAIN_MERGE_GREP" "$ref" || true)
if [ -z "$last" ]; then
last=$(git tag --merged "$ref" --sort=-v:refname | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | head -n1 || true)
fi
[ -n "$last" ] || { echo 0; return 0; }
if git diff --quiet "$last" "$ref" -- 2>/dev/null; then echo 0; return 0; fi
git rev-list --count --no-merges "$last..$ref"
}

apply_version_rules() {
local branch="${1:-}" base ahead
base=$(release_line_version HEAD)
[ -n "$base" ] || return 0
MAJOR_MINOR_PATCH="$base"
# A commit that carries a stable tag IS that tag, whatever the pipeline calls the branch: a tag run passes the tag's name (0.7.0), which would
# otherwise count commits past the last main merge it can see and print 0.7.0-1.
if git tag --points-at HEAD | grep -qE '^v?[0-9]+\.[0-9]+\.[0-9]+$'; then branch=main; fi
case "$branch" in
main|master)
VERSION="$base"
FULL_SEMVER="$base"
COMMIT_COUNT=0
;;
*)
ahead=$(branch_commits HEAD)
VERSION="$base-$ahead"
VERSION="${VERSION%-0}"
COMMIT_COUNT="$ahead"
;;
esac
}
Loading