3
2
1

Jiraのフィールドタイプ変更で、以下のようなことができればと考えております。

1.テキストフィールドを単一選択リストAにタイプ変更

2.単一選択リストAはオートコンプリート機能付き(1文字入力すると選択候補が表示される)

3.単一選択リストAのオプションは、別の単一選択リストBのオプションの値をもってくる


1、2はBehavioursかこのナレッジ(https://ja.confluence.atlassian.com/jirakb/how-to-enable-autocomplete-renderer-for-multi-select-custom-field-in-jira-754978239.html)を参考にすれば実現できそうなのですが、3についてはわからず…APIなどを使えば可能なのでしょうか。

ご存知の方いらっしゃいましたらお力お貸しください。


    Commentコメントを追加...

    1 回答

    1.  
      2
      1
      0

      アドオンによってはもしかしたら色々と方法が他にもありそうですがBehavioursでというのであれば…

      https://scriptrunner.adaptavist.com/5.3.7/jira/behaviours-conversions.html

      https://scriptrunner.adaptavist.com/5.3.7/jira/rest-endpoints.html

      より

      Behaviour側のInitialiserに以下のようなソースコードを設定。

      import com.atlassian.jira.component.ComponentAccessor;
      import com.atlassian.jira.issue.IssueManager;
      import com.atlassian.jira.issue.Issue;
      
      def id = getFieldById("id");
      def pid = getFieldById("pid");
      def issuetype = getFieldById("issuetype");
      
      String projectId = null;
      String issueTypeId  = null;
      if (id != null && id.getValue() != null) {
          IssueManager issueManager = ComponentAccessor.getIssueManager();
          Issue issue = issueManager.getIssueObject((Long)id.getValue());
          if (issue != null) {
              projectId = issue.getProjectId();
              issueTypeId = issue.getIssueTypeId();
          }
      } else {
          projectId = pid.getValue();
          issueTypeId = issuetype.getValue();
      }
      getFieldById("customfield_11100").convertToSingleSelect([ 
          ajaxOptions: [
              url : getBaseUrl() + "/rest/scriptrunner/latest/custom/customfieldOption_11101?projectId=${projectId}&issueTypeId=${issueTypeId}",
              query: true, 
              minQueryLength: 1,
              formatResponse: "general" 
          ],
      ])

      (情報) customfield_11100 の部分はテキストフィールドにフィールドIDに読み替えてください。

      (情報) /rest/scriptrunner/latest/custom/customfieldOption_11101 は後述するREST APIのエンドポイントURLですので customfieldOption_11101 の部分は好きに指定できます。

      REST Endpoints に以下のようなソースコードを設定。

      import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
      import groovy.json.JsonBuilder
      import groovy.transform.BaseScript
      
      import javax.ws.rs.core.MultivaluedMap
      import javax.ws.rs.core.Response
      
      import com.atlassian.jira.component.ComponentAccessor;
      import com.atlassian.jira.issue.CustomFieldManager;
      import com.atlassian.jira.issue.context.IssueContextImpl;
      import com.atlassian.jira.issue.fields.config.FieldConfig;
      import com.atlassian.jira.issue.customfields.option.Options;
      import com.atlassian.jira.issue.customfields.option.Option;
      import com.atlassian.jira.issue.fields.CustomField;
      import com.atlassian.jira.issue.customfields.manager.OptionsManager;
      
      @BaseScript CustomEndpointDelegate delegate
      
      customfieldOption_11101(httpMethod: "GET") { MultivaluedMap queryParams, String body ->
      	String query = queryParams.getFirst("query") as String;
      	String projectId = queryParams.getFirst("projectId") as String;
      	String issueTypeId = queryParams.getFirst("issueTypeId") as String;
      
      	CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
      	OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
      
      	CustomField customField  = customFieldManager.getCustomFieldObject("customfield_11101");
      	Options options = optionsManager.getOptions(customField.getRelevantConfig(new IssueContextImpl(Long.parseLong(projectId), issueTypeId)));
      
      	List<String> selectOptions = new ArrayList<String>();
      	for (Option option : options) {
      		if (query) {
      			if (option.getValue().indexOf(query) != -1) {
      				selectOptions.add(option.getValue());
      			}
      		} else {
      			selectOptions.add(option.getValue());
      		}
      	}
      
      	def rt = [
      		items : selectOptions.collect { optionName ->
      			def html = "${optionName}"
      			if (query) {
      				html = optionName.replaceAll(/(?i)$query/) { "<b>${it}</b>" }
      			}
      		    [
      				value: optionName,
      				html : html,
      				label: optionName,
      		    ]
      		},
      		total : selectOptions.size(),
      		footer: "選択してください...",
      		]
      	return Response.ok(new JsonBuilder(rt).toString()).build()
      }

      (情報) customfield_11101 の部分は別の単一選択リストBフィールドのフィールドIDに読み替えてください。

      (情報) customfieldOption_11101(httpMethod の関数名はBehaviourで指定したエンドポイント名になるので、好きに変更してください。



      1. 篠原

        Kengo Ohsakiさん

        ご回答ありがとうございます。

        検証環境で試したところ、想定通りの挙動ができました!

        別のフィールドオプションから値を持ってくるところで詰まっていたので助かりました。

      2. Kengo Ohsaki

        篠原-san,

        良かったです。なかなか難しいことを要求されてますね…

      Commentコメントを追加...