Skip to content

Commit 0f3ede3

Browse files
committed
Use createAndPushBranchIfNeeded for npm/pnpm fix
1 parent b940b80 commit 0f3ede3

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

src/commands/fix/npm-fix.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ export async function npmFix(
146146
}
147147

148148
const targetVersion = node.package.version!
149+
150+
let branch: string | undefined
151+
let owner: string | undefined
152+
let repo: string | undefined
153+
let shouldOpenPr = false
154+
// Lazily access constants.ENV[CI].
155+
if (constants.ENV[CI]) {
156+
;({ owner, repo } = getGitHubRepoInfo())
157+
branch = getSocketBranchName(name, targetVersion)
158+
// eslint-disable-next-line no-await-in-loop
159+
shouldOpenPr = !(await doesPullRequestExistForBranch(
160+
owner,
161+
repo,
162+
branch
163+
))
164+
}
165+
149166
const fixSpec = `${name}@^${targetVersion}`
150167
const revertData = {
151168
...(editablePkgJson.content.dependencies
@@ -164,9 +181,7 @@ export async function npmFix(
164181

165182
spinner?.info(`Installing ${fixSpec}`)
166183

167-
const { owner, repo } = getGitHubRepoInfo()
168184
const baseBranch = getBaseBranch()
169-
const branch = getSocketBranchName(name, targetVersion)
170185

171186
// eslint-disable-next-line no-await-in-loop
172187
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
@@ -211,20 +226,15 @@ export async function npmFix(
211226
return
212227
}
213228

214-
if (
215-
// Lazily access constants.ENV[CI].
216-
constants.ENV[CI] &&
217-
// eslint-disable-next-line no-await-in-loop
218-
!(await doesPullRequestExistForBranch(owner, repo, branch))
219-
) {
229+
if (shouldOpenPr) {
220230
let prResponse
221231
try {
222232
// eslint-disable-next-line no-await-in-loop
223233
prResponse = await openGitHubPullRequest(
224-
owner,
225-
repo,
234+
owner!,
235+
repo!,
226236
baseBranch,
227-
branch,
237+
branch!,
228238
name,
229239
targetVersion,
230240
cwd

src/commands/fix/pnpm-fix.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212

1313
import {
1414
checkoutBaseBranchIfAvailable,
15+
createAndPushBranchIfNeeded,
1516
getBaseBranch,
1617
getSocketBranchName
1718
} from './git'
@@ -152,6 +153,22 @@ export async function pnpmFix(
152153
return
153154
}
154155

156+
let branch: string | undefined
157+
let owner: string | undefined
158+
let repo: string | undefined
159+
let shouldOpenPr = false
160+
// Lazily access constants.ENV[CI].
161+
if (constants.ENV[CI]) {
162+
;({ owner, repo } = getGitHubRepoInfo())
163+
branch = getSocketBranchName(name, targetVersion)
164+
// eslint-disable-next-line no-await-in-loop
165+
shouldOpenPr = !(await doesPullRequestExistForBranch(
166+
owner,
167+
repo,
168+
branch
169+
))
170+
}
171+
155172
const oldPnpm = editablePkgJson.content[PNPM] as
156173
| StringKeyValueObject
157174
| undefined
@@ -207,9 +224,7 @@ export async function pnpmFix(
207224

208225
spinner?.info(`Installing ${fixSpec}`)
209226

210-
const { owner, repo } = getGitHubRepoInfo()
211227
const baseBranch = getBaseBranch()
212-
const branch = getSocketBranchName(name, targetVersion)
213228

214229
// eslint-disable-next-line no-await-in-loop
215230
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
@@ -255,27 +270,30 @@ export async function pnpmFix(
255270
return
256271
}
257272

258-
if (
259-
// Lazily access constants.ENV[CI].
260-
constants.ENV[CI] &&
273+
if (shouldOpenPr) {
261274
// eslint-disable-next-line no-await-in-loop
262-
!(await doesPullRequestExistForBranch(owner, repo, branch))
263-
) {
275+
await createAndPushBranchIfNeeded(
276+
branch!,
277+
`fix: upgrade ${name} to ${targetVersion}`,
278+
cwd
279+
)
280+
264281
let prResponse
265282
try {
266283
// eslint-disable-next-line no-await-in-loop
267284
prResponse = await openGitHubPullRequest(
268-
owner,
269-
repo,
285+
owner!,
286+
repo!,
270287
baseBranch,
271-
branch,
288+
branch!,
272289
name,
273290
targetVersion,
274291
cwd
275292
)
276293
} catch (e) {
277294
logger.error('Failed to open pull request', e)
278295
}
296+
279297
if (prResponse && autoMerge) {
280298
try {
281299
// eslint-disable-next-line no-await-in-loop

0 commit comments

Comments
 (0)