Inserting value with script runner in idalko table grid Edited

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 vote
Maarten Cautreels Community Champion Aug 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

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

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.

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

90 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you