Groovy + Insight (Jira) 拆分方法异常

Groovy + Insight (Jira) Exception on split method

我正在尝试使用 Groovy 创建一个脚本,以便能够将 Jira 问题自动影响到 Insight(Jira 的附加组件)中的特定对象。

其实我需要拆​​分一个值。值为“2629351(AFAW16-FS01.francois.int)”,我只想要 AFAW16-FS01.francois.int 部分。如果我直接将该方法应用于文本但它不适用于字符串,我可以做到这一点。

这是我的代码:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.riadalabs.jira.plugins.insight.services.model.CommentBean;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
 
  
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);
  
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);
  
def objects = iqlFacade.findObjectsByIQLAndSchema(10,"objectTypeId = 2443");
//def test = "AF-172738"
//def ObjectInsightBean = objectFacade.loadObjectBean(test)
//log.warn("ObjectInsightBean " + ObjectInsightBean)
def n = 0
(objects).each {
    CurrentObject = objects[n]
    def FQDNValue = objectFacade.loadObjectAttributeBean(CurrentObject.getId(),47464).getObjectAttributeValueBeans()[0]; //Load Attribute Value
    //log.warn("Server " + objects[n])
    //log.warn("FQDNValue " + FQDNValue)
    //FQDNValueSTR = FQDNValue.ToString()
    log.warn("FQDNValue brut" + FQDNValue)   
    def values = '2629351(AFAW16-FS01.francois.int)'.split("\("); //WORKS !
    def FQDNSplit = FQDNValue.split("\("); // NOT WORKS

    
    def Value1 = values[1]
    def Value2 = Value1.substring(0, Value1.length() - 1);
    
    //log.warn("Values " + Value2)
    //result = (issue.getSummary()) //Show subject
      
    //log.warn("result " + result)
    n ++
      
}

FQDNSplit 包含“2629351(AFAW16-FS01.francois.int)”,但出现以下错误:

class com.riadalabs.jira.plugins.insight.common.exception.GroovyInsightException

GroovyInsightException: No signature of method: com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeValueBean.split() is applicable for argument types: (java.lang.String) values: [\(] Possible solutions: split(groovy.lang.Closure), wait(), wait(long), getAt(java.lang.String), print(java.lang.Object), sprintf(java.lang.String, java.lang.Object)'

有什么想法吗?谢谢!!

简单的命令提取您想要的子字符串:

String res = '2629351(AFAW16-FS01.francois.int)'.replaceFirst( /\d\(([^\(\)]+)\)/, '' )
assert res == 'AFAW16-FS01.francois.int'

此外,您应该坚持 Java 命名约定,并且不要命名以大写字母开头的变量。

您似乎正试图对非字符串的对象使用字符串操作。如果将值转换为字符串,则此代码将起作用:

FQDNValue.replaceAll(/.*\((.*)\)/,'')

您需要一种获取字符串值的方法。

例如

FQDNValue.getTextValue().replaceAll(/.*\((.*)\)/,'')

您更新后的代码可能如下所示:

  def FQDNValue =
    objectFacade.loadObjectAttributeBean( 
      CurrentObject.getId(),47464
    ).getObjectAttributeValueBeans()[0].getTextValue() 

  assert FQDNValue == '2629351(AFAW16-FS01.francois.int)'

  println "old: $FQDNValue"
  def newValue = FQDNValue.replaceAll(/.*\((.*)\)/,'')
  println "new: $newValue"

有了这个输出:

old: 2629351(AFAW16-FS01.francois.int)
new: AFAW16-FS01.francois.int

这应该作为 ObjectAttributeValueBean has a getTextValue 方法。

使用 getTextValue

ObjectAttributeValueBean has a getTextValue 方法,即 returns 一个 String:

public String getTextValue()

您能描述一种将 FQDNValue 转换为字符串的简单方法吗?

我在Google上没有找到好的方法。似乎与 class 一起工作,但我不明白好的方法。

我继续编写脚本并找到拆分的好解决方案。这是我使用的(我重命名了字符串以便更好地理解)

要转换为字符串,我只是这样做(哈哈)

def fqdn_string = fqdnvalue.toString()

现在我被屏蔽到代码的最后一部分:(也许你能理解为什么我不能更新问题

    import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField

import com.riadalabs.jira.plugins.insight.services.model.CommentBean;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectAttributeBean
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import groovy.transform.ToString
  
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade");
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass);

//CustomField jiraCustomField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12124);
CustomField valueCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12124);  
CustomField insightCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(12124);


Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade");
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass);
  
def objects = iqlFacade.findObjectsByIQLAndSchema(10,"objectTypeId = 2443");
//def test = "AF-172738"
//def ObjectInsightBean = objectFacade.loadObjectBean(test)
//log.warn("ObjectInsightBean " + ObjectInsightBean)
def n = 0
(objects).each {
    CurrentObject = objects[n]
    def fqdnvalue = objectFacade.loadObjectAttributeBean(CurrentObject.getId(),47464).getObjectAttributeValueBeans()[0]; //Load Attribute Value

    
   
    def fqdn_string = fqdnvalue.toString()
    def fqdn_string_split = fqdn_string.split("\("); 
    def fqdn_string_split_1 = fqdn_string_split[1]
    def fqdn_string_split_2 = fqdn_string_split_1.substring(0, fqdn_string_split_1.length() - 2);
    
    result = (issue.getSummary().contains(fqdn_string_split_2)) // if the value fqdn_string_split_2 present in the summary => result = true
    if (result==true){
        log.info("Statement " + "True" + CurrentObject);
        MutableIssue mi = (MutableIssue) issue;
        mi.setCustomFieldValue(insightCF, objects[n]); // => Work if i replace objects[n] by objects
        ComponentAccessor.getIssueManager().updateIssue(currentUser, mi, EventDispatchOption.DO_NOT_DISPATCH, false); // Error  GroovyInsightException: com.riadalabs.jira.plugins.insight.services.model.ObjectBean cannot be cast to java.util.Collection'
        }
    log.info("fqdn_string_split_2 " + fqdn_string_split_2)
    log.info("Result " + result)
    n ++
    return result;
}

您可以通过现有的方法访问零件

def FQDNValue = objectFacade.loadObjectAttributeBean(CurrentObject.getId(),47464).getObjectAttributeValueBeans()[0]; //Load Attribute Value

def id = FQDNValue.getId(); // "2629351"
def value = FQDNValue.getValue(); // "AFAW16-FS01.francois.int"