goldstift's blogspot my 2 cent

03 Juni 2011

How to Set Default Comment Security Level in Atlassian JIRA

Atlassian JIRA in my humble opinion one of the greatest issue tracking systems around. Nevertheless there are still many missing features, one of them may be "setting the default comment security level".

There is a nice little confluence wiki page which describes some kind of dirty hack to solve or at least work around this problem. The URL to that page is: http://confluence.atlassian.com/display/JIRA/How+to+Set+Default+Comment+Security+Level

Unfortunately the workaround works only for JIRA 4.1 and lower as Atlassian completely rewrote there UI for version 4.2 onwards. Therefore the <select>-element for the "comment security level" is hidden and the user gets to see a HTML dropdown which is much more beautiful and nicer to work with. But it breaks the above mentioned hack as the HTML dropdown doesn't really care about the state of the <select>-element, as it holds the state inside of its own "model".
There is already a JIRA issue describing the problem: JRA-23365

I couldn't solve the problem completely as I have absolutely no knowledge about the inner workings of "AJS", Atlassians Javascript Library, but I somehow could extend the above mentioned workaround.

Here's the code I put into my "announcement banner" field:
<script language="JavaScript">
    <!--
    jQuery.noConflict();
    var defaultRoleName = 'role:10030'
    window.onload = function() {
        var commentLevelSelect = document.getElementById("commentLevel");
        if (commentLevelSelect) {
            for (var i = 1; i < commentLevelSelect.options.length; i++) {
                if (commentLevelSelect.options[i].value == defaultRoleName) {
                    commentLevelSelect.options[i].selected;
                    commentLevelSelect.value = defaultRoleName;
                    jQuery("a.drop span.icon").removeClass("icon-unlocked").addClass("icon-locked");

                    var htmlEscapedLabel = AJS.$("<div/>").text(commentLevelSelect.options[i].text).html();
                    jQuery("span.current-level").html(AJS.format(AJS.params.securityLevelViewableRestrictedTo, htmlEscapedLabel))

                    break;
                }
            }
        }
    }
    //-->
</script>
<style>
   .alertHeader{display:none;}
</style>
Any comments and suggestions on this are very much appreciated!

Update 2012-02-02: I updated the code and tested it with JIRA 4.3.4

6 Kommentare:

  1. Thank you. Works fine when commenting in Jira v4.2.4-b591.
    But when attaching a file, the comment reverts back to original value. Can you help?

    AntwortenLöschen
  2. Sorry for taking so long to get back to you. I know this issue but unfortunately haven't had time to get rid of it, yet.

    The problem occurs with the "modal dialogs" as the form content is loaded via ajax requests. Therefore the form is not on the page when my jQuery-function is executed. The solution would be to attach a listener to the callback and re-execute the function whenever a form is loaded via ajax.

    Nevertheless you should definitely vote for the issue over at Atlassian.

    AntwortenLöschen
  3. Thanks a lot for this! I agree that this is one of the more important features that JIRA is currently missing.

    I did have to do a tad of debugging to get this to work... for some reason the window.onload event wasn't firing properly, so instead of setting the above function as the onload handler, I set it into the jQuery() method (which achieves the same result, but is wrapped for all that lovely jQuery abstraction).

    So I basically ended up doing the following:

    var defaultRoleName = 'role:10030'
    jQuery(function() {
    var commentLevelSelect = document.getElementById("commentLevel");
    ....
    });

    Cheers!
    Jared

    AntwortenLöschen
    Antworten
    1. Hi Jared,

      thanks for your idea on this one. I updated the code to also work in the in-HTML popup dialogs. Unfortunately the HTML-dropdown list still doesn't recognize the change and shows the wrong entry as "active".

      Cheers
      Alex

      Löschen
  4. Thanks a lot for an extremely helpful work around.
    Is it possible to make the script detect if the security level is not available, and then back off (don't do a selection)

    We are using this to set default security level for comments to employees only (hidden for customers), but when a customer logs in he/she does not have access to this security role. Causing the drop down selection to look a bit weird.

    Running Jira 5.0.6 btw.

    Cheers
    Aleksander

    AntwortenLöschen
    Antworten
    1. Hi Aleksander,

      we're using the script exactly this way. All you have to do is set the "defaultRoleName" variable. If there is no entry with this value, jQuery doesn't change the selected item.

      Cheers
      Alex

      Löschen