Inserting value with script runner in idalko table grid

Dante Labate August 22, 2017

Some time ago I found this script on the internet and adapted to my need. This script only works if the user has the jira-administrator permission. Could someone help me to adapt it to work for jira-user?



import com.atlassian.crowd.embedded.api.User;
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;

// get an issue
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);

// get TGE custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectsByName("Disponibilidade / SLA").get(0);
Long tgeCustomFieldId = tgeCustomField.getIdAsLong();
//Campos

def nova_categoria = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12100"))
def novo_tempo_reparo = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12102"))
def nova_disponibilidade = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12103"))
// get current user
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = jiraAuthenticationContext.getLoggedInUser();
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;

// read grid data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");
def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);

StringBuilder result = new StringBuilder();
def callResult = null;
try {
callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
result.append("Grid ID=" + tgeCustomFieldId + " content before modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}

// let's add some rows
Map<String, Object> row = new HashMap<String, Object>();
row.put("categoria_do_site", "${nova_categoria.toString()}");
row.put("tempo_de_reparo", "${novo_tempo_reparo}");
row.put("disponibilidade", "${nova_disponibilidade}");

try {
List<Long> rowIds = tgeGridDataManager.addRows(issue.getId(), tgeCustomFieldId, Arrays.asList(row), user);
result.append("Grid ID=" + tgeCustomFieldId + " data was successfully added. IDs of added rows: " + rowIds + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be added: " + e.getMessage() + "\n");
}

// check if data was really added
try {
callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
result.append("Grid ID=" + tgeCustomFieldId + " content after modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}

 

2 answers

0 votes
francis
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 22, 2017

Thanks @Maarten Cautreels (Belgium)

 

 

Dante,

The TGE Api requires a user to read/edit grid data as it checks if the required permissions (edit for modifying the grid and browse for reading it) are set. 

There is no need to have administration permisisons.

Could you check ?

If the grid is not behaving as expected, please raise an issue by sending a mail to support dot tge at idalko dot com

 

 

Thanks

 

 

Francis

Dante Labate August 23, 2017

Thanks for the answer.

Could you inform me where I check this permission (read and write in the grid)?

I removed those grid configuration lines thinking it could solve, and it did not work.

gd.delete.allowed=false
gd.allowAddRows=false
gd.hideFooter=false

I gave permission for the user to edit the issue and it also did not work. (In the transition screen has the custom fields of the jira where the user will enter the information and then down the grid to see what he has already registered).

 

Nothing prevents the user from manually entering information directly on the grid (but I do not want it done this way, so I have these lines and hide the grid interaction buttons)

If I test by manually inserting directly into the grid I can save the information.

francis
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 24, 2017

Hi Dante,

 

First of all - the gd.* properties will affect the UI behaviour of the grid and not the REST behaviour.

 

The fact that you're user cannot update the grid through REST but can through the UI can be rooted in either a bug in the add-on or a configuration problem

 

Can you enable debug logging on the grid as detailed here

https://wiki.idalko.com/display/TGPD/Send+us+data+about+your+support+case

 

And send it to support dot tge at idalko dot com

 

We'll be handling the case from there

 

Thanks

 

Francis

0 votes
Maarten Cautreels
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 22, 2017

Hi @Dante Labate,

In case you were wondering why your post was not visible, it was marked as spam. Probably because the code snippet. 

I've now taken it out of the SPAM quarantine and I was told that the SPAM filter learns from these decissions, so hopefully this won't happen again in the future.

Unfortunately I don't know the answer to your question but I'll see if I can reach someone from iDalko to get your question answered.

Best,

Maarten

Suggest an answer

Log in or Sign up to answer