Skip to content

chore(changesets): Automatically find PR number #10377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2024
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
2 changes: 1 addition & 1 deletion tasks/changesets/changesets.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function main() {
return
}

const { prNumber } = resolveArgv()
const { prNumber } = await resolveArgv()

const changesetFilePath = getChangesetFilePath(prNumber)
const placeholder = await getPlaceholder(prNumber)
Expand Down
67 changes: 51 additions & 16 deletions tasks/changesets/changesetsHelpers.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { fileURLToPath } from 'node:url'

import { humanId } from 'human-id'
import { argv, path, fs } from 'zx'
import { $, argv, path, fs, ProcessPromise } from 'zx'

$.verbose = false

const ROOT_DIR_PATH = fileURLToPath(new URL('../../', import.meta.url))
const DIRNAME = path.dirname(fileURLToPath(new URL(import.meta.url)))
Expand All @@ -14,8 +16,9 @@ Usage: yarn changesets [prNumber]
prNumber: A PR number. If provided, the changeset will use the PR's title and body as its placeholder.

Examples:
yarn changesets # Create a changeset with the default placeholder
yarn changesets 10075 # Create a changeset with PR 10075's title and body
yarn changesets # Tries to determine the PR based on the current branch name.
# If no PR is found, it uses the default placeholder.
yarn changesets 10075 # Create a changeset with PR 10075's title and body.
`)
}

Expand All @@ -42,12 +45,31 @@ export async function getPlaceholder(prNumber?: number) {
return getDefaultPlaceholder()
}

export function resolveArgv() {
export async function resolveArgv() {
const maybePrNumber = argv._[0]
if (!maybePrNumber) {
return { prNumber: undefined }
const currentBranch = await getStdout($`git branch --show-current`)

const url =
'https://api.github.com/repos/redwoodjs/redwood/pulls?state=open&sort=updated&direction=desc&per_page=100'
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${getGitHubToken()}`,
['X-GitHub-Api-Version']: '2022-11-28',
Accept: 'application/vnd.github+json',
},
})
const prs = await res.json()
const pr = prs.find(
(pr: { head: { ref: string } }) => pr.head.ref === currentBranch,
)

return { prNumber: pr?.number ?? undefined }
}
if (typeof maybePrNumber === 'string' && maybePrNumber.startsWith('#')) {
if (
typeof maybePrNumber === 'string' &&
maybePrNumber.startsWith('#')
) {
return { prNumber: +maybePrNumber.replace('#', '') }
}
if (typeof maybePrNumber === 'number') {
Expand Down Expand Up @@ -86,20 +108,11 @@ async function fetchFromGitHub({
query: string
variables: Record<string, any>
}) {
const token = process.env.GITHUB_TOKEN || process.env.REDWOOD_GITHUB_TOKEN

if (!token) {
throw new Error(
'\nNo GitHub token found. Please set GITHUB_TOKEN or ' +
'REDWOOD_GITHUB_TOKEN',
)
}

const res = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
Authorization: `Bearer ${getGitHubToken()}`,
},
body: JSON.stringify({
query,
Expand Down Expand Up @@ -154,3 +167,25 @@ function getPlaceholderForPr(pr: PR) {
pr.body,
].join('\n')
}

async function getStdout(cmd: ProcessPromise) {
const res = await cmd
if (res.exitCode !== 0) {
throw new Error(`Command failed (${res.exitCode}): ${res.stderr}`)
}

return res.stdout.trim()
}

function getGitHubToken() {
const token = process.env.GITHUB_TOKEN || process.env.REDWOOD_GITHUB_TOKEN

if (!token) {
throw new Error(
'\nNo GitHub token found. Please set GITHUB_TOKEN or ' +
'REDWOOD_GITHUB_TOKEN',
)
}

return token
}