Project

General

Profile

cannot update a custom field via ruby ActiveResource (REST API)

Added by Volker Kopetzky about 8 years ago

Hi there,

I'm trying to update a redmine issue's custom field with a ruby script.

#!/usr/bin/ruby
require 'rubygems'
require 'active_resource'
require 'digest'

# Helper class to avoid ArgumentError
class JsonFormatter
  include ActiveResource::Formats::JsonFormat

  attr_reader :collection_name

  def initialize(collection_name)
    @collection_name = collection_name.to_s
  end

  def decode(json)
    remove_root(ActiveSupport::JSON.decode(json))
  end

  private
  def remove_root(data)
    if data.is_a?(Hash) && data[collection_name]
      data[collection_name]
    else
      data
    end
  end
end

class Issues < ActiveResource::Base
  self.site = "HTTP://SERVER" 
  self.user = "USER" 
  self.password = "PASSWORD" 
  self.format = ::JsonFormatter.new(:issues)
end

@redmine_issues = Issues.find(:all)
@redmine_issueids_collected = @redmine_issues.collect {|i| i.id}

def render_hash(project, subject,description)
  md5 = Digest::MD5.new
  md5 << project.to_s
  md5 << subject.to_s
  md5 << description.to_s
  md5.hexdigest
end

Testlist = [338]

@redmine_issues.each do |rissue|
    if Testlist.include? rissue.id
        puts "working on issue #{rissue.id.to_s}" 
        hashes = rissue.custom_fields.select { |f| f.id == 2 }
        hashfield = hashes[0]
        isnewHash = ''
        begin
            myhash = hashfield.value
            puts("  initial hash= '#{myhash}'")
        rescue NoMethodError
            myhash = ''
            isnewHash = '**'
            hashfield = { '2' => 'Init' }
        end

        if myhash == ''
            myhash = render_hash(rissue.project.name, 
                             rissue.subject, 
                             rissue.description)
            isnewHash = '*'
            # this already sets the custom_field value in the rissue object 
            hashfield.value = myhash

            # not needed:
            # rissue.custom_fields << hashfield
            if rissue.save
                # getting here, no errors, but the hash value is not saved!
                # checking value
                lissue = Issues.find(rissue.id).issue
                newhashes = lissue.custom_fields.select { |f| f.id == 2 }
                newval = newhashes[0].value
                isnewHash += " (updated to '#{newval}')" 
            end
        end
        puts "  #{rissue.id.to_s} (#{hashes.length}): #{myhash} #{isnewHash}" 
    end
end

Both calls to the server rissue.save and rissue.reload do not seem to work properly as a refresh in the web UI shows no change to the custom field!

Example Output:


$ ./test.rb 
working on issue 338
  initial hash= ''
  338 (1): ccfb8a248e3d86da4ba041a7e7e02a39 * (updated to 'ccfb8a248e3d86da4ba041a7e7e02a39')

# second run
$ ./test.rb 
working on issue 338
  initial hash= ''  # this should be ccfb8a248e3d86da4ba041a7e7e02a39 !
  338 (1): ccfb8a248e3d86da4ba041a7e7e02a39 * (updated to 'ccfb8a248e3d86da4ba041a7e7e02a39')

What am I doing wrong?


Replies (1)

RE: ruby ActiveResource REST API: updating a custom field - Added by Volker Kopetzky about 8 years ago

Additional information about redmine:

Environment:
  Redmine version                3.0.3.stable
  Ruby version                   2.0.0-p645 (2015-04-13) [x86_64-linux]
  Rails version                  4.2.1
  Environment                    production
  Database adapter               Mysql2
SCM:
  Subversion                     1.8.13
  Git                            1.9.5
  Filesystem                     
Redmine plugins:
  redmine_agile                  1.3.10
  redmine_checklists             3.1.2
  redmine_contacts               4.0.2
  redmine_contacts_helpdesk      3.0.1
  redmine_contacts_invoices      4.0.1
  redmine_products               2.0.0

Also, the log shows this when running the script:

Started GET "/issues.json" for 49.228.58.65 at 2016-02-17 02:08:57 +0000
Processing by IssuesController#index as JSON
  Current user: USER (id=1)
  Rendered issues/index.api.rsb (71.1ms)
Completed 200 OK in 152ms (Views: 67.5ms | ActiveRecord: 21.2ms)
Started PUT "/issues/338.json" for 49.228.58.65 at 2016-02-17 02:08:57 +0000
Processing by IssuesController#update as JSON
  Parameters: 
{
    "id" => "338", "project" => {
        "id" => 33, "name" => "NAME" 
    }, "tracker" => {
        "id" => 3, "name" => "Support" 
    }, "status" => {
        "id" => 1, "name" => "New" 
    }, "priority" => {
        "id" => 2, "name" => "Normal" 
    }, "author" => {
        "id" => 1, "name" => "USER" 
    }, "assigned_to" => {
        "id" => 4, "name" => "USER2" 
    }, "subject" => "SUBJECT", "description" => "DESCRIPTION", "start_date" => "2016-02-11", "done_ratio" => 0, "custom_fields" => [{
        "id" => 2,
        "name" => "rhash",
        "value" => "ccfb8a248e3d86da4ba041a7e7e02a39" 
    }], "created_on" => "2016-02-11T07:41:51Z", "updated_on" => "2016-02-17T02:07:34Z" 
}
  Current user: USER (id=1)
  Rendered text template (0.0ms)
Completed 200 OK in 55ms (Views: 1.0ms | ActiveRecord: 9.3ms)

The log actually shows the new custom_field value, but it just won't be updated in redmine.

    (1-1/1)