Skip to content

Commit

Permalink
work on release
Browse files Browse the repository at this point in the history
  • Loading branch information
jtoar committed Feb 28, 2024
1 parent a87d719 commit 42bcd84
Show file tree
Hide file tree
Showing 21 changed files with 418 additions and 230 deletions.
64 changes: 64 additions & 0 deletions lib/cherry_pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { chalk, question, $ } from 'zx'

import { consoleBoxen } from './console_helpers.js'
import { getUserLogin } from './github.js'
import { resolveRes } from './prompts.js'
import type { Commit, Range } from './types.js'

export async function cherryPickCommits(commits: Commit[], {
range,
afterCherryPick,
}: {
range: Range
afterCherryPick?: (commit: Commit) => Promise<void>
}) {
const login = await getUserLogin()

for (let i = 0; i < commits.length; i++) {
const commit = commits[i]
consoleBoxen(`🧮 Triaging ${i + 1} of ${commits.length}`, commit.line)

while (true) {
const res = resolveRes(await question('Ok to cherry pick? [Y/n/o(pen)] > '))

if (res === 'open') {
if (commit.url) {
await $`open ${commit.url}`
} else {
console.log("There's no PR associated with this commit")
}
continue
} else if (res === 'no') {
let res = await question('Add a note explaining why not > ')
res = `(${login}) ${res}`
await $`git notes add -m ${res} ${commit.hash}`
await $`git notes show ${commit.hash}`
console.log(`You can edit the note with \`git notes edit ${commit.hash}\``)
break
}

try {
await $`git switch ${range.to}`
await $`git cherry-pick ${commit.hash}`
console.log()
console.log(chalk.green('🌸 Successfully cherry picked'))
await afterCherryPick?.(commit)
break
} catch (error) {
console.log()
console.log(chalk.yellow("✋ Couldn't cleanly cherry pick. Resolve the conflicts and run `git cherry-pick --continue`"))
await question('Press anything to continue > ')
await afterCherryPick?.(commit)
break
}
}

console.log()
}

consoleBoxen('🏁 Finish!', `All ${commits.length} commits have been triaged`)
}

export function reportCommitsEligibleForCherryPick(commits: Commit[]) {
consoleBoxen(`🧮 ${commits.length} commits to triage`, commits.map(commit => commit.line).join('\n'))
}
3 changes: 3 additions & 0 deletions lib/boxen.ts → lib/console_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import boxen from 'boxen'
import { chalk } from 'zx'

export function consoleBoxen(title: string, message: string) {
console.error(
Expand All @@ -15,3 +16,5 @@ export function consoleBoxen(title: string, message: string) {
})
)
}

export const separator = chalk.dim('-'.repeat(process.stdout.columns))
File renamed without changes.
5 changes: 0 additions & 5 deletions lib/debug_logger.ts

This file was deleted.

37 changes: 35 additions & 2 deletions lib/git.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import semver from 'semver'
import { chalk, $ } from 'zx'

import { CustomError } from './error.js'
import { CustomError } from './custom_error.js'
import { unwrap } from './zx_helpers.js'

/** Gets release branches (e.g. `release/major/v7.0.0`, etc.) */
Expand All @@ -13,7 +13,7 @@ export async function getReleaseBranches() {

const releaseBranches = releaseBranchesStdout
.split('\n')
.map((branch) => branch.trim())
.map((branch) => branch.trim().replace('* ', ''))
.sort((releaseBranchA, releaseBranchB) => {
const [, , versionA] = releaseBranchA.split('/')
const [, , versionB] = releaseBranchB.split('/')
Expand All @@ -30,6 +30,7 @@ export async function assertWorkTreeIsClean() {
`The working tree at ${chalk.magenta(process.cwd())} isn't clean. Commit or stash your changes.`
);
}
console.log('✨ The working tree is clean')
}

export async function branchExists(branch: string) {
Expand All @@ -44,6 +45,7 @@ export async function assertBranchExists(branch: string) {
chalk.green(` git checkout -b ${branch} <your-redwood-remote>/${branch}`),
].join('\n'))
}
console.log(`🏠 The ${chalk.magenta(branch)} branch exists locally`)
}

export async function getRedwoodRemote() {
Expand All @@ -52,9 +54,40 @@ export async function getRedwoodRemote() {
for (const remote of remotes.split('\n')) {
const match = remote.match(/^(?<remote>.+)\s.+redwoodjs\/redwood/)
if (match?.groups) {
console.log(`📡 Got Redwood remote ${chalk.magenta(match.groups.remote)}`)
return match.groups.remote
}
}

throw new CustomError(`Couldn't find the remote for the Redwood monorepo.`)
}

export const commitRegExps = {
hash: /(?<hash>\w{40})\s/,
pr: /\(#(?<pr>\d+)\)$/,
annotatedTag: /^v\d.\d.\d$/,
}

/** Get a commit's hash */
export function getCommitHash(line: string) {
const match = line.match(commitRegExps.hash)

if (!match?.groups) {
throw new Error([
`Couldn't find a commit hash in the line "${line}"`,
"This most likely means that a line that's UI isn't being identified as such",
].join('\n'))
}

return match.groups.hash
}

/** Square brackets (`[` or `]`) in commit messages need to be escaped */
function sanitizeMessage(message: string) {
return message.replace('[', '\\[').replace(']', '\\]')
}

export async function commitIsInRef(ref: string, message: string) {
message = sanitizeMessage(message)
return unwrap(await $`git log ${ref} --oneline --grep ${message}`)
}
26 changes: 13 additions & 13 deletions lib/github.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { $ } from 'zx'
import { chalk, $ } from 'zx'

import { CustomError } from './error.js'
import { CustomError } from './custom_error.js'

/** Get the GitHub token from REDWOOD_GITHUB_TOKEN */
export function getGitHubToken() {
const gitHubToken = process.env.REDWOOD_GITHUB_TOKEN

if (!gitHubToken) {
throw new CustomError("The `REDWOOD_GITHUB_TOKEN` environment variable isn't set")
}

return gitHubToken
}

export async function gqlGitHub({ query, variables }: { query: string; variables?: Record<string, any> }) {
export function getGitHubFetchHeaders() {
const gitHubToken = getGitHubToken()
return {
'Content-Type': 'application/json',
'Authorization': `Bearer ${gitHubToken}`,
}
}

export async function gqlGitHub({ query, variables }: { query: string; variables?: Record<string, any> }) {
const headers = getGitHubFetchHeaders()
const res = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${gitHubToken}`,
},
headers,
body: JSON.stringify({
query,
variables,
}),
})

const body = await res.json()

return body
}

export async function getUserLogin() {
const { data } = await gqlGitHub({ query: `query { viewer { login } }`})
const { data } = await gqlGitHub({ query: `query { viewer { login } }` })
return data.viewer.login
}

Expand All @@ -54,6 +53,7 @@ export async function pushBranch(branch: string, remote: string) {
*/
export async function fetchNotes(remote: string) {
await $`git fetch ${remote} 'refs/notes/*:refs/notes/*'`
console.log(`Fetched notes from ${remote}`)
}

export async function pushNotes(remote: string) {
Expand Down
3 changes: 2 additions & 1 deletion lib/cwd.ts → lib/set_cwd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cd, chalk, fs } from "zx"

import { CustomError } from './error.js'
import { CustomError } from './custom_error.js'

export async function setCwd() {
let RWFW_PATH = process.env.RWFW_PATH;
Expand Down Expand Up @@ -30,5 +30,6 @@ export async function setCwd() {

const originalCwd = process.cwd()
cd(RWFW_PATH)
console.log(`📂 Working in ${chalk.magenta(RWFW_PATH)}`)
return () => cd(originalCwd)
}
File renamed without changes.
23 changes: 13 additions & 10 deletions release/lib/milestones.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ describe('getPrsWithMilestone', () => {
expect(prs).toMatchInlineSnapshot(`
[
{
"id": "PR_kwDOC2M2f85nyT1x",
"id": "PR_kwDOC2M2f85nkDDh",
"mergeCommit": {
"messageHeadline": "Update studio.md (#10062)",
"messageHeadline": "Update MetaTags to be MetaData in Docs (#10053)",
},
"number": 10062,
"title": "Update studio.md",
"url": "https://github.com/redwoodjs/redwood/pull/10062",
"mergedAt": "2024-02-22T17:44:49Z",
"number": 10053,
"title": "Update MetaTags to be Metadata in Docs",
"url": "https://github.com/redwoodjs/redwood/pull/10053",
},
{
"id": "PR_kwDOC2M2f85nszPR",
"mergeCommit": {
"messageHeadline": "fix(render): reduce memory and handle server file (#10055)",
},
"mergedAt": "2024-02-23T10:04:32Z",
"number": 10055,
"title": "fix(render): reduce memory and handle server file ",
"url": "https://github.com/redwoodjs/redwood/pull/10055",
},
{
"id": "PR_kwDOC2M2f85nkDDh",
"id": "PR_kwDOC2M2f85nyT1x",
"mergeCommit": {
"messageHeadline": "Update MetaTags to be MetaData in Docs (#10053)",
"messageHeadline": "Update studio.md (#10062)",
},
"number": 10053,
"title": "Update MetaTags to be Metadata in Docs",
"url": "https://github.com/redwoodjs/redwood/pull/10053",
"mergedAt": "2024-02-24T01:21:51Z",
"number": 10062,
"title": "Update studio.md",
"url": "https://github.com/redwoodjs/redwood/pull/10062",
},
]
`)
Expand Down
Loading

0 comments on commit 42bcd84

Please sign in to comment.