I'm looking for a javascript example for hiding an issue field based on user project role or Jira group membership. Whichever is easier to accomplish.
Here is a sample script I found which dates back to 2013:
<script type="text/javascript"> jQuery(document).ready(function($) { JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) { hideFieldForGroup(); }); hideFieldForGroup(); function hideFieldForGroup(){ var user=getCurrentUserName(); var statusText=$('#status-val').text(); var status=$.trim(statusText); if(isUserInGroup(user,'Developers') && status=='In Progress'){ $("#customfield_10571").closest('div.field-group').hide(); }else if(isUserInGroup(user,'Users') && status=='Open') { AJS.$("#customfield_10571").closest('div.field-group').show(); } } 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; } function getGroups(user){ var groups; AJS.$.ajax({ url: "/rest/api/2/user?username="+user+"&expand=groups", type: 'get', dataType: 'json', async: false, success: function(data) { groups = data.groups.items; } }); return groups; } function isUserInGroup(user, group){ var groups = getGroups(user); for (i = 0; i < groups.length; i++){ if (groups[i].name == group){ return true; } } return false; } }); </script>
Is this still a valid script at all?
Thanks!
This should work
This in general checks if the user is in a certain group and then you have a line where you can insert your function. The hide() method will still work for your case
<script>
AJS.$(document).ready(function($) {
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e,context) {
group();
});
AJS.$("#issuetype").bind("DOMSubtreeModified", function (e, $ctx) {group(); });
group();
//here is a function to get the username via the rest API
function getCurrentUserName() {
var user;
AJS.$.ajax({
url: "/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.username;
}
});
//console.log("##################### getCurrentUserName:"+user); <--------------this is a debugging line and can be removed in the final code
return user;
}
// then is use the username to get the groups he belongs too, also via the rest API
function getGroups(user) {
var groups;
AJS.$.ajax({
url: "/rest/api/2/user?username="+user+"&expand=groups",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
groups = data.groups.items;
}
});
//console.log("##################### getGroups:", groups); <--------------this is a debugging line and can be removed in the final code
return groups;
}
function isUserInGroup(user, group) {
var groups = getGroups(user);
for (i = 0; i < groups.length; i++){
if (groups[i].name == group){
////console.log("##################### user in group "+group+": true ("+groups[i].name+")"); <--------------this is a debugging line and can be removed in the final code
return true;
}
}
//console.log("##################### user in group: false");
return false;
}
// here i chec if the user is in the group "MyGroup" <--------------this is a debugging line and can be removed in the final code
function group() {
//console.log("##################### group"); <--------------this is a debugging line and can be removed in the final code
var user = getCurrentUserName();
if(isUserInGroup(user,'MyGroup') ){
// here you can trigger your action. for hiding a field you can use $hide("id of the field")
$(do something)
};
//console.log("##################### end"); <--------------this is a debugging line and can be removed in the final code
});
</script>
By the way.. this kind of script might still work for one version or two, but sooner or later all JS will be filtered out by JIRA... so if you need a long term solution, you might have to consider something else.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Michael Schultz did you try this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.