Skip to content
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

[Feat] Add --create-issue character limit check #491

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
90 changes: 67 additions & 23 deletions src/createIssue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import { join, resolve } from "./path"
const repoSpecifier = /^([\w.-]+)\/([\w.-]+)$/
const githubURL = /github.com(:|\/)([\w.-]+\/[\w.-]+?)(.git|\/.*)?$/

function parseRepoString(
repository: string,
): null | { repo: string; org: string; provider: "GitHub" } {
type VCS =
| {
repo: string
org: string
provider: "GitHub"
}
| null
| undefined

function parseRepoString(repository: string): VCS {
if (repository.startsWith("github:")) {
repository = repository.replace(/^github:/, "")
}
Expand All @@ -29,7 +36,7 @@ function parseRepoString(
return { org, repo, provider: "GitHub" }
}

export function getPackageVCSDetails(packageDetails: PackageDetails) {
export function getPackageVCSDetails(packageDetails: PackageDetails): VCS {
const repository = require(resolve(join(packageDetails.path, "package.json")))
.repository as undefined | string | { url: string }

Expand All @@ -46,6 +53,38 @@ export function getPackageVCSDetails(packageDetails: PackageDetails) {
}
}

function createIssueUrl({
vcs,
packageDetails,
packageVersion,
diff,
}: {
vcs: VCS
packageDetails: PackageDetails
packageVersion: string
diff: string
}): string {
return `https:/${vcs?.org}/${vcs?.repo}/issues/new?${stringify({
title: "",
body: `Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used [patch-package](https:/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on.

<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with a description of your problem, and any other relevant context 🔺️🔺️🔺️ -->

Here is the diff that solved my problem:

\`\`\`diff
${diff}
\`\`\`

<em>This issue body was [partially generated by patch-package](https:/ds300/patch-package/issues/296).</em>
`,
})}`
}

export function shouldRecommendIssue(
vcsDetails: ReturnType<typeof getPackageVCSDetails>,
) {
Expand Down Expand Up @@ -81,10 +120,12 @@ export function openIssueCreationLink({
packageDetails,
patchFileContents,
packageVersion,
patchPath,
}: {
packageDetails: PackageDetails
patchFileContents: string
packageVersion: string
patchPath: string
}) {
const vcs = getPackageVCSDetails(packageDetails)

Expand All @@ -100,25 +141,28 @@ export function openIssueCreationLink({
patchFileContents = patchFileContents.slice(0, -1)
}

open(
`https:/${vcs.org}/${vcs.repo}/issues/new?${stringify({
title: "",
body: `Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used [patch-package](https:/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on.

<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with a description of your problem, and any other relevant context 🔺️🔺️🔺️ -->

Here is the diff that solved my problem:
let issueUrl = createIssueUrl({
vcs,
packageDetails,
packageVersion,
diff: patchFileContents,
})

\`\`\`diff
${patchFileContents}
\`\`\`
const urlExceedsLimit = patchFileContents.length > 1950

<em>This issue body was [partially generated by patch-package](https:/ds300/patch-package/issues/296).</em>
`,
})}`,
)
if (urlExceedsLimit) {
const diffMessage = `<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with the diff contents of ${patchPath
.split("/")
.pop()}. 🔺️🔺️🔺️ -->`
console.log(
`📋 Copy the contents in [ ${patchPath} ] and paste it in the new issue's diff section.`,
)
issueUrl = createIssueUrl({
vcs,
packageDetails,
packageVersion,
diff: diffMessage,
})
}
open(issueUrl)
}
3 changes: 2 additions & 1 deletion src/makePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export function makePatch({
sequenceNumber,
})

const patchPath = join(patchesDir, patchFileName)
const patchPath: string = join(patchesDir, patchFileName)
if (!existsSync(dirname(patchPath))) {
// scoped package
mkdirSync(dirname(patchPath))
Expand Down Expand Up @@ -538,6 +538,7 @@ export function makePatch({
packageDetails,
patchFileContents: diffResult.stdout.toString(),
packageVersion,
patchPath,
})
} else {
maybePrintIssueCreationPrompt(vcs, packageDetails, packageManager)
Expand Down