@@ -2,7 +2,17 @@ package cc.unitmesh.devti.observer
2
2
3
3
import cc.unitmesh.devti.observer.agent.AgentProcessor
4
4
import cc.unitmesh.devti.provider.observer.AgentObserver
5
+ import cc.unitmesh.devti.flow.kanban.impl.GitHubIssue
6
+ import cc.unitmesh.devti.flow.model.SimpleStory
7
+ import cc.unitmesh.devti.settings.devops.devopsPromptsSettings
8
+ import com.intellij.openapi.diagnostic.logger
5
9
import com.intellij.openapi.project.Project
10
+ import com.intellij.openapi.vcs.ProjectLevelVcsManager
11
+ import com.intellij.dvcs.repo.VcsRepositoryManager
12
+ import git4idea.repo.GitRepository
13
+ import com.intellij.openapi.vcs.VcsRoot
14
+ import com.intellij.util.concurrency.AppExecutorUtil
15
+ import java.util.concurrent.TimeUnit
6
16
7
17
/* *
8
18
* Remote Hook observer will receive the remote hook event and process it.
@@ -11,14 +21,108 @@ import com.intellij.openapi.project.Project
11
21
* - [ ] GitHub/Gitlab issue
12
22
* and Trigger after processor, and send the notification to the chat window.
13
23
*/
14
- class RemoteHookObserver : AgentObserver {
24
+ open class RemoteHookObserver : AgentObserver {
25
+ private var issueWorker: IssueWorker ? = null
26
+
15
27
override fun onRegister (project : Project ) {
16
- // TODO("Not yet implemented")
28
+ // issueWorker = IssueWorker(project)
29
+ // // Schedule periodic checks for new issues
30
+ // AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(
31
+ // { issueWorker?.process() },
32
+ // 0, 30, TimeUnit.MINUTES
33
+ // )
17
34
}
18
35
}
19
36
20
- class IssueWorker : AgentProcessor {
37
+ class IssueWorker (private val project : Project ) : AgentProcessor {
38
+ private val log = logger<IssueWorker >()
39
+ private val processedIssues = mutableSetOf<String >()
40
+
21
41
override fun process () {
22
- // TODO("Not yet implemented")
42
+ try {
43
+ // Get git repositories in the project
44
+ val repositories = getGitRepositories()
45
+ if (repositories.isEmpty()) {
46
+ log.debug(" No Git repositories found in the project" )
47
+ return
48
+ }
49
+
50
+ for (repository in repositories) {
51
+ processRepository(repository)
52
+ }
53
+ } catch (e: Exception ) {
54
+ log.warn(" Error processing GitHub/GitLab issues" , e)
55
+ }
23
56
}
24
- }
57
+
58
+ private fun processRepository (repository : GitRepository ) {
59
+ val remoteUrl = repository.remotes.firstOrNull()?.firstUrl ? : return
60
+
61
+ if (remoteUrl.contains(" github.com" )) {
62
+ processGitHubIssues(remoteUrl)
63
+ } else if (remoteUrl.contains(" gitlab" )) {
64
+ // GitLab implementation would go here
65
+ log.debug(" GitLab integration not yet implemented" )
66
+ }
67
+ }
68
+
69
+ private fun processGitHubIssues (repoUrl : String ) {
70
+ val token = project.devopsPromptsSettings.state.githubToken
71
+ if (token.isBlank()) {
72
+ log.debug(" GitHub token not configured" )
73
+ return
74
+ }
75
+
76
+ try {
77
+ val github = GitHubIssue (repoUrl, token)
78
+ // In a real implementation, we'd fetch recent issues and filter for new ones
79
+ // For now, let's assume we have a way to get the latest issue ID
80
+ val latestIssueId = getLatestIssueId(github)
81
+
82
+ if (latestIssueId != null && ! processedIssues.contains(latestIssueId)) {
83
+ val story = github.getStoryById(latestIssueId)
84
+ if (story != null ) {
85
+ processedIssues.add(latestIssueId)
86
+ notifyNewIssue(story)
87
+ }
88
+ }
89
+ } catch (e: Exception ) {
90
+ log.warn(" Error processing GitHub issues for repo: $repoUrl " , e)
91
+ }
92
+ }
93
+
94
+ private fun getLatestIssueId (github : GitHubIssue ): String? {
95
+ // This is a placeholder - in a complete implementation,
96
+ // we would query the API for the latest issues
97
+ // For now, return null to indicate no new issues
98
+ return null
99
+ }
100
+
101
+ private fun notifyNewIssue (story : SimpleStory ) {
102
+ val prompt = buildIssuePrompt(story)
103
+ // sendErrorNotification(project, prompt)
104
+ }
105
+
106
+ private fun buildIssuePrompt (story : SimpleStory ): String {
107
+ return """
108
+ New issue received: #${story.id} - ${story.title}
109
+
110
+ ```
111
+ ${story.description}
112
+ ```
113
+
114
+ How would you like to proceed with this issue?
115
+ """ .trimIndent()
116
+ }
117
+
118
+ private fun getGitRepositories (): List <GitRepository > {
119
+ val vcsManager = ProjectLevelVcsManager .getInstance(project)
120
+ val roots: Array <VcsRoot > = vcsManager.allVcsRoots
121
+ val repositoryManager = VcsRepositoryManager .getInstance(project)
122
+
123
+ return roots.mapNotNull { root ->
124
+ val repo = repositoryManager.getRepositoryForRoot(root.path)
125
+ if (repo is GitRepository ) repo else null
126
+ }
127
+ }
128
+ }
0 commit comments