Skip to content

Instantly share code, notes, and snippets.

@issacg
Created July 16, 2013 15:44
Show Gist options
  • Save issacg/6009893 to your computer and use it in GitHub Desktop.
Save issacg/6009893 to your computer and use it in GitHub Desktop.
JIRA Script-Runner canned script to increment the value of an (assumed numeric) custom field
/*
* Copyright 2013 Issac Goldstand <margol@beamartyr.net>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.onresolve.jira.groovy.canned.workflow.postfunctions
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.util.SimpleErrorCollection
import com.onresolve.jira.groovy.canned.CannedScript
import org.apache.log4j.Category
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.fields.CustomField
class UpdateCFValue implements CannedScript{
public static final String FIELD_CUSTOM_FIELD = 'FIELD_CUSTOM_FIELD'
ComponentManager componentManager = ComponentManager.getInstance()
Category log = Category.getInstance(UpdateCFValue.class)
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String getName() {
return "Increment CF count"
}
String getDescription() {
return "A postfunction used for incrementing the value of a numeric custom field"
}
List getCategories() {
["Function"]
}
List getParameters(Map params) {
[
[
Name:FIELD_CUSTOM_FIELD,
Label:"Counter Field",
Type: "list",
Description:"""Custom field to increment
""",
Values: getAllCFields(true),
],
]
}
Map getAllCFields(boolean withBlankFirstEntry) {
Map<String,String> rt = [:] as Map<String,String>
if (withBlankFirstEntry) {
rt.put("", "")
}
customFieldManager.getCustomFieldObjects().each { CustomField cf ->
rt.put(cf.getIdAsLong(), cf.getName())
}
rt
}
public ErrorCollection doValidate(Map params, boolean forPreview) {
ErrorCollection errorCollection = new SimpleErrorCollection()
if (!params[FIELD_CUSTOM_FIELD]) {
errorCollection.addError(FIELD_CUSTOM_FIELD, "You must provide the target custom field.")
}
// To-do: ensure that the subtask is a compatible (numeric, text?) field type
return errorCollection
}
Map doScript(Map params) {
log.debug ("called doScript with params: ${params}")
Issue issue = params['issue'] as Issue
def cf = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObject(params[FIELD_CUSTOM_FIELD] as Long)
Double val = (issue.getCustomFieldValue(cf) as Double) + 1
log.debug ("Setting " + cf.getName() + " to " + val)
issue.setCustomFieldValue(cf, val)
return params
}
String getDescription(Map params, boolean forPreview) {
return getName() + " : will increment the number stored in <b>" + customFieldManager.getCustomFieldObject(params[FIELD_CUSTOM_FIELD] as Long).getName() + "</b>"
}
public Boolean isFinalParamsPage(Map params) {
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment