|
| 1 | +import { |
| 2 | + LogLevel, |
| 3 | + createVersioningPlan, |
| 4 | + getCommitHistory, |
| 5 | + getWorkspace, |
| 6 | + logger, |
| 7 | + readJsoncFile, |
| 8 | +} from "conventional-versioning"; |
| 9 | +import { $ } from "execa"; |
| 10 | +import assert from "node:assert/strict"; |
| 11 | +import { Octokit } from "octokit"; |
| 12 | + |
| 13 | +logger.pipe((log) => { |
| 14 | + if (log.level <= LogLevel.Warning) { |
| 15 | + console.error(log.text); |
| 16 | + } |
| 17 | +}); |
| 18 | + |
| 19 | +const { |
| 20 | + GITHUB_TOKEN, |
| 21 | + GITHUB_REPO, |
| 22 | + GITHUB_HEAD_BRANCH = "automated-versioning", |
| 23 | + GITHUB_BASE_BRANCH = "main", |
| 24 | + CONVER_CONFIG = "conver.json", |
| 25 | +} = process.env; |
| 26 | +assert(GITHUB_TOKEN, "Missing 'GITHUB_TOKEN' environment variable."); |
| 27 | +assert(GITHUB_REPO, "Missing 'GITHUB_REPO' environment variable."); |
| 28 | + |
| 29 | +const [owner, repo] = GITHUB_REPO.split("/"); |
| 30 | + |
| 31 | +// Make sure working directory is clean. |
| 32 | +if (!(await isClean())) { |
| 33 | + console.error("Working directory is not clean. Exiting."); |
| 34 | + process.exit(1); |
| 35 | +} |
| 36 | + |
| 37 | +// Make sure current branch is base branch. |
| 38 | +if ((await getCurrentBranch()) !== GITHUB_BASE_BRANCH) { |
| 39 | + console.error("Switch to base branch before running. Exiting."); |
| 40 | + process.exit(1); |
| 41 | +} |
| 42 | + |
| 43 | +// Switch to head branch. |
| 44 | +console.info(`Switching branch to ${GITHUB_HEAD_BRANCH}.`); |
| 45 | +await $`git checkout -B ${GITHUB_HEAD_BRANCH}`; |
| 46 | + |
| 47 | +// Get versioning plan. |
| 48 | +/** @type {import('conventional-versioning').Options} */ |
| 49 | +const options = await readJsoncFile(CONVER_CONFIG); |
| 50 | +const workspace = await getWorkspace(options); |
| 51 | +const history = await getCommitHistory(options); |
| 52 | +const updates = createVersioningPlan(workspace, history, options); |
| 53 | + |
| 54 | +// Do versioning. |
| 55 | +await $`pnpm conver version --yes`; |
| 56 | + |
| 57 | +if (await isClean()) { |
| 58 | + console.log("No changes. Exiting."); |
| 59 | + process.exit(); |
| 60 | +} |
| 61 | + |
| 62 | +// Push changes. |
| 63 | +console.info(`Pushing changes to ${GITHUB_HEAD_BRANCH}.`); |
| 64 | +await $`git commit -a -m ${"chore: version package(s)"}`; |
| 65 | +await $`git push --force -u origin ${GITHUB_HEAD_BRANCH}`; |
| 66 | + |
| 67 | +let pullBody = |
| 68 | + "This pull request was opened by the " + |
| 69 | + "[release action](https://github.com/tscpp/knuckles/actions/workflows/release.yml) " + |
| 70 | + "since new versions are available and will automatically stay in sync.\n\n" + |
| 71 | + updates |
| 72 | + .map( |
| 73 | + (update) => |
| 74 | + `- ${update.name}: ${update.oldVersion} -> ${update.newVersion}`, |
| 75 | + ) |
| 76 | + .join("\n") + |
| 77 | + "\n"; |
| 78 | + |
| 79 | +const octokit = new Octokit({ |
| 80 | + auth: GITHUB_TOKEN, |
| 81 | +}); |
| 82 | + |
| 83 | +console.info("Searching for existing pull request."); |
| 84 | +const response = await octokit.rest.pulls.list({ |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + state: "open", |
| 88 | + head: owner + ":" + GITHUB_HEAD_BRANCH, |
| 89 | + base: GITHUB_BASE_BRANCH, |
| 90 | + per_page: 1, |
| 91 | +}); |
| 92 | +assert.equal(response.status, 200, "Expected response status code 200."); |
| 93 | +const [pull] = response.data; |
| 94 | + |
| 95 | +if (pull) { |
| 96 | + console.info(`Found existing pull request #${pull.number}.`); |
| 97 | + assert.equal(pull.head.ref, GITHUB_HEAD_BRANCH); |
| 98 | + assert.equal(pull.base.ref, GITHUB_BASE_BRANCH); |
| 99 | + console.info("Updating existing pull request."); |
| 100 | + const response = await octokit.rest.pulls.update({ |
| 101 | + owner, |
| 102 | + repo, |
| 103 | + pull_number: pull.number, |
| 104 | + body: pullBody, |
| 105 | + }); |
| 106 | + assert.equal(response.status, 200, "Expected response status code 200."); |
| 107 | +} else { |
| 108 | + console.info("Did not find existing pull request."); |
| 109 | + console.info("Creating a new pull request."); |
| 110 | + const response = await octokit.rest.pulls.create({ |
| 111 | + owner, |
| 112 | + repo, |
| 113 | + head: GITHUB_HEAD_BRANCH, |
| 114 | + base: GITHUB_BASE_BRANCH, |
| 115 | + title: "chore: version package(s)", |
| 116 | + body: pullBody, |
| 117 | + }); |
| 118 | + assert.equal(response.status, 201, "Expected response status code 201."); |
| 119 | + console.info(`Created pull request #${response.data.number}.`); |
| 120 | +} |
| 121 | + |
| 122 | +console.info(`Switching branch to ${GITHUB_BASE_BRANCH}.`); |
| 123 | +await $`git checkout ${GITHUB_BASE_BRANCH}`; |
| 124 | + |
| 125 | +async function isClean() { |
| 126 | + const { stdout } = await $`git status --porcelain`; |
| 127 | + return stdout.trim() === ""; |
| 128 | +} |
| 129 | + |
| 130 | +async function getCurrentBranch() { |
| 131 | + const { stdout } = await $`git branch --show-current`; |
| 132 | + return stdout.trim(); |
| 133 | +} |
0 commit comments