It is actually a follow-up post. A follow-up to the question Mizan asked in my last post. Before you proceed, here is warning: Strictly for AJS novices, like me!
In this post, we are looking to prepopulate jira fields based on the current user's group. And we are gonna use AJS for the same!
Let us first split the problem into 3 pieces:
1. Get the current user
2. Get the groups of current user and check if the user belongs to a given group
3. Pre-populate the needed fields
With AJS, using Javascript in JIRA is damn easy. And with the introduction of REST services, getting information from the server is even easier. Let us attack the steps one by one.
1. Get the current user
Retrieving the current user is pretty easy, thanks to the following REST method:
/rest/gadget/1.0/currentUser
It returns a JSON output of the following format:
{"username":"jobin","fullName":"Jobin Kuruvilla","email":"jobinkk@xxxx.com"}
Retrieving the user name from this using AJS is done using the jQuery's ajax method as shown:
function getCurrentUserName()
{
var user;
AJS.$.ajax({
url: "/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.username;
}
});
return user;
}
For more information on ajax() method, have a look here.
2. Get the groups of current user and check if the user belongs to a given group
Now that we have the current user, we need to get the groups the user belongs to! Back to REST APIs and here is the method that you need:
/rest/api/2.0.alpha1/user?username=xxxx
Make sure the groups is expanded by appending &expand=groups at the end. Assuming jobin is the user name, here is the full URL:
/rest/api/2.0.alpha1/user?username=jobin&expand=groups
It returns the following JSON output:
{"self":"https://localhost:8080/rest/api/2.0.alpha1/user?username=jobin","name":"jobin","emailAddress":"jobinkk@xxxx.com","avatarUrl":"https://localhost:8080/secure/useravatar?size=large&ownerId=jobin","displayName":"Jobin Kuruvilla","active":true,"timeZone":"America/Los_Angeles","groups":{"size":3,"items":[{"name":"jira-administrators"},{"name":"jira-developers"},{"name":"jira-users"}]},"expand":"groups"}
Again, we use the ajax method to get the above output and extract the groups array from it as shown:
function getGroups(user)
{
var groups;
AJS.$.ajax({
url: "/rest/api/2.0.alpha1/user?username="+user+"&expand=groups",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
groups = data.groups.items;
}
});
return groups;
}
Once the groups array is retrieved, checking for a given group is easy, isn't it?
function isUserInGroup(user, group){
var groups = getGroups(user);
for (i = 0; i < groups.length; i++){
if (groups[i].name == group){
return true;
}
}
return false;
}
3. Pre-populate the needed fields
Over to Step 3. If the user is indeed in the given group, do whatever you want to! For example, you can pre-populate the decription as follows:
var user = getCurrentUserName();
if (isUserInGroup(user, 'jira-users')){
AJS.$("#description").val('This is raised by a JIRA User!');
}
And if you are Mizan and you need to pre-populate the security level, make sure you have issue security schemes correctly configured and use the following snippet in place of description:
AJS.$("#security").val("10001");
where 10001 is the id of the appropriate securiyt level. And we are done! There might be an easier approach, but this isn't too bad, is it?
Attached is the full script in the end. Don't forget to have a look at the book ;)
| checkgroups |


RSS Feed