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 ;) ![]()
41 Comments
Mizan
2/5/2012 05:01:14 pm
Best tutorial for understanding AJS and REST . Thank you JTricks :)
Reply
J-Tricks
2/6/2012 12:33:45 am
Thanks :) Btw, if you just need to get the current user and do something with it, you can use the query 'get' method like this:
Reply
Dave
2/21/2012 12:29:54 pm
Hi J-Tricks,
Reply
J-Tricks
2/22/2012 02:05:00 pm
Did you try including the resources as follows?
Reply
Dave
2/27/2012 07:55:28 pm
Hi J-Tricks,
Sebastian Wainer
7/16/2012 12:50:14 am
hello J-Tricks,
Reply
J-Tricks
7/20/2012 12:41:46 pm
Try adding the script in the description of the field under field configurations instead of adding it in the announcement banner.
Reply
Dan
6/26/2013 12:44:35 am
Hi Sebastian,
Reply
Mizan
8/28/2012 01:11:11 am
Hi JTricks ,
Reply
J-Tricks
8/28/2012 04:23:02 am
Mizan,
Reply
9/1/2012 07:46:31 pm
Great tutorial on REST and AJS. Atlassian's documentation is rather hard to follow whereas this feels very easy for beginners.
Reply
Srinivas
10/8/2012 01:11:59 pm
Hi Jobin, Could you please let me know if there any tutorials on the usage of AUI components?
Reply
J-Tricks
10/8/2012 02:23:51 pm
I usually find these pages useful.
Reply
Srinivas
10/8/2012 03:59:27 pm
Thanks for your reply. I would like to know more about the steps to be followed for configuring and working on AUI. I knew those links for the demo pages and the high level documentation. Can you tell me what dependencies need to include and what resources are required in the atlassian plugin.xml file? 4/9/2013 12:03:43 am
I have found your post because I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject.
Reply
dhaval
5/7/2013 09:08:46 pm
Thanks for this tutorial.
Reply
J-Tricks
5/8/2013 10:38:30 am
You can use the JIRA User picker custom field, right? If you want to customize, that custom field is the best place to start from.
Reply
dhaval
5/8/2013 06:58:37 pm
Thanks for your comments. So, i just found "userpickersearcher" at below link:
J-Tricks
5/9/2013 12:41:58 am
Well, that is different. You need to start with the custom field and not its searcher. See http://www.j-tricks.com/1/post/2010/08/custom-fields.html
Dan
6/25/2013 03:27:03 am
great guide, although I cannot seem to get it to work with jira 5.1?
Reply
J-Tricks
6/26/2013 03:58:35 am
It is because of the change in REST API url. Modify URL in getGroups method to have /api/2/user instead of /api/2.0.alpha1/user.
Reply
Dan
6/26/2013 04:36:52 am
Thank you for your reply I have update the code and it is now working. 6/26/2013 01:14:23 am
I have no previous experience in jquery and AJAX. Is there any other method to extract the username, I mean other than the one you specified here? Can you please explain it in a bit more detail? Thanks in advance.
Reply
J-Tricks
8/14/2014 01:37:29 am
Thanks. Seems it came in the later version. Really useful :)
Reply
J-Tricks
9/20/2014 02:07:21 am
You can do this:
Reply
Hi Jtricks,
J-Tricks
9/21/2014 04:39:37 am
What do you mean by teams? JIRA doesn't have the concept of a team. Maybe you are using a plugin?
Hi,
J-Tricks
9/23/2014 01:32:59 am
You need to check the plugin documentation to find out if they have a method that exposes it.
Asif Khan
10/27/2014 09:22:32 pm
Hi ,
Reply
J-Tricks
10/28/2014 02:13:29 am
/rest/tempo-teams/1/team will get you teams.
Reply
Asif Khan
10/29/2014 01:29:22 am
Hi J-Tricks,
Asif Khan
10/29/2014 01:32:11 am
I used the url; /rest/tempo-teams/2/team/{2}/member
J-Tricks
10/30/2014 03:00:35 pm
Yes, all the 4 returned data for me. Maybe the name space (2 in this case) is different for the version of the plugin that you have.
bash
11/16/2015 07:50:54 am
Hi J-Tricks,
Reply
J-Tricks
11/16/2015 01:40:43 pm
You can tweak the above javascript and add it in the announcement banner to do the redirection when a user is not in the admin group.
Reply
J-Tricks
11/16/2015 01:41:26 pm
Having said that, our recommendation would be to use Apache to serve the maintenance page and do the redirection ;) 3/17/2016 02:35:43 pm
This information is quite informative and very useful. For a upcoming web developer this would be a good choice to being with code.
Reply
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorJobin Kuruvilla - Works in Adaptavist as Head of DevOps Professional Services. Categories
All
Archives
October 2016
|