#feature: improve Jira planning and release branch creation

This commit is contained in:
2026-05-29 14:25:43 +08:00
parent ade18a0aa8
commit 4875031cc3
4 changed files with 213 additions and 31 deletions
+21 -20
View File
@@ -620,6 +620,8 @@ class GitMonitorService
private function ensureReleaseBranchExists(string $repoKey, array $repoConfig, string $branch, ?string $description): void
{
$path = $this->resolveProjectPath($repoKey, $repoConfig);
$worktreePath = null;
$worktreeCreated = false;
if (!is_dir($path) || !is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
Log::warning('Invalid git repository path for branch creation', ['repository' => $repoKey, 'path' => $path]);
@@ -636,34 +638,27 @@ class GitMonitorService
$version = str_replace('release/', '', $branch);
try {
// 从 origin/master 创建新分支
// 在临时 worktree 中创建并推送分支,避免切换或修改用户正在工作的仓库。
$this->runGit($path, ['git', 'fetch', 'origin', 'master']);
// 创建本地分支(基于 origin/master
try {
// 先尝试删除可能存在的本地分支
$this->runGit($path, ['git', 'branch', '-D', $branch]);
} catch (ProcessFailedException) {
// 忽略,分支可能不存在
}
$this->runGit($path, ['git', 'checkout', '-b', $branch, 'origin/master']);
$worktreePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'toolbox-release-' . str_replace(['/', '\\'], '-', $repoKey . '-' . $version) . '-' . bin2hex(random_bytes(4));
$this->runGit($path, ['git', 'worktree', 'add', '--detach', $worktreePath, 'origin/master']);
$worktreeCreated = true;
// 修改 version.txt 文件
$versionFile = $path . DIRECTORY_SEPARATOR . 'version.txt';
$versionFile = $worktreePath . DIRECTORY_SEPARATOR . 'version.txt';
if (!file_put_contents($versionFile, $version)) {
throw new \RuntimeException("Failed to write version.txt");
}
// 添加并提交更改
$this->runGit($path, ['git', 'add', 'version.txt']);
$this->runGit($worktreePath, ['git', 'add', 'version.txt']);
// 构建提交信息:分支名 + 空格 + Jira 描述
$commitMessage = $branch . ($description ? ' ' . $description : '');
$this->runGit($path, ['git', 'commit', '-m', $commitMessage]);
$this->runGit($worktreePath, ['git', 'commit', '-m', $commitMessage]);
// 推送到远程
$this->runGit($path, ['git', 'push', '-u', 'origin', $branch]);
$this->runGit($worktreePath, ['git', 'push', 'origin', 'HEAD:refs/heads/' . $branch]);
Log::info('Created and pushed release branch', [
'repository' => $repoKey,
@@ -687,11 +682,17 @@ class GitMonitorService
'error' => $e->getMessage(),
]);
} finally {
// 切回 develop 分支,避免影响后续操作
try {
$this->runGit($path, ['git', 'checkout', self::DEVELOP_BRANCH]);
} catch (ProcessFailedException) {
// 忽略
if ($worktreeCreated && $worktreePath !== null) {
try {
$this->runGit($path, ['git', 'worktree', 'remove', '--force', $worktreePath]);
} catch (ProcessFailedException $e) {
Log::warning('Failed to remove temporary release worktree', [
'repository' => $repoKey,
'branch' => $branch,
'path' => $worktreePath,
'error' => $e->getMessage(),
]);
}
}
}
}