简体   繁体   English

如果自定义字段值小于 1,则使用 Groovy 阻止 Jira 开票

[英]Block Jira opening ticket if custom field value is less than 1 using Groovy

i need to block the creation of an issue on Jira if a custom field value is less than 1.如果自定义字段值小于 1,我需要阻止在 Jira 上创建问题。

I tried with this code with no success, the custom field ID is: 13200.我尝试使用此代码没有成功,自定义字段 ID 为:13200。

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.opensymphony.workflow.InvalidInputException;
import org.apache.log4j.Category;


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import groovy.xml.MarkupBuilder;
import com.atlassian.jira.config.properties.APKeys;

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_13200")
def cFieldValue = issue.getCustomFieldValue(cField) as Integer


if (cFieldValue > 0){
log.debug("ERRORE, QUANTITA' NON DISPONIBILI");
throw "ERRORE";
throw new InvalidInputException("QUANTITA' NON DISPONIBILI");
return cFieldValue;
return false;
}

That custom field is taken from another DB (linked via Elements) and it's the quantity of a particular item, if the quantity is less than 0 the ticket should throw an exception but it doesnt work as i would, giving me this error in the LOG:该自定义字段取自另一个数据库(通过元素链接),它是特定项目的数量,如果数量小于 0,票证应该抛出异常,但它不能像我一样工作,在日志中给我这个错误:

For input string: "{"keys":["47"]}"
java.lang.NumberFormatException: For input string: "{"keys":["47"]}"
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
 at java.lang.Integer.parseInt(Integer.java:580)
 at java.lang.Integer.valueOf(Integer.java:766)

The taken value is right (47) but maybe it's in a wrong type?取值是正确的(47),但可能是错误的类型?

Can anybody help me?有谁能够帮我?

Thank you, Luca谢谢你,卢卡

Your variable content is "{"keys":["47"]}", not "47".您的变量内容是“{“keys”:[“47”]}”,而不是“47”。 You can see this here: java.lang.NumberFormatException: For input string: "{"keys":["47"]}"你可以在这里看到这个:java.lang.NumberFormatException: For input string: "{"keys":["47"]}"

that's why you get this exception, as "{"keys":["47"]}" cannot be converted to a number, so please double-check the value of the variable you are converting to a number这就是你得到这个异常的原因,因为 "{"keys":["47"]}" 不能转换为数字,所以请仔细检查要转换为数字的变量的值

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Scorte = customFieldManager.getCustomFieldObject("customfield_13200")
def ValoreScorte = issueObject.getCustomFieldValue(Scorte)
def QuantitaRichiesta = customFieldManager.getCustomFieldObject("customfield_13201")
def ValoreQuantitaRichiesta = issueObject.getCustomFieldValue(QuantitaRichiesta)
def casting_ValoreScorte = ValoreScorte.toString().replaceAll("[^\\d.]", "") 
def Scorte_Intero = casting_ValoreScorte as int
def QuantitaRichiesta_Intero = ValoreQuantitaRichiesta as int

if (Scorte_Intero <= 0 || QuantitaRichiesta_Intero > Scorte_Intero){
log.debug("ERRORE, QUANTITA' NON DISPONIBILI")
throw new InvalidInputException("","QUANTITA' NON DISPONIBILI")
}

This is the final code:) Thank you for your help!这是最终的代码:)谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM