Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】合并 githubIssueClose/githubPRClose 中重复的关闭前评论校验与发布 (#673) (#674)

fx-kurumi6fb39a58ebc9
diff --git a/internal/httpapi/agent_tool_github_close_comment_test.go b/internal/httpapi/agent_tool_github_close_comment_test.go
new file mode 100644
index 00000000..2a1a7bf7
--- /dev/null
+++ b/internal/httpapi/agent_tool_github_close_comment_test.go
@@ -0,0 +1,61 @@
+package httpapi
+
+import (
+ "context"
+ "net/http"
+ "strings"
+ "testing"
+)
+
+func TestGitHubCloseCommentArg(t *testing.T) {
+ cases := []struct {
+ name string
+ args map[string]any
+ want string
+ wantErr bool
+ }{
+ {name: "missing", args: map[string]any{}, want: ""},
+ {name: "trimmed", args: map[string]any{"comment": " 由 #7 解决 "}, want: "由 #7 解决"},
+ {name: "at limit", args: map[string]any{"comment": strings.Repeat("评", 10000)}, want: strings.Repeat("评", 10000)},
+ {name: "over limit", args: map[string]any{"comment": strings.Repeat("评", 10001)}, wantErr: true},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ got, err := githubCloseCommentArg(tc.args)
+ if tc.wantErr {
+ if err == nil || err.Error() != "comment must be at most 10000 characters" {
+ t.Fatalf("expected length error, got %q, %v", got, err)
+ }
+ return
+ }
+ if err != nil || got != tc.want {
+ t.Fatalf("githubCloseCommentArg() = %q, %v; want %q", got, err, tc.want)
+ }
+ })
+ }
+}
+
+func TestGitHubAddCloseCommentReturnsURL(t *testing.T) {
+ api := &fakeGitHubAPI{routes: map[string]func(http.ResponseWriter, *http.Request){
+ "POST /repos/octo/app/issues/3/comments": githubJSON(http.StatusCreated, `{"id":11,"html_url":"https://github.com/octo/app/issues/3#issuecomment-11"}`),
+ }}
+ api.serve(t)
+ url, err := githubAddCloseComment(context.Background(), githubToolTestToken, "octo/app", 3, "done")
+ if err != nil || url != "https://github.com/octo/app/issues/3#issuecomment-11" {
+ t.Fatalf("githubAddCloseComment() = %q, %v", url, err)
+ }
+ if len(api.bodies) != 1 || !strings.Contains(api.bodies[0], `"body":"done"`) {
+ t.Fatalf("unexpected comment payload: %v", api.bodies)
+ }
+}
+
+func TestGitHubAddCloseCommentPropagatesError(t *testing.T) {
+ api := &fakeGitHubAPI{routes: map[string]func(http.ResponseWriter, *http.Request){
+ "POST /repos/octo/app/issues/3/comments": githubJSON(http.StatusForbidden, `{"message":"Resource not accessible"}`),
+ }}
+ api.serve(t)
+ url, err := githubAddCloseComment(context.Background(), githubToolTestToken, "octo/app", 3, "done")
+ if err == nil || url != "" || !strings.Contains(err.Error(), "添加评论") {
+ t.Fatalf("expected comment error, got %q, %v", url, err)
+ }
+}
diff --git a/internal/httpapi/agent_tool_github_pr.go b/internal/httpapi/agent_tool_github_pr.go
index 8e938801..f286e83d 100644
--- a/internal/httpapi/agent_tool_github_pr.go
+++ b/internal/httpapi/agent_tool_github_pr.go
@@ -356,9 +356,9 @@ func githubIssueClose(ctx context.Context, token, path string, args map[string]a
if reason != "completed" && reason != "not_planned" {
return "", fmt.Errorf("reason 只能是 completed 或 not_planned")
}
- comment := githubArgString(args, "comment")
- if len([]rune(comment)) > 10000 {
- return "", fmt.Errorf("comment must be at most 10000 characters")
+ comment, err := githubCloseCommentArg(args)
+ if err != nil {
+ return "", err
}
client := githubHTTPClient()
resp, raw, err := githubRequest(ctx, client, http.MethodGet, fmt.Sprintf("%s/repos/%s/issues/%d", githubAPIBase, path, number), token, nil)
@@ -385,15 +385,9 @@ func githubIssueClose(ctx context.Context, token, path string, args map[string]a
}
commentURL := ""
if comment != "" {
- added, err := githubIssueCommentAdd(ctx, token, path, number, comment)
- if err != nil {
+ if commentURL, err = githubAddCloseComment(ctx, token, path, number, comment); err != nil {
return "", err
}
- var c struct {
- URL string `json:"url"`
- }
- _ = json.Unmarshal([]byte(added), &c)
- commentURL = c.URL
}
payload, _ := json.Marshal(map[string]string{"state": "closed", "state_reason": reason})
resp, raw, err = githubRequest(ctx, client, http.MethodPatch, fmt.Sprintf("%s/repos/%s/issues/%d", githubAPIBase, path, number), token, payload)
@@ -460,9 +454,9 @@ func githubPRClose(ctx context.Context, token, path string, args map[string]any)
if err != nil {
return "", err
}
- comment := githubArgString(args, "comment")
- if len([]rune(comment)) > 10000 {
- return "", fmt.Errorf("comment must be at most 10000 characters")
+ comment, err := githubCloseCommentArg(args)
+ if err != nil {
+ return "", err
}
client := githubHTTPClient()
prURL := fmt.Sprintf("%s/repos/%s/pulls/%d", githubAPIBase, path, number)
@@ -490,15 +484,11 @@ func githubPRClose(ctx context.Context, token, path string, args map[string]any)
}
out := map[string]any{"repository": path, "number": number, "state": "closed", "url": current.HTMLURL}
if comment != "" {
- added, err := githubIssueCommentAdd(ctx, token, path, number, comment)
+ commentURL, err := githubAddCloseComment(ctx, token, path, number, comment)
if err != nil {
return "", err
}
- var c struct {
- URL string `json:"url"`
- }
- _ = json.Unmarshal([]byte(added), &c)
- out["commentUrl"] = c.URL
+ out["commentUrl"] = commentURL
}
payload, _ := json.Marshal(map[string]string{"state": "closed"})
resp, raw, err = githubRequest(ctx, client, http.MethodPatch, prURL, token, payload)
@@ -512,6 +502,29 @@ func githubPRClose(ctx context.Context, token, path string, args map[string]any)
return string(result), nil
}
+// githubCloseCommentArg reads the optional comment left before closing an
+// issue or PR.
+func githubCloseCommentArg(args map[string]any) (string, error) {
+ comment := githubArgString(args, "comment")
+ if len([]rune(comment)) > 10000 {
+ return "", fmt.Errorf("comment must be at most 10000 characters")
+ }
+ return comment, nil
+}
+
+// githubAddCloseComment posts the closing comment and returns its URL.
+func githubAddCloseComment(ctx context.Context, token, path string, number int, comment string) (string, error) {
+ added, err := githubIssueCommentAdd(ctx, token, path, number, comment)
+ if err != nil {
+ return "", err
+ }
+ var c struct {
+ URL string `json:"url"`
+ }
+ _ = json.Unmarshal([]byte(added), &c)
+ return c.URL, nil
+}
+
func githubErrorDetail(raw []byte) string {
var body struct {
Message string `json:"message"`