Skip to content

Instantly share code, notes, and snippets.

@francismartens318
Created October 7, 2013 07:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francismartens318/6863710 to your computer and use it in GitHub Desktop.
Save francismartens318/6863710 to your computer and use it in GitHub Desktop.
import java.util.List;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.version.VersionManager;
import com.atlassian.jira.issue.MutableIssue;
import com.idalko.jira.groovy.FieldValueUtil;
import org.apache.log4j.Category;
log = Category.getInstance("com.idalko.scripts.manageVersions");
log.debug("Adapt the versions on issue ${issue.getKey()}");
VersionManager vm = ComponentManager.getInstance().getVersionManager();
FieldValueUtil fvuAddFixVersion = new FieldValueUtil("Add fix version");
FieldValueUtil fvuDelFixVersion = new FieldValueUtil("Remove fix version");
/*
* Add the selected version to the issue if not yet there.
*/
MutableIssue mi = (MutableIssue) issue;
Collection<Version> issueFixVersions = mi.getFixVersions();
Boolean updateFixVersions = false;
/*
* For all versions, if the version is not yet in the list, add it
*/
List vList = null;
vList = fvuAddFixVersion.getFieldValue(mi);
if (vList) {
/*
* User wants to add a version.
*/
String addFixVersionName = vList.get(0);
Boolean addNewFixVersion = true;
issueFixVersions.each {
Version iVersion = (Version) it;
if (addFixVersionName == iVersion.getName()) {
addNewFixVersion = false;
}
}
if (addNewFixVersion) {
log.debug("${addFixVersionName} should be added to ${issueFixVersions}");
Version newFixVersion = vm.getVersion(mi.getProjectObject().getId(), addFixVersionName);
if (newFixVersion) {
issueFixVersions.add(newFixVersion);
updateFixVersions = true;
}
}
}
/*
* For all versions, if the remove version is in the list, remove it
*/
Version delVersion = null;
vList = fvuDelFixVersion.getFieldValue(mi);
if (vList) {
String delFixVersionName = vList.get(0);
issueFixVersions.each {
Version iVersion = (Version) it;
if (delFixVersionName == iVersion.getName()) {
delVersion = iVersion;
}
}
if (delVersion) {
log.debug("${delFixVersionName} should be removed from ${issueFixVersions}")
issueFixVersions.remove (delVersion);
updateFixVersions = true;
}
else {
log.debug("'${delFixVersionName}' not found in list ${issueFixVersions}")
}
}
if (updateFixVersions) {
log.debug("Resulting fixVersion list = ${issueFixVersions}");
mi.setFixVersions(issueFixVersions);
mi.store();
}
@migr1
Copy link

migr1 commented Nov 27, 2013

I'd really like to use this script - but where do I get the package "com.idalko.jira.groovy.FieldValueUtil"?

@ajroylance
Copy link

Me too - where can I get the package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment