Worklog Verification Scripts
Both "Create/Update" and "Delete" scripts are provided with following build-in objects:
worklog: An instance of Worklog object and using this object it is possible to access Worklog parameters such as Issue, Project, Author and some other objects related with worklog.
worklogAttributes: A map of worklog attribute id as integer to provided attribute value as string.
If script returns any result other than null worklog verification fails and string value of returned object is displayed to user as error message.
All worklog attributes are accessible using "worklogAttributes" map as string , so don't forget to apply necessary conversion. It is a map from attribute id to attribute value.
Boolean: Either "yes" or "no"
Number: Number value as string, such as "12345". If you need integer value use Integer.valueOf.
Single Select: Id of attribute value as string, such as "1", "2".
Duration: String representation of duration in seconds. For example "2h 30m" is "9000".
Accessing Various Jira and WorklogPRO Services
This script access various Jira and WorklogPRO services using ComponentAccessor class. Note that to access WorklogPRO services you need to use "getOSGiComponentInstanceOfType" method. This script checked whether a "Billable" boolean attribute exist. If it doesn't exist logging work is allowed. If it exist, check worklog is marked as billable. If it is billable, it check whether the current user has "Service Desk Team" role in the project. If the user doesn't have "Service Desk Team" role, logging work is disallowed.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.deniz.jira.worklog.services.attr.AttrTypeService;
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class);
//We need to load WorklogPRO classes differently using getOSGiComponentInstanceOfType
def attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class);
def billableAttrType = attrTypeService.getAttrTypeWithName("Billable").get();
if (billableAttrType == null) {
return; //there is no billable attribute
}
def isBillable = worklogAttributes.get(billableAttrType.getID());
def loggedInUser = authenticationContext.getLoggedInUser();
def serviceDeskTeamRole = projectRoleManager.getProjectRole("Service Desk Team");
def project = worklog.getIssue().getProjectObject();
println(worklogAttributes);
if (isBillable == "yes") { //boolean attributes are has either "no" or "yes" value.
if (!projectRoleManager.isUserInProjectRole(loggedInUser, serviceDeskTeamRole, project)) {
return "Only Service Desk Team can register billable worklogs!"
}
}
Accessing Other Apps's Components
import com.deniz.jira.versioning.CmpVersionMappingService;
import com.deniz.jira.worklog.scripting.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.deniz.jira.versioning") //this is the plugin key. you can get it from "manage apps" screen
def cmpVersionMappingService = ComponentAccessor.getOSGiComponentInstanceOfType(CmpVersionMappingService.class);
Accessing Database within Scripts
import com.atlassian.jira.component.ComponentAccessor
import org.slf4j.*;
import com.deniz.jira.worklog.*;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import groovy.sql.Sql;
import java.sql.*;
def issueId = worklog.getIssue().getId().toString();
def issueKey = worklog.getIssue();
def project = issue.getProjectObject().getId();
String SQLStmt = """INSERT INTO mydb.schema.table VALUES (""" + issueId + """, '""" + issueKey + """', """ + project + """) """;
// username and passowrd
def props = new Properties();
props.setProperty("user", "theuser");
props.setProperty("password", "thepassword");
def driver = Class.forName('net.sourceforge.jtds.jdbc.Driver').newInstance() as Driver;
def conn = driver.connect("jdbc:jtds:sqlserver://SERVERNAME:1433/INSTANCE", props);
if (conn) {
def sql = new Sql(conn)
try {
def result = sql.executeQuery(SQLStmt);
sql.close()
conn.close()
}
catch (Exception e){
log.error(e.toString())
}
} else {
log.error("Could not connect to SQL database.")
}
You can use following sample scripts as a baseline for your own scripts
If you have a specific requirement please let us know, we can work on your script together. If you have a useful script and if you want to share it with community you can send it to us and we can add it to our list of sample scripts.