Get every member of a project (inc. those in groups) with ruby ?

Added by Loris Demaria 5 months ago

I'm trying to parse on every member of a project with ruby code, including those in a group.
I managed to get every member that is not included in a group with this code :

Returns a hash of project users grouped by role
def scm_users_by_role_internal
h = {}
MemberRole.joins(:member).where("#{Member.table_name}.project_id = ?", project.id).each do |mr|
r = mr.role
if (!mr.member.user.nil?)
scm_user = mr.member.user.scm_user
h[r] ||= []
h[r] << scm_user if !scm_user.nil? && scm_user.scm_enable_parameters_c?
end
end
h
end

edit : can't find a way to indent my code, seems like it's not working

However I can't find a way to parse on the users of a group included in the project. Any help would be appreciated !

Replies (5)

RE: Get every member of a project (inc. those in groups) with ruby ? - Added by kumar abhinav 5 months ago

project.principals

will return all the active members of the project including the ones in a group.

RE: Get every member of a project (inc. those in groups) with ruby ? - Added by Loris Demaria 5 months ago

Thank you very much for your answer, I will try this on monday and let you know about the results !

RE: Get every member of a project (inc. those in groups) with ruby ? - Added by Loris Demaria 5 months ago

Hi,
After testing your idea my code still doesn't work, now I have an error message saying :

undefined local variable or method `r' for #<Com::xxxx::RedminePlugin::ScmAccessRights::ScmProject:0x000000071314b8>

However I don't see at all why do I have such an error message. My code looks like this now :

def scm_users_by_role_internal
h = {}
project.principals.each do |user|
r = user.role
if (!r.nil?)
scm_user = user.scm_user
h[r] ||= []
h[r] << scm_user if !scm_user.nil? && scm_user.scm_enable_parameters_c?
end
end
h
end

Do you have any idea why I'm having this error ?

RE: Get every member of a project (inc. those in groups) with ruby ? - Added by kumar abhinav 5 months ago

Could you add more logs to show which line is giving this error?

RE: Get every member of a project (inc. those in groups) with ruby ? - Added by Loris Demaria 5 months ago

The backtrace of this error was :

["/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activemodel-4.2.8/lib/active_model/attribute_methods.rb:433:in `method_missing'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:73:in `block in scm_users_by_role_internal'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:69:in `scm_users_by_role_internal'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:54:in `scm_users_by_role'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/chain/enable_sql_caching.rb:34:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/chain/enable_sql_caching.rb:29:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:13:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:13:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/synchronization_task.rb:22:in `block in initialize'"]

Since the error message appeared, I tried to fix it by using @r instead of r. Now i get this error message :

undefined method `role' for #<User:0x000000063c4c80> Did you mean? roles>

With the following backtrace :

["/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activemodel-4.2.8/lib/active_model/attribute_methods.rb:433:in `method_missing'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:72:in `block in scm_users_by_role_internal'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:69:in `scm_users_by_role_internal'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/app/models/com/xxxx/redmine_plugin/scm_access_rights/scm_project.rb:54:in `scm_users_by_role'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/chain/enable_sql_caching.rb:34:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/vendor/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/relation/delegation.rb:46:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/chain/enable_sql_caching.rb:29:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:13:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:13:in `block in process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `each'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/file_processor_chain.rb:12:in `process'", "/home/devel/Redmine/integration/redmine-3.4.2/plugins/scm_access_rights/lib/com/xxxx/redmine_plugin/scm_access_rights/connector/svn/synchronization_task.rb:22:in `block in initialize'"]

I guess that is due to the fact that the original code (before I started reworking it) was using the MemberRole table to parse on users, which have a role attribute, but that was when we could not get our hands on the users of a group so I had to change that. The initial code was like this :

def scm_users_by_role_internal
h = {}
MemberRole.joins(:member).where("#{Member.table_name}.project_id = ?", project.id).each do |mr|
r = mr.role
if (!mr.member.user.nil?)
scm_user = mr.member.user.scm_user
h[r] ||= []
h[r] << scm_user if !scm_user.nil? && scm_user.scm_enable_parameters_c?
end
end
h
end

Thanks for your time !

(1-5/5)