Skip to content

Commit

Permalink
Merge pull request #491 from bombillazo/master
Browse files Browse the repository at this point in the history
[Feat] Add --create-issue character limit check
  • Loading branch information
orta authored Dec 16, 2024
2 parents 40c0035 + fe46169 commit bd2e9a4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
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://github.com/${vcs?.org}/${vcs?.repo}/issues/new?${stringify({
title: "",
body: `Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used [patch-package](https://github.com/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://github.com/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://github.com/${vcs.org}/${vcs.repo}/issues/new?${stringify({
title: "",
body: `Hi! 👋
Firstly, thanks for your work on this project! 🙂
Today I used [patch-package](https://github.com/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://github.com/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

0 comments on commit bd2e9a4

Please sign in to comment.