September 17, 2013

JIRA Agile Script


We recently converted to JIRA and apart of our process allows engineerings to accept Ad-Hoc requests from the business owners. So I wanted to the ability for a developer to quickly be able to grab a new ticket and add it to the current sprint. The only solution I could find was to write a script using the JIRA Script Runner plugin. 

The following is the script that I wrote. It does the following:
  • automatically assigns members of the "jira-product" group as watchers of the issue
  • adds the issues to the current sprint. The algorithm is to find the lowest number sprint ID that is not closed.
  • sets our custom field "Ad Hoc Requests" (id=10305) to 1. This way we can run reports on these requests.



importPackage(java.lang);

run();

function run() {
 var componentManager = com.atlassian.jira.ComponentManager.getInstance();
 var user = componentManager.getJiraAuthenticationContext().getUser();

 var watcherManager = componentManager.getWatcherManager();
 var userManager = componentManager.getComponentInstanceOfType( com.atlassian.jira.user.util.UserManager );
 var groupManager = componentManager.getComponentInstanceOfType( com.atlassian.jira.security.groups.GroupManager );

 if ( ! userManager ) {
     log("User Manager not found");
     return 0;
 }

 if ( ! groupManager ) {
     log("Group Manager not found");
     return 0;
 }


 var admin = userManager.getUserByName("admin");
 log("JIRA="+admin);

 var productGroup = groupManager.getGroup("jira-product");
 if ( productGroup ) {
     var users = com.atlassian.jira.user.ApplicationUsers.from(groupManager.getUsersInGroup(productGroup)).iterator();
     while ( users.hasNext() ) {
  var usr = users.next();
  
  log("Adding Watcher : " + usr.getClass() );
  watcherManager.startWatching( usr , issue );
     }
 }

     // finish adding watchers

 log("JS : " + user + " " + issue + " " + issue.getClass() );

 var commentManager = componentManager.getComponentInstanceOfType( com.atlassian.jira.issue.comments.CommentManager ); 
 var customFieldManager = componentManager.getComponentInstanceOfType( com.atlassian.jira.issue.CustomFieldManager );

 if ( ! customFieldManager ) {
  log("Null Custom Field Manager");
         return 0;
 }

 var sprintFld = customFieldManager.getCustomFieldObject( 10000 );
 var adhocFld = customFieldManager.getCustomFieldObject( 10305 );  
 if ( ! sprintFld ) {
  log("Sprint field not found");
  return 0;
 }

 if ( ! adhocFld ) {
     log("Ad Hock Field Not Found");
 }

        var sprintManager = getSprintManager();

 if ( ! sprintManager ) {
     log("SprintManager not found");
     return 0;
 } else {
     log("Found SprintManager");
 }

 if ( commentManager && issue ) {
     if ( sprintManager ) {
  var sprintId = null;
  var sprint = null;
  var value = issue.getCustomFieldValue(sprintFld);
  
  log("Sprint = " + value );
  var soutcome = sprintManager.getAllSprints();
  if ( soutcome.isValid() ) {
      var allSprints = soutcome.getValue();
      
      if ( allSprints ) {
   var itt = allSprints.iterator();
   while( itt.hasNext() ) {
       var next = itt.next();
       if ( next.isClosed() ) continue;
       
       if ( ! sprintId || sprintId.longValue() > next.getId().longValue() ) {
    sprintId = next.getId();
    sprint = next;
       }
   }
      }
  }
  
  if ( sprint ) {
      var newValue = new java.util.ArrayList();
      newValue.add( sprint );
      sprintFld.updateValue(null, issue, new com.atlassian.jira.issue.ModifiedValue(value,newValue), new com.atlassian.jira.issue.util.DefaultIssueChangeHolder());

      adhocFld.updateValue(null,issue,new com.atlassian.jira.issue.ModifiedValue(issue.getCustomFieldValue(adhocFld),1), new com.atlassian.jira.issue.util.DefaultIssueChangeHolder());
  }
     }

     var msg = user + " accepted story as ad-hoc request";
     if ( sprint ) {
  msg = msg + " and assigned to sprint: " + sprint.getName();
     }
     commentManager.create(issue, admin , msg , true);
     issue.store();
 }
}


function getSprintManager() {
    var appCtx = getGreenHopperAppCtx();
    if ( appCtx ) {
 log("Found AppCtx");
 return appCtx.getBean( "sprintManagerImpl" );
    }
    return null;
}

function getGreenHopperAppCtx() {
    var componentManager = com.atlassian.jira.ComponentManager.getInstance();
    var osgi = componentManager.getComponentInstanceOfType (com.atlassian.plugin.osgi.container.OsgiContainerManager);
    if ( ! osgi ) {
 java.lang.System.out.println("OSGI Not Found");
 return null;
    }

    var bundles = osgi.getBundles();

    var sprintManager;
    for(var i=0;i<bundles.length;i++) {
 var bundle = bundles[i];

        if ( "com.pyxis.greenhopper.jira".equals( bundle.getSymbolicName() ) ) {
     log("Found JIRA Agile Plugin");

     var bctx = bundle.getBundleContext();
            var refs = bctx.getAllServiceReferences(null,null);
     if ( refs ) {
  for(var j=0; j<refs.length;j++) {
      var prop = refs[j].getProperty("org.springframework.context.service.name");
      if ( "com.pyxis.greenhopper.jira".equals(prop) ) {
   log ( "Found Greenhopper ServiceReference : " +  prop );
   return bctx.getService( refs[j] );
      }
  }
     }
 }

 //log ( bundle );

    }

    return null;
}

 
function log(msg) {
    java.lang.System.out.println( "" + msg);
}

No comments: