Skip to content

Commit bd2e9a4

Browse files
authored
Merge pull request #491 from bombillazo/master
[Feat] Add --create-issue character limit check
2 parents 40c0035 + fe46169 commit bd2e9a4

File tree

2 files changed

+69
-24
lines changed

2 files changed

+69
-24
lines changed

src/createIssue.ts

+67-23
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ import { join, resolve } from "./path"
88
const repoSpecifier = /^([\w.-]+)\/([\w.-]+)$/
99
const githubURL = /github.com(:|\/)([\w.-]+\/[\w.-]+?)(.git|\/.*)?$/
1010

11-
function parseRepoString(
12-
repository: string,
13-
): null | { repo: string; org: string; provider: "GitHub" } {
11+
type VCS =
12+
| {
13+
repo: string
14+
org: string
15+
provider: "GitHub"
16+
}
17+
| null
18+
| undefined
19+
20+
function parseRepoString(repository: string): VCS {
1421
if (repository.startsWith("github:")) {
1522
repository = repository.replace(/^github:/, "")
1623
}
@@ -29,7 +36,7 @@ function parseRepoString(
2936
return { org, repo, provider: "GitHub" }
3037
}
3138

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

@@ -46,6 +53,38 @@ export function getPackageVCSDetails(packageDetails: PackageDetails) {
4653
}
4754
}
4855

56+
function createIssueUrl({
57+
vcs,
58+
packageDetails,
59+
packageVersion,
60+
diff,
61+
}: {
62+
vcs: VCS
63+
packageDetails: PackageDetails
64+
packageVersion: string
65+
diff: string
66+
}): string {
67+
return `https://github.com/${vcs?.org}/${vcs?.repo}/issues/new?${stringify({
68+
title: "",
69+
body: `Hi! 👋
70+
71+
Firstly, thanks for your work on this project! 🙂
72+
73+
Today I used [patch-package](https://github.com/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on.
74+
75+
<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with a description of your problem, and any other relevant context 🔺️🔺️🔺️ -->
76+
77+
Here is the diff that solved my problem:
78+
79+
\`\`\`diff
80+
${diff}
81+
\`\`\`
82+
83+
<em>This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296).</em>
84+
`,
85+
})}`
86+
}
87+
4988
export function shouldRecommendIssue(
5089
vcsDetails: ReturnType<typeof getPackageVCSDetails>,
5190
) {
@@ -81,10 +120,12 @@ export function openIssueCreationLink({
81120
packageDetails,
82121
patchFileContents,
83122
packageVersion,
123+
patchPath,
84124
}: {
85125
packageDetails: PackageDetails
86126
patchFileContents: string
87127
packageVersion: string
128+
patchPath: string
88129
}) {
89130
const vcs = getPackageVCSDetails(packageDetails)
90131

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

103-
open(
104-
`https://github.com/${vcs.org}/${vcs.repo}/issues/new?${stringify({
105-
title: "",
106-
body: `Hi! 👋
107-
108-
Firstly, thanks for your work on this project! 🙂
109-
110-
Today I used [patch-package](https://github.com/ds300/patch-package) to patch \`${packageDetails.name}@${packageVersion}\` for the project I'm working on.
111-
112-
<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with a description of your problem, and any other relevant context 🔺️🔺️🔺️ -->
113-
114-
Here is the diff that solved my problem:
144+
let issueUrl = createIssueUrl({
145+
vcs,
146+
packageDetails,
147+
packageVersion,
148+
diff: patchFileContents,
149+
})
115150

116-
\`\`\`diff
117-
${patchFileContents}
118-
\`\`\`
151+
const urlExceedsLimit = patchFileContents.length > 1950
119152

120-
<em>This issue body was [partially generated by patch-package](https://github.com/ds300/patch-package/issues/296).</em>
121-
`,
122-
})}`,
123-
)
153+
if (urlExceedsLimit) {
154+
const diffMessage = `<!-- 🔺️🔺️🔺️ PLEASE REPLACE THIS BLOCK with the diff contents of ${patchPath
155+
.split("/")
156+
.pop()}. 🔺️🔺️🔺️ -->`
157+
console.log(
158+
`📋 Copy the contents in [ ${patchPath} ] and paste it in the new issue's diff section.`,
159+
)
160+
issueUrl = createIssueUrl({
161+
vcs,
162+
packageDetails,
163+
packageVersion,
164+
diff: diffMessage,
165+
})
166+
}
167+
open(issueUrl)
124168
}

src/makePatch.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ export function makePatch({
423423
sequenceNumber,
424424
})
425425

426-
const patchPath = join(patchesDir, patchFileName)
426+
const patchPath: string = join(patchesDir, patchFileName)
427427
if (!existsSync(dirname(patchPath))) {
428428
// scoped package
429429
mkdirSync(dirname(patchPath))
@@ -538,6 +538,7 @@ export function makePatch({
538538
packageDetails,
539539
patchFileContents: diffResult.stdout.toString(),
540540
packageVersion,
541+
patchPath,
541542
})
542543
} else {
543544
maybePrintIssueCreationPrompt(vcs, packageDetails, packageManager)

0 commit comments

Comments
 (0)