groovy Bitbucket获取关联Jira问题的ScriptRunner合并检查

v1l68za4  于 2023-03-11  发布在  其他
关注(0)|答案(1)|浏览(182)

我想设置一个合并检查,如果关联的Jira问题有任何阻塞任务,它将阻塞合并。
我在来这里之前已经搜索了解决方案,找到了the following example。但是,就在import com.atlassian.jira.component.ComponentAccessor处,我得到了一个“无法解析类”的错误。我正在运行最新版本的Scriptrunner for Bitbucket Server v.7.10.0。
我尝试使用the following example中所示的AppLinks,并看到我能够以这种方式访问Jira API,但我不确定如何实际确定链接到触发合并分支的问题的任务ID。
我很难找到任何不涉及随机Adaptavist或Atlasian论坛帖子的最新文档。有人能告诉我如何开始吗?

kiayqfof

kiayqfof1#

ComponentAccessor不再受支持,我的非描述性错误是较低权限的结果。一旦被提升为管理员,我就可以访问脚本编辑器窗口,该窗口允许我执行以下操作...
为了替换提供的"Is Blocked on Jira" example,我创建了一个Jira问题控制器类,负责使用appLinks和Jira API调用访问信息。

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 responsiblefor retrieving specific issues from the JiraAppLink (api)
class IssueController{
    private static String issueEndpoint = "/rest/api/2/issue/"

    // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkResponseHandler.html
    private static ApplicationLinkResponseHandler<Map> handler = new ApplicationLinkResponseHandler<Map>() { 
        // Triggered when a call to a remote server failed because the caller does not have an established session 
        // with the server and will need to authorize requests first by visiting the authorization URL provided by 
        // AuthorisationURIGenerator.
        @Override
        Map credentialsRequired(Response response) throws ResponseException {
            return null
        }
    
        @Override
        Map handle(Response response) throws ResponseException {
            assert response.statusCode == 200
            new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map
        }
    }

    static RestObjects.JiraIssue GetJiraIssue(String issueKey){
        def appLinkService = ComponentLocator.getComponent(ApplicationLinkService)
        def appLink = appLinkService.getPrimaryApplicationLink(JiraApplicationType)
        def appLinkUrl = appLink.getDisplayUrl().toString()
        // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequestFactory.html
        def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()
        // doc - https://docs.atlassian.com/applinks-api/3.2/com/atlassian/applinks/api/ApplicationLinkRequest.html
        def linkRequest = applicationLinkRequestFactory.createRequest(GET, issueEndpoint + issueKey )
        def response = linkRequest.execute(handler)
        return new RestObjects.JiraIssue(response);
    }
}

这个类返回一个自定义对象RestObjects.JiraIssue,我用它来将特定的API响应Map解析为自定义类。下面是JiraIssue类的一个简短示例,以及我如何访问链接的阻塞问题。

package JiraApp.RestObjects

class JiraIssue
{
    String key
    Fields fields

    //public
    JiraIssue(keyStr){
        this.key = keyStr
    }
    JiraIssue(Map apiResponseMap)
    {
        this.key = apiResponseMap["key"]
        this.fields = new Fields(apiResponseMap["fields"] as Map)
    }

    //Returns list of the keys linked blocking issues 
    String[] blockingIssueKeys(){
        String[] keys = []
        fields.issueLinks.each { 
            link -> 
                if(link.type.name == "Blocks" && link.getInwardIssue() != null)
                {
                    def inwardIssue = link.getInwardIssue()
                    keys += inwardIssue.key
                }
        }
        return keys;
    }

    class Fields{
        Fields(Map fieldsMap){.....

这个类的其余部分使用嵌套类将其字段的输入Map解析为可用的对象。它还包含我为特定应用程序创建的对象/函数。
我希望这个答案对某些人有帮助,关于ScriptRunner for bitbucket的文档有限。

相关问题