import jira classes into bitbucket
Asked by artafon:

HI!

I need to use ComponentAccessor in scripts listeners for Bitbucket by Scriptrunner. I get error - unable resolve class after import. Is there a way to load jira class?


Answers:
author: Robert Giddings [Adaptavist]
body: Hi [@artafon](/t5/user/viewprofilepage/user-id/630504) ,

Please can you provide a sample of the code that you are currently getting an error with, so I can debug it for you?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

comments: author: artafon
body: Hi [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283)

Thx for your attention. Below code returns - unable to resolve class ComponentAccessor

```
package examples.bitbucket.handler

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.applinks.api.ApplicationLinkResponseHandler
import com.atlassian.plugin.PluginAccessor
import com.atlassian.sal.api.UrlMode
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Request
import com.atlassian.bitbucket.pull.PullRequest
import com.atlassian.sal.api.net.Response
import com.atlassian.bitbucket.event.ApplicationEvent
import com.atlassian.bitbucket.integration.jira.JiraIssueService
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import com.atlassian.mail.Email
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovy.json.JsonOutput

@BaseScript BitbucketBaseScript baseScript

int transitionId = 1111
def inputTransition //json ?????????? ????????????
def assignUser //json ?????????? ???????????
List rev=[]
def targetGitRepos = ["test_reb_bb"]
PullRequest pullRequest = event.getPullRequest()
def repository = pullRequest.fromRef.repository
assert pullRequest
String projectPR = repository.getProject().name
String reponame = repository.name
def keys = jiraIssueService.getIssuesForPullRequest(repository.id, pullRequest.id)*.key

//***** start logging
log.info("event: " + event)
log.info("reponame: " + reponame)
log.info("repository.id: " + repository.id)
log.info("pullRequest.id: " + pullRequest.id)
log.info("keys: " + keys)
log.info("projectPR: " + projectPR)
log.info("repository: " + repository)
//***** end logging
if (targetGitRepos.contains(repository.slug)) {
def review=pullRequest.getReviewers()
def k = 0
review.each{
String b = review[k].getUser().getName()
rev.push(b)
k++
}

inputTransition = new JsonBuilder(
transition: [
id: transitionId
]).toString()

if (rev != null){
assignUser = new JsonBuilder(
name: rev[0]).toString()
}
//***** start logging
log.info("reviewer = " + rev[0])
log.info("assignee json = " + assignUser)
//***** end logging
def jiraLink = getJiraAppLink()
def authenticatedRequestFactory = jiraLink.createImpersonatingAuthenticatedRequestFactory()

for (String key in keys) {
if (key.substring(0, key.lastIndexOf("-")) == "TESTTEST") {
authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "/rest/api/2/issue/$key/transitions")
.addHeader("Content-Type", "application/json")
.setEntity(inputTransition)
.execute([
handle: { Response response ->
if (response.successful) {
log.debug "Issue transit success: $key."
} else {
log.warn "Failed to transit issue: $response.responseBodyAsStream"
String body="<font size = 1em color = #b35900>[?????? ?????? ???????????? ?????????????, ?? ????? ?? ???? ????????]</font><br>"
body=body+"-----<br>"
body=body+"<b>?????? ?? ? ??????? ???????? ???? " + key + "<br>"
body=body+"-----<br>"
def subject = "?????????? ????????? ?????? ? ?????? - ???????? ????"
//sendEmail (key.getReporter().getEmailAddress(), subject, body)
}
}] as ApplicationLinkResponseHandler<Void>
)
if (assignUser != null){
authenticatedRequestFactory
.createRequest(Request.MethodType.PUT, "/rest/api/2/issue/$key/assignee")
.addHeader("Content-Type", "application/json")
.setEntity(assignUser)
.execute([
handle: { Response response ->
if (response.successful) {
log.debug "Assign user on issue: $key."
} else {
log.warn "Failed to assign user: " + response.getResponseBodyAsStream()
}
}] as ApplicationLinkResponseHandler<Void>
)}
}
}
}

//???????? ??????????? ?????? ??????? ? ???????, ??????? ?? ?????????? ? ?????? - ???????? ????
/*def sendEmail(String emailAddr, String subject, String body) {
SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();
if (mailServer) {
Email email = new Email(emailAddr);
email.setSubject(subject);
email.setMimeType("text/html");
email.setBody(body);
mailServer.send(email);
} else {
// Problem getting the mail server from JIRA configuration, log this error
}
}
*/
```


author: artafon
body: [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283) Hi! Is there any idea?


author: Robert Giddings [Adaptavist]
body: Hi [@artafon](/t5/user/viewprofilepage/user-id/630504) ,

Sorry for the late response.

The first thing to solve is that ComponentAccessor is a Jira class:

```
com.atlassian.jira.component.ComponentAccessor
```

For Bitbucket you should use ComponentLocator:

```
com.atlassian.sal.api.component.ComponentLocator
```

Secondly, I see that you are wanting to use ComponentAccessor, because you are wanting to import in a Jira mail server manager into your ScriptRunner for Bitbucket Listener?

This, I don't think will work.

Instead I suggest the following. Either:

1) Set up a Post Function in Jira to send the email, following the transition.

2) Use ScriptRunner for Jira to setup a Custom REST Endpoint, which you can call from the ScriptRunner for Bitbucket Listener, in the same way as the standard Jira REST API.

3) Use Bitbucket's mail server manager and send the email directly from Bitbucket.

Please let me know if any of those suggestions help you progress?

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket


author: artafon
body: [@Robert Giddings \[Adaptavist\]](/t5/user/viewprofilepage/user-id/1196283) Hi!

In post function I cann't send email because I need bitbucket data from PR. Custom end point from JIRA - that my solution.

Thanks for your attention in my question. Happy new Year!


Tags: bitbucket-server

jira

scriptrunner

server