Redmine でチケット更新時にフォームの値からウォッチャーを追加


対象

  • Redmine : 3.3.1
  • クライアント : Linux, Firefox 50.1.0

やりたいこと

ユーザー選択のフィールドを持つチケットを更新するときに、選択したユーザーをついでにウォッチャーに追加したい場合がある。
ウォッチャーの追加を自動にしたい。

方法

View Customize Plugin (https://github.com/onozaty/redmine-view-customize) を使う。

/issues/\d+$
$(document).on("submit", "#issue-form", function() {
    var trackerId = "1";
    var statusId = "1";
    var userFieldIds = [
        "issue_custom_field_values_1",
        "issue_custom_field_values_2"
    ];

    if ($("#issue_tracker_id").length > 0) {
        if ($("#issue_tracker_id").val() !== trackerId) {
            return;
        }
    } else {
        if (!$("#content > div.issue").hasClass("tracker-" + trackerId)) {
            return;
        }
    }

    if ($("#issue_status_id").val() !== statusId) {
        return;
    }

    var userIds = [];

    for (var i = 0; i < userFieldIds.length; i++) {
        $("#" + userFieldIds[i]).each(function() {
            var $this = $(this);

            if ($this.is("select")) {
                if ($this.prop("multiple")) {
                    userIds = userIds.concat($this.val() || []);
                } else {
                    userIds.push($this.val());
                }
            } else if ($this.is("input")) {
                $("input[name='" + $this.attr("name") + "'][value!='']:checked").each(function() {
                    userIds.push($(this).val());
                });
            }
        });
    }

    if (!userIds) {
        return;
    }

    $.post(location.href + "/watchers", { "watcher": { "user_ids": userIds } });
});