Commit 723104c45f83937cd87bee87ac16d3dfc6ce4d88

Authored by miks
1 parent 53ce00f7

Initial deploy_key feature commit

app/controllers/deploy_keys_controller.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +class DeployKeysController < ApplicationController
  2 + respond_to :js
  3 + layout "project"
  4 + before_filter :project
  5 + # before_filter :authorize_admin_project!
  6 + # before_filter :require_non_empty_project
  7 +
  8 + def project
  9 + @project ||= Project.find_by_code(params[:project_id])
  10 + end
  11 +
  12 + def index
  13 + @keys = @project.deploy_keys.all
  14 + end
  15 +
  16 + def show
  17 + @key = @project.deploy_keys.find(params[:id])
  18 + end
  19 +
  20 + def new
  21 + @key = @project.deploy_keys.new
  22 +
  23 + respond_with(@key)
  24 + end
  25 +
  26 + def create
  27 + @key = @project.deploy_keys.new(params[:key])
  28 + @key.save
  29 +
  30 + respond_with(@key)
  31 + end
  32 +
  33 + def destroy
  34 + @key = @project.deploy_keys.find(params[:id])
  35 + @key.destroy
  36 +
  37 + respond_to do |format|
  38 + format.html { redirect_to deploy_keys_url }
  39 + format.js { render :nothing => true }
  40 + end
  41 + end
  42 +end
... ...
app/models/deploy_key.rb 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +class DeployKey < ActiveRecord::Base
  2 + belongs_to :project
  3 +
  4 + validates :title,
  5 + :presence => true,
  6 + :length => { :within => 0..255 }
  7 +
  8 + validates :key,
  9 + :presence => true,
  10 + :uniqueness => true,
  11 + :length => { :within => 0..5000 }
  12 +
  13 + before_save :set_identifier
  14 + after_save :update_repository
  15 + after_destroy :repository_delete_key
  16 +
  17 + def set_identifier
  18 + self.identifier = "deploy_#{project.code}_#{Time.now.to_i}"
  19 + end
  20 +
  21 + def update_repository
  22 + Gitlabhq::GitHost.system.new.configure do |c|
  23 + c.update_keys(identifier, key)
  24 + c.update_project(project)
  25 + end
  26 + end
  27 +
  28 + def repository_delete_key
  29 + Gitlabhq::GitHost.system.new.configure do |c|
  30 + c.delete_key(identifier)
  31 + c.update_project(project)
  32 + end
  33 + end
  34 +
  35 +end
  36 +# == Schema Information
  37 +#
  38 +# Table name: keys
  39 +#
  40 +# id :integer not null, primary key
  41 +# project_id :integer not null
  42 +# created_at :datetime
  43 +# updated_at :datetime
  44 +# key :text
  45 +# title :string(255)
  46 +# identifier :string(255)
  47 +#
  48 +
  49 +
... ...
app/models/project.rb
... ... @@ -14,6 +14,7 @@ class Project &lt; ActiveRecord::Base
14 14 has_many :users, :through => :users_projects
15 15 has_many :notes, :dependent => :destroy
16 16 has_many :snippets, :dependent => :destroy
  17 + has_many :deploy_keys, :dependent => :destroy
17 18  
18 19 acts_as_taggable
19 20  
... ...
app/views/deploy_keys/_form.html.haml 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +%div
  2 + = form_for [@project, @key], :remote => true do |f|
  3 + -if @key.errors.any?
  4 + %ul
  5 + - @key.errors.full_messages.each do |msg|
  6 + %li= msg
  7 +
  8 + .form-row
  9 + = f.label :title
  10 + = f.text_field :title, :style => "width:300px"
  11 + .form-row
  12 + = f.label :key
  13 + = f.text_area :key, :style => "width:300px; height:130px"
  14 + .form-row
  15 + = f.submit 'Save', :class => "grey-button"
  16 +
... ...
app/views/deploy_keys/_show.html.haml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +%a.update-item{:href => project_deploy_key_path(key)}
  2 + %span.update-title
  3 + = key.title
  4 + %span.update-author
  5 + Added
  6 + = time_ago_in_words(key.created_at)
  7 + ago
... ...
app/views/deploy_keys/create.js.haml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +- if @key.valid?
  2 + :plain
  3 + $("#new_key_dialog").dialog("close");
  4 + $("#keys-table .data").append("#{escape_javascript(render(:partial => 'show', :locals => {:key => @key} ))}");
  5 + $("#no_ssh_key_defined").hide();
  6 +- else
  7 + :plain
  8 + $("#new_key_dialog").empty();
  9 + $("#new_key_dialog").append("#{escape_javascript(render('form'))}");
... ...
app/views/deploy_keys/edit.html.haml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +%h1 Editing key
  2 +
  3 += render 'form'
  4 +
  5 += link_to 'Show', @key
  6 +\|
  7 += link_to 'Back', project_deploy_keys_path
... ...
app/views/deploy_keys/index.html.haml 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +%h2.icon
  2 + %span>
  3 + SSH Keys
  4 +%div#new-key-holder.right
  5 + = link_to "Add new", new_project_deploy_key_path, :remote => true, :class => "grey-button"
  6 +%br
  7 +
  8 +%div#keys-table{ :class => "update-data ui-box ui-box-small ui-box-big" }
  9 + .data
  10 + - @keys.each do |key|
  11 + = render(:partial => 'show', :locals => {:key => key})
  12 +
  13 +:javascript
  14 + $('.delete-key').live('ajax:success', function() {
  15 + $(this).closest('.update-item').fadeOut(); });
  16 +
... ...
app/views/deploy_keys/new.html.haml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +%h1 New key
  2 +
  3 += render 'form'
  4 +
  5 += link_to 'Back', project_deploy_keys_path
... ...
app/views/deploy_keys/new.js.haml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +:plain
  2 + var new_key_dialog = $("<div id='new_key_dialog'></div>");
  3 + new_key_dialog.html("#{escape_javascript(render('form'))}");
  4 + $(new_key_dialog).dialog({
  5 + width: 350,
  6 + resizable: false,
  7 + draggable: false,
  8 + title: "Add new public key",
  9 + close: function(event, ui) { $("#new_key_dialog").remove();},
  10 + modal: true
  11 + });
... ...
app/views/deploy_keys/show.html.haml 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +.ui-box.width-100p
  2 + %h3= @key.title
  3 + .data
  4 + %pre= @key.key
  5 + .clear
  6 + .buttons
  7 + = link_to 'Remove', @key, :confirm => 'Are you sure?', :method => :delete, :class => "red-button delete-key right"
  8 + .clear
  9 +
  10 +
... ...
app/views/layouts/project.html.haml
... ... @@ -49,6 +49,7 @@
49 49  
50 50 - if can? current_user, :admin_project, @project
51 51 = link_to "Admin", edit_project_path(@project), :class => (current_page?(edit_project_path(@project))) ? "current" : nil
  52 + = link_to "Deploy keys", project_deploy_keys_path(@project), :class => (current_page?(project_deploy_keys_path(@project))) ? "current" : nil
52 53  
53 54 .medium-tags{:style => 'padding: 10px 0 0 10px; width: 210px;'}= tag_list @project
54 55  
... ...
config/routes.rb
... ... @@ -41,6 +41,8 @@ Gitlab::Application.routes.draw do
41 41 get "graph"
42 42 end
43 43  
  44 + resources :deploy_keys
  45 +
44 46 resources :refs, :only => [], :path => "/" do
45 47 collection do
46 48 get "switch"
... ...
db/migrate/20111225202855_create_deploy_keys.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class CreateDeployKeys < ActiveRecord::Migration
  2 + def change
  3 + create_table :deploy_keys do |t|
  4 + t.integer "project_id", :null => false
  5 + t.datetime "created_at"
  6 + t.datetime "updated_at"
  7 + t.text "key"
  8 + t.string "title"
  9 + t.string "identifier"
  10 + end
  11 + end
  12 +end
... ...
lib/gitlabhq/gitolite.rb
... ... @@ -71,7 +71,7 @@ module Gitlabhq
71 71 ::Gitolite::Config::Repo.new(repo_name)
72 72 end
73 73  
74   - name_readers = project.repository_readers
  74 + name_readers = project.repository_readers + project.deploy_keys
75 75 name_writers = project.repository_writers
76 76  
77 77 repo.clean_permissions
... ...
spec/models/deploy_key_spec.rb 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +require 'spec_helper'
  2 +
  3 +describe DeployKey do
  4 + describe "Associations" do
  5 + it { should belong_to(:project) }
  6 + end
  7 +
  8 + describe "Validation" do
  9 + it { should validate_presence_of(:title) }
  10 + it { should validate_presence_of(:key) }
  11 + end
  12 +
  13 + describe "Methods" do
  14 + it { should respond_to :projects }
  15 + end
  16 +
  17 + it { Factory.create(:key,
  18 + :project => Factory(:project)).should be_valid }
  19 +end
  20 +# == Schema Information
  21 +#
  22 +# Table name: deploy_keys
  23 +#
  24 +# id :integer not null, primary key
  25 +# project_id :integer not null
  26 +# created_at :datetime
  27 +# updated_at :datetime
  28 +# key :text
  29 +# title :string(255)
  30 +# identifier :string(255)
  31 +#
  32 +
  33 +
... ...