Updating Jira comment with new attachment media ID fails with ATTACHMENT_VALIDATION_ERROR

When I try and update a comment with a new attachment it fails with:

INFO    09:49:46.063  1e238f12-05f3-4703-be2c-2720d7c4f8fd  Update Response: 400 Bad Request
INFO    09:49:46.063  1e238f12-05f3-4703-be2c-2720d7c4f8fd  { errorMessages: [ 'ATTACHMENT_VALIDATION_ERROR' ], errors: {} }

What I am doing is roughly:

  1. Get comment
  2. Replace an attachment media id with another attachment media id
  3. Update comment

In code, this looks like this:

        // Fetch the comment body
        const commentResponse = await api.asApp().requestJira(route`/rest/api/3/issue/${issueIdOrKey}/comment/${commentId}`, {
            headers: {
                'Accept': 'application/json'
            }
        });

        const commentData = await commentResponse.json();
        const commentBody = commentData.body;

        // Replace the original attachment ID with the new one
        commentBody.content.forEach(contentBlock => {
            if (contentBlock.type === 'mediaGroup') {
                contentBlock.content.forEach(media => {
                    if (media.type === 'media' && media.attrs.id === originalAttachmentMediaId) {
                        media.attrs.id = newAttachmentId;
                    }
                });
            }
        });

        // Update the comment

        // Currently this fails with:
        // INFO    09:49:46.063  1e238f12-05f3-4703-be2c-2720d7c4f8fd  Update Response: 400 Bad Request
        // INFO    09:49:46.063  1e238f12-05f3-4703-be2c-2720d7c4f8fd  { errorMessages: [ 'ATTACHMENT_VALIDATION_ERROR' ], errors: {} }
        const updateResponse = await api.asApp().requestJira(route`/rest/api/3/issue/${issueIdOrKey}/comment/${commentId}`, {
            method: 'PUT',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                body: commentBody,
                // Include other fields you want to update or keep the same
            })
        });

        console.log(`Update Response: ${updateResponse.status} ${updateResponse.statusText}`);
        console.log(await updateResponse.json());

The app already created the new attachment so we know that there aren’t any permission issues there.

I have also tested this across different project types and it’s not limited to JSM or JSW.

There doesn’t seem to be any relevant topics on CDAC either so I am a bit stuck.

Ok so I dug into this and it’s even weirder.

If I comment out:

        commentBody.content.forEach(contentBlock => {
            if (contentBlock.type === 'mediaGroup') {
                contentBlock.content.forEach(media => {
                    if (media.type === 'media' && media.attrs.id === originalAttachmentMediaId) {
                        media.attrs.id = newAttachmentId;
                    }
                });
            }
        });

Then the code works. But here is the crazy thing, it should be the inverse. If I don’t update the comment body, then I would be submitting a comment body with an invalid media id. If I do update it, then I am submitting a valid media id.

I also double checked the above code to ensure it’s not screwing up the comment body and as far as I can tell it’s not:

INFO    17:54:01.540  22db2c72-0b01-4d45-bb57-7e6b3c5bab88  Original Comment Body:
INFO    17:54:01.541  22db2c72-0b01-4d45-bb57-7e6b3c5bab88  {"version":1,"type":"doc","content":[{"type":"mediaGroup","content":[{"type":"media","attrs":{"id":"1bf36952-f1c6-4fb7-8bfe-91bc09b5428a","type":"file","collection":""}}]},{"type":"paragraph","content":[]}]}
INFO    17:54:01.541  22db2c72-0b01-4d45-bb57-7e6b3c5bab88  Updated Comment Body:
INFO    17:54:01.541  22db2c72-0b01-4d45-bb57-7e6b3c5bab88  {"version":1,"type":"doc","content":[{"type":"mediaGroup","content":[{"type":"media","attrs":{"id":"e31953d8-0a96-435a-ac70-ec72898f7431","type":"file","collection":""}}]},{"type":"paragraph","content":[]}]}

So not sure whats going on, but seems like a problem on the Atlassian side.

ibuchanan any chance you can take a look at this?

Edit: Not sure how to strikethrough but it looks like I was getting the wrong media ID because a UUID is returned from the attachment endpoint but it’s no the right media UUID and it looked right. So continuing to dig into that, and I guess this will ultimately get resolved with an update to How to work with attachments in comments? Media vs Attachments nightmare - #3 by JulianWolf