RE: [gitorious] Transferring ownership of a project and repository

266 views
Skip to first unread message

Peter Kjellerstedt

unread,
Oct 5, 2012, 6:17:06 AM10/5/12
to gito...@googlegroups.com

I use the following function to change the ownership of a project to another user:

 

def change_project_owner(project, owner, keep_old = true)

  p = Project.find_by_title(project)

  u = User.find_by_login(owner)

  old_owner = p.owner

 

  return if u == old_owner

 

  p.repositories.mainlines.each do |r|

    cs = r.committerships.find_by_committer_id_and_committer_type(u.id, "User")

    if cs

      cs.destroy

    end

  end

  p.change_owner_to(u).each do |r|

    r.change_owner_to!(u)

    r.user_id = u.id

    r.save!

    r.committerships.find_all_by_committer_type("User").each do |cs|

      if cs.committer_id == u.id

        cs.creator_id = nil

        cs.save!

      elsif cs.creator_id == old_owner.id

        cs.creator_id = u.id

        cs.save!

      end

    end

    if keep_old

      cs = r.committerships.create!(:committer => old_owner, :creator_id => u.id)

      cs.build_permissions(:review, :commit, :admin)

      cs.save!

    end

  end

  p.save!

end

 

Note that I use Project.find_by_title() which you can easily change to Project.find_by_slug(). I also do not know what will happen if there are group committerships involved (we do not use groups). Set keep_old to true if you want the old owner to retain his/her committerships after the owner change.

 

//Peter

 

From: gito...@googlegroups.com [mailto:gito...@googlegroups.com] On Behalf Of Allan Caffee
Sent: den 5 oktober 2012 02:15
To: gito...@googlegroups.com
Subject: [gitorious] Transferring ownership of a project and repository

 

I'm trying to transfer the ownership of a project and repository belonging to someone whose account has been disabled. Since the account is disabled I'm trying to use the ruby console to transfer ownership, but I'm getting a NoMethodError using the methods defined on these models. I'm new to ruby/rails so it's possible I'm overlooking something fairly obvious, but here's what I'm doing:

% env RAILS_ENV=production ruby script/console
>> user = User.find_by_login('username')
=> #<User id: 23, ...>
>> proj = Project.find_by_slug('foo-project)
=> #<Project id: 121, ...>
>> proj.change_owner_to user
NoMethodError: undefined method `change_owner_to' for #<Project:0x7f8ad0202da0>
    from /var/www/gitorious/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:260:in `method_missing'
    from (irb):32


What am I missing here? Is there a better way to do this? I'm using gitorious v2.1.1.

Thanks,
Allan

--
To post to this group, send email to gito...@googlegroups.com
To unsubscribe from this group, send email to
gitorious+...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages