ScriptRunner Merge Check for Bitbucket 获取关联的 Jira 问题

huangapple go评论139阅读模式
英文:

ScriptRunner Merge Check for Bitbucket Get Associated Jira Issue

问题

我想设置一个合并检查,如果关联的Jira问题有任何阻塞任务,将会阻止合并。

在我来这里之前,我已经搜索了解决方案,并找到了以下示例。但是,就在import com.atlassian.jira.component.ComponentAccessor的地方,我遇到了"无法解析类"的错误。我正在运行最新版本的Scriptrunner for Bitbucket Server v.7.10.0。

我尝试使用AppLinks,就像以下示例中所示,我发现我可以通过这种方式访问Jira API,但我不确定如何实际确定与触发合并分支关联的问题的任务ID。

我很难找到任何不涉及随机Adaptavist或Atlasian论坛帖子的最新文档。是否有人能提供如何入门的见解?

英文:

I would like to set up a Merge Check that will block a merge if the associated Jira Issue has any blocking tasks.

I have searched for the solution before coming here and found the following example. However, right at import com.atlassian.jira.component.ComponentAccessor I get an "unable to resolve class" error. I am running the latest version of Scriptrunner for Bitbucket Server v.7.10.0.

I have attempted to use AppLinks as shown in the following example and see that I am able to access the Jira Api this way, however I am unsure of how to actually determine the Task Id of the issue linked to the triggering Merge Branch.

I am having a hard time finding any up to date documentation that doesn't involve random Adaptavist or Atlasian forumn posts. Does anyone have any insight for how I can get started?

答案1

得分: 1

以下是您要翻译的内容:

"The ComponentAccessor is no longer supported, and my non-descript errors were a result of lower permissions. Once being promoted to admin I was able to access the Script Editor window which allowed me to perform the following..."

"To replace the provided 'Is Blocked on Jira' example I instead created a Jira Issue Controller class that is responsible for accessing the information using appLinks and Jira API calls."

"import com.atlassian.applinks.api.ApplicationLinkResponseHandler
import com.atlassian.applinks.api.ApplicationLinkService
import com.atlassian.applinks.api.application.jira.JiraApplicationType
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.sal.api.net.Response
import com.atlassian.sal.api.net.ResponseException
import groovy.json.JsonSlurper

import static com.atlassian.sal.api.net.Request.MethodType.GET

// This class is responsible for retrieving specific issues from the Jira AppLink (api)
class IssueController{
private static String issueEndpoint = '/rest/api/2/issue/'

  1. // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkResponseHandler.html
  2. private static ApplicationLinkResponseHandler<Map> handler = new ApplicationLinkResponseHandler<Map>() {
  3. // Triggered when a call to a remote server failed because the caller does not have an established session
  4. // with the server and will need to authorize requests first by visiting the authorization URL provided by
  5. // AuthorisationURIGenerator.
  6. @Override
  7. Map credentialsRequired(Response response) throws ResponseException {
  8. return null
  9. }
  10. @Override
  11. Map handle(Response response) throws ResponseException {
  12. assert response.statusCode == 200
  13. new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map
  14. }
  15. }
  16. static RestObjects.JiraIssue GetJiraIssue(String issueKey){
  17. def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
  18. def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
  19. def appLinkUrl = appLink.getDisplayUrl().toString()
  20. // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequestFactory.html
  21. def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
  22. // doc - https://docs.atlassian.applinks.api/3.2/com/atlassian/applinks/api/ApplicationLinkRequest.html
  23. def linkRequest = applicationLinkRequestFactory.createRequest(GET, issueEndpoint + issueKey )
  24. def response = linkRequest.execute(handler)
  25. return new RestObjects.JiraIssue(response);
  26. }

}"

"This class returns a custom object RestObjects.JiraIssue I use to parse the specific API response Map into custom Classes. A short example of the JiraIssue class and how I am able to access the linked blocking issues is shown below."

"package JiraApp.RestObjects

class JiraIssue
{
String key
Fields fields

  1. //public
  2. JiraIssue(keyStr){
  3. this.key = keyStr
  4. }
  5. JiraIssue(Map apiResponseMap)
  6. {
  7. this.key = apiResponseMap['key']
  8. this fields = new Fields(apiResponseMap['fields'] as Map)
  9. }
  10. //Returns list of the keys linked blocking issues
  11. String[] blockingIssueKeys(){
  12. String[] keys = []
  13. fields.issueLinks.each {
  14. link -&gt;
  15. if(link.type.name == 'Blocks' &amp;&amp; link.getInwardIssue() != null)
  16. {
  17. def inwardIssue = link.getInwardIssue()
  18. keys += inwardIssue.key
  19. }
  20. }
  21. return keys;
  22. }
  23. class Fields{
  24. Fields(Map fieldsMap){....."

"The rest of this class uses nested classes to parse the input Maps for their fields into usable objects. It also contains objects/functions I have created for my specific application."

"I hope this answer is helpful to someone, the documentation on ScriptRunner for bitbucket is limited."

英文:

The ComponentAccessor is no longer supported, and my non-descript errors were a result of lower permissions. Once being promoted to admin I was able to access the Script Editor window which allowed me to perform the following...

To replace the provided "Is Blocked on Jira" example I instead created a Jira Issue Controller class that is responsible for accessing the information using appLinks and Jira API calls.

  1. import com.atlassian.applinks.api.ApplicationLinkResponseHandler
  2. import com.atlassian.applinks.api.ApplicationLinkService
  3. import com.atlassian.applinks.api.application.jira.JiraApplicationType
  4. import com.atlassian.sal.api.component.ComponentLocator
  5. import com.atlassian.sal.api.net.Response
  6. import com.atlassian.sal.api.net.ResponseException
  7. import groovy.json.JsonSlurper
  8. import static com.atlassian.sal.api.net.Request.MethodType.GET
  9. // This class is responsiblefor retrieving specific issues from the JiraAppLink (api)
  10. class IssueController{
  11. private static String issueEndpoint = &quot;/rest/api/2/issue/&quot;
  12. // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkResponseHandler.html
  13. private static ApplicationLinkResponseHandler&lt;Map&gt; handler = new ApplicationLinkResponseHandler&lt;Map&gt;() {
  14. // Triggered when a call to a remote server failed because the caller does not have an established session
  15. // with the server and will need to authorize requests first by visiting the authorization URL provided by
  16. // AuthorisationURIGenerator.
  17. @Override
  18. Map credentialsRequired(Response response) throws ResponseException {
  19. return null
  20. }
  21. @Override
  22. Map handle(Response response) throws ResponseException {
  23. assert response.statusCode == 200
  24. new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map
  25. }
  26. }
  27. static RestObjects.JiraIssue GetJiraIssue(String issueKey){
  28. def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
  29. def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
  30. def appLinkUrl = appLink.getDisplayUrl().toString()
  31. // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequestFactory.html
  32. def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
  33. // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequest.html
  34. def linkRequest = applicationLinkRequestFactory.createRequest(GET, issueEndpoint + issueKey )
  35. def response = linkRequest.execute(handler)
  36. return new RestObjects.JiraIssue(response);
  37. }
  38. }

This class returns a custom object RestObjects.JiraIssue I use to parse the specific API response Map into custom Classes. A short example of the JiraIssue class and how I am able to access the linked blocking issues is shown below.

  1. package JiraApp.RestObjects
  2. class JiraIssue
  3. {
  4. String key
  5. Fields fields
  6. //public
  7. JiraIssue(keyStr){
  8. this.key = keyStr
  9. }
  10. JiraIssue(Map apiResponseMap)
  11. {
  12. this.key = apiResponseMap[&quot;key&quot;]
  13. this.fields = new Fields(apiResponseMap[&quot;fields&quot;] as Map)
  14. }
  15. //Returns list of the keys linked blocking issues
  16. String[] blockingIssueKeys(){
  17. String[] keys = []
  18. fields.issueLinks.each {
  19. link -&gt;
  20. if(link.type.name == &quot;Blocks&quot; &amp;&amp; link.getInwardIssue() != null)
  21. {
  22. def inwardIssue = link.getInwardIssue()
  23. keys += inwardIssue.key
  24. }
  25. }
  26. return keys;
  27. }
  28. class Fields{
  29. Fields(Map fieldsMap){.....

The rest of this class uses nested classes to parse the input Maps for their fields into usable objects. It also contains objects/functions I have created for my specific application.

I hope this answer is helpful to someone, the documentation on ScriptRunner for bitbucket is limited.

huangapple
  • 本文由 发表于 2023年2月18日 01:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定