Skip to content

Commit 7953fd7

Browse files
authored
Cleanup git and pr related methods (#586)
1 parent 5215a61 commit 7953fd7

File tree

4 files changed

+201
-134
lines changed

4 files changed

+201
-134
lines changed

src/commands/fix/git.mts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,21 @@ export async function gitCreateAndPushBranch(
106106
cwd = process.cwd(),
107107
): Promise<boolean> {
108108
const stdioIgnoreOptions: SpawnOptions = { cwd, stdio: 'ignore' }
109-
await gitEnsureIdentity(cwd)
110-
await spawn('git', ['checkout', '-b', branch], stdioIgnoreOptions)
111-
await spawn('git', ['add', ...filepaths], stdioIgnoreOptions)
112-
await spawn('git', ['commit', '-m', commitMsg], stdioIgnoreOptions)
113109
try {
110+
await gitEnsureIdentity(cwd)
111+
await spawn('git', ['checkout', '-b', branch], stdioIgnoreOptions)
112+
await spawn('git', ['add', ...filepaths], stdioIgnoreOptions)
113+
await spawn('git', ['commit', '-m', commitMsg], stdioIgnoreOptions)
114114
await spawn(
115115
'git',
116116
['push', '--force', '--set-upstream', 'origin', branch],
117117
stdioIgnoreOptions,
118118
)
119119
return true
120120
} catch {}
121-
await spawn('git', ['branch', '-D', branch], stdioIgnoreOptions)
121+
try {
122+
await spawn('git', ['branch', '-D', branch], stdioIgnoreOptions)
123+
} catch {}
122124
return false
123125
}
124126

src/commands/fix/npm-fix.mts

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getGitHubEnvRepoInfo,
2727
openPr,
2828
prExistForBranch,
29+
setGitRemoteGitHubRepoUrl,
2930
} from './open-pr.mts'
3031
import { getAlertMapOptions } from './shared.mts'
3132
import constants from '../../constants.mts'
@@ -127,8 +128,15 @@ export async function npmFix(
127128
return
128129
}
129130

130-
// Lazily access constants.ENV.CI.
131-
const isCi = constants.ENV.CI
131+
// Lazily access constants.ENV properties.
132+
const token =
133+
constants.ENV.SOCKET_SECURITY_GITHUB_PAT || constants.ENV.GITHUB_TOKEN
134+
const isCi = !!(
135+
constants.ENV.CI &&
136+
constants.ENV.GITHUB_ACTIONS &&
137+
constants.ENV.GITHUB_REPOSITORY &&
138+
token
139+
)
132140
const baseBranch = isCi ? getBaseGitBranch() : ''
133141
const workspacePkgJsonPaths = await globWorkspace(
134142
pkgEnvDetails.agent,
@@ -143,6 +151,7 @@ export async function npmFix(
143151
spinner?.stop()
144152

145153
let count = 0
154+
146155
const sortedInfoEntries = [...infoByPkgName.entries()].sort((a, b) =>
147156
naturalCompare(a[0], b[0]),
148157
)
@@ -164,7 +173,7 @@ export async function npmFix(
164173
// eslint-disable-next-line no-await-in-loop
165174
const packument = await fetchPackagePackument(name)
166175
if (!packument) {
167-
logger.warn(`Unexpected condition: No packument found for ${name}\n`)
176+
logger.warn(`Unexpected condition: No packument found for ${name}.\n`)
168177
logger.dedent()
169178
spinner?.dedent()
170179
continue infoEntriesLoop
@@ -296,7 +305,7 @@ export async function npmFix(
296305
}
297306

298307
spinner?.start()
299-
spinner?.info(`Installing ${newId} in ${workspaceName}`)
308+
spinner?.info(`Installing ${newId} in ${workspaceName}.`)
300309

301310
let error
302311
let errored = false
@@ -308,7 +317,7 @@ export async function npmFix(
308317
// eslint-disable-next-line no-await-in-loop
309318
await runScript(testScript, [], { spinner, stdio: 'ignore' })
310319
}
311-
spinner?.success(`Fixed ${name} in ${workspaceName}`)
320+
spinner?.success(`Fixed ${name} in ${workspaceName}.`)
312321
} catch (e) {
313322
errored = true
314323
error = e
@@ -317,11 +326,6 @@ export async function npmFix(
317326
spinner?.stop()
318327

319328
if (!errored && isCi) {
320-
const branch = getSocketBranchName(
321-
oldPurl,
322-
newVersion,
323-
workspaceName,
324-
)
325329
try {
326330
const moddedFilepaths =
327331
// eslint-disable-next-line no-await-in-loop
@@ -339,29 +343,28 @@ export async function npmFix(
339343
continue infosLoop
340344
}
341345

342-
const { owner, repo } = getGitHubEnvRepoInfo()
343-
// eslint-disable-next-line no-await-in-loop
344-
if (await prExistForBranch(owner, repo, branch)) {
345-
debugLog(`Branch "${branch}" exists, skipping PR creation.`)
346-
// eslint-disable-next-line no-await-in-loop
347-
await gitResetAndClean(baseBranch, cwd)
346+
const repoInfo = getGitHubEnvRepoInfo()!
347+
const branch = getSocketBranchName(
348+
oldPurl,
349+
newVersion,
350+
workspaceName,
351+
)
352+
353+
let skipPr = false
354+
if (
348355
// eslint-disable-next-line no-await-in-loop
349-
actualTree = await install(arb, { cwd })
350-
continue infosLoop
356+
await prExistForBranch(repoInfo.owner, repoInfo.repo, branch)
357+
) {
358+
skipPr = true
359+
debugLog(`Branch "${branch}" exists, skipping PR creation.`)
351360
}
352361
// eslint-disable-next-line no-await-in-loop
353-
if (await gitRemoteBranchExists(branch, cwd)) {
362+
else if (await gitRemoteBranchExists(branch, cwd)) {
363+
skipPr = true
354364
debugLog(
355365
`Remote branch "${branch}" exists, skipping PR creation.`,
356366
)
357-
// eslint-disable-next-line no-await-in-loop
358-
await gitResetAndClean(baseBranch, cwd)
359-
// eslint-disable-next-line no-await-in-loop
360-
actualTree = await install(arb, { cwd })
361-
continue infosLoop
362-
}
363-
364-
if (
367+
} else if (
365368
// eslint-disable-next-line no-await-in-loop
366369
!(await gitCreateAndPushBranch(
367370
branch,
@@ -370,23 +373,41 @@ export async function npmFix(
370373
cwd,
371374
))
372375
) {
376+
skipPr = true
373377
logger.warn(
374378
'Unexpected condition: Push failed, skipping PR creation.',
375379
)
380+
}
381+
if (skipPr) {
376382
// eslint-disable-next-line no-await-in-loop
377383
await gitResetAndClean(baseBranch, cwd)
378384
// eslint-disable-next-line no-await-in-loop
379385
actualTree = await install(arb, { cwd })
380386
continue infosLoop
381387
}
388+
382389
// eslint-disable-next-line no-await-in-loop
383-
await cleanupOpenPrs(owner, repo, oldPurl, newVersion, {
384-
workspaceName,
385-
})
390+
await Promise.allSettled([
391+
setGitRemoteGitHubRepoUrl(
392+
repoInfo.owner,
393+
repoInfo.repo,
394+
token,
395+
cwd,
396+
),
397+
cleanupOpenPrs(
398+
repoInfo.owner,
399+
repoInfo.repo,
400+
oldPurl,
401+
newVersion,
402+
{
403+
workspaceName,
404+
},
405+
),
406+
])
386407
// eslint-disable-next-line no-await-in-loop
387408
const prResponse = await openPr(
388-
owner,
389-
repo,
409+
repoInfo.owner,
410+
repoInfo.repo,
390411
branch,
391412
oldPurl,
392413
newVersion,
@@ -398,12 +419,23 @@ export async function npmFix(
398419
)
399420
if (prResponse) {
400421
const { data } = prResponse
401-
logger.success(`Opened PR #${data.number}`)
422+
const prRef = `PR #${data.number}`
423+
logger.success(`Opened ${prRef}.`)
402424
if (autoMerge) {
403425
logger.indent()
404426
spinner?.indent()
405427
// eslint-disable-next-line no-await-in-loop
406-
await enablePrAutoMerge(data)
428+
const { details, enabled } = await enablePrAutoMerge(data)
429+
if (enabled) {
430+
logger.info(`Auto-merge enabled for ${prRef}.`)
431+
} else {
432+
const message = `Failed to enable auto-merge for ${prRef}${
433+
details
434+
? `:\n${details.map(d => ` - ${d}`).join('\n')}`
435+
: '.'
436+
}`
437+
logger.error(message)
438+
}
407439
logger.dedent()
408440
spinner?.dedent()
409441
}

0 commit comments

Comments
 (0)