Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

import jira classes into bitbucket

artafon December 4, 2021

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?

1 answer

0 votes
Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 6, 2021

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

artafon December 6, 2021

Hi @Robert Giddings [Adaptavist] 

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
}
}
*/
artafon December 9, 2021

@Robert Giddings [Adaptavist] Hi! Is there any idea?

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 21, 2021

Hi @artafon ,

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

artafon January 5, 2022

@Robert Giddings [Adaptavist] 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!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events