Problems with Rest API Commenting with attachments

Hi, I’m currently developing an Issue Action Application that copies the most recent comment into linked issues. However, I’m running into problems posting comments that include Attachments. I’ve tested my current code and it seems to work perfectly fine posting everything except comments with attachements. When I try to copy a comment with an attachment to other issues, I get a 400 error indicating the request is invalid. Help!

//function to add a comment given an issue Id and a body
async function addComment(issueId, body) {
    const requestUrl = `/rest/api/3/issue/${issueId}/comment`;
	const newbody = { 
		"body": body
	}
	console.log(newbody);
	console.log(newbody.body);
	console.log(newbody.body.content);
	
     // Use the Forge Runtime API to fetch data from an HTTP server using your (the app developer) Authorization header
     let response = await api.asApp().requestJira(requestUrl, {
         method: "POST",
         headers: {
             "Content-Type": "application/json"
         },
         body: JSON.stringify(newbody)
     });

	 console.log(`posted new comment`);
	 
     // Error checking: the Jira issue comment Rest API returns a 201 if the request is successful
     if (response.status !== 201) {
         console.log(response.status);
         throw `Unable to add comment to issueId ${issueId} Status: ${response.status}.`;
     }

     return response.json();
 }

Hi @AlexFang, thanks for letting us know about this issue. We will investigate this and get back to you.

Just to help us understand whether the issue is scoped to Forge or not, do you happen to have other instances of this same API request outside of Forge that are working for comments with attachments?

Hi, thanks for getting back to me! I’m fairly new to forge and I was testing to see if it can reproduce the functionality of scriptrunner by trying to recreate the same script in forge so sadly I have no other instance of this API failing or working.

Hey @AlexFang, no problem! I’ve actually just had a go at reproducing, but the API works for me with image and PDF file attachments. See image below.

Could you describe exactly what is contained in the comment that isn’t able to be copied?

PDF’s or Images in the comments produce an error when im trying to run the API call

There was an error invoking the function - Cannot read property ‘match’ of undefined

TypeError: Cannot read property ‘match’ of undefined
at Object.transformScriptError [as default] (/tunnel/node_modules/@forge/runtime/out/sandbox/transform-script-error.js:14:21)
at /tunnel/node_modules/@forge/runtime/out/sandbox-isolate/index.js:151:57
at async IsolateSandbox.execute (/tunnel/node_modules/@forge/runtime/out/sandbox-isolate/index.js:41:23)
at async LocalInvocationService.invoke (/tunnel/node_modules/@forge/tunnel/out/tunnelling/local-invocation-service.js:47:26)
at async LocalDevelopmentServer.handleInvocation (/tunnel/node_modules/@forge/tunnel/out/tunnelling/dev-server.js:23:30)

Hey @AlexFang, have you included the write:jira-work scope in your manifest.yml file?

Once you have done taht, you may also need to run forge deploy and forge install --upgrade in order to see the changes take effect.

Hi @kchan, here’s my full manifest.yml file. I’ve already added the scope before I got the error and I’ve tried reinstalling so I’m not sure what the problem is. Is there anything else that could be causing the error. Thanks!

permissions:
scopes:
- read:jira-work
- write:jira-work
modules:
jira:issueAction:
- key: notify-linked-issues-hello-world
function: main
title: Notify Linked Issues
function:
- key: main
handler: index.run
app:
id: ari:cloud:ecosystem::app/eeeb35f6-0197-4c45-a144-50cfef988701
name: Notify Linked Issues

Hey @AlexFang, would you be able to run forge deploy and then forge install:list and let me know what you see? I’m interested in whether it shows that you have any out of date installations.

Hey @AlexFang, again, sorry for the delay. I’ve been very busy over the last few days. I’ll get to this soon!

1 Like

Usually this error (There was an error invoking the function - Cannot read property ‘match’ of undefined) occurs when you don’t get a 200 from your request, and you try to await response.json(). Are you able to console.log the entire response, or at least the response.status to help determine what’s going wrong?

It doesn’t look like there’s anything wrong with your installations

Actually, I managed to reproduce the issue! I will do some further investigation and talk to the people who own the API and then get back to you :slight_smile:

In the meantime, it seems from my usage like this problem is only occurring for asApp, not asUser. Would it be possible for you to use asUser?

Update 8 March 2pm Sydney time:

@AlexFang It seems there are some complex permissions issues relating to copying attachments in comments, so it is not a trivial change to support this. I’ve chatted with the team responsible for the API and it’s something that they’re looking into but are unlikely to be able to get around to in the short term.

Their suggested workaround is to try parsing the comment body for attachments and add the attachments to the issue beforehand before adding the comment. Unfortunately, the add attachments API does not quite work in Forge yet, since it uses the multipart/form-data API, as we have discovered together already in Copying attachments Rest API Post.

While we work on the multipart/form-data functionality, is it possible in the meantime to parse the comment bodies for attachments and omit them from the copied comment?

Parsing out the attachments should be a way to get the app working without attachments. I’ll do that for now and wait until a fix comes around!

1 Like