Skip to content

Commit ee5ad2a

Browse files
committed
refactor(devops): improve GitHub Actions generation safety
- Add try-catch error handling with logging - Improve variable naming (githubActions -> buildSystem) - Add null safety for project directory resolution - Reorganize imports for better readability
1 parent 0b547c4 commit ee5ad2a

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

core/src/main/kotlin/cc/unitmesh/devti/actions/GenerateGitHubActionsAction.kt

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package cc.unitmesh.devti.actions
22

33
import cc.unitmesh.devti.AutoDevBundle
4+
import cc.unitmesh.devti.actions.context.DevOpsContext
45
import cc.unitmesh.devti.custom.tasks.FileGenerateTask
56
import cc.unitmesh.devti.provider.BuildSystemProvider
67
import cc.unitmesh.devti.template.GENIUS_CICD
78
import cc.unitmesh.devti.template.TemplateRender
8-
import cc.unitmesh.devti.actions.context.DevOpsContext
99
import com.intellij.openapi.actionSystem.AnAction
1010
import com.intellij.openapi.actionSystem.AnActionEvent
11+
import com.intellij.openapi.diagnostic.logger
1112
import com.intellij.openapi.progress.ProgressManager
1213
import com.intellij.openapi.progress.Task
1314
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
@@ -18,20 +19,31 @@ class GenerateGitHubActionsAction : AnAction(AutoDevBundle.message("action.new.g
1819
override fun actionPerformed(e: AnActionEvent) {
1920
val project = e.project ?: return
2021

21-
// first, we need to guess language
22-
val githubActions = BuildSystemProvider.guess(project);
23-
val templateRender = TemplateRender(GENIUS_CICD)
24-
templateRender.context = DevOpsContext.from(githubActions)
25-
val template = templateRender.getTemplate("generate-github-action.vm")
22+
try {
23+
// 改进变量命名
24+
val buildSystem = BuildSystemProvider.guess(project)
25+
val templateRender = TemplateRender(GENIUS_CICD)
26+
templateRender.context = DevOpsContext.from(buildSystem)
27+
val template = templateRender.getTemplate("generate-github-action.vm")
28+
29+
// 安全的路径处理
30+
val projectDir = project.guessProjectDir()?.toNioPath()
31+
?: throw IllegalStateException("Cannot determine project directory")
32+
33+
val workflowDir = projectDir.resolve(".github").resolve("workflows")
34+
workflowDir.createDirectories()
2635

27-
project.guessProjectDir()!!.toNioPath().resolve(".github").resolve("workflows")
28-
.createDirectories()
36+
val msgs = templateRender.buildMsgs(template)
37+
val task: Task.Backgroundable = FileGenerateTask(project, msgs, "ci.yml")
2938

30-
val msgs = templateRender.buildMsgs(template)
39+
ProgressManager.getInstance()
40+
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
3141

32-
val task: Task.Backgroundable = FileGenerateTask(project, msgs, "ci.yml")
33-
ProgressManager.getInstance()
34-
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
42+
} catch (e: Exception) {
43+
// 添加错误处理,可以显示错误通知给用户
44+
logger<GenerateGitHubActionsAction>().error("Failed to generate GitHub Actions workflow", e)
45+
// 可以添加用户通知
46+
}
3547
}
3648
}
3749

0 commit comments

Comments
 (0)