Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(cli): keep description edits made while issue update uploads an attachment (#11469)

pseudoyu6934bb6c7236
diff --git a/server/cmd/multica/cmd_issue.go b/server/cmd/multica/cmd_issue.go
index 05cb1325a0..a69eb66e38 100644
--- a/server/cmd/multica/cmd_issue.go
+++ b/server/cmd/multica/cmd_issue.go
@@ -2163,16 +2163,11 @@ func runIssueUpdate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("no local attachments to update; --attachment accepts file paths, not URLs")
}
desc, changed := body["description"].(string)
+ var descRevision *int64
if len(pending) > 0 && !changed {
- var issue struct {
- Description *string `json:"description"`
- }
- if err := client.GetJSON(ctx, "/api/issues/"+url.PathEscape(issueRef.ID), &issue); err != nil {
+ if desc, descRevision, err = fetchIssueDescription(ctx, client, issueRef.ID); err != nil {
return fmt.Errorf("get issue description before binding attachments: %w", err)
}
- if issue.Description != nil {
- desc = *issue.Description
- }
}
attachmentIDs := make([]string, 0, len(pending))
attachmentRefs := make([]cli.AttachmentResponse, 0, len(pending))
@@ -2185,9 +2180,17 @@ func runIssueUpdate(cmd *cobra.Command, args []string) error {
attachmentRefs = append(attachmentRefs, uploaded)
fmt.Fprintf(os.Stderr, "Uploaded %s\n", att.path)
}
+ // setDescription adds the uploaded files to current. A revision makes the
+ // server refuse the write if the description changed after it was read.
+ setDescription := func(current string, revision *int64) {
+ current = applyAttachmentLinks(os.Stderr, current, "issue description", pending, attachmentRefs)
+ body["description"] = appendAttachmentReferences(current, attachmentRefs)
+ if revision != nil {
+ body["description_base_revision"] = *revision
+ }
+ }
if len(attachmentRefs) > 0 {
- desc = applyAttachmentLinks(os.Stderr, desc, "issue description", pending, attachmentRefs)
- body["description"] = appendAttachmentReferences(desc, attachmentRefs)
+ setDescription(desc, descRevision)
body["attachment_ids"] = attachmentIDs
}
if noStart && len(body) > 0 {
@@ -2196,7 +2199,17 @@ func runIssueUpdate(cmd *cobra.Command, args []string) error {
var result map[string]any
if len(body) > 0 {
- if err := client.PutJSON(ctx, "/api/issues/"+issueRef.ID, body, &result); err != nil {
+ err := client.PutJSON(ctx, "/api/issues/"+issueRef.ID, body, &result)
+ // Someone edited the description during the upload: add the files to
+ // the new text instead of writing the old text over it.
+ for retry := 0; retry < 3 && descRevision != nil && isIssueDescriptionConflict(err); retry++ {
+ if desc, descRevision, err = fetchIssueDescription(ctx, client, issueRef.ID); err != nil {
+ break
+ }
+ setDescription(desc, descRevision)
+ err = client.PutJSON(ctx, "/api/issues/"+issueRef.ID, body, &result)
+ }
+ if err != nil {
if len(attachmentIDs) > 0 {
return fmt.Errorf("update issue (uploaded IDs: %v): %w", attachmentIDs, err)
}
@@ -2241,6 +2254,29 @@ func runIssueUpdate(cmd *cobra.Command, args []string) error {
return cli.PrintJSON(os.Stdout, result)
}
+// fetchIssueDescription reads an issue's description and its revision. The
+// revision is nil when the server does not report one.
+func fetchIssueDescription(ctx context.Context, client *cli.APIClient, issueID string) (string, *int64, error) {
+ var issue struct {
+ Description *string `json:"description"`
+ Revision *int64 `json:"description_revision"`
+ }
+ if err := client.GetJSON(ctx, "/api/issues/"+url.PathEscape(issueID), &issue); err != nil {
+ return "", nil, err
+ }
+ if issue.Description == nil {
+ return "", issue.Revision, nil
+ }
+ return *issue.Description, issue.Revision, nil
+}
+
+// isIssueDescriptionConflict reports whether an update was refused because the
+// description changed after it was read.
+func isIssueDescriptionConflict(err error) bool {
+ var httpErr *cli.HTTPError
+ return errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusConflict && strings.Contains(httpErr.Body, "issue_description")
+}
+
// issueDescriptionWriteError explains a refused description write and how to
// redo it; other errors pass through.
func issueDescriptionWriteError(action string, err error) error {
diff --git a/server/cmd/multica/cmd_issue_test.go b/server/cmd/multica/cmd_issue_test.go
index 6cb30504dd..255655d379 100644
--- a/server/cmd/multica/cmd_issue_test.go
+++ b/server/cmd/multica/cmd_issue_test.go
@@ -4144,6 +4144,63 @@ func TestRunIssueUpdateAppendsLocalAttachmentToDescription(t *testing.T) {
}
}
+// Someone edits the description while the file uploads: the update must add the
+// file to their text, not write the text read before the upload over it.
+func TestRunIssueUpdateAttachmentKeepsDescriptionEditedDuringUpload(t *testing.T) {
+ t.Chdir(t.TempDir())
+ const issueID = "11111111-1111-4111-8111-111111111111"
+ const uploadedID = "33333333-3333-4333-8333-333333333333"
+ path := writeIssueCreateAttachment(t, "revised.png")
+ description, revision := "Existing body", int64(4)
+ uploads := 0
+ var body map[string]any
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.URL.Path {
+ case "/api/upload-file":
+ uploads++
+ description, revision = "Existing body, edited", 5
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "id": uploadedID, "filename": "revised.png", "content_type": "image/png",
+ "markdown_url": "https://api.example/api/attachments/" + uploadedID + "/download",
+ })
+ case "/api/issues/" + issueID:
+ if r.Method == http.MethodGet {
+ _ = json.NewEncoder(w).Encode(map[string]any{"description": description, "description_revision": revision})
+ return
+ }
+ body = nil
+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
+ t.Errorf("decode update: %v", err)
+ }
+ if base, ok := body["description_base_revision"]; ok && base != float64(revision) {
+ w.WriteHeader(http.StatusConflict)
+ _ = json.NewEncoder(w).Encode(map[string]any{"code": "revision_conflict", "resource_type": "issue_description"})
+ return
+ }
+ description = body["description"].(string)
+ _ = json.NewEncoder(w).Encode(map[string]any{"id": issueID, "title": "Updated"})
+ default:
+ http.NotFound(w, r)
+ }
+ }))
+ defer srv.Close()
+ setCLITestServerEnv(t, srv.URL)
+ cmd := newIssueUpdateTestCmd()
+ _ = cmd.Flags().Set("attachment", path)
+ if err := runIssueUpdate(cmd, []string{issueID}); err != nil {
+ t.Fatalf("runIssueUpdate: %v", err)
+ }
+ if want := "Existing body, edited\n\n![revised.png](https://api.example/api/attachments/" + uploadedID + "/download)"; description != want {
+ t.Fatalf("description = %q, want %q", description, want)
+ }
+ if ids, ok := body["attachment_ids"].([]any); !ok || !reflect.DeepEqual(ids, []any{uploadedID}) {
+ t.Fatalf("attachment_ids = %#v", body["attachment_ids"])
+ }
+ if uploads != 1 {
+ t.Fatalf("uploads = %d, want 1", uploads)
+ }
+}
+
func TestRunIssueUpdateAttachmentUsesProvidedDescriptionWithoutFetching(t *testing.T) {
t.Chdir(t.TempDir())
const issueID = "11111111-1111-4111-8111-111111111111"