2 回答
- 321
Scripted Fields で Insight の値を取得して計算するサンプルを作ってみました。
カスタムフィールドとして以下を作成しました。
名前 タイプ 備考 商品1 Insight Object (single select) Insight オブジェクトの「商品」を利用。商品には単価を登録しておく。
標準単価のオブジェクトIDを Script 内で参照します。数量1 数値フィールド 金額1 Scripted Fields 標準単価 * 数量で金額を計算します。 商品2 Insight Object (single select) 数量2 数値フィールド 金額2 Scripted Fields 金額1の Scripted Field には下記のスクリプトを記載します。Template は、Number Field を使います。
カスタムフィールドのIDは、管理画面でカスタムフィールドの編集画面を表意した時のURLにて、https://jira-server.localdomain/jira/secure/admin/ConfigureCustomField!default.jspa?customFieldId=13401 などと表示さるので確認できます。
サンプルスクリプトimport com.onresolve.scriptrunner.runner.customisers.WithPlugin; import com.onresolve.scriptrunner.runner.customisers.PluginModule; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.component.ComponentAccessor; import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade; import com.riadalabs.jira.plugins.insight.services.model.ObjectBean; @WithPlugin("com.riadalabs.jira.plugins.insight") @PluginModule ObjectFacade objectFacade //カスタムフィールドのID // 商品1 16400 // 数量1 16402 // 商品2 16401 // 数量2 16403 /* Get Insight Object Facade from plugin accessor これはお約束の処理です */ Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"); objectFacade = (ObjectFacade)ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass); //商品1の金額を取得 def product1CF = (java.util.List)issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject((long)16400)); log.debug("product1CF = " + product1CF); if (product1CF == null) { return null; } ObjectBean product1 = (ObjectBean) product1CF[0]; // 213 は Insight の Attribute 画面に表示される id Integer product1Price = objectFacade.loadObjectAttributeBean(product1.getId(), 213).getObjectAttributeValueBeans()[0].getIntegerValue(); log.debug("product1Price = " + product1Price); //数量1を取得 Double qty1 = (Double)issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject((long)16402)); if (qty1 == null) { return null; } // 金額を計算 Double money1 = product1Price * qty1; log.debug("money1 = " + money1); return money1;
Sciprt Runner から別のアドオンの機能を参照するためには、下記の宣言が必要でした。
(参考:https://scriptrunner.adaptavist.com/latest/jira/scripting-other-plugins.html)アノテーションimport com.onresolve.scriptrunner.runner.customisers.WithPlugin; import com.onresolve.scriptrunner.runner.customisers.PluginModule; @WithPlugin("com.riadalabs.jira.plugins.insight") @PluginModule ObjectFacade objectFacade
WithPlugin アノテーションの引数の値は、アドオンキーの値を指定します。上記の指定を入れる事で、Insight のクラス( com.riadalabs.jira.plugins.insight.* のクラス)を利用する事が出来るようになります。
log.debug で記載されている部分は、管理画面の「システム」=>「ログとプロファイルの作成」にて「別のパッケージのログ レベルを設定する」で com.onresolve.scriptrunner.customfield.GroovyCustomField パッケージを DEBUG で加する事で確認できます。
- せるおん
ありがとうございます。
アノテーションに関してはサンプルの上部で挿入すればよいと思っていたのですが、
どうしても8~10行目にてエラーを吐いてしまいます。
こちらは宣言箇所が別にあるのでしょうか。
- 樋口晃
どうしても8~10行目にてエラーを吐いてしまいます。
アノテーションを利用するためには1行目の
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
が必要ですので、アノテーションの前に上記の宣言が必要です。
それでも原因が解らなかったら、エラーメッセージを見せて頂ければ解るかも知れません。
- せるおん
このようなエラーになります。
- 樋口晃
こちらで紹介したスクリプトは Insight for JIRA 3.3 で確認しましたが、API の仕様が変わった様で
最新の 5.5 では動作しませんでした
す。ご注意下さい。
5.5 用のサンプルもできれば書いてみたいです。
- せるおん
コメントありがとうございます。
新たに5.5用の質問として課題を上げたほうがよいですかね。
- 樋口晃
新たな質問でご要望頂ければ回答を準備します。
コメントを追加... - 321
マスター管理なので insite => Insight - Asset Management for JIRA のことですかね。
ScriptRunnerということでしたが
Insight にも事後処理(post-function)、条件(Conditions)、検証(Validators)で
Groovyスクリプトを記述できる機能があります。
スクリプト例は以下にサンプルがありますのでご参考までに
多分ご質問のスクリプトとしては以下が近いですかね。
事後操作なのでワークフローのトランジションしないといけませんが…
- https://documentation.riada.se/display/ICV50/Set+an+Insight+custom+field+value+to+the+organization+of+the+reporter
ある条件(報告者)で Insight にあるオブジェクト(所属)を取得してその値を課題の任意のカスタムフィールドにコピーする。
ScriptRunenr からも以下方法で Insight plugin accessor を同じように呼び出せると思いますので
あとは指定された Insight オブジェクトからを値を取得して、計算して表示すればいいかなと思います。
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade"); def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass); Class customFieldFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.services.core.CustomFieldService"); def customFieldFacade = ComponentAccessor.getOSGiComponentInstanceOfType(customFieldFacadeClass);
ScriptRunnerからアドオンの公開APIを呼び出す方法は以下にサンプルがあるのでご確認ください。
- https://scriptrunner.adaptavist.com/latest/jira/scripting-other-plugins.html
- すべてのアドオンAPIが呼び出せるとは限りません。アドオンがAPIをComponentをpublic(公開)してないと呼び出せません。正確には以下もご確認ください
- すべてのアドオンAPIが呼び出せるとは限りません。アドオンがAPIをComponentをpublic(公開)してないと呼び出せません。正確には以下もご確認ください
ワークフローのトランジションではなく、課題編集をして動的に値が切り替わるのが期待する場合は
具体的な要件がわからないのでスクリプトはご紹介できませんが…
ScriptRunner の Scripted Fields でInsightのAPIを使って実現できるかと思います。
- せるおん
ご回答ありがとうございます。
理想は課題編集等を行っても動的に変更がかかるようにしたいので、
どうにかカスタムフィールド上に値を転記できれば、Scripted Fieldsでもってこれるなと思っていたのですが、同一ステータスにトランジションさせて計算させるみたいな事をするほうがやりやすいでしょうか。
APIもちょっと調べてみます。
- Kengo Ohsaki
せるおん-san,
理想は課題編集等を行っても動的に変更がかかるようにしたいので、
ですと Scripted Fields ですかね。
どうにかカスタムフィールド上に値を転記できれば、Scripted Fieldsでもってこれるなと思っていたのですが、同一ステータスにトランジションさせて計算させるみたいな事をするほうがやりやすいでしょうか。
どういったのを想定しているのかにもよりますが…
スクリプト的には Scripted Fields でも 事後操作(Post-Function)も変わらないかと思います。
あくまでも自分の想定ですが...
Insightに商品マスタ(属性として値段情報が付与されている)があり
JIRAの1課題でユーザー?が選択した商品によって自動的な見積をする。
課題上にInsightのカスタムフィールドを設定して、そのフィールドで商品マスタを選択させる…
課題のInsightのカスタムフィールドから選択した商品マスタのオブジェクト・属性を取得して
計算するような Scripted Fields でスクリプトを作るという感じですかね…
- Kengo Ohsaki
せるおん-san,
なるほど。それであれば Scripted Fields でできそうですね。
もしAPIでわからないことがあれば私の技術的な可能な範囲でお答えできるかもしれませんので…
新しいQAでご質問くださいませ
コメントを追加... - https://documentation.riada.se/display/ICV50/Set+an+Insight+custom+field+value+to+the+organization+of+the+reporter
insite上で、例えば商品のマスタを作り、値段項目を持たせた場合
scriptrunnerにて、その値段の合算等をしたい場合は、どのようにすればよろしいでしょうか。