├── .gitignore ├── Gemfile ├── LICENSE ├── README.rdoc ├── app ├── controllers │ ├── gitolite_hook_controller.rb │ └── gitolite_public_keys_controller.rb ├── helpers │ ├── gitolite_hook_helper.rb │ └── gitolite_public_keys_helper.rb ├── models │ ├── gitolite_observer.rb │ └── gitolite_public_key.rb └── views │ ├── gitolite_public_keys │ ├── _form.html.erb │ ├── edit.html.erb │ ├── index.html.erb │ └── new.html.erb │ ├── projects │ └── _redmine_gitolite.html.erb │ ├── repositories │ └── git_instructions.html.erb │ └── settings │ └── _redmine_gitolite.html.erb ├── config ├── locales │ └── en.yml └── routes.rb ├── contrib └── hooks │ ├── post-receive-redmine_gitolite │ └── post-receive-redmine_gitolite.simple ├── db └── migrate │ └── 20091119162427_create_gitolite_public_keys.rb ├── init.rb └── lib ├── gitolite └── patches │ ├── repositories_controller_patch.rb │ └── repositories_helper_patch.rb ├── gitolite_redmine.rb └── tasks └── gitolite.rake /.gitignore: -------------------------------------------------------------------------------- 1 | extra/ssh/private_key 2 | extra/ssh/private_key.pub 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | gem "net-ssh" 2 | gem "gitolite", ">= 0.0.3.alpha" 3 | gem "lockfile" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Kah Seng Tay - Gitolite modifications 2 | Copyright (c) 2010 Jakob Skjerning - Original code 3 | Copyright (c) 2009-2010 Jan Schulz-Hofen, ROCKET RENTALS GmbH (http://www.rocket-rentals.de). MIT License. 4 | Copyright (c) 2010 Eric Bishop (ericpaulbishop@gmail.com) MIT License. 5 | Copyright (c) 2011 Arkadiusz Hiler 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = redmine-gitolite 2 | 3 | CURRENT HEAD VERSION WORKS WITH TRUNK REDMINE 4 | 5 | This is {Redmine Gitolite Hook}[https://github.com/kahseng/redmine_gitolite_hook] combined with {redmine-gitolite}[https://github.com/jhogendorn/redmine-gitolite] 6 | 7 | A Redmine plugin which manages your gitolite configuration based on your projects and user memberships in Redmine. 8 | 9 | Provides Gitolite hook, for automaticaly pulling changes to local clone of repository. You don't need to any access to your, this plugin will create local clone and keep it up to date. 10 | 11 | Includes Public Key management views (extracted from http://plan.io). 12 | 13 | == Requirements: 14 | 15 | === Gems: 16 | * net-ssh 17 | * lockfile 18 | * {gitolite}[https://github.com/wingrunr21/gitolite/] (works with 1.8.7) 19 | 20 | === Other: 21 | * Gitolite server 22 | * accessible Git executable 23 | * curl 24 | 25 | 26 | == Setup: 27 | 1. Install Redmine and put this plugin in vendor/plugins directory and migrate 28 | database (plugins) 29 | $ cd redmine/plugins 30 | $ git clone git://github.com/ivyl/redmine-gitolite.git redmine_gitolite 31 | $ cd .. 32 | $ RAILS_ENV=production rake db:migrate_plugins 33 | 34 | 2. User running redmine (his key must be named named "redmine") must have RW+ access to gitolite-admin (assuming that you have gitolite installed). 35 | 36 | 3. Make sure that redmine user has gitolite server in his known_host list 37 | $ sudo su - redmine 38 | $ ssh gitolite.server 39 | * [accept key] 40 | 41 | 4. Configure email and name of git user for your redmine account 42 | $ git config --global user.email "redmine@gitolite.org" 43 | $ git config --global user.name "Redmine Gitolite" 44 | 45 | 5. Add post-receive hook to common gitolite hooks (examples in contrib dir) and configure it 46 | (host, api key, etc, see sources) 47 | $ sudo su - gitolite #login on gitolite user 48 | $ cat > .gitolite/hooks/common/post-receive 49 | * [paste hook] 50 | $ vim .gitolite/hooks/common/post-receive 51 | * [enable WS for repository management in administration->settings->repositories] 52 | * [copy generated API key] 53 | * [configure, in complex hook you can do this by git config options] 54 | $ chmod +x .gitolite/hooks/common/post-receive 55 | $ gl-setup 56 | 57 | 6. Configure plugin in redmine settings 58 | * [Adminitration -> Plugins -> Redmine Gitolite] 59 | * [Gitolite URL should be set to your gitolite-admin git repository] 60 | * [Base path should point directory which will hold local copies (must exist) 61 | * [Set developer and ro urls as in given examples (just slightly modify them)] 62 | * [%{name} will be replaced with your repository identifier] 63 | 64 | == Pro Tips: 65 | 66 | * You should manage your keys only from Redmine to avoid conflicts. 67 | * Need new bare git repo? Create it via Redmine, disable other features. 68 | * This is most convenient way to manage keys by your user. 69 | 70 | == Found bug? 71 | 72 | Open new issue and complain. You can also fix it and sent pull request. 73 | This plugin is in active usage in current, edge Redmine. Any suggestions are welcome. 74 | -------------------------------------------------------------------------------- /app/controllers/gitolite_hook_controller.rb: -------------------------------------------------------------------------------- 1 | require 'open3' 2 | 3 | class GitoliteHookController < ApplicationController 4 | unloadable 5 | 6 | skip_before_filter :verify_authenticity_token, :check_if_login_required 7 | 8 | def index 9 | repository = find_repository 10 | update_repository(repository) 11 | repository.fetch_changesets 12 | render(:text => 'OK') 13 | end 14 | 15 | private 16 | 17 | def exec(command) 18 | logger.debug { "GitoliteHook: Executing command: '#{command}'" } 19 | stdin, stdout, stderr = Open3.popen3(command) 20 | 21 | output = stdout.readlines.collect(&:strip) 22 | errors = stderr.readlines.collect(&:strip) 23 | 24 | logger.debug { "GitoliteHook: Output from git:" } 25 | logger.debug { "GitoliteHook: * STDOUT: #{output}"} 26 | logger.debug { "GitoliteHook: * STDERR: #{errors}"} 27 | end 28 | 29 | def update_repository(repository) 30 | origin = Setting.plugin_redmine_gitolite['developerBaseUrls'].lines.first 31 | origin = origin.gsub("%{name}", repository.project.identifier) 32 | exec("git clone --mirror '#{origin}' '#{repository.url}'") if !File.directory?(repository.url) 33 | exec("cd '#{repository.url}' && git remote update --prune") 34 | end 35 | 36 | def get_identifier 37 | identifier = params[:project_id] 38 | # TODO: Can obtain 'oldrev', 'newrev', 'refname', 'user' in POST params for further action if needed. 39 | raise ActiveRecord::RecordNotFound, "Project identifier not specified" if identifier.nil? 40 | return identifier 41 | end 42 | 43 | def find_project 44 | identifier = get_identifier 45 | project = Project.find_by_identifier(identifier.downcase) 46 | raise ActiveRecord::RecordNotFound, "No project found with identifier '#{identifier}'" if project.nil? 47 | return project 48 | end 49 | 50 | def find_repository 51 | project = find_project 52 | repository = project.repository 53 | raise TypeError, "Project '#{project.to_s}' ('#{project.identifier}') has no repository" if repository.nil? 54 | raise TypeError, "Repository for project '#{project.to_s}' ('#{project.identifier}') is not a Git repository" unless repository.is_a?(Repository::Git) 55 | return repository 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /app/controllers/gitolite_public_keys_controller.rb: -------------------------------------------------------------------------------- 1 | class GitolitePublicKeysController < ApplicationController 2 | unloadable 3 | 4 | 5 | before_filter :require_login 6 | before_filter :set_user_variable 7 | before_filter :find_gitolite_public_key, :except => [:index, :new, :create] 8 | 9 | def index 10 | @status = if (session[:gitolite_public_key_filter_status]=params[:status]).nil? 11 | GitolitePublicKey::STATUS_ACTIVE 12 | elsif params[:status].blank? 13 | nil 14 | else 15 | params[:status].to_i != 0 16 | end 17 | 18 | scope = @user.gitolite_public_keys 19 | scope = scope.where(active: true) if @status 20 | @gitolite_public_keys = scope.all(:order => 'active DESC, created_at DESC') 21 | 22 | 23 | respond_to do |format| 24 | format.html # index.html.erb 25 | format.json { render :json => @gitolite_public_keys } 26 | end 27 | end 28 | 29 | def edit 30 | end 31 | 32 | def update 33 | if params[:public_key][:active] 34 | status = params[:public_key].delete(:active).to_i 35 | if status == GitolitePublicKey::STATUS_ACTIVE 36 | @gitolite_public_key.active = true 37 | elsif status == GitolitePublicKey::STATUS_LOCKED 38 | @gitolite_public_key.active = false 39 | end 40 | end 41 | 42 | if @gitolite_public_key.update_attributes(params[:public_key]) 43 | flash[:notice] = l(:notice_public_key_updated) 44 | redirect_to url_for(:action => 'index', :status => session[:gitolite_public_key_filter_status]) 45 | else 46 | render :action => 'edit' 47 | end 48 | end 49 | 50 | def new 51 | @gitolite_public_key = GitolitePublicKey.new(:user => @user) 52 | end 53 | 54 | def create 55 | @gitolite_public_key = GitolitePublicKey.new(params[:public_key].merge(:user => @user)) 56 | if @gitolite_public_key.save 57 | flash[:notice] = l(:notice_public_key_added) 58 | redirect_to url_for(:action => 'index', :status => session[:gitolite_public_key_filter_status]) 59 | else 60 | render :action => 'new' 61 | end 62 | end 63 | 64 | def show 65 | respond_to do |format| 66 | format.html # show.html.erb 67 | format.json { render :json => @gitolite_public_key } 68 | end 69 | end 70 | 71 | protected 72 | 73 | def set_user_variable 74 | @user = User.current 75 | end 76 | 77 | def find_gitolite_public_key 78 | key = GitolitePublicKey.find_by_id(params[:id]) 79 | if key and key.user == @user 80 | @gitolite_public_key = key 81 | elsif key 82 | render_403 83 | else 84 | render_404 85 | end 86 | end 87 | end 88 | -------------------------------------------------------------------------------- /app/helpers/gitolite_hook_helper.rb: -------------------------------------------------------------------------------- 1 | module GitoliteHookHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/gitolite_public_keys_helper.rb: -------------------------------------------------------------------------------- 1 | module GitolitePublicKeysHelper 2 | def gitolite_public_keys_status_options_for_select(user, selected) 3 | key_count_by_active = user.gitolite_public_keys.count(:group => 'active').to_hash 4 | options_for_select([[l(:label_all), nil], 5 | ["#{l(:status_active)} (#{key_count_by_active[GitolitePublicKey::STATUS_ACTIVE].to_i})", GitolitePublicKey::STATUS_ACTIVE], 6 | ["#{l(:status_locked)} (#{key_count_by_active[GitolitePublicKey::STATUS_LOCKED].to_i})", GitolitePublicKey::STATUS_LOCKED]], selected) 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /app/models/gitolite_observer.rb: -------------------------------------------------------------------------------- 1 | class GitoliteObserver < ActiveRecord::Observer 2 | unloadable 3 | 4 | observe :project, :user, :gitolite_public_key, :member, :role, :repository 5 | 6 | def after_save(object) ; update_repositories(object) ; end 7 | def after_destroy(object) ; update_repositories(object) ; end 8 | 9 | protected 10 | 11 | def update_repositories(object) 12 | gr = GitoliteRedmine::AdminHandler.new 13 | case object 14 | when Repository then gr.update_projects(object.project) 15 | when User then (gr.update_projects(object.projects) && gr.update_user(object)) unless is_login_save?(object) 16 | when GitolitePublicKey then gr.update_user(object.user) 17 | when Member then gr.update_projects(object.project) 18 | when Role then gr.update_projects(object.members.map(&:project).uniq.compact) 19 | end 20 | end 21 | 22 | private 23 | 24 | # Test for the fingerprint of changes to the user model when the User actually logs in. 25 | def is_login_save?(user) 26 | user.changed? && user.changed.length == 2 && user.changed.include?("updated_on") && user.changed.include?("last_login_on") 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /app/models/gitolite_public_key.rb: -------------------------------------------------------------------------------- 1 | class GitolitePublicKey < ActiveRecord::Base 2 | unloadable 3 | STATUS_ACTIVE = 1 4 | STATUS_LOCKED = 0 5 | 6 | belongs_to :user 7 | validates_uniqueness_of :title, :scope => :user_id 8 | validates_uniqueness_of :identifier, :score => :user_id 9 | validates_presence_of :title, :key, :identifier 10 | 11 | scope :active, {:conditions => {:active => GitolitePublicKey::STATUS_ACTIVE}} 12 | scope :inactive, {:conditions => {:active => GitolitePublicKey::STATUS_LOCKED}} 13 | 14 | validate :has_not_been_changed, :is_valid_key 15 | 16 | before_validation :set_identifier 17 | 18 | def has_not_been_changed 19 | unless new_record? 20 | %w(identifier key user_id).each do |attribute| 21 | errors.add(attribute, 'may not be changed') unless changes[attribute].blank? 22 | end 23 | end 24 | end 25 | 26 | def is_valid_key 27 | valid = check_key(key) 28 | if !valid 29 | begin 30 | # We try validating again, after adding a newline, since some 4096-bit 31 | # public keys fail validation when a newline isn't present 32 | key = "#{key}\n" 33 | valid = check_key(key) 34 | if !valid 35 | errors[:base] = "invalid key" 36 | else 37 | self.key = key 38 | end 39 | rescue 40 | errors[:base] = "invalid key" 41 | end 42 | end 43 | end 44 | 45 | def check_key(key) 46 | File.open('/tmp/check_key', 'w') {|f| f.write(key)} 47 | stdin, stdout, stderr = Open3.popen3('ssh-keygen -l -f /tmp/check_key') 48 | data = stdout.gets() 49 | hash_regex = /([0-9a-f][0-9a-f](:[0-9a-f][0-9a-f])+)/ 50 | matching = hash_regex.match data 51 | File.delete('/tmp/check_key') 52 | if matching == nil 53 | false 54 | else 55 | true 56 | end 57 | end 58 | 59 | def set_identifier 60 | self.identifier ||= "#{self.user.login.underscore}@#{self.title.underscore}".gsub(/[^0-9a-zA-Z\-\_]/,'_') 61 | end 62 | 63 | def to_s ; title ; end 64 | 65 | def location 66 | self.title.underscore.gsub(/[^0-9a-zA-Z\-\_]/,'_') 67 | end 68 | 69 | def owner 70 | self.user.login.underscore.gsub(/[^0-9a-zA-Z\-\_]/,'_') 71 | end 72 | end 73 | -------------------------------------------------------------------------------- /app/views/gitolite_public_keys/_form.html.erb: -------------------------------------------------------------------------------- 1 |
<%= f.text_field :title, :required => true %>
3 |<%= f.text_area :key, :required => true, :disabled => !@gitolite_public_key.new_record?, :style => 'width:99%;height:140px;', :label => :field_public_key %>
4 | <% if !@gitolite_public_key.new_record?%>
5 |
<%= l(:label_key_cannot_be_changed_please_create_new_key) %>
6 | <% end %>
7 |
<%= f.check_box :active %>
9 |<%= l(:field_name) %> | 18 |<%= l(:field_created_on) %> | 19 |<%= l(:field_active) %> | 20 |21 | |
---|---|---|---|
<%= link_to h(key), :action => 'edit', :id => key %> | 25 |<%= format_time(key.created_at) %> | 26 |<%= image_tag('true.png') if key.active? %> | 27 |28 | <%= link_to l(key.active? ? :button_lock : :button_unlock), public_key_path(key, :public_key => {:active => key.active? ? GitolitePublicKey::STATUS_LOCKED : GitolitePublicKey::STATUS_ACTIVE}), :method => :put, :class => "icon #{key.active? ? 'icon-lock' : 'icon-unlock'}" %> 29 | | 30 |
<%= link_to l(:label_enumeration_new), { :action => 'new'} %>
37 | 38 | <% html_title(l(:label_public_keys)) -%> 39 | -------------------------------------------------------------------------------- /app/views/gitolite_public_keys/new.html.erb: -------------------------------------------------------------------------------- 1 |
5 | <% baseUrlStr = Setting.plugin_redmine_gitolite['readOnlyBaseUrls'] -%>
6 | <% if !baseUrlStr.empty? && User.current.allowed_to?(:view_changesets, @project) %>
7 | Read Only URL:
8 |
17 | <% baseUrlStr = Setting.plugin_redmine_gitolite['developerBaseUrls'] %>
18 | <% if !baseUrlStr.empty? && User.current.allowed_to?(:commit_access, @project) %>
19 | Developer URL:
20 |
Download and Install Git 7 | git config --global user.name "<%= User.current.login %>" 8 | git config --global user.email <%= User.current.mail %> 9 |10 | 11 |
<%= link_to "Upload SSH Public Key", {:controller => 'gitolite_public_keys', :action => 'index'} %> 13 | Add yourself as a project developer: <%= link_to @project.name + " Settings", {:controller => 'projects', :action => 'settings'} %> -> Members Tab -> New Member 14 |15 | 16 |
cd <%= @project.identifier %> 18 | git init 19 | git add . 20 | git commit -m 'Initializing <%= @project %> repository' 21 | git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'].gsub("%{name}", @project.identifier) %> 22 | git push origin master 23 |24 | 25 |
cd existing_git_repo 27 | git remote add origin <%= Setting.plugin_redmine_gitolite['developerBaseUrls'].gsub("%{name}", @project.identifier) %> 28 | git push origin master 29 |30 | 31 |
3 |
4 | <%= text_field_tag("settings[gitoliteUrl]", @settings['gitoliteUrl'], :size => 60) %>
5 |
6 |
8 |
9 | <%= text_field_tag("settings[basePath]", @settings['basePath'], :size => 60) %>
10 |
11 |
13 |
14 | <%= text_area_tag("settings[developerBaseUrls]", @settings['developerBaseUrls'].split(/[\r\n\t ,;]+/).join("\n"), :size => 60) %>
15 |
16 |
18 |
19 | <%= text_area_tag("settings[readOnlyBaseUrls]", @settings['readOnlyBaseUrls'].split(/[\r\n\t ,;]+/).join("\n"), :size => 60) %>
20 |
21 |