読者です 読者をやめる 読者になる 読者になる

RedmineのREST APIを拡張する話

RedmineREST APIは良く出来てるんだけど,このカラムがほしいみたいなときにはどうすればよいのか.

結構かんたんな話で,api viewを作ればいい.参考:

qiita.com

この通りに拡張したい*.api.rsbを持ってきて,追記すれば良い.

たとえば今回issueの拡張がしたかったので,app/views/issues/index.api.rsbを持ってきて

api.array :issues, api_meta(:total_count => @issue_count, :offset => @offset, :limit => @limit) do
  @issues.each do |issue|
    api.issue do
      api.id issue.id
      api.project(:id => issue.project_id, :name => issue.project.name) unless issue.project.nil?
      api.tracker(:id => issue.tracker_id, :name => issue.tracker.name) unless issue.tracker.nil?
      api.status(:id => issue.status_id, :name => issue.status.name) unless issue.status.nil?
      api.priority(:id => issue.priority_id, :name => issue.priority.name) unless issue.priority.nil?
      api.author(:id => issue.author_id, :name => issue.author.name) unless issue.author.nil?
      api.assigned_to(:id => issue.assigned_to_id, :name => issue.assigned_to.name) unless issue.assigned_to.nil?
      api.category(:id => issue.category_id, :name => issue.category.name) unless issue.category.nil?
      api.fixed_version(:id => issue.fixed_version_id, :name => issue.fixed_version.name) unless issue.fixed_version.nil?
      api.parent(:id => issue.parent_id) unless issue.parent.nil?

      api.subject     issue.subject
      api.description issue.description
      api.start_date  issue.start_date
      api.due_date    issue.due_date
      api.done_ratio  issue.done_ratio
      api.is_private  issue.is_private
      api.estimated_hours issue.estimated_hours
      # ここに追記したりする

      render_api_custom_values issue.visible_custom_field_values, api

      api.created_on issue.created_on
      api.updated_on issue.updated_on
      api.closed_on  issue.closed_on

      api.array :relations do
        issue.relations.each do |relation|
          api.relation(:id => relation.id, :issue_id => relation.issue_from_id, :issue_to_id => relation.issue_to_id, :relation_type => relation.relation_type, :delay => relation.delay)
        end
      end if include_in_api_response?('relations')
    end
  end
end

あとは空気を読んで生やすだけ,このrsbって仕様よく知らないんですけどまあ気合で.

あとはplugin入れるだけですね.かんたん.

今回のハマり

plugin名(init.rbに書くやつ)とpluginとして配置したときのディレクトリ名が異なるとうまく動かない.気をつけよう.