Skip to content

Commit e6839cf

Browse files
authored
Ensure git identity is created for prs (#555)
1 parent 3e9fa3b commit e6839cf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/commands/fix/git.mts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import {
1212
getSocketDevPackageOverviewUrlFromPurl
1313
} from '../../utils/socket-url.mts'
1414

15+
const GITHUB_ACTIONS_BOT_USERNAME = 'github-actions[bot]'
16+
const GITHUB_ACTIONS_BOT_EMAIL = `${GITHUB_ACTIONS_BOT_USERNAME}@users.noreply.github.com`
17+
1518
function formatBranchName(str: string): string {
1619
return str
1720
.replace(/[-_.\\/]+/g, '-')
@@ -113,6 +116,7 @@ export async function gitCreateAndPushBranchIfNeeded(
113116
logger.warn('Nothing to commit, skipping push.')
114117
return false
115118
}
119+
await gitEnsureIdentity(cwd)
116120
await spawn('git', ['checkout', '-b', branch], { cwd })
117121
await spawn('git', ['add', ...moddedFilepaths], { cwd })
118122
await spawn('git', ['commit', '-m', commitMsg], { cwd })
@@ -133,6 +137,33 @@ export async function gitCreateAndPushBranchIfNeeded(
133137
return false
134138
}
135139

140+
export async function gitEnsureIdentity(cwd = process.cwd()): Promise<void> {
141+
let hasUserName = false
142+
try {
143+
const { stdout } = await spawn('git', ['config', '--get', 'user.name'], {
144+
cwd
145+
})
146+
hasUserName = !!stdout.trim()
147+
} catch {}
148+
if (!hasUserName) {
149+
await spawn('git', ['config', 'user.name', GITHUB_ACTIONS_BOT_USERNAME], {
150+
cwd
151+
})
152+
}
153+
let hasUserEmail = false
154+
try {
155+
const { stdout } = await spawn('git', ['config', '--get', 'user.email'], {
156+
cwd
157+
})
158+
hasUserEmail = !!stdout.trim()
159+
} catch {}
160+
if (!hasUserEmail) {
161+
await spawn('git', ['config', 'user.email', GITHUB_ACTIONS_BOT_EMAIL], {
162+
cwd
163+
})
164+
}
165+
}
166+
136167
export async function gitResetAndClean(
137168
branch = 'HEAD',
138169
cwd = process.cwd()

0 commit comments

Comments
 (0)