Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

JAVA API Examples


java
Code Block
languagejavagroovy
themeEclipse
firstline1
titlereadFieldData usage example
linenumberstrue
collapsetrue
import com.atlassian.crowd.embedded.api.User;// This script reads all data from a Grid custom field with default
// configuration named "API sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
import
org.apache.log4j.Logger;

// setGet upissue logger
Logger log=Logger.getLogger("com.idalko.scripts");

// get an issue
id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TEST-31");
Long issueId = issue.getId().toLong();

// getGet TGETGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomFieldtgngCustomField = customFieldManager.getCustomFieldObjectsByName("GridAPI Onesample").get(0);
Long tgeCustomFieldIdtgngCustomFieldId = tgeCustomFieldtgngCustomField.getIdAsLong();

ApplicationUser applicationUseruser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// readGet gridGridService
data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService");
def gridFieldDatagridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass);)

// Read field data
def fieldData = gridService.readFieldData(issueId, tgngCustomFieldId, user, null)
def gridRows = fieldData.getRows()

StringBuilder result = new StringBuilder();
deffor callResult(row =in null;gridRows) {
try {   def  callResultcolumns = gridFieldDatarow.readFieldDatagetColumns(issueId,)
tgeCustomFieldId, applicationUser, null); } catch (Exception e) { result.append("Order: ${columns.get("jseq")}, ")
     logresult.error(e.getMessage())
}append("Summary: ${columns.get("jsummary")}, ")
     def gridRowsassignee = callResultcolumns.getRowsget("jassignee")
 for   (row in gridRows) {result.append("Assignee: ${assignee == null ? "null" : assignee.get("username")}, ")
    def columns = row.getColumns();
    def rowId = row.getRowId();
    //log.error(columns.toString())
    StringBuilder tempResult = new StringBuilder();
    
    for (column in columns.keySet()) {
        tempResult.append("column ID: " + column + ", value: " + columns.get(column) + ", " + System.lineSeparator())        
    }
    
    result.append("Grid ID=" + tgeCustomFieldId + ", row ID: " + rowId + ", columns " + tempResult + "<br>")
}


return result;

Code Block
language
result.append("Status: ${columns.get("jstatus")};<br>")
}

return result


Code Block
languagegroovy
themeEclipse
firstline1
title addRows usage example
linenumberstrue
collapsetrue
// This script adds two rows to Grid custom field with default configuration
// named "API sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.user.ApplicationUser

// Get issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
Issue issue = issueManager.getIssueObject("TEST-1")
Long issueId = issue.getId()

// Get TGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class)
CustomField tgngCustomField = customFieldManager.getCustomFieldObjectsByName("API sample").get(0)
Long tgngCustomFieldId = tgngCustomField.getIdAsLong()

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get GridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService")
def gridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass)

// Add a row
// Only "key" is required for userlist columns
gridService.addRows(issueId, tgngCustomFieldId, user, [
    ["jsummary": "Buy groceries", "jassignee": ["key": user.getKey()], "jstatus": "Open"],
    ["jsummary": "Plan vacation", "jassignee": ["key": user.getKey()], "jstatus": "In Progress"]
])


Code Block
languagegroovy
themeEclipse
firstline1
title addRows updateRows usage example
linenumberstrue
collapsetrue
import com.atlassian.crowd.embedded.api.User;// This script moves all rows assigned to a current user in status "In Review"
// to status "Closed" in a Grid custom field with default configuration named
// "API sample" in issue TEST-1

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
import
org.apache.log4j.Logger;

// setGet up logger
Logger log=Logger.getLogger("com.idalko.scripts");

// get an issue
issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TEST-31");
Long issueId = issue.getId().toLong();

// getGet TGETGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomFieldtgngCustomField = customFieldManager.getCustomFieldObjectsByName("GridAPI Onesample").get(0);
Long tgeCustomFieldIdtgngCustomFieldId = tgeCustomFieldtgngCustomField.getIdAsLong();

ApplicationUser applicationUseruser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// readGet grid dataGridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService");
def gridFieldDatagridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass);

StringBuilder// resultGet =GridRow new StringBuilder();
def callResult = null;

try {
    callResult = gridFieldDataclass
Class gridRowClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridRow")

// Read field data
def fieldData = gridService.readFieldData(issueId, tgeCustomFieldIdtgngCustomFieldId, applicationUseruser, null);
}def catchgridRows (Exception e) {
    log.error(e.getMessage())
}

// let's add some rows
Map<String, Object> row= fieldData.getRows()

// Update rows
def updates = new HashMap<String, Object>ArrayList();
for row.put("jseq", callResult.getRows().get(callResult.getRows().size() - 1).getColumns()(row in gridRows) {
    def columns = row.getColumns()
    def assignee = columns.get("jseqjassignee") + 1);
row.put("jsummary", "Some summary");
row.put("jstatus", "Done");

//Script Runner can show validation errors here as well. Ignore them.
try {
    if (assignee != null && assignee.get("key") == user.getKey() && columns.get("jstatus") == "In Review") {
        def update = gridRowClass.newInstance()
     List<Long> rowIds = gridFieldDataupdate.addRowssetRowId(issuerow.getIdgetRowId(), tgeCustomFieldId, applicationUser, [row]);
    result.append("Grid ID=" + tgeCustomFieldId + " data was successfully added. IDs of added rows: " + rowIds + "\n");
} catch (Exception e) {)
        // We need to specify only the columns which are being updated
        update.setColumns(["jstatus": "Closed"])
        logupdates.error(e.getMessage())add(update)
    }
}

return result;gridService.updateRows(issueId, tgngCustomFieldId, user, updates)



Code Block
languagejavagroovy
themeEclipse
firstline1
titleupdateRows deleteRows usage example
linenumberstrue
collapsetrue
import com.atlassian.crowd.embedded.api.User;// This script deletes all rows assigned to a current user in status "Closed" in
// a Grid custom field with default configuration named "API sample" in issue
// TEST-1

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;
import
org.apache.log4j.Logger;

// set up logger
Logger log=Logger.getLogger("com.idalko.scripts");

//grid named 'Grid One' with the default configuration, in issue 'TEST-3'

// get an issue
// Get issue id
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("TEST-31");
Long issueId = issue.getId().toLong();

// getGet TGETGNG custom field id
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomFieldtgngCustomField = customFieldManager.getCustomFieldObjectsByName("GridAPI Onesample").get(0);
Long tgeCustomFieldIdtgngCustomFieldId = tgeCustomFieldtgngCustomField.getIdAsLong();

ApplicationUser applicationUseruser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

// readGet grid dataGridService
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class apiServiceClass = pluginAccessor.getClassLoader().findClass("com.idalko.tgng.jira.server.api.GridService");
def gridFieldDatagridService = ComponentAccessor.getOSGiComponentInstanceOfType(apiServiceClass);

StringBuilder// resultRead = new StringBuilder();field data
def callResultfieldData = null;

try {
    callResult = gridFieldDatagridService.readFieldData(issueId, tgeCustomFieldIdtgngCustomFieldId, applicationUseruser, null);
def }gridRows catch= fieldData.getRows(Exception e)
{
// Ids of  log.error(e.getMessage())
}
    def gridRows = callResult.getRows()
rows to delete
def toDelete = new ArrayList()
for (row in gridRows) {
    def columns = row.getColumns();
    def rowIdassignee = rowcolumns.getRowIdget("jassignee");
    if (assignee != null  StringBuilder columnResult = new StringBuilder();
    
    for (column in columns.keySet()) {
        columnResult.append("column ID: " + column + ", value: " + columns.get(column) + ", " + System.lineSeparator&& assignee.get("key") == user.getKey() && columns.get("jstatus") == "Closed") {
        toDelete.add(row.getRowId())
    }
  
    }

   
    result.append("Grid ID=" + tgeCustomFieldId + ", row ID: " + rowId + ", columns " + columnResult + "<br>")
}




return result;gridService.deleteRows(issueId, tgngCustomFieldId, user, toDelete)