Skip to content

Commit 6e8292b

Browse files
committed
Don't pave over files when resetting the base branch
1 parent 5d5aa04 commit 6e8292b

File tree

4 files changed

+96
-65
lines changed

4 files changed

+96
-65
lines changed

src/commands/fix/git.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { logger } from '@socketsecurity/registry/lib/logger'
2+
import { spawn } from '@socketsecurity/registry/lib/spawn'
3+
4+
import constants from '../../constants'
5+
6+
const { GITHUB_REF_NAME } = constants
7+
8+
export async function branchExists(
9+
branch: string,
10+
cwd: string | undefined = process.cwd()
11+
): Promise<boolean> {
12+
try {
13+
await spawn(
14+
'git',
15+
['show-ref', '--verify', '--quiet', `refs/heads/${branch}`],
16+
{
17+
cwd,
18+
stdio: 'ignore'
19+
}
20+
)
21+
return true
22+
} catch {}
23+
return false
24+
}
25+
26+
export async function checkoutBaseBranchIfAvailable(
27+
baseBranch: string,
28+
cwd: string | undefined = process.cwd()
29+
) {
30+
try {
31+
await spawn('git', ['checkout', baseBranch], { cwd })
32+
await spawn('git', ['reset', '--hard', `origin/${baseBranch}`], { cwd })
33+
logger.info(`Checked out and reset to ${baseBranch}`)
34+
} catch {
35+
logger.warn(`Could not switch to ${baseBranch}. Proceeding with HEAD.`)
36+
}
37+
}
38+
39+
export async function createAndPushBranchIfNeeded(
40+
branch: string,
41+
commitMsg: string,
42+
cwd: string = process.cwd()
43+
): Promise<boolean> {
44+
if (await branchExists(branch, cwd)) {
45+
logger.warn(`Branch "${branch}" already exists. Skipping creation.`)
46+
return false
47+
}
48+
await spawn('git', ['checkout', '-b', branch], { cwd })
49+
await spawn('git', ['add', 'package.json', 'pnpm-lock.yaml'], { cwd })
50+
await spawn('git', ['commit', '-m', commitMsg], { cwd })
51+
await spawn('git', ['push', '--set-upstream', 'origin', branch], { cwd })
52+
return true
53+
}
54+
55+
export function getBaseBranch() {
56+
// Lazily access constants.ENV[GITHUB_REF_NAME].
57+
return (
58+
constants.ENV[GITHUB_REF_NAME] ??
59+
// GitHub defaults to branch name "main"
60+
// https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches#about-the-default-branch
61+
'main'
62+
)
63+
}
64+
65+
export function getSocketBranchName(name: string, version: string): string {
66+
return `socket-fix-${name}-${version.replace(/\./g, '-')}`
67+
}

src/commands/fix/npm-fix.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import {
77
readPackageJson
88
} from '@socketsecurity/registry/lib/packages'
99

10+
import {
11+
checkoutBaseBranchIfAvailable,
12+
getBaseBranch,
13+
getSocketBranchName
14+
} from './git'
1015
import {
1116
doesPullRequestExistForBranch,
1217
enableAutoMerge,
1318
getGitHubRepoInfo,
14-
getSocketBranchName,
1519
openGitHubPullRequest
1620
} from './open-pr'
1721
import { NormalizedFixOptions } from './types'
@@ -160,6 +164,13 @@ export async function npmFix(
160164

161165
spinner?.info(`Installing ${fixSpec}`)
162166

167+
const { owner, repo } = getGitHubRepoInfo()
168+
const baseBranch = getBaseBranch()
169+
const branch = getSocketBranchName(name, targetVersion)
170+
171+
// eslint-disable-next-line no-await-in-loop
172+
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
173+
163174
let installed = false
164175
let saved = false
165176
try {
@@ -200,8 +211,6 @@ export async function npmFix(
200211
return
201212
}
202213

203-
const { owner, repo } = getGitHubRepoInfo()
204-
const branch = getSocketBranchName(name, targetVersion)
205214
if (
206215
// Lazily access constants.ENV[CI].
207216
constants.ENV[CI] &&
@@ -214,6 +223,7 @@ export async function npmFix(
214223
prResponse = await openGitHubPullRequest(
215224
owner,
216225
repo,
226+
baseBranch,
217227
branch,
218228
name,
219229
targetVersion,

src/commands/fix/open-pr.ts

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,8 @@ import type { OctokitResponse } from '@octokit/types'
1010

1111
type PullsCreateResponseData = components['schemas']['pull-request']
1212

13-
const {
14-
GITHUB_ACTIONS,
15-
GITHUB_REF_NAME,
16-
GITHUB_REPOSITORY,
17-
SOCKET_SECURITY_GITHUB_PAT
18-
} = constants
19-
20-
async function branchExists(
21-
branch: string,
22-
cwd: string | undefined = process.cwd()
23-
): Promise<boolean> {
24-
try {
25-
await spawn(
26-
'git',
27-
['show-ref', '--verify', '--quiet', `refs/heads/${branch}`],
28-
{
29-
cwd,
30-
stdio: 'ignore'
31-
}
32-
)
33-
return true
34-
} catch {}
35-
return false
36-
}
37-
38-
async function checkoutBaseBranchIfAvailable(
39-
baseBranch: string,
40-
cwd: string | undefined = process.cwd()
41-
) {
42-
try {
43-
await spawn('git', ['checkout', baseBranch], { cwd })
44-
await spawn('git', ['reset', '--hard', `origin/${baseBranch}`], { cwd })
45-
logger.info(`Checked out and reset to ${baseBranch}`)
46-
} catch {
47-
logger.warn(`Could not switch to ${baseBranch}. Proceeding with HEAD.`)
48-
}
49-
}
13+
const { GITHUB_ACTIONS, GITHUB_REPOSITORY, SOCKET_SECURITY_GITHUB_PAT } =
14+
constants
5015

5116
type GitHubRepoInfo = {
5217
owner: string
@@ -125,13 +90,10 @@ export function getGitHubRepoInfo(): GitHubRepoInfo {
12590
}
12691
}
12792

128-
export function getSocketBranchName(name: string, version: string): string {
129-
return `socket-fix-${name}-${version.replace(/\./g, '-')}`
130-
}
131-
13293
export async function openGitHubPullRequest(
13394
owner: string,
13495
repo: string,
96+
baseBranch: string,
13597
branch: string,
13698
name: string,
13799
version: string,
@@ -144,30 +106,12 @@ export async function openGitHubPullRequest(
144106
if (!pat) {
145107
throw new Error('Missing SOCKET_SECURITY_GITHUB_PAT environment variable')
146108
}
147-
const baseBranch =
148-
// Lazily access constants.ENV[GITHUB_REF_NAME].
149-
constants.ENV[GITHUB_REF_NAME] ??
150-
// GitHub defaults to branch name "main"
151-
// https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches#about-the-default-branch
152-
'main'
153-
154109
const commitMsg = `chore: upgrade ${name} to ${version}`
155110
const url = `https://x-access-token:${pat}@github.com/${owner}/${repo}`
156111

157112
await spawn('git', ['remote', 'set-url', 'origin', url], {
158113
cwd
159114
})
160-
161-
if (await branchExists(branch, cwd)) {
162-
logger.warn(`Branch "${branch}" already exists. Skipping creation.`)
163-
} else {
164-
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
165-
await spawn('git', ['checkout', '-b', branch], { cwd })
166-
await spawn('git', ['add', 'package.json', 'pnpm-lock.yaml'], { cwd })
167-
await spawn('git', ['commit', '-m', commitMsg], { cwd })
168-
await spawn('git', ['push', '--set-upstream', 'origin', branch], { cwd })
169-
}
170-
171115
const octokit = getOctokit()
172116
return await octokit.pulls.create({
173117
owner,

src/commands/fix/pnpm-fix.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ import {
1010
readPackageJson
1111
} from '@socketsecurity/registry/lib/packages'
1212

13+
import {
14+
checkoutBaseBranchIfAvailable,
15+
getBaseBranch,
16+
getSocketBranchName
17+
} from './git'
1318
import {
1419
doesPullRequestExistForBranch,
1520
enableAutoMerge,
1621
getGitHubRepoInfo,
17-
getSocketBranchName,
1822
openGitHubPullRequest
1923
} from './open-pr'
2024
import { applyRange } from './shared'
@@ -203,6 +207,13 @@ export async function pnpmFix(
203207

204208
spinner?.info(`Installing ${fixSpec}`)
205209

210+
const { owner, repo } = getGitHubRepoInfo()
211+
const baseBranch = getBaseBranch()
212+
const branch = getSocketBranchName(name, targetVersion)
213+
214+
// eslint-disable-next-line no-await-in-loop
215+
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
216+
206217
let installed = false
207218
let saved = false
208219
try {
@@ -244,8 +255,6 @@ export async function pnpmFix(
244255
return
245256
}
246257

247-
const { owner, repo } = getGitHubRepoInfo()
248-
const branch = getSocketBranchName(name, targetVersion)
249258
if (
250259
// Lazily access constants.ENV[CI].
251260
constants.ENV[CI] &&
@@ -258,6 +267,7 @@ export async function pnpmFix(
258267
prResponse = await openGitHubPullRequest(
259268
owner,
260269
repo,
270+
baseBranch,
261271
branch,
262272
name,
263273
targetVersion,

0 commit comments

Comments
 (0)