Forums

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

I am having trouble with confluence page auto-creation through Jira post function

Jonathan Feagans
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 25, 2023

I am attempting to set a post function when my issues in a Jira project transition to done, it creates a confluence page in my space. I have a two-way app link from the two servers using OAuth (impersonation) and also have a link set from the specific Jira project and specific confluence space as well. 

The script I am attempting to use came from here. https://www.scriptrunnerhq.com/inspiration/blog/how-to-create-a-confluence-page-from-a-jira-post-function 


import com.atlassian.applinks.api.ApplicationLink
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.confluence.ConfluenceApplicationType
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import com.atlassian.sal.api.net.ResponseHandler
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import org.jsoup.Jsoup

/**
* Retrieve the primary confluence application link
* @return confluence app link
*/
def getPrimaryConfluenceLink() {
def applicationLinkService = ComponentLocator.getComponent(ApplicationLinkService.class)
final ApplicationLink conflLink = applicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class)
conflLink
}

// the issue provided to us in the binding
Issue issue = issue

def confluenceLink = getPrimaryConfluenceLink()
assert confluenceLink // must have a working app link set up

def authenticatedRequestFactory = confluenceLink.createImpersonatingAuthenticatedRequestFactory()

// set the page title - this should be unique in the space or page creation will fail
def pageTitle = issue.key + " Auto Test"
def htmlCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10201")
def xmlCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10202")
def xml = Jsoup.parse(issue.getCustomFieldValue(xmlCustomField).toString())
def locations = xml.getElementsByTag("locations").collect {
it.text()
}.join(" ")
// since my field renders html with the html macro, I need to strip it out before adding my html to the page
def parsedHtmlFieldValue = issue.getCustomFieldValue(htmlCustomField).toString().replaceAll("\\{html}", '')

def pageBody = """<p> ${issue.description} </p>
${parsedHtmlFieldValue}

<p> Locations: ${locations} </p>

<p> Use this page to discuss our sales improvement plan </p>
"""

def params = [
type : "page",
title: pageTitle,
space: [
key: "Auto" // set the space key - or calculate it from the project or something
],
body : [
storage: [
value : pageBody,
representation: "storage"
],
],
]


 

This is the error I am getting, everything I found about authentication was saying to switch from OAuth(not impersonating) to OAuth (impersonation). As stated I above that's how I have it set but am still having issues. This is the full error I am getting. Am I overlooking/missing something?

 


2023-09-25 20:05:04,229 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue AUTO-1 for user 'jonathan.feagans.pri'. View here: https://jira./secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Software+Simplified+Workflow+for+Project+IT&descriptorTab=postfunctions&workflowTransition=41&highlight=7
java.lang.Exception: {"statusCode":403,"data":{"authorized":false,"valid":true,"allowedInReadOnlyMode":true,"errors":[],"successful":false},"message":"You're not allowed to view that space, or it does not exist.","reason":"Forbidden"}
at Script3$1.handle(Script3.groovy:74)
at com.atlassian.applinks.core.auth.ApplicationLinkAnalyticsRequest$AnalyticsResponseHandler.handle(ApplicationLinkAnalyticsRequest.java:264)
at com.atlassian.applinks.oauth.auth.OAuthResponseHandler.handle(OAuthResponseHandler.java:48)
at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:115)
at com.atlassian.plugins.rest.module.jersey.JerseyRequest$1.handle(JerseyRequest.java:113)
at com.atlassian.plugins.rest.module.jersey.JerseyRequest$2.handle(JerseyRequest.java:134)
at com.atlassian.sal.core.net.HttpClientRequest.executeAndReturn(HttpClientRequest.java:102)
at com.atlassian.plugins.rest.module.jersey.JerseyRequest.executeAndReturn(JerseyRequest.java:131)
at com.atlassian.plugins.rest.module.jersey.JerseyRequest.execute(JerseyRequest.java:113)
at com.atlassian.applinks.core.auth.ApplicationLinkRequestAdaptor.execute(ApplicationLinkRequestAdaptor.java:50)
at com.atlassian.applinks.oauth.auth.OAuthRequest.execute(OAuthRequest.java:71)
at com.atlassian.applinks.core.auth.ApplicationLinkAnalyticsRequest.execute(ApplicationLinkAnalyticsRequest.java:164)
at com.atlassian.sal.api.net.Request$execute$1.call(Unknown Source)
at Script3.run(Script3.groovy:70)

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.6
TAGS
AUG Leaders

Atlassian Community Events