LoginSignup
7
9

More than 5 years have passed since last update.

Redmineプラグインでチケットの作成、更新時に内容を書き換える試行

Posted at

コントローラフックの :controller_issues_new_before_save:controller_issues_edit_before_save を使用しました。

plugins/redmine_plugin_test1/lib/redmine_plugin_test1/hooks.rb
module PluginTest1
  class Hooks < Redmine::Hook::Listener
    def controller_issues_new_before_save(context={})
      issue = context[:issue]
      params = context[:params]
      issue.description << "\r\n"
      issue.description << params[:project_id]  # 番号ではなく識別子 (ここでは test1 が入っている)
      issue.description << "\r\n"
      issue.description << issue.inspect
      issue.custom_field_values.each do |cfv|
        if cfv.custom_field.name == 'カスタムフィールド' then  # カスタムフィールドを名前で特定
          cfv.value << 'xyz'
        end
      end
    end

    def controller_issues_edit_before_save(context={})
      issue = context[:issue]
      params = context[:params]
      issue.notes << "\r\n"
      issue.notes << params[:issue][:notes]
      issue.custom_field_values.each do |cfv|
        if cfv.custom_field.name == 'かすたむふぃーるど' then  # カスタムフィールドを名前で特定
          cfv.value = 'https://www.google.co.jp/'
        end
      end
    end
  end
end

上記の処理でとりあえず書き換えできて、通知メールの内容も書き換え後のものになりました。

以下は作成時の通知メール。

Issue #10 has been reported by Redmine Ayweak.

----------------------------------------
バグ #10: test
http://localhost:3000/issues/10

* Author: Redmine Ayweak
* Status: 新規
* Priority: 通常
* Assignee: 
* Category: 
* Target version: 
* カスタムフィールド: xyz
* かすたむふぃーるど: http://www.example.co.jp/
----------------------------------------
テスト
test1
#<Issue id: nil, tracker_id: 1, project_id: 1, subject: "test", description: "テスト\r\ntest1\r\n", due_date: nil, category_id: nil, status_id: 1, assigned_to_id: nil, priority_id: 2, fixed_version_id: nil, author_id: 5, lock_version: 0, created_on: nil, updated_on: nil, start_date: "2014-11-03", done_ratio: 0, estimated_hours: nil, parent_id: nil, root_id: nil, lft: nil, rgt: nil, is_private: false, closed_on: nil>

以下は更新時の通知メール。

Issue #10 has been updated by Redmine Ayweak.

かすたむふぃーるど changed from http://www.example.co.jp/ to https://www.google.co.jp/

あいうえお
あいうえお


----------------------------------------
バグ #10: test
http://localhost:3000/issues/10#change-6

* Author: Redmine Ayweak
* Status: 新規
* Priority: 通常
* Assignee: 
* Category: 
* Target version: 
* カスタムフィールド: xyz
* かすたむふぃーるど: https://www.google.co.jp/
----------------------------------------
テスト
test1
#<Issue id: nil, tracker_id: 1, project_id: 1, subject: "test", description: "テスト\r\ntest1\r\n", due_date: nil, category_id: nil, status_id: 1, assigned_to_id: nil, priority_id: 2, fixed_version_id: nil, author_id: 5, lock_version: 0, created_on: nil, updated_on: nil, start_date: "2014-11-03", done_ratio: 0, estimated_hours: nil, parent_id: nil, root_id: nil, lft: nil, rgt: nil, is_private: false, closed_on: nil>
7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9