How can I copy attachments from a JIRA Issue to a Confluence page on transition?

Christopher Siebert November 8, 2018

I am using JIRA for approval process and when the attachments are transitioned to "Done" I would like to use ScriptRunner (or maybe something else?) to copy the attachments from the JIRA issue into a specific Confluence Page. 

4 answers

1 vote
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2018

Why would that be helpful? I would use references rather than copying the actual data

1 vote
Simon Merrick November 8, 2018

Cloud or Server? Might be possible with Script Runner in cloud and probably also server with the REST API only - but I haven't tried it.

How's your scripting? I'm not going to go to deep into the scripting but I will post some snippets which might help you.

There's a Jira REST API endpoint to get attachments
https://docs.atlassian.com/software/jira/docs/api/REST/7.12.0/#api/2/attachment-getAttachment

And a Confluence REST API endpoint to create attachments
https://docs.atlassian.com/ConfluenceServer/rest/6.12.1/#content/{id}/child/attachment-createAttachments

So from Jira you could have a Workflow Postfunction or ScriptRunner Listener to run a groovy script to get attachments from the Jira issue and Post to a confluence page.

You can download the attachment(s), keeping them in memory, and upload them elsewhere, still from memory. Here's a code snippet of copying attachments from one jira ticket to another in this fashion. (The language is Groovy).

if (issue.fields.attachment) {
    issue.fields.attachment.collect { attachment ->
        def url = attachment.content as String
        url = url.substring(url.indexOf("/secure"))
        def is = Unirest.get("${url}").asBinary().body
        def resp = Unirest.post("/rest/api/2/issue/${clonedIssue.id}/attachments")
                .header("X-Atlassian-Token", "no-check")
                .field("file", is, ContentType.create(attachment.mimeType), attachment.filename)
                .asObject(List)
        assert resp.status >= 200 && resp.status < 300
    }
}


The only thing I am not sure about is that I think that scriptrunner limits what API's you can call - I am pretty sure you can't make HTTP requests to arbitrary services (google, facebook etc). I don't know how constrained this limitation is.

The other challenge to solve is unless you are posting attachments to the same confluence page every time then you would need a way to dynamically specify which confluence page it should go to. You could store the ID or URL of the confluence page in a custom field on the issue and then read the value and interpolate it into the url.

0 votes
Vamshee Rangu July 31, 2019

I am looking solution for the similar problem. I have a requirement similar to this. All the work is done JIRA but the attached documentation should be Confluence. People will be working off JIRA but all the attachments should go into Confluence with JIRA issue number, date and time.

I was hoping automation for JIRA would help (still waiting on approvals).

Thank you

Vamshee

0 votes
Simon Merrick November 8, 2018

Actually I don't think it will work talking to another system using script runner as I am pretty sure script runner talks to your Jira using JWT and you wont be able to auth with the confluence - that might be a question to raise with Adaptavist support.

You could use Webhooks to talk to your own Webservice which listens for events / webhooks from Jira and then once again uses the REST API to get the attachments from Jira and push it to confluence.

The web service wouldn't have to be very big - maybe 30 lines of python running as a flask application. But it comes with the additional overhead of needing to be hosted.

The short answer is I think it's definitely possible depending on how far you are willing to go and what you consider reasonable effort. Maybe someone else in the community has a simpler solution?

Simon Merrick November 8, 2018

...did my original comment get deleted?

Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2018

And ... it's back

Like Simon Merrick likes this
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 8, 2018

I think you could use the App Link set up between your Jira and your Confluence to modify Confluence pages

Simon Merrick November 8, 2018

Weird, anyway seeing your response I tend to agree - this 'solution' would be duplicating data.

My answer addresses the question of 'could I?' but not 'should I?'

Suggest an answer

Log in or Sign up to answer