Posts Tagged HTML
Part 2: Using jQuery and Custom fields to rename the Fix Version/s field in Jira
Posted by suddenelfilio in Programming, Web & Design on 07/09/2010
Following up on my previous article on this topic I would like to show how using similar techniques you can rename the Fix Version/s field (analogue for the Afffected Version/s field) when creating, editing or viewing an issue. I’ve seen requests on the Jira Issue site for this, mostly because there are teams that are using Agile/scrum development and they want to rename the Version to Milestone, Sprint or just something else. Jira currently does not support this out-of-the-box unless you are willing to dive into the localization jars and change those yourself, which kind of a bummer since they do support agile development through the – in my opinion overpriced - Greenhopper plugin.
Okay this is how the issue creation screen looks like without our little gimmick:

I blacked out some values just for privacy reasons, but you get the point. Now if all you need is to change the text Fix Version/s to in our case Milestone all it takes is the following script:
<script language="JavaScript">
jQuery(function(){
AJS.$("label[for=fixVersions]").replaceWith(" <label for='fixVersions'><span class='required' title='Fields in italics are required'><sup>*</sup>Milestone:</span></label>");
});
</script>
As you can see I’m using jQuery to look for a label element where the for attribute is set to ‘fixVersions‘ and replace it with a new label element.
That’s all! Well not all, you still need to get it on your pages. To do this I’ve used a Message Custom Field (for edit). If you also want this on your viewing page you need to create a second custom field of type Message Custom Field (for view) and set the default value of both custom fields to the script. To apply this text replacment to all screens, issue types and projects (create,edit and viewing) it’s best to set the context to ‘Global‘ and ‘Any issue type‘. and you are good to go. When applied the screen screen will look like this:
The Message Custom Field can be found in the Jira Toolkit plugin
Of course this technique can be applied to whatever field if you like, just remember it is a text replacement on rendering of the page so in the system nothing changes! If there is an error message about the Fix Version/s field for example when it is required and no version is selected the error message will not be using the term ‘Milestone‘.
Using jQuery and Custom fields to enforce issue type security in Jira
Posted by suddenelfilio in Programming, Web & Design on 06/09/2010
When working on our companies new issue tracking system I ran into the need to be able to prevent certain users or groups to create a specific type of issue. This however strange it may seem is not supported out-of-the-box by Jira and browsing the Jira Issue site the nice people at Atlassian don’t seem to put high value in this much requested feature. Anyways looking at the comments in the issue I discovered that you can use a custom field called Velocity processed Custom Message Field (for edit) to use a mix of Apache Velocity templating and javascript to do the trick. Okay it is a hack, but for now it is the only possibility out there that I’ve discovered.
Now before you go looking for the custom field you have to know that is not installed by default. The Velocity processed Custom Message Field can be found in a Jira plugin called Jira Toolkit over at the Plugin exchange. So if you want this to work go and install this plugin.
Now with the plugin installed create a new custom field of type Velocity processed Custom Message Field (for edit), give it a name and make sure you select All issue types and the global context! Don’t worry I’ll explain later why.
Having created the custmom field it is time for the logic. What we are going to do is to create a mix of velocity script and javascript to show an error message to a user when he/she does not belongs to a certain user group and remove all the input fields from the form. The script I’m going to use for that looks as follows:
#if ($authcontext.user.inGroup('developers'))
##do nothing
#else
<script language="JavaScript">
jQuery(function ()
{
AJS.$("<div class='warningBox maxWidth' style='text-align:center;color:DarkRed'><h1>$authcontext.user.getFullName() ,<br/>This issue type is for internal use only. <br/>Please create an Incident, Enhancement or New Feature instead!</h1></div>").appendTo('.intform');
AJS.$(".jiraform").remove();
});
</script>
#end
A little explenation is in order here I guess. All the lines preceded by the # sign is Velocity markup. Jira uses the Velocity templating engine to create its webpages. What the velocity code does is rather straight forward it checks if the current user is in the group ‘developers’ - $authcontext.user.inGroup(‘developers’) -. If that is the case then do nothing. When the current user is not in the ‘developers’ user group a jQuery script will be executed which appends a div with a warning to the page and then removes the HTML table that contains all the input fields. A little note on this is that Jira must load the jQuery library. I found that my instance has got a line like this that load the jQuery library:
<script type="text/javascript" src="/s/531/1/2.1.3/_/download/batch/com.atlassian.auiplugin:ajs/com.atlassian.auiplugin:ajs.js" ></script>
If you don’t have the jQuery library you need to make sure it is loaded.
So that is the script that will do the magic stuff. Now all you need to do is enter it as the default value for your custom field you created above. You can do this by clicking the ‘configure’ link and on the new page ‘edit default value’. In the textbox paste the script and update the field.
Remember that I said you needed to select all issue types and the global context when you created the custom field? The reason for that is that in Jira 4.1.x there seems to be an issue with setting the default value when you have not selected as is. When you select specific issue types and projects instead of all issue types and global context you’ll get strange page when you go and try to set the custom field’s default value. The textbox is not there. This is a known issue and the woirkaround for when you need to edit the default value is to set it back to all issue types and global context. I know that’s not really fun to do, but it will get it done!
Now to secure certain issue types you don’t want non developers to create just select the types you want and if necessary select the project(s). You can do this by clicking the ‘configure’ link of the custom field and then select the ‘Edit configuration’ option.
After you’ve saved these settings creating a prohibited issue type will render like this. What you see below is a customer trying to create a bug while a customer is only allowed to create an incident, New Feature or Enhancement
As you can see you address the the user in this case Customer Person (which is the Fullname of the logged in jira user) that he/she can only create an Incident, Enhancement or New Feature. Also notice that there are no input fields on the form.
So depending on the version of jira you are using you will need to tweak the jQuery to show and remove the correct elements, but this should be applicable to any version where you can use the Velocity processed Custom Message Field.
A small note I wish to add is that when a user disable javascipt in his/hers browser this solution will become useless since it uses javascript to disable those fields, but then again so becomes the rest of the internet just try and surf without javascript enabled
In part 2 I’ll show you how to change the label of the Version field using similar techniques.




