Search code examples
textboxjira

Jira custom script validator: check if input textbox exists


I am totally new to Jira. In fact I don't even know where to start. I went to the jira atlassian website but got nothing solid enough to help me. I would like to validate if the information entered into a textbox already exists. I clicked around jira and ended up on the screen below: enter image description here

Now I would like to find out the following:

  1. Which programming language should be used for validation ? Is it Java
  2. If the name of the custom field(of type Textbox) is XYZ and I wanna if check if value entered into XYZ already exist, how do I go about doing that ? Can I just write conditional statements in Java ?

I wrote some stuff and nothing worked.


Solution

  • That's a screenshot from the Script Runner add-on.

    There are some documentation and examples for custom validators here.

    You can also find an example here that shows how to query the JIRA (or an external) database from a groovy script. Ie.:

    import com.atlassian.jira.component.ComponentAccessor
    import groovy.sql.Sql
    import org.ofbiz.core.entity.ConnectionFactory
    import org.ofbiz.core.entity.DelegatorInterface
    
    import java.sql.Connection
    
    def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
    String helperName = delegator.getGroupHelperName("default");
    
    def sqlStmt = """
        SELECT     project.pname, COUNT(*) AS kount
        FROM       project
                   INNER JOIN jiraissue ON project.ID = jiraissue.PROJECT
        GROUP BY project.pname
        ORDER BY kount DESC
    """
    
    Connection conn = ConnectionFactory.getConnection(helperName);
    Sql sql = new Sql(conn)
    
    try {
        StringBuffer sb = new StringBuffer()
        sql.eachRow(sqlStmt) {
            sb << "${it.pname}\t${it.kount}\n"
        }
        log.debug sb.toString()
    }
    finally {
        sql.close()
    }
    

    For anything that gets a bit complex it's easier to implement your script in a groovy file and make it available to Script Runner via the file system. That also allows you use a vcs like git to easily push/pull your changes. More info about how to go about that, is here.