|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +CONFIG_PATH="${CONFIG_PATH:-.github/nsolid-release-lines.json}" |
| 6 | +NODE_RELEASE_INDEX_URL="${NODE_RELEASE_INDEX_URL:-https://nodejs.org/download/release/index.json}" |
| 7 | +NODE_LINE="${NODE_LINE:-}" |
| 8 | +ISSUE_NUMBER="${ISSUE_NUMBER:-}" |
| 9 | +GITHUB_REPOSITORY="${GITHUB_REPOSITORY:-}" |
| 10 | + |
| 11 | +if [[ -z "$NODE_LINE" ]]; then |
| 12 | + echo "Usage: NODE_LINE=22 $0" >&2 |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +node -e ' |
| 17 | + const fs = require("node:fs"); |
| 18 | + const [configPath, nodeLine] = process.argv.slice(1); |
| 19 | + const config = JSON.parse(fs.readFileSync(configPath, "utf8")); |
| 20 | + if (!config.nodeReleaseLines.map(String).includes(nodeLine)) { |
| 21 | + throw new Error(`Node.js v${nodeLine} is not enabled in ${configPath}`); |
| 22 | + } |
| 23 | +' "$CONFIG_PATH" "$NODE_LINE" |
| 24 | + |
| 25 | +latest_node_tag() { |
| 26 | + node -e ' |
| 27 | + const line = Number(process.argv[1]); |
| 28 | + const url = process.env.NODE_RELEASE_INDEX_URL; |
| 29 | + const response = await fetch(url); |
| 30 | + if (!response.ok) throw new Error(`Failed to fetch ${url}: ${response.status}`); |
| 31 | + const releases = await response.json(); |
| 32 | + const stable = releases |
| 33 | + .map((release) => release.version) |
| 34 | + .filter((version) => new RegExp(`^v${line}\\.\\d+\\.\\d+$`).test(version)) |
| 35 | + .sort((a, b) => { |
| 36 | + const left = a.slice(1).split(".").map(Number); |
| 37 | + const right = b.slice(1).split(".").map(Number); |
| 38 | + for (let i = 0; i < 3; i++) { |
| 39 | + if (left[i] !== right[i]) return right[i] - left[i]; |
| 40 | + } |
| 41 | + return 0; |
| 42 | + }); |
| 43 | + if (stable.length === 0) throw new Error(`No stable Node.js v${line} release found`); |
| 44 | + console.log(stable[0]); |
| 45 | + ' "$NODE_LINE" |
| 46 | +} |
| 47 | + |
| 48 | +BASE_BRANCH="node-v${NODE_LINE}.x-nsolid-v6.x" |
| 49 | +CHANGELOG_PATH="doc/changelogs/NSOLID_CHANGELOG_V6_NODE_V${NODE_LINE}.md" |
| 50 | +NODE_TAG="${NODE_TAG:-$(latest_node_tag)}" |
| 51 | +NODE_VERSION="${NODE_TAG#v}" |
| 52 | +RELEASE_DATE="${RELEASE_DATE:-$(date -u +%F)}" |
| 53 | +CONFLICT_REPORT="nsolid-release-conflicts.md" |
| 54 | + |
| 55 | +if [[ ! "$NODE_TAG" =~ ^v${NODE_LINE}\.[0-9]+\.[0-9]+$ ]]; then |
| 56 | + echo "Latest Node.js tag for v${NODE_LINE} is invalid: $NODE_TAG" >&2 |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | + |
| 60 | +comment_issue() { |
| 61 | + local body_file="$1" |
| 62 | + if [[ -n "$ISSUE_NUMBER" && -n "${GH_TOKEN:-${GITHUB_TOKEN:-}}" ]]; then |
| 63 | + gh issue comment "$ISSUE_NUMBER" --repo "$GITHUB_REPOSITORY" --body-file "$body_file" || true |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +write_conflict_report() { |
| 68 | + { |
| 69 | + echo "## Release proposal merge conflict" |
| 70 | + echo |
| 71 | + echo "- Base branch: \`$BASE_BRANCH\`" |
| 72 | + echo "- Upstream Node.js tag: \`$NODE_TAG\`" |
| 73 | + echo "- Release branch: \`$RELEASE_BRANCH\`" |
| 74 | + echo |
| 75 | + echo "The merge could not be completed automatically. No release PR was opened." |
| 76 | + echo |
| 77 | + echo "### Conflicted files" |
| 78 | + echo |
| 79 | + git diff --name-only --diff-filter=U | sed 's/^/- `/' | sed 's/$/`/' |
| 80 | + echo |
| 81 | + echo "### Git status" |
| 82 | + echo |
| 83 | + echo '```text' |
| 84 | + git status --short |
| 85 | + echo '```' |
| 86 | + } > "$CONFLICT_REPORT" |
| 87 | +} |
| 88 | + |
| 89 | +git config --local user.email "${GIT_AUTHOR_EMAIL:-nsolid-bot@nodesource.com}" |
| 90 | +git config --local user.name "${GIT_AUTHOR_NAME:-N|Solid GitHub Bot}" |
| 91 | + |
| 92 | +git fetch --no-tags origin "$BASE_BRANCH" |
| 93 | +git remote add nodejs https://github.com/nodejs/node.git 2>/dev/null || true |
| 94 | +git fetch --no-tags nodejs "refs/tags/${NODE_TAG}:refs/tags/${NODE_TAG}" |
| 95 | + |
| 96 | +git switch --detach "origin/$BASE_BRANCH" |
| 97 | +NSOLID_VERSION="$(python3 tools/getnsolidversion.py)" |
| 98 | +RELEASE_BRANCH="node-${NODE_TAG}-nsolid-v${NSOLID_VERSION}-release" |
| 99 | +NSOLID_TAG="node-${NODE_TAG}-nsolid-v${NSOLID_VERSION}" |
| 100 | + |
| 101 | +git fetch --no-tags origin "+refs/heads/${RELEASE_BRANCH}:refs/remotes/origin/${RELEASE_BRANCH}" 2>/dev/null || true |
| 102 | +if git rev-parse --verify "origin/${RELEASE_BRANCH}" >/dev/null 2>&1; then |
| 103 | + echo "Release branch origin/${RELEASE_BRANCH} already exists; nothing to do." |
| 104 | + exit 0 |
| 105 | +fi |
| 106 | + |
| 107 | +git switch -c "$RELEASE_BRANCH" |
| 108 | + |
| 109 | +if ! git merge "$NODE_TAG" --no-edit; then |
| 110 | + write_conflict_report |
| 111 | + comment_issue "$CONFLICT_REPORT" |
| 112 | + echo "::error::Merge conflict while merging $NODE_TAG into $BASE_BRANCH" |
| 113 | + exit 2 |
| 114 | +fi |
| 115 | + |
| 116 | +node -e ' |
| 117 | + const fs = require("node:fs"); |
| 118 | + const path = "src/node_version.h"; |
| 119 | + const text = fs.readFileSync(path, "utf8"); |
| 120 | + const next = text.replace( |
| 121 | + /#define NSOLID_VERSION_IS_RELEASE [01]/, |
| 122 | + "#define NSOLID_VERSION_IS_RELEASE 1", |
| 123 | + ); |
| 124 | + if (text === next) { |
| 125 | + throw new Error("Could not update NSOLID_VERSION_IS_RELEASE"); |
| 126 | + } |
| 127 | + fs.writeFileSync(path, next); |
| 128 | +' |
| 129 | + |
| 130 | +CODENAME="$(awk '/#define NODE_VERSION_IS_LTS / { gsub(/"/, "", $3); print $3; exit }' src/node_version.h)" |
| 131 | +MERGE_SHA="$(git rev-parse --short=10 HEAD)" |
| 132 | +MERGE_SUBJECT="$(git log -1 --format=%s)" |
| 133 | +MERGE_AUTHOR="$(git log -1 --format=%an)" |
| 134 | + |
| 135 | +node -e ' |
| 136 | + const fs = require("node:fs"); |
| 137 | + const [ |
| 138 | + changelogPath, |
| 139 | + releaseDate, |
| 140 | + nodeVersion, |
| 141 | + nsolidVersion, |
| 142 | + codename, |
| 143 | + mergeSha, |
| 144 | + mergeSubject, |
| 145 | + mergeAuthor, |
| 146 | + ] = process.argv.slice(1); |
| 147 | + const text = fs.readFileSync(changelogPath, "utf8"); |
| 148 | + const displayCodename = codename ? " \x27" + codename + "\x27" : ""; |
| 149 | + const entry = `## ${releaseDate}, Version ${nodeVersion}-nsolid-v${nsolidVersion}${displayCodename} |
| 150 | +
|
| 151 | +### Commits |
| 152 | +
|
| 153 | +* \\[[\`${mergeSha}\`](https://github.com/nodesource/nsolid/commit/${mergeSha})] - ${mergeSubject} (${mergeAuthor}) |
| 154 | +
|
| 155 | +`; |
| 156 | + fs.writeFileSync(changelogPath, entry + text); |
| 157 | +' "$CHANGELOG_PATH" "$RELEASE_DATE" "$NODE_VERSION" "$NSOLID_VERSION" "$CODENAME" "$MERGE_SHA" "$MERGE_SUBJECT" "$MERGE_AUTHOR" |
| 158 | + |
| 159 | +git add src/node_version.h "$CHANGELOG_PATH" |
| 160 | +git commit -m "${RELEASE_DATE}, Version ${NODE_VERSION}-nsolid-v${NSOLID_VERSION} '${CODENAME}'" |
| 161 | + |
| 162 | +git push origin "$RELEASE_BRANCH" |
| 163 | + |
| 164 | +PR_BODY="nsolid-release-pr-body.md" |
| 165 | +{ |
| 166 | + echo "Release PR for Node.js ${NODE_TAG} with N|Solid v${NSOLID_VERSION}." |
| 167 | + echo |
| 168 | + echo "Generated by the N|Solid release workflow." |
| 169 | +} > "$PR_BODY" |
| 170 | + |
| 171 | +PR_URL="$(gh pr create \ |
| 172 | + --repo "$GITHUB_REPOSITORY" \ |
| 173 | + --base "$BASE_BRANCH" \ |
| 174 | + --head "$RELEASE_BRANCH" \ |
| 175 | + --title "${RELEASE_DATE}, Version ${NODE_VERSION}-nsolid-v${NSOLID_VERSION} '${CODENAME}'" \ |
| 176 | + --body-file "$PR_BODY" \ |
| 177 | + --draft)" |
| 178 | + |
| 179 | +COMMENT_BODY="nsolid-release-success.md" |
| 180 | +{ |
| 181 | + echo "## Release proposal created" |
| 182 | + echo |
| 183 | + echo "- PR: $PR_URL" |
| 184 | + echo "- Release branch: \`$RELEASE_BRANCH\`" |
| 185 | + echo "- Release tag: not created by automation" |
| 186 | +} > "$COMMENT_BODY" |
| 187 | +comment_issue "$COMMENT_BODY" |
| 188 | + |
| 189 | +echo "$PR_URL" |
0 commit comments