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?
Hi @artafon ,
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
Hi @Robert Giddings _Adaptavist_
Thx for your attention. Below code returns - unable to resolve class ComponentAccessor
package examples.bitbucket.handlerimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.applinks.api.ApplicationLinkResponseHandlerimport com.atlassian.plugin.PluginAccessorimport com.atlassian.sal.api.UrlModeimport com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.sal.api.net.Requestimport com.atlassian.bitbucket.pull.PullRequestimport com.atlassian.sal.api.net.Responseimport com.atlassian.bitbucket.event.ApplicationEventimport com.atlassian.bitbucket.integration.jira.JiraIssueServiceimport com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScriptimport com.onresolve.scriptrunner.runner.ScriptRunnerImplimport groovy.json.JsonBuilderimport groovy.transform.BaseScriptimport com.atlassian.mail.Emailimport com.atlassian.mail.server.MailServerManagerimport com.atlassian.mail.server.SMTPMailServerimport groovyx.net.http.HTTPBuilderimport static groovyx.net.http.ContentType.*import groovyx.net.http.ContentTypeimport static groovyx.net.http.Method.*import groovy.json.JsonSlurperimport net.sf.json.groovy.JsonSlurperimport groovy.json.JsonOutput@BaseScript BitbucketBaseScript baseScriptint transitionId = 1111def inputTransition //json выполнения автопереходаdef assignUser //json назначения исполнителяList rev=[]def targetGitRepos = ["test_reb_bb"]PullRequest pullRequest = event.getPullRequest()def repository = pullRequest.fromRef.repositoryassert pullRequestString projectPR = repository.getProject().nameString reponame = repository.namedef keys = jiraIssueService.getIssuesForPullRequest(repository.id, pullRequest.id)*.key//***** start logginglog.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 loggingif (targetGitRepos.contains(repository.slug)) {def review=pullRequest.getReviewers()def k = 0review.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 logginglog.info("reviewer = " + rev[0])log.info("assignee json = " + assignUser)//***** end loggingdef 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}}*/
@Robert Giddings _Adaptavist_ Hi! Is there any idea?
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?
It looks like you're new here. Sign in or register to get started.