Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner REST Endpoints script runs twice everytime

Michael Smith August 21, 2020

Hello everyone,

We use GET request to download attachments from another Jira instance and attach it to our Jira so every time script runs twice and upload the same file to issue. I suppose I need to configure httpclient somehow but I can't find a solution. My code:


@BaseScript CustomEndpointDelegate delegate
attach ( httpMethod: "POST") {
MultivaluedMap queryParams, String body ->

//parsing json body from jira cloud
JSONObject json = new JSONObject(new JSONTokener(body))
JSONObject issue = json.getJSONObject("issue")
JSONObject fields = issue.getJSONObject("fields")
JSONArray attachments = fields.getJSONArray("attachment")
JSONObject attachment = attachments.getJSONObject(attachments.length() - 1)
String vendorRequest = fields.getString("customfield_11616")
String linkToFile = attachment.getString("content")
String fileName = StringUtils.substringAfterLast(linkToFile, "/")

//connecting to jira cloud to download file
CloseableHttpClient httpclient = HttpClients.createDefault()
HttpGet get = new HttpGet(linkToFile)
get.setHeader("X-Atlassian-Token", "no-check")
get.setHeader("Authorization", "Basic xxxxxxxxxxxxxxxxxxxxxxxx==")
CloseableHttpResponse responseFromCloud = httpclient.execute(get)
HttpEntity entity = responseFromCloud.getEntity()

//attaching file to our jira
HttpPost post = new HttpPost("http://our-jira/rest/api/2/issue/" + vendorRequest + "/attachments")
post.setHeader("X-Atlassian-Token", "no-check")
post.setHeader("Authorization", "Basic xxxxxxxxxxxxxxxxxxxxxxxx==")
InputStreamBody fileBody = new InputStreamBody(entity.getContent(), fileName)
HttpEntity entityToAttach = MultipartEntityBuilder.create().addPart("file", fileBody).build()
post.setEntity(entityToAttach)
CloseableHttpResponse responseFromServer = httpclient.execute(post)

responseFromCloud.close()
responseFromServer.close()
httpclient.close()
}

Any suggestions?

Regards,
Michael

 

0 answers

Suggest an answer

Log in or Sign up to answer