Skip to content

The Unified and Autonomous Development Platform

feat: AI PR review prompt improvement.

Robin Shen029e093f0184
diff --git a/server-core/src/main/java/io/onedev/server/model/PullRequest.java b/server-core/src/main/java/io/onedev/server/model/PullRequest.java
index 27f80b78c2..e926cba045 100644
--- a/server-core/src/main/java/io/onedev/server/model/PullRequest.java
+++ b/server-core/src/main/java/io/onedev/server/model/PullRequest.java
@@ -1556,7 +1556,8 @@ public class PullRequest extends ProjectBelonging
public ToolSpecification getSpecification() {
return ToolSpecification.builder()
.name("approvePullRequest")
- .description("Approve current OneDev pull request")
+ .description("Record an approval for current OneDev pull request. "
+ + "Call this exactly once after you have finished reviewing and decided to approve.")
.build();
}
@@ -1567,6 +1568,14 @@ public class PullRequest extends ProjectBelonging
if (!SecurityUtils.canReadCode(subject, request.getProject()) || user == null)
throw new UnauthorizedException();
+ var existingReview = request.getReview(user);
+ if (existingReview != null && existingReview.getStatus() == PullRequestReview.Status.APPROVED) {
+ var data = Map.of(
+ "successful", true,
+ "message", "Approval has already been recorded, do not call this tool again");
+ return new ToolExecutionResult(convertToJson(data), false);
+ }
+
try {
getPullRequestReviewService().review(user, request, true, null);
return new ToolExecutionResult(convertToJson(Map.of("successful", true)), false);
@@ -1585,7 +1594,8 @@ public class PullRequest extends ProjectBelonging
public ToolSpecification getSpecification() {
return ToolSpecification.builder()
.name("requestChangesForPullRequest")
- .description("Request changes for current OneDev pull request")
+ .description("Record a request for changes for current OneDev pull request. "
+ + "Call this exactly once after you have finished reviewing and decided changes are needed.")
.build();
}
@@ -1596,6 +1606,14 @@ public class PullRequest extends ProjectBelonging
if (!SecurityUtils.canReadCode(subject, request.getProject()) || user == null)
throw new UnauthorizedException();
+ var existingReview = request.getReview(user);
+ if (existingReview != null && existingReview.getStatus() == PullRequestReview.Status.REQUESTED_FOR_CHANGES) {
+ var data = Map.of(
+ "successful", true,
+ "message", "Request for changes has already been recorded, do not call this tool again");
+ return new ToolExecutionResult(convertToJson(data), false);
+ }
+
try {
getPullRequestReviewService().review(user, request, false, null);
return new ToolExecutionResult(convertToJson(Map.of("successful", true)), false);
diff --git a/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java b/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
index 1b92233037..7a15937613 100644
--- a/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
+++ b/server-core/src/main/java/io/onedev/server/notification/PullRequestNotificationManager.java
@@ -299,10 +299,16 @@ public class PullRequestNotificationManager {
"""
Review current pull request for major issues (ignore styling/format/documentation issues) \
introduced in the change. Check full content of relevant files to understand the change \
- if necessary. Check existing comments for conversation context. Approve the pull request \
- if you are satisfied with it, or request for changes if you think it needs more work. \
- Summarize found issues in response and for each issue, make sure to quote relevant code \
- snippets if applicable""",
+ if necessary. Check existing comments for conversation context. \
+
+ You MUST record your final decision by calling exactly one of these tools, and call it \
+ only once: `approvePullRequest` if you are satisfied with the change, or \
+ `requestChangesForPullRequest` if you think it needs more work. If you are unsure, \
+ call `requestChangesForPullRequest` and explain what is unclear. Do not call either \
+ tool more than once. \
+
+ After calling the decision tool, summarize found issues in your final response and for \
+ each issue, make sure to quote relevant code snippets if applicable""",
request.getTools(true),
new AddPullRequestComment(request.getId()));
userService.execute(reviewer, task);