背景:因业务须要,须要添加一个文本字段,要求该字段支持权限控制,对指定用户可编辑,其余用户只读。html
建立插件的过程不作赘述,须要的请参考:建立HelloWorld插件工程。下述步骤基于一个已经建立好的插件工程展开,基于Jira 6.3.1。api
该总结基于官方教程:建立一个新的定制字段类型ide
添加一个新类,命名为EditPermissionControllableTextField,继承TextAreaCFType。其构造方法以下:post
public EditPermissionControllableTextField( @ComponentImport CustomFieldValuePersister customFieldValuePersister, @ComponentImport GenericConfigManager genericConfigManager) { super(customFieldValuePersister, genericConfigManager); }
上述构造方法基于JIRA 6.3.1,根据JIRA官方关于7.0以上的API介绍,TextAreaCFType参数发生了变化,如7.6.1:
ui
public TextAreaCFType(CustomFieldValuePersister customFieldValuePersister, GenericConfigManager genericConfigManager, TextFieldCharacterLengthValidator textFieldCharacterLengthValidator, JiraAuthenticationContext jiraAuthenticationContext)
所以对于不一样的JIRA版本,构造方法可能不一样,以实际状况为准。能够查阅JIRA官方对于指定版本的API介绍,不过JIRA对于旧版本API维护的很差,查阅不太容易。因此本地下载一套对应版本的SDK,而后直接源码查询更便捷一些,上述6.3.1版本就是经过SDK查询到的。url
构造方法能够构造本身的各类变量或组织逻辑,在本需求中不须要,因此不作任何实现。spa
在resouces/template文件夹下,添加vm模板文件,用于定义该字段的显示、编辑和xml格式显示,注意确保文件名的惟一性。插件
其中view模板,支持折叠显示,直接参考了JIRA的Description的view模板:excel
#disable_html_escaping() #if ($value) #if (${displayParams.excel_view}) $textutils.br($textutils.htmlEncode($!value.toString(), false)) #else #if ($value && $value.length() > 255) <div id="field-${field.id}" class="twixi-block#if($invertedCollapsedState) twixi-block-inverted collapsed #else expanded#end"> <div Class="twixi-wrap verbose"> <a href="#" class="#if($invertedCollapsedState)twixi #else twixi#end"><span class="icon twixi-opened"><span>$i18n.getText("admin.common.words.hide")</span></span></a> <div class="flooded"> $!value.toString() </div> </div> <div Class="twixi-wrap concise"> <a href="#" class="#if($invertedCollapsedState)twixi #else twixi#end"><span class="icon twixi-closed"><span>$i18n.getText("admin.common.words.show")</span></span></a> <div class="flooded"> #if ($value) $velocityhelper.removeHtmlTags($value.toString()) #end </div> </div> </div> #else $!value.toString() #end #end #end
在 edit模板实现权限的控制,直接上实现:code
#disable_html_escaping() #customControlHeader ($action $customField.id $i18n.getText($customField.nameKey) $fieldLayoutItem.required $displayParameters $auiparams) #if ($jiraUserUtils.getGroupNamesForUser($authcontext.loggedInUser.name).contains('editable_group')) $!rendererParams.put("class", "long-field") $!rendererParams.put("rows", "5") $!rendererParams.put("wrap", "virtual") $rendererDescriptor.getEditVM($!value, $!issue.key, $!fieldLayoutItem.rendererType, $!customField.id, $!customField.name, $rendererParams, false) #else #if($value && ! $value.equals("")) #set ($displayValue = ${value}) #else #set ($displayValue = 'N/A') #end <span title="This field is editable only by editable_group">$!displayValue</span> <input type="hidden" name="$customField.id" value="$!value" /> #end #customControlFooter ($action $customField.id $fieldLayoutItem.getFieldDescription() $displayParameters $auiparams)
#disable_html_escaping() 这一句很重要,它决定了字段的内容是纯文本仍是html格式,在有HTML格式需求时,须要添加该句。
tips:在filed configurations中对应的字段上,选择Renderers,而后选择Wiki Style Renderer,那该字段即支持WIKI编辑选项了,相似Description字段,能够实现一些格式化输出,对于较大篇幅的内容,有更好的阅读性。
其中,editable_group为群组名,即在JIRA中建立 一个editable_group的群组,而后将须要编辑权限的人员加到群组中,而非该群组的人员就只读了。
atlassian-plugin.xml是插件的配置文件,在这里声明一个customfield-type标签,标签中代表该模块的实现类以及模板文件的信息。
<customfield-type name="Edit Permission Controllable Text Field" key="editPermissionControllableTextField" class="com.lxd.reallytek.customfield.EditPermissionControllableTextField"> <description>The custom text field can be edited by specified group only. Group name is "editable_group"</description> <resource name="view" type="velocity" location="/templates/view-collapsible_lxd.vm"/> <resource name="edit" type="velocity" location="/templates/edit-by_jira_admin_only_text_lxd.vm"/> <resource name="xml" type="velocity" location="templates/plugins/fields/xml/xml-basictext.vm"/> </customfield-type>
在上边这段配置中,name是指在JIRA中添加这个类型时显示的名称,key是惟一识别码,class即实现类。resource配置了三个类型的模板,除了view和edit外,还配置了xml显示模板,由于在本次需求中不须要考虑xml显示的问题,直接指向了JIRA系统模板。
上述三步完成后,编译并安装插件,便可在custom field页面中,经过add Fields来选择“Edit Permission Controllable Text Field”进行添加了。