Problems using ApplicationLinks in JIRA background tasks

Mark_Kobold July 1, 2015

Working on an automation plug-in for JIRA that when the workflow post function fires it uses the scheduler to post something like this to the real work of the workflow action and not block the web ui. 

public class WorkflowPostFunction extends AbstractJiraFunctionProvider {
    private final PluginScheduler scheduler;
    public WorkflowPostFunction(final PluginScheduler scheduler) {
        this.scheduler = scheduler;
    }
    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(final Map transientVars, final Map args, final PropertySet ps) throws WorkflowException {
        final MutableIssue issue = getIssue(transientVars);
        // this will identify the job uniquely within JIRA 
        final String jobKey = String.format("XXX-%s", issue.getKey());
        final Class<ApertureProjectCreatorTask> jobClass = WorkflowTask.class;
        final Map<String, Object> jobEnv = new HashMap<String, Object>();
        jobEnv.put("issue-key", issue.getKey());
        LOG.info(String.format("Scheduling task for [%s]", issue.getKey()));
        scheduler.scheduleJob(jobKey, jobClass, jobEnv, new Date(), 0);
    }
}

What I really want to be able to do is use the already configured application links between JIRA and Confluence in the said task. As part of the task would like to do rest calls as the JIRA administrator to confluence using the Application Link API as part of the background task.

However, one strong problem exists with this. Background tasks run in a separate thread so they do not have the same context and thread local variables that make some of the more complex tools in JIRA work. I suspect this is true on the other applications.

public class WorkflowBackgroundTask implements PluginJob {
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(final Map<String, Object> env) { 
		// do long running task and we want to report to confluence the results. 
		ApplicationLink link = getConfluenceLink(); 
	    // this line doesn't actually work since we are in the background 
		ApplicationLinkRequestFactory f = link.createAuthenticatedRequestFactory();
		ApplicationLinkRequest r = f.createRequest(Request.MethodType.POST, "/rest/api/XXXX"); 
		// configure request with JSON, etc. 
		String s = r.execute(handlerRef); 
		// contains a JSON response saying the function failed and is unauthorized
		// confluence logs show that the request was made anonymously 
    }
 
    private ApplicationLink getConfluenceLink() {
		// assume this works; it does i promise // 
    }
}

 

So how would one go about this? If the code runs in the actual workflow function it blocks for a potentially long time but works because 'someone' is clicking the button and JIRA knows who. 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Mark_Kobold July 1, 2015

Apparently I didn't see this method on the request interface; The following method allows the impersonating authentication to work as whomever we need using the following snippet 

ApplicationLinkRequest r = f.createRequest(Request.MethodType.POST, "/rest/api/XXXX"); 
       
r.addTrustedTokenAuthentication("admin");
TAGS
AUG Leaders

Atlassian Community Events