-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathopen-pr.ts
More file actions
170 lines (155 loc) · 4.86 KB
/
open-pr.ts
File metadata and controls
170 lines (155 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Octokit } from '@octokit/rest'
import { logger } from '@socketsecurity/registry/lib/logger'
import { spawn } from '@socketsecurity/registry/lib/spawn'
import constants from '../../constants'
import type { components } from '@octokit/openapi-types'
import type { OctokitResponse } from '@octokit/types'
type PullsCreateResponseData = components['schemas']['pull-request']
const {
GITHUB_ACTIONS,
GITHUB_REF_NAME,
GITHUB_REPOSITORY,
SOCKET_SECURITY_GITHUB_PAT
} = constants
async function branchExists(
branch: string,
cwd: string | undefined = process.cwd()
): Promise<boolean> {
try {
await spawn(
'git',
['show-ref', '--verify', '--quiet', `refs/heads/${branch}`],
{
cwd,
stdio: 'ignore'
}
)
return true
} catch {}
return false
}
async function checkoutBaseBranchIfAvailable(
baseBranch: string,
cwd: string | undefined = process.cwd()
) {
try {
const currentBranch = (
await spawn('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd })
).stdout.trim()
if (currentBranch === baseBranch) {
logger.info(`Already on ${baseBranch}`)
return
}
logger.info(`Switching branch from ${currentBranch} to ${baseBranch}...`)
await spawn('git', ['checkout', baseBranch], { cwd })
logger.info(`Checked out ${baseBranch}`)
} catch {
logger.warn(`Could not switch to ${baseBranch}. Proceeding with HEAD.`)
}
}
type GitHubRepoInfo = {
owner: string
repo: string
}
function getGitHubRepoInfo(): GitHubRepoInfo {
// Lazily access constants.ENV[GITHUB_REPOSITORY].
const ownerSlashRepo = constants.ENV[GITHUB_REPOSITORY]
const slashIndex = ownerSlashRepo.indexOf('/')
if (slashIndex === -1) {
throw new Error('GITHUB_REPOSITORY environment variable not set')
}
return {
owner: ownerSlashRepo.slice(0, slashIndex),
repo: ownerSlashRepo.slice(slashIndex + 1)
}
}
let _octokit: Octokit | undefined
function getOctokit() {
if (_octokit === undefined) {
_octokit = new Octokit({
// Lazily access constants.ENV[SOCKET_SECURITY_GITHUB_PAT].
auth: constants.ENV[SOCKET_SECURITY_GITHUB_PAT]
})
}
return _octokit
}
export async function enableAutoMerge(
prResponseData: PullsCreateResponseData
): Promise<void> {
const octokit = getOctokit()
const { node_id: prId, number: prNumber } = prResponseData
try {
await octokit.graphql(
`
mutation EnableAutoMerge($pullRequestId: ID!) {
enablePullRequestAutoMerge(input: {
pullRequestId: $pullRequestId,
mergeMethod: SQUASH
}) {
pullRequest {
number
autoMergeRequest {
enabledAt
}
}
}
}
`,
{
pullRequestId: prId
}
)
logger.info(`Auto-merge enabled for PR #${prNumber}`)
} catch (e) {
logger.error(`Failed to enable auto-merge for PR #${prNumber}:`, e)
}
}
export async function openGitHubPullRequest(
name: string,
targetVersion: string,
cwd = process.cwd()
): Promise<OctokitResponse<PullsCreateResponseData>> {
// Lazily access constants.ENV[GITHUB_ACTIONS].
if (constants.ENV[GITHUB_ACTIONS]) {
// Lazily access constants.ENV[SOCKET_SECURITY_GITHUB_PAT].
const pat = constants.ENV[SOCKET_SECURITY_GITHUB_PAT]
if (!pat) {
throw new Error('Missing SOCKET_SECURITY_GITHUB_PAT environment variable')
}
const baseBranch =
// Lazily access constants.ENV[GITHUB_REF_NAME].
constants.ENV[GITHUB_REF_NAME] ??
// GitHub defaults to branch name "main"
// 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
'main'
const branch = `socket-fix-${name}-${targetVersion.replace(/\./g, '-')}`
const commitMsg = `chore: upgrade ${name} to ${targetVersion}`
const { owner, repo } = getGitHubRepoInfo()
const url = `https://x-access-token:${pat}@github.com/${owner}/${repo}`
await spawn('git', ['remote', 'set-url', 'origin', url], {
cwd
})
if (await branchExists(branch, cwd)) {
logger.warn(`Branch "${branch}" already exists. Skipping creation.`)
} else {
await checkoutBaseBranchIfAvailable(baseBranch, cwd)
await spawn('git', ['checkout', '-b', branch], { cwd })
await spawn('git', ['add', 'package.json', 'pnpm-lock.yaml'], { cwd })
await spawn('git', ['commit', '-m', commitMsg], { cwd })
await spawn('git', ['push', '--set-upstream', 'origin', branch], { cwd })
}
const octokit = getOctokit()
return await octokit.pulls.create({
owner,
repo,
title: commitMsg,
head: branch,
base: baseBranch,
body: `[socket] Upgrade \`${name}\` to ${targetVersion}`
})
} else {
throw new Error(
'Unsupported CI platform or missing GITHUB_ACTIONS environment variable'
)
}
}