Skip to content

Commit

Permalink
feat(vcs): add PrepushReviewAction
Browse files Browse the repository at this point in the history
Add a new `PrepushReviewAction` class to handle pre-push code review. This action is triggered when the user initiates a push operation. It retrieves the selected changes and performs a code review using the `doReviewWithChanges` method. The `doReviewWithChanges` method takes the project, commit details, selected changes, and associated stories as parameters and prompts the user for a commit message. If there are no commit details, the commit message prompt is skipped. The changes are then displayed, and the user is prompted to review the code. The code review is performed using the `doCodeReview` method, which renders a code review template and displays it to the user.
  • Loading branch information
phodal committed Jan 11, 2024
1 parent 6ccb3e7 commit d16bb38
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/actions/vcs/CodeReviewAction.kt
Expand Up @@ -20,16 +20,18 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.VcsDataKeys
import com.intellij.openapi.vcs.changes.Change
import com.intellij.vcs.log.VcsFullCommitDetails
import com.intellij.vcs.log.VcsLogDataKeys
import git4idea.repo.GitRepository
import kotlinx.coroutines.runBlocking
import org.changelog.CommitParser
import java.nio.file.FileSystems
import java.nio.file.PathMatcher

val githubUrlRegex: Regex = Regex("^(https?://|git://)?(www\\.)?github\\.com/[\\w-]+/[\\w-]+(/.*)?\$")

class CodeReviewAction : ChatBaseAction() {
open class CodeReviewAction : ChatBaseAction() {
override fun getActionType(): ChatActionType = ChatActionType.CODE_REVIEW

private val commitParser: CommitParser = CommitParser()
Expand Down Expand Up @@ -61,6 +63,15 @@ class CodeReviewAction : ChatBaseAction() {
stories = fetchKanbanByCommits(repository, details)
}, "Prepare Repository", true, project)

doReviewWithChanges(project, details, selectList, stories)
}

fun doReviewWithChanges(
project: Project,
details: List<VcsFullCommitDetails>,
selectList: Array<out Change>,
stories: List<String>
) {
val vcsPrompting = project.service<VcsPrompting>()
val fullChangeContent =
vcsPrompting.buildDiffPrompt(details, selectList.toList(), project, defaultIgnoreFilePatterns)
Expand All @@ -73,7 +84,7 @@ class CodeReviewAction : ChatBaseAction() {
val creationContext =
ChatCreationContext(ChatOrigin.Intention, getActionType(), null, listOf(), null)

val contextItems: List<ChatContextItem> = kotlinx.coroutines.runBlocking {
val contextItems: List<ChatContextItem> = runBlocking {
return@runBlocking ChatContextProvider.collectChatContextList(project, creationContext)
}

Expand All @@ -86,7 +97,7 @@ class CodeReviewAction : ChatBaseAction() {
doCodeReview(project, context)
}

private fun doCodeReview(project: Project, context: CodeReviewContext) {
fun doCodeReview(project: Project, context: CodeReviewContext) {
val templateRender = TemplateRender("genius/practises")
val template = templateRender.getTemplate("code-review.vm")
templateRender.context = context
Expand Down
@@ -0,0 +1,14 @@
package cc.unitmesh.devti.actions.vcs

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.vcs.VcsDataKeys

class PrepushReviewAction : CodeReviewAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return

val selectList = event.getData(VcsDataKeys.CHANGES) ?: return

doReviewWithChanges(project, listOf(), selectList, listOf())
}
}
8 changes: 5 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/prompting/VcsPrompting.kt
Expand Up @@ -118,10 +118,12 @@ class VcsPrompting(private val project: Project) {
project: Project,
ignoreFilePatterns: List<PathMatcher> = listOf(),
): String? {
val writer = StringWriter()
writer.write("Commit Message: ")

details.forEach { writer.write(it.fullMessage + "\n\n") }
val writer = StringWriter()
if (details.isNotEmpty()) {
writer.write("Commit Message: ")
details.forEach { writer.write(it.fullMessage + "\n\n") }
}

writer.write("Changes:\n\n")
val changeText = calculateDiff(selectList, project, ignoreFilePatterns)
Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/META-INF/autodev-core.xml
Expand Up @@ -254,7 +254,16 @@
<add-to-group group-id="Vcs.Log.ContextMenu" relative-to-action="Vcs.ShowDiffWithLocal" anchor="after"/>

<!-- Todo: spike when in Group -->
<add-to-group group-id="ChangesViewToolbar" relative-to-action="ChangesView.Revert" anchor="after"/>
</action>

<action id="autodev.Vcs.PrepushReview"
class="cc.unitmesh.devti.actions.vcs.PrepushReviewAction" text="Prepush Review (AutoDev)"
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT"
description="Ask AI to review code">

<add-to-group group-id="ChangesViewToolbar" anchor="last"/>
<add-to-group group-id="ChangesViewPopupMenu" relative-to-action="ChangesView.Revert" anchor="after"/>

</action>

<action id="cc.unitmesh.devti.actions.chat.CodeCompleteChatAction"
Expand Down

0 comments on commit d16bb38

Please sign in to comment.