From 723104c45f83937cd87bee87ac16d3dfc6ce4d88 Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 21:44:16 +0200 Subject: [PATCH 01/15] Initial deploy_key feature commit --- app/controllers/deploy_keys_controller.rb | 42 ++++++++++++++++ app/models/deploy_key.rb | 49 +++++++++++++++++++ app/models/project.rb | 1 + app/views/deploy_keys/_form.html.haml | 16 ++++++ app/views/deploy_keys/_show.html.haml | 7 +++ app/views/deploy_keys/create.js.haml | 9 ++++ app/views/deploy_keys/edit.html.haml | 7 +++ app/views/deploy_keys/index.html.haml | 16 ++++++ app/views/deploy_keys/new.html.haml | 5 ++ app/views/deploy_keys/new.js.haml | 11 +++++ app/views/deploy_keys/show.html.haml | 10 ++++ app/views/layouts/project.html.haml | 1 + config/routes.rb | 2 + .../20111225202855_create_deploy_keys.rb | 12 +++++ lib/gitlabhq/gitolite.rb | 2 +- spec/models/deploy_key_spec.rb | 33 +++++++++++++ 16 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 app/controllers/deploy_keys_controller.rb create mode 100644 app/models/deploy_key.rb create mode 100644 app/views/deploy_keys/_form.html.haml create mode 100644 app/views/deploy_keys/_show.html.haml create mode 100644 app/views/deploy_keys/create.js.haml create mode 100644 app/views/deploy_keys/edit.html.haml create mode 100644 app/views/deploy_keys/index.html.haml create mode 100644 app/views/deploy_keys/new.html.haml create mode 100644 app/views/deploy_keys/new.js.haml create mode 100644 app/views/deploy_keys/show.html.haml create mode 100644 db/migrate/20111225202855_create_deploy_keys.rb create mode 100644 spec/models/deploy_key_spec.rb diff --git a/app/controllers/deploy_keys_controller.rb b/app/controllers/deploy_keys_controller.rb new file mode 100644 index 00000000..ea8b4c5f --- /dev/null +++ b/app/controllers/deploy_keys_controller.rb @@ -0,0 +1,42 @@ +class DeployKeysController < ApplicationController + respond_to :js + layout "project" + before_filter :project + # before_filter :authorize_admin_project! + # before_filter :require_non_empty_project + + def project + @project ||= Project.find_by_code(params[:project_id]) + end + + def index + @keys = @project.deploy_keys.all + end + + def show + @key = @project.deploy_keys.find(params[:id]) + end + + def new + @key = @project.deploy_keys.new + + respond_with(@key) + end + + def create + @key = @project.deploy_keys.new(params[:key]) + @key.save + + respond_with(@key) + end + + def destroy + @key = @project.deploy_keys.find(params[:id]) + @key.destroy + + respond_to do |format| + format.html { redirect_to deploy_keys_url } + format.js { render :nothing => true } + end + end +end diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb new file mode 100644 index 00000000..cbfe211c --- /dev/null +++ b/app/models/deploy_key.rb @@ -0,0 +1,49 @@ +class DeployKey < ActiveRecord::Base + belongs_to :project + + validates :title, + :presence => true, + :length => { :within => 0..255 } + + validates :key, + :presence => true, + :uniqueness => true, + :length => { :within => 0..5000 } + + before_save :set_identifier + after_save :update_repository + after_destroy :repository_delete_key + + def set_identifier + self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" + end + + def update_repository + Gitlabhq::GitHost.system.new.configure do |c| + c.update_keys(identifier, key) + c.update_project(project) + end + end + + def repository_delete_key + Gitlabhq::GitHost.system.new.configure do |c| + c.delete_key(identifier) + c.update_project(project) + end + end + +end +# == Schema Information +# +# Table name: keys +# +# id :integer not null, primary key +# project_id :integer not null +# created_at :datetime +# updated_at :datetime +# key :text +# title :string(255) +# identifier :string(255) +# + + diff --git a/app/models/project.rb b/app/models/project.rb index 4ffb9c4c..67320f6e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -14,6 +14,7 @@ class Project < ActiveRecord::Base has_many :users, :through => :users_projects has_many :notes, :dependent => :destroy has_many :snippets, :dependent => :destroy + has_many :deploy_keys, :dependent => :destroy acts_as_taggable diff --git a/app/views/deploy_keys/_form.html.haml b/app/views/deploy_keys/_form.html.haml new file mode 100644 index 00000000..8d38535b --- /dev/null +++ b/app/views/deploy_keys/_form.html.haml @@ -0,0 +1,16 @@ +%div + = form_for [@project, @key], :remote => true do |f| + -if @key.errors.any? + %ul + - @key.errors.full_messages.each do |msg| + %li= msg + + .form-row + = f.label :title + = f.text_field :title, :style => "width:300px" + .form-row + = f.label :key + = f.text_area :key, :style => "width:300px; height:130px" + .form-row + = f.submit 'Save', :class => "grey-button" + diff --git a/app/views/deploy_keys/_show.html.haml b/app/views/deploy_keys/_show.html.haml new file mode 100644 index 00000000..26372cf9 --- /dev/null +++ b/app/views/deploy_keys/_show.html.haml @@ -0,0 +1,7 @@ +%a.update-item{:href => project_deploy_key_path(key)} + %span.update-title + = key.title + %span.update-author + Added + = time_ago_in_words(key.created_at) + ago diff --git a/app/views/deploy_keys/create.js.haml b/app/views/deploy_keys/create.js.haml new file mode 100644 index 00000000..0e8757f8 --- /dev/null +++ b/app/views/deploy_keys/create.js.haml @@ -0,0 +1,9 @@ +- if @key.valid? + :plain + $("#new_key_dialog").dialog("close"); + $("#keys-table .data").append("#{escape_javascript(render(:partial => 'show', :locals => {:key => @key} ))}"); + $("#no_ssh_key_defined").hide(); +- else + :plain + $("#new_key_dialog").empty(); + $("#new_key_dialog").append("#{escape_javascript(render('form'))}"); diff --git a/app/views/deploy_keys/edit.html.haml b/app/views/deploy_keys/edit.html.haml new file mode 100644 index 00000000..9b1b9aac --- /dev/null +++ b/app/views/deploy_keys/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing key + += render 'form' + += link_to 'Show', @key +\| += link_to 'Back', project_deploy_keys_path diff --git a/app/views/deploy_keys/index.html.haml b/app/views/deploy_keys/index.html.haml new file mode 100644 index 00000000..9cd28de9 --- /dev/null +++ b/app/views/deploy_keys/index.html.haml @@ -0,0 +1,16 @@ +%h2.icon + %span> + SSH Keys +%div#new-key-holder.right + = link_to "Add new", new_project_deploy_key_path, :remote => true, :class => "grey-button" +%br + +%div#keys-table{ :class => "update-data ui-box ui-box-small ui-box-big" } + .data + - @keys.each do |key| + = render(:partial => 'show', :locals => {:key => key}) + +:javascript + $('.delete-key').live('ajax:success', function() { + $(this).closest('.update-item').fadeOut(); }); + diff --git a/app/views/deploy_keys/new.html.haml b/app/views/deploy_keys/new.html.haml new file mode 100644 index 00000000..9be37204 --- /dev/null +++ b/app/views/deploy_keys/new.html.haml @@ -0,0 +1,5 @@ +%h1 New key + += render 'form' + += link_to 'Back', project_deploy_keys_path diff --git a/app/views/deploy_keys/new.js.haml b/app/views/deploy_keys/new.js.haml new file mode 100644 index 00000000..86e9db03 --- /dev/null +++ b/app/views/deploy_keys/new.js.haml @@ -0,0 +1,11 @@ +:plain + var new_key_dialog = $("
"); + new_key_dialog.html("#{escape_javascript(render('form'))}"); + $(new_key_dialog).dialog({ + width: 350, + resizable: false, + draggable: false, + title: "Add new public key", + close: function(event, ui) { $("#new_key_dialog").remove();}, + modal: true + }); diff --git a/app/views/deploy_keys/show.html.haml b/app/views/deploy_keys/show.html.haml new file mode 100644 index 00000000..9dcaa093 --- /dev/null +++ b/app/views/deploy_keys/show.html.haml @@ -0,0 +1,10 @@ +.ui-box.width-100p + %h3= @key.title + .data + %pre= @key.key + .clear + .buttons + = link_to 'Remove', @key, :confirm => 'Are you sure?', :method => :delete, :class => "red-button delete-key right" + .clear + + diff --git a/app/views/layouts/project.html.haml b/app/views/layouts/project.html.haml index 3dbb69c4..a5ea76d3 100644 --- a/app/views/layouts/project.html.haml +++ b/app/views/layouts/project.html.haml @@ -49,6 +49,7 @@ - if can? current_user, :admin_project, @project = link_to "Admin", edit_project_path(@project), :class => (current_page?(edit_project_path(@project))) ? "current" : nil + = link_to "Deploy keys", project_deploy_keys_path(@project), :class => (current_page?(project_deploy_keys_path(@project))) ? "current" : nil .medium-tags{:style => 'padding: 10px 0 0 10px; width: 210px;'}= tag_list @project diff --git a/config/routes.rb b/config/routes.rb index 882ba00a..20890b76 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,8 @@ Gitlab::Application.routes.draw do get "graph" end + resources :deploy_keys + resources :refs, :only => [], :path => "/" do collection do get "switch" diff --git a/db/migrate/20111225202855_create_deploy_keys.rb b/db/migrate/20111225202855_create_deploy_keys.rb new file mode 100644 index 00000000..60e73375 --- /dev/null +++ b/db/migrate/20111225202855_create_deploy_keys.rb @@ -0,0 +1,12 @@ +class CreateDeployKeys < ActiveRecord::Migration + def change + create_table :deploy_keys do |t| + t.integer "project_id", :null => false + t.datetime "created_at" + t.datetime "updated_at" + t.text "key" + t.string "title" + t.string "identifier" + end + end +end diff --git a/lib/gitlabhq/gitolite.rb b/lib/gitlabhq/gitolite.rb index 0822c25e..93000876 100644 --- a/lib/gitlabhq/gitolite.rb +++ b/lib/gitlabhq/gitolite.rb @@ -71,7 +71,7 @@ module Gitlabhq ::Gitolite::Config::Repo.new(repo_name) end - name_readers = project.repository_readers + name_readers = project.repository_readers + project.deploy_keys name_writers = project.repository_writers repo.clean_permissions diff --git a/spec/models/deploy_key_spec.rb b/spec/models/deploy_key_spec.rb new file mode 100644 index 00000000..c20d0030 --- /dev/null +++ b/spec/models/deploy_key_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe DeployKey do + describe "Associations" do + it { should belong_to(:project) } + end + + describe "Validation" do + it { should validate_presence_of(:title) } + it { should validate_presence_of(:key) } + end + + describe "Methods" do + it { should respond_to :projects } + end + + it { Factory.create(:key, + :project => Factory(:project)).should be_valid } +end +# == Schema Information +# +# Table name: deploy_keys +# +# id :integer not null, primary key +# project_id :integer not null +# created_at :datetime +# updated_at :datetime +# key :text +# title :string(255) +# identifier :string(255) +# + + From afa7f3a17a3f798fa9ff1776b21b0f19e0e51f0c Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 22:00:50 +0200 Subject: [PATCH 02/15] DB schema updated --- db/schema.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 3014579c..f6ac7d8d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,16 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111220190817) do +ActiveRecord::Schema.define(:version => 20111225202855) do + + create_table "deploy_keys", :force => true do |t| + t.integer "project_id", :null => false + t.datetime "created_at" + t.datetime "updated_at" + t.text "key" + t.string "title" + t.string "identifier" + end create_table "issues", :force => true do |t| t.string "title" From 78edd7469cb9a92ae6fb5c84904d5a9999796936 Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 23:08:08 +0200 Subject: [PATCH 03/15] Removed unneeded merge --- lib/gitlabhq/gitolite.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlabhq/gitolite.rb b/lib/gitlabhq/gitolite.rb index 397d9fbb..e6eb8e51 100644 --- a/lib/gitlabhq/gitolite.rb +++ b/lib/gitlabhq/gitolite.rb @@ -71,7 +71,7 @@ module Gitlabhq ::Gitolite::Config::Repo.new(repo_name) end - name_readers = project.repository_readers + project.deploy_keys + name_readers = project.repository_readers name_writers = project.repository_writers repo.clean_permissions From 3a597db7739ee0e1703d9daf9f6a869a97542263 Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 23:08:26 +0200 Subject: [PATCH 04/15] Merge deploy keys identifier with read only keys --- app/models/project.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/project.rb b/app/models/project.rb index 06b9e711..05b2dd8f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -191,7 +191,7 @@ class Project < ActiveRecord::Base def repository_readers keys = Key.joins({:user => :users_projects}). where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R) - keys.map(&:identifier) + keys.map(&:identifier) + deploy_keys.map(&:identifier) end def repository_writers From 753d4cb25e2cc39159963c82e164451f43500ccd Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 23:09:46 +0200 Subject: [PATCH 05/15] Pass correct parameters --- app/models/deploy_key.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb index cbfe211c..254e766c 100644 --- a/app/models/deploy_key.rb +++ b/app/models/deploy_key.rb @@ -21,14 +21,14 @@ class DeployKey < ActiveRecord::Base def update_repository Gitlabhq::GitHost.system.new.configure do |c| c.update_keys(identifier, key) - c.update_project(project) + c.update_project(project.path, project) end end def repository_delete_key Gitlabhq::GitHost.system.new.configure do |c| c.delete_key(identifier) - c.update_project(project) + c.update_project(project.path, project) end end From f7b911d23911fa5fb18332495b18c1f4f820f745 Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 23:31:27 +0200 Subject: [PATCH 06/15] Path correction --- app/controllers/deploy_keys_controller.rb | 4 ++-- app/views/deploy_keys/_show.html.haml | 2 +- app/views/deploy_keys/show.html.haml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/deploy_keys_controller.rb b/app/controllers/deploy_keys_controller.rb index ea8b4c5f..2a5b1bce 100644 --- a/app/controllers/deploy_keys_controller.rb +++ b/app/controllers/deploy_keys_controller.rb @@ -24,7 +24,7 @@ class DeployKeysController < ApplicationController end def create - @key = @project.deploy_keys.new(params[:key]) + @key = @project.deploy_keys.new(params[:deploy_key]) @key.save respond_with(@key) @@ -35,7 +35,7 @@ class DeployKeysController < ApplicationController @key.destroy respond_to do |format| - format.html { redirect_to deploy_keys_url } + format.html { redirect_to project_deploy_keys_url } format.js { render :nothing => true } end end diff --git a/app/views/deploy_keys/_show.html.haml b/app/views/deploy_keys/_show.html.haml index 26372cf9..b1622f35 100644 --- a/app/views/deploy_keys/_show.html.haml +++ b/app/views/deploy_keys/_show.html.haml @@ -1,4 +1,4 @@ -%a.update-item{:href => project_deploy_key_path(key)} +%a.update-item{:href => project_deploy_key_path(key.project, key)} %span.update-title = key.title %span.update-author diff --git a/app/views/deploy_keys/show.html.haml b/app/views/deploy_keys/show.html.haml index 9dcaa093..2c5c6149 100644 --- a/app/views/deploy_keys/show.html.haml +++ b/app/views/deploy_keys/show.html.haml @@ -4,7 +4,7 @@ %pre= @key.key .clear .buttons - = link_to 'Remove', @key, :confirm => 'Are you sure?', :method => :delete, :class => "red-button delete-key right" + = link_to 'Remove', project_deploy_key_path(@key.project, @key), :confirm => 'Are you sure?', :method => :delete, :class => "red-button delete-key right" .clear From b85ae02856a522838898442953157006501dde3e Mon Sep 17 00:00:00 2001 From: miks Date: Thu, 29 Dec 2011 23:46:18 +0200 Subject: [PATCH 07/15] Only project admin have access to deploy_keys section --- app/controllers/deploy_keys_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/deploy_keys_controller.rb b/app/controllers/deploy_keys_controller.rb index 2a5b1bce..6e04bb4f 100644 --- a/app/controllers/deploy_keys_controller.rb +++ b/app/controllers/deploy_keys_controller.rb @@ -2,8 +2,10 @@ class DeployKeysController < ApplicationController respond_to :js layout "project" before_filter :project - # before_filter :authorize_admin_project! - # before_filter :require_non_empty_project + + # Authorize + before_filter :add_project_abilities + before_filter :authorize_admin_project! def project @project ||= Project.find_by_code(params[:project_id]) From 65601ade2b5c7f255cba752baee65ead148af58c Mon Sep 17 00:00:00 2001 From: miks Date: Fri, 30 Dec 2011 00:33:26 +0200 Subject: [PATCH 08/15] Specs for deploy_keys --- spec/factories.rb | 5 ++ spec/models/deploy_key_spec.rb | 6 +- spec/requests/projects_deploy_keys_spec.rb | 68 ++++++++++++++++++++++ spec/requests/projects_security_spec.rb | 9 +++ 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 spec/requests/projects_deploy_keys_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index 15e54ed2..b96afe2c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -56,6 +56,11 @@ Factory.add(:key, Key) do |obj| obj.key = File.read(File.join(Rails.root, "db", "pkey.example")) end +Factory.add(:deploy_key, DeployKey) do |obj| + obj.title = "Example key" + obj.key = File.read(File.join(Rails.root, "db", "pkey.example")) +end + Factory.add(:web_hook, WebHook) do |obj| obj.url = Faker::Internet.url end diff --git a/spec/models/deploy_key_spec.rb b/spec/models/deploy_key_spec.rb index c20d0030..2c9641ae 100644 --- a/spec/models/deploy_key_spec.rb +++ b/spec/models/deploy_key_spec.rb @@ -10,11 +10,7 @@ describe DeployKey do it { should validate_presence_of(:key) } end - describe "Methods" do - it { should respond_to :projects } - end - - it { Factory.create(:key, + it { Factory.create(:deploy_key, :project => Factory(:project)).should be_valid } end # == Schema Information diff --git a/spec/requests/projects_deploy_keys_spec.rb b/spec/requests/projects_deploy_keys_spec.rb new file mode 100644 index 00000000..017aa3b7 --- /dev/null +++ b/spec/requests/projects_deploy_keys_spec.rb @@ -0,0 +1,68 @@ +require 'spec_helper' + +describe "DeployKeys" do + let(:project) { Factory :project } + + before do + login_as :user + project.add_access(@user, :read, :write, :admin) + end + + describe "GET /keys" do + before do + @key = Factory :deploy_key, :project => project + visit project_deploy_keys_path(project) + end + + subject { page } + + it { should have_content(@key.title) } + + describe "Destroy" do + before { visit project_deploy_key_path(project, @key) } + + it "should remove entry" do + expect { + click_link "Remove" + }.to change { project.deploy_keys.count }.by(-1) + end + end + end + + describe "New key", :js => true do + before do + visit project_deploy_keys_path(project) + click_link "Add new" + end + + it "should open new key popup" do + page.should have_content("Add new public key") + end + + describe "fill in" do + before do + fill_in "deploy_key_title", :with => "laptop" + fill_in "deploy_key_key", :with => "publickey234=" + end + + it { expect { click_button "Save" }.to change {Key.count}.by(1) } + + it "should add new key to table" do + click_button "Save" + + page.should_not have_content("Add new public key") + page.should have_content "laptop" + end + end + end + + describe "Show page" do + before do + @key = Factory :deploy_key, :project => project + visit project_deploy_key_path(project, @key) + end + + it { page.should have_content @key.title } + it { page.should have_content @key.key[0..10] } + end +end diff --git a/spec/requests/projects_security_spec.rb b/spec/requests/projects_security_spec.rb index 2ddeb6e1..fb9f3d8c 100644 --- a/spec/requests/projects_security_spec.rb +++ b/spec/requests/projects_security_spec.rb @@ -105,6 +105,15 @@ describe "Projects" do it { edit_project_path(@project).should be_denied_for :visitor } end + describe "GET /project_code/deploy_keys" do + it { project_deploy_keys_path(@project).should be_allowed_for @u1 } + it { project_deploy_keys_path(@project).should be_denied_for @u3 } + it { project_deploy_keys_path(@project).should be_denied_for :admin } + it { project_deploy_keys_path(@project).should be_denied_for @u2 } + it { project_deploy_keys_path(@project).should be_denied_for :user } + it { project_deploy_keys_path(@project).should be_denied_for :visitor } + end + describe "GET /project_code/issues" do it { project_issues_path(@project).should be_allowed_for @u1 } it { project_issues_path(@project).should be_allowed_for @u3 } From 46d212832f3d0a4668732a3434e6732c69f6b571 Mon Sep 17 00:00:00 2001 From: miks Date: Fri, 30 Dec 2011 15:14:14 +0200 Subject: [PATCH 09/15] Spec updates --- spec/monkeypatch.rb | 10 ++++++++++ spec/requests/projects_deploy_keys_spec.rb | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/monkeypatch.rb b/spec/monkeypatch.rb index 75099e05..5ad86df2 100644 --- a/spec/monkeypatch.rb +++ b/spec/monkeypatch.rb @@ -24,6 +24,16 @@ class Key end end +class DeployKey + def update_repository + true + end + + def repository_delete_key + true + end +end + class UsersProject def update_repository true diff --git a/spec/requests/projects_deploy_keys_spec.rb b/spec/requests/projects_deploy_keys_spec.rb index 017aa3b7..3c9d170f 100644 --- a/spec/requests/projects_deploy_keys_spec.rb +++ b/spec/requests/projects_deploy_keys_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe "DeployKeys" do +describe "Projects", "DeployKeys" do let(:project) { Factory :project } before do From c973fce6062f2054d69423cf10eecff8f34bb151 Mon Sep 17 00:00:00 2001 From: miks Date: Fri, 30 Dec 2011 15:19:23 +0200 Subject: [PATCH 10/15] Use correct model for testing --- spec/requests/projects_deploy_keys_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/requests/projects_deploy_keys_spec.rb b/spec/requests/projects_deploy_keys_spec.rb index 3c9d170f..29a1aefd 100644 --- a/spec/requests/projects_deploy_keys_spec.rb +++ b/spec/requests/projects_deploy_keys_spec.rb @@ -45,7 +45,7 @@ describe "Projects", "DeployKeys" do fill_in "deploy_key_key", :with => "publickey234=" end - it { expect { click_button "Save" }.to change {Key.count}.by(1) } + it { expect { click_button "Save" }.to change {DeployKey.count}.by(1) } it "should add new key to table" do click_button "Save" From 5b4382e12e060528c42bd3e19f61df88d6316785 Mon Sep 17 00:00:00 2001 From: miks Date: Fri, 30 Dec 2011 21:59:59 +0200 Subject: [PATCH 11/15] Validate key uniqueness across Key and DeployKey tables --- app/models/deploy_key.rb | 4 ++++ app/models/key.rb | 4 ++++ lib/unique_public_key_validator.rb | 7 +++++++ 3 files changed, 15 insertions(+) create mode 100644 lib/unique_public_key_validator.rb diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb index 254e766c..c720f09b 100644 --- a/app/models/deploy_key.rb +++ b/app/models/deploy_key.rb @@ -1,3 +1,5 @@ +require 'unique_public_key_validator' + class DeployKey < ActiveRecord::Base belongs_to :project @@ -10,6 +12,8 @@ class DeployKey < ActiveRecord::Base :uniqueness => true, :length => { :within => 0..5000 } + validates_with UniquePublicKeyValidator + before_save :set_identifier after_save :update_repository after_destroy :repository_delete_key diff --git a/app/models/key.rb b/app/models/key.rb index 359538d2..0e4088e4 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -1,3 +1,5 @@ +require 'unique_public_key_validator' + class Key < ActiveRecord::Base belongs_to :user @@ -10,6 +12,8 @@ class Key < ActiveRecord::Base :uniqueness => true, :length => { :within => 0..5000 } + validates_with UniquePublicKeyValidator + before_save :set_identifier after_save :update_repository after_destroy :repository_delete_key diff --git a/lib/unique_public_key_validator.rb b/lib/unique_public_key_validator.rb new file mode 100644 index 00000000..b2306bda --- /dev/null +++ b/lib/unique_public_key_validator.rb @@ -0,0 +1,7 @@ +class UniquePublicKeyValidator < ActiveModel::Validator + def validate(record) + if (DeployKey.where('key = ? AND id !=?', record.key , record.id).count > 0 || Key.where('key = ? AND id !=?', record.key , record.id).count > 0) + record.errors.add :key, 'already exist.' + end + end +end From 99b8b577e92831d9cfe4f98fca4c6b8017c95a7a Mon Sep 17 00:00:00 2001 From: miks Date: Sat, 31 Dec 2011 16:24:10 +0200 Subject: [PATCH 12/15] DeployKey moved to Key model --- app/controllers/deploy_keys_controller.rb | 10 ++-- app/models/deploy_key.rb | 53 ------------------- app/models/key.rb | 21 +++++--- app/models/project.rb | 10 ++-- app/views/deploy_keys/_form.html.haml | 2 +- .../20111225202855_create_deploy_keys.rb | 12 ----- .../20111231111825_add_project_id_to_key.rb | 6 +++ db/schema.rb | 14 ++--- 8 files changed, 35 insertions(+), 93 deletions(-) delete mode 100644 app/models/deploy_key.rb delete mode 100644 db/migrate/20111225202855_create_deploy_keys.rb create mode 100644 db/migrate/20111231111825_add_project_id_to_key.rb diff --git a/app/controllers/deploy_keys_controller.rb b/app/controllers/deploy_keys_controller.rb index 6e04bb4f..ca22e02b 100644 --- a/app/controllers/deploy_keys_controller.rb +++ b/app/controllers/deploy_keys_controller.rb @@ -12,28 +12,28 @@ class DeployKeysController < ApplicationController end def index - @keys = @project.deploy_keys.all + @keys = @project.keys.all end def show - @key = @project.deploy_keys.find(params[:id]) + @key = @project.keys.find(params[:id]) end def new - @key = @project.deploy_keys.new + @key = @project.keys.new respond_with(@key) end def create - @key = @project.deploy_keys.new(params[:deploy_key]) + @key = @project.keys.new(params[:key]) @key.save respond_with(@key) end def destroy - @key = @project.deploy_keys.find(params[:id]) + @key = @project.keys.find(params[:id]) @key.destroy respond_to do |format| diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb deleted file mode 100644 index c720f09b..00000000 --- a/app/models/deploy_key.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'unique_public_key_validator' - -class DeployKey < ActiveRecord::Base - belongs_to :project - - validates :title, - :presence => true, - :length => { :within => 0..255 } - - validates :key, - :presence => true, - :uniqueness => true, - :length => { :within => 0..5000 } - - validates_with UniquePublicKeyValidator - - before_save :set_identifier - after_save :update_repository - after_destroy :repository_delete_key - - def set_identifier - self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" - end - - def update_repository - Gitlabhq::GitHost.system.new.configure do |c| - c.update_keys(identifier, key) - c.update_project(project.path, project) - end - end - - def repository_delete_key - Gitlabhq::GitHost.system.new.configure do |c| - c.delete_key(identifier) - c.update_project(project.path, project) - end - end - -end -# == Schema Information -# -# Table name: keys -# -# id :integer not null, primary key -# project_id :integer not null -# created_at :datetime -# updated_at :datetime -# key :text -# title :string(255) -# identifier :string(255) -# - - diff --git a/app/models/key.rb b/app/models/key.rb index 0e4088e4..e5975368 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -1,7 +1,6 @@ -require 'unique_public_key_validator' - class Key < ActiveRecord::Base belongs_to :user + belongs_to :project validates :title, :presence => true, @@ -12,14 +11,16 @@ class Key < ActiveRecord::Base :uniqueness => true, :length => { :within => 0..5000 } - validates_with UniquePublicKeyValidator - before_save :set_identifier after_save :update_repository after_destroy :repository_delete_key def set_identifier - self.identifier = "#{user.identifier}_#{Time.now.to_i}" + if is_deploy_key + self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" + else + self.identifier = "#{user.identifier}_#{Time.now.to_i}" + end end def update_repository @@ -35,10 +36,18 @@ class Key < ActiveRecord::Base c.update_projects(projects) end end + + def is_deploy_key + true if project_id + end #projects that has this key def projects - user.projects + if is_deploy_key + [project] + else + user.projects + end end end # == Schema Information diff --git a/app/models/project.rb b/app/models/project.rb index 05b2dd8f..102efc45 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -14,7 +14,7 @@ class Project < ActiveRecord::Base has_many :users, :through => :users_projects has_many :notes, :dependent => :destroy has_many :snippets, :dependent => :destroy - has_many :deploy_keys, :dependent => :destroy + has_many :keys, :dependent => :destroy has_many :web_hooks, :dependent => :destroy acts_as_taggable @@ -189,15 +189,15 @@ class Project < ActiveRecord::Base end def repository_readers - keys = Key.joins({:user => :users_projects}). + read_keys = Key.joins({:user => :users_projects}). where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R) - keys.map(&:identifier) + deploy_keys.map(&:identifier) + read_keys.map(&:identifier) + keys.map(&:identifier) end def repository_writers - keys = Key.joins({:user => :users_projects}). + write_keys = Key.joins({:user => :users_projects}). where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW) - keys.map(&:identifier) + write_keys.map(&:identifier) end def readers diff --git a/app/views/deploy_keys/_form.html.haml b/app/views/deploy_keys/_form.html.haml index 8d38535b..d3a2682a 100644 --- a/app/views/deploy_keys/_form.html.haml +++ b/app/views/deploy_keys/_form.html.haml @@ -1,5 +1,5 @@ %div - = form_for [@project, @key], :remote => true do |f| + = form_for [@project, @key], :url => project_deploy_keys_path, :remote => true do |f| -if @key.errors.any? %ul - @key.errors.full_messages.each do |msg| diff --git a/db/migrate/20111225202855_create_deploy_keys.rb b/db/migrate/20111225202855_create_deploy_keys.rb deleted file mode 100644 index 60e73375..00000000 --- a/db/migrate/20111225202855_create_deploy_keys.rb +++ /dev/null @@ -1,12 +0,0 @@ -class CreateDeployKeys < ActiveRecord::Migration - def change - create_table :deploy_keys do |t| - t.integer "project_id", :null => false - t.datetime "created_at" - t.datetime "updated_at" - t.text "key" - t.string "title" - t.string "identifier" - end - end -end diff --git a/db/migrate/20111231111825_add_project_id_to_key.rb b/db/migrate/20111231111825_add_project_id_to_key.rb new file mode 100644 index 00000000..dc80cbdb --- /dev/null +++ b/db/migrate/20111231111825_add_project_id_to_key.rb @@ -0,0 +1,6 @@ +class AddProjectIdToKey < ActiveRecord::Migration + def change + add_column :keys, :project_id, :integer, :null => true + change_column :keys, :user_id, :integer, :null => true + end +end diff --git a/db/schema.rb b/db/schema.rb index f6ac7d8d..aab847e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20111225202855) do - - create_table "deploy_keys", :force => true do |t| - t.integer "project_id", :null => false - t.datetime "created_at" - t.datetime "updated_at" - t.text "key" - t.string "title" - t.string "identifier" - end +ActiveRecord::Schema.define(:version => 20111231111825) do create_table "issues", :force => true do |t| t.string "title" @@ -36,12 +27,13 @@ ActiveRecord::Schema.define(:version => 20111225202855) do end create_table "keys", :force => true do |t| - t.integer "user_id", :null => false + t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" t.text "key" t.string "title" t.string "identifier" + t.integer "project_id" end create_table "merge_requests", :force => true do |t| From b8b6515f935ffc9a27b7a735514dc39e8c092148 Mon Sep 17 00:00:00 2001 From: miks Date: Sat, 31 Dec 2011 19:37:14 +0200 Subject: [PATCH 13/15] Renamed project "keys" back to "deploy_keys" --- app/controllers/deploy_keys_controller.rb | 10 +++++----- app/models/project.rb | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/controllers/deploy_keys_controller.rb b/app/controllers/deploy_keys_controller.rb index ca22e02b..7bbf6a3d 100644 --- a/app/controllers/deploy_keys_controller.rb +++ b/app/controllers/deploy_keys_controller.rb @@ -12,28 +12,28 @@ class DeployKeysController < ApplicationController end def index - @keys = @project.keys.all + @keys = @project.deploy_keys.all end def show - @key = @project.keys.find(params[:id]) + @key = @project.deploy_keys.find(params[:id]) end def new - @key = @project.keys.new + @key = @project.deploy_keys.new respond_with(@key) end def create - @key = @project.keys.new(params[:key]) + @key = @project.deploy_keys.new(params[:key]) @key.save respond_with(@key) end def destroy - @key = @project.keys.find(params[:id]) + @key = @project.deploy_keys.find(params[:id]) @key.destroy respond_to do |format| diff --git a/app/models/project.rb b/app/models/project.rb index 102efc45..51fb7cc3 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -14,7 +14,7 @@ class Project < ActiveRecord::Base has_many :users, :through => :users_projects has_many :notes, :dependent => :destroy has_many :snippets, :dependent => :destroy - has_many :keys, :dependent => :destroy + has_many :deploy_keys, :dependent => :destroy, :foreign_key => "project_id", :class_name => "Key" has_many :web_hooks, :dependent => :destroy acts_as_taggable @@ -189,15 +189,15 @@ class Project < ActiveRecord::Base end def repository_readers - read_keys = Key.joins({:user => :users_projects}). + keys = Key.joins({:user => :users_projects}). where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R) - read_keys.map(&:identifier) + keys.map(&:identifier) + keys.map(&:identifier) + deploy_keys.map(&:identifier) end def repository_writers - write_keys = Key.joins({:user => :users_projects}). + keys = Key.joins({:user => :users_projects}). where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW) - write_keys.map(&:identifier) + keys.map(&:identifier) end def readers From df583bf12d4aa5bdffed6d43aa985e937e08e883 Mon Sep 17 00:00:00 2001 From: miks Date: Sat, 31 Dec 2011 19:37:51 +0200 Subject: [PATCH 14/15] Specs for deploy_keys updated --- spec/factories.rb | 5 ---- spec/models/deploy_key_spec.rb | 29 ---------------------- spec/models/key_spec.rb | 2 +- spec/monkeypatch.rb | 10 -------- spec/requests/projects_deploy_keys_spec.rb | 10 ++++---- 5 files changed, 6 insertions(+), 50 deletions(-) delete mode 100644 spec/models/deploy_key_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index b96afe2c..15e54ed2 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -56,11 +56,6 @@ Factory.add(:key, Key) do |obj| obj.key = File.read(File.join(Rails.root, "db", "pkey.example")) end -Factory.add(:deploy_key, DeployKey) do |obj| - obj.title = "Example key" - obj.key = File.read(File.join(Rails.root, "db", "pkey.example")) -end - Factory.add(:web_hook, WebHook) do |obj| obj.url = Faker::Internet.url end diff --git a/spec/models/deploy_key_spec.rb b/spec/models/deploy_key_spec.rb deleted file mode 100644 index 2c9641ae..00000000 --- a/spec/models/deploy_key_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'spec_helper' - -describe DeployKey do - describe "Associations" do - it { should belong_to(:project) } - end - - describe "Validation" do - it { should validate_presence_of(:title) } - it { should validate_presence_of(:key) } - end - - it { Factory.create(:deploy_key, - :project => Factory(:project)).should be_valid } -end -# == Schema Information -# -# Table name: deploy_keys -# -# id :integer not null, primary key -# project_id :integer not null -# created_at :datetime -# updated_at :datetime -# key :text -# title :string(255) -# identifier :string(255) -# - - diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb index 6522b825..dec0b996 100644 --- a/spec/models/key_spec.rb +++ b/spec/models/key_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe Key do describe "Associations" do - it { should belong_to(:user) } + it { should belong_to(:user) or belong_to(:project) } end describe "Validation" do diff --git a/spec/monkeypatch.rb b/spec/monkeypatch.rb index 5ad86df2..75099e05 100644 --- a/spec/monkeypatch.rb +++ b/spec/monkeypatch.rb @@ -24,16 +24,6 @@ class Key end end -class DeployKey - def update_repository - true - end - - def repository_delete_key - true - end -end - class UsersProject def update_repository true diff --git a/spec/requests/projects_deploy_keys_spec.rb b/spec/requests/projects_deploy_keys_spec.rb index 29a1aefd..80991244 100644 --- a/spec/requests/projects_deploy_keys_spec.rb +++ b/spec/requests/projects_deploy_keys_spec.rb @@ -10,7 +10,7 @@ describe "Projects", "DeployKeys" do describe "GET /keys" do before do - @key = Factory :deploy_key, :project => project + @key = Factory :key, :project => project visit project_deploy_keys_path(project) end @@ -41,11 +41,11 @@ describe "Projects", "DeployKeys" do describe "fill in" do before do - fill_in "deploy_key_title", :with => "laptop" - fill_in "deploy_key_key", :with => "publickey234=" + fill_in "key_title", :with => "laptop" + fill_in "key_key", :with => "publickey234=" end - it { expect { click_button "Save" }.to change {DeployKey.count}.by(1) } + it { expect { click_button "Save" }.to change {Key.count}.by(1) } it "should add new key to table" do click_button "Save" @@ -58,7 +58,7 @@ describe "Projects", "DeployKeys" do describe "Show page" do before do - @key = Factory :deploy_key, :project => project + @key = Factory :key, :project => project visit project_deploy_key_path(project, @key) end From dda6b0ab63eb8080e34b4273cfb6aadb7a29c028 Mon Sep 17 00:00:00 2001 From: miks Date: Sat, 31 Dec 2011 19:40:38 +0200 Subject: [PATCH 15/15] Unused validator deleted --- lib/unique_public_key_validator.rb | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 lib/unique_public_key_validator.rb diff --git a/lib/unique_public_key_validator.rb b/lib/unique_public_key_validator.rb deleted file mode 100644 index b2306bda..00000000 --- a/lib/unique_public_key_validator.rb +++ /dev/null @@ -1,7 +0,0 @@ -class UniquePublicKeyValidator < ActiveModel::Validator - def validate(record) - if (DeployKey.where('key = ? AND id !=?', record.key , record.id).count > 0 || Key.where('key = ? AND id !=?', record.key , record.id).count > 0) - record.errors.add :key, 'already exist.' - end - end -end