Commit cb59aade4e568e6146a966362385ac41af4dc604
1 parent
74f8bc72
Exists in
master
and in
4 other branches
Cleaning and refactoring
Showing
12 changed files
with
135 additions
and
232 deletions
Show diff stats
app/controllers/admin/projects_controller.rb
| @@ -31,8 +31,7 @@ class Admin::ProjectsController < ApplicationController | @@ -31,8 +31,7 @@ class Admin::ProjectsController < ApplicationController | ||
| 31 | UsersProject.bulk_import( | 31 | UsersProject.bulk_import( |
| 32 | @admin_project, | 32 | @admin_project, |
| 33 | params[:user_ids], | 33 | params[:user_ids], |
| 34 | - params[:project_access], | ||
| 35 | - params[:repo_access] | 34 | + params[:project_access] |
| 36 | ) | 35 | ) |
| 37 | 36 | ||
| 38 | redirect_to [:admin, @admin_project], notice: 'Project was successfully updated.' | 37 | redirect_to [:admin, @admin_project], notice: 'Project was successfully updated.' |
app/controllers/admin/users_controller.rb
| @@ -23,8 +23,7 @@ class Admin::UsersController < ApplicationController | @@ -23,8 +23,7 @@ class Admin::UsersController < ApplicationController | ||
| 23 | UsersProject.user_bulk_import( | 23 | UsersProject.user_bulk_import( |
| 24 | @admin_user, | 24 | @admin_user, |
| 25 | params[:project_ids], | 25 | params[:project_ids], |
| 26 | - params[:project_access], | ||
| 27 | - params[:repo_access] | 26 | + params[:project_access] |
| 28 | ) | 27 | ) |
| 29 | 28 | ||
| 30 | redirect_to [:admin, @admin_user], notice: 'Teams were successfully updated.' | 29 | redirect_to [:admin, @admin_user], notice: 'Teams were successfully updated.' |
app/models/project.rb
| @@ -54,27 +54,6 @@ class Project < ActiveRecord::Base | @@ -54,27 +54,6 @@ class Project < ActiveRecord::Base | ||
| 54 | UsersProject.access_roles | 54 | UsersProject.access_roles |
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | - def repository | ||
| 58 | - @repository ||= Repository.new(self) | ||
| 59 | - end | ||
| 60 | - | ||
| 61 | - delegate :repo, | ||
| 62 | - :url_to_repo, | ||
| 63 | - :path_to_repo, | ||
| 64 | - :update_repository, | ||
| 65 | - :destroy_repository, | ||
| 66 | - :tags, | ||
| 67 | - :repo_exists?, | ||
| 68 | - :commit, | ||
| 69 | - :commits, | ||
| 70 | - :commits_with_refs, | ||
| 71 | - :tree, | ||
| 72 | - :heads, | ||
| 73 | - :commits_since, | ||
| 74 | - :fresh_commits, | ||
| 75 | - :commits_between, | ||
| 76 | - :to => :repository, :prefix => nil | ||
| 77 | - | ||
| 78 | def to_param | 57 | def to_param |
| 79 | code | 58 | code |
| 80 | end | 59 | end |
| @@ -213,18 +192,6 @@ class Project < ActiveRecord::Base | @@ -213,18 +192,6 @@ class Project < ActiveRecord::Base | ||
| 213 | keys.map(&:identifier) | 192 | keys.map(&:identifier) |
| 214 | end | 193 | end |
| 215 | 194 | ||
| 216 | - def readers | ||
| 217 | - @readers ||= users_projects.includes(:user).map(&:user) | ||
| 218 | - end | ||
| 219 | - | ||
| 220 | - def writers | ||
| 221 | - @writers ||= users_projects.includes(:user).map(&:user) | ||
| 222 | - end | ||
| 223 | - | ||
| 224 | - def admins | ||
| 225 | - @admins ||= users_projects.includes(:user).where(:project_access => UsersProject::MASTER).map(&:user) | ||
| 226 | - end | ||
| 227 | - | ||
| 228 | def allow_read_for?(user) | 195 | def allow_read_for?(user) |
| 229 | !users_projects.where(:user_id => user.id).empty? | 196 | !users_projects.where(:user_id => user.id).empty? |
| 230 | end | 197 | end |
| @@ -269,10 +236,6 @@ class Project < ActiveRecord::Base | @@ -269,10 +236,6 @@ class Project < ActiveRecord::Base | ||
| 269 | end | 236 | end |
| 270 | end | 237 | end |
| 271 | 238 | ||
| 272 | - def last_activity_date_cached(expire = 1.hour) | ||
| 273 | - last_activity_date | ||
| 274 | - end | ||
| 275 | - | ||
| 276 | def check_limit | 239 | def check_limit |
| 277 | unless owner.can_create_project? | 240 | unless owner.can_create_project? |
| 278 | errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") | 241 | errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") |
| @@ -293,7 +256,127 @@ class Project < ActiveRecord::Base | @@ -293,7 +256,127 @@ class Project < ActiveRecord::Base | ||
| 293 | errors.add(:path, "Invalid repository path") | 256 | errors.add(:path, "Invalid repository path") |
| 294 | false | 257 | false |
| 295 | end | 258 | end |
| 259 | + | ||
| 260 | + def commit(commit_id = nil) | ||
| 261 | + commit = if commit_id | ||
| 262 | + repo.commits(commit_id).first | ||
| 263 | + else | ||
| 264 | + repo.commits.first | ||
| 265 | + end | ||
| 266 | + Commit.new(commit) if commit | ||
| 267 | + end | ||
| 268 | + | ||
| 269 | + def fresh_commits(n = 10) | ||
| 270 | + commits = heads.map do |h| | ||
| 271 | + repo.commits(h.name, n).map { |c| Commit.new(c, h) } | ||
| 272 | + end.flatten.uniq { |c| c.id } | ||
| 273 | + | ||
| 274 | + commits.sort! do |x, y| | ||
| 275 | + y.committed_date <=> x.committed_date | ||
| 276 | + end | ||
| 277 | + | ||
| 278 | + commits[0...n] | ||
| 279 | + end | ||
| 280 | + | ||
| 281 | + def commits_with_refs(n = 20) | ||
| 282 | + commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) } | ||
| 283 | + | ||
| 284 | + commits.sort! do |x, y| | ||
| 285 | + y.committed_date <=> x.committed_date | ||
| 286 | + end | ||
| 287 | + | ||
| 288 | + commits[0..n] | ||
| 289 | + end | ||
| 290 | + | ||
| 291 | + def commits_since(date) | ||
| 292 | + commits = heads.map do |h| | ||
| 293 | + repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) } | ||
| 294 | + end.flatten.uniq { |c| c.id } | ||
| 295 | + | ||
| 296 | + commits.sort! do |x, y| | ||
| 297 | + y.committed_date <=> x.committed_date | ||
| 298 | + end | ||
| 299 | + | ||
| 300 | + commits | ||
| 301 | + end | ||
| 302 | + | ||
| 303 | + def commits(ref, path = nil, limit = nil, offset = nil) | ||
| 304 | + if path | ||
| 305 | + repo.log(ref, path, :max_count => limit, :skip => offset) | ||
| 306 | + elsif limit && offset | ||
| 307 | + repo.commits(ref, limit, offset) | ||
| 308 | + else | ||
| 309 | + repo.commits(ref) | ||
| 310 | + end.map{ |c| Commit.new(c) } | ||
| 311 | + end | ||
| 312 | + | ||
| 313 | + def commits_between(from, to) | ||
| 314 | + repo.commits_between(from, to).map { |c| Commit.new(c) } | ||
| 315 | + end | ||
| 316 | + | ||
| 317 | + def project_id | ||
| 318 | + self.id | ||
| 319 | + end | ||
| 320 | + | ||
| 321 | + def write_hooks | ||
| 322 | + %w(post-receive).each do |hook| | ||
| 323 | + write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook"))) | ||
| 324 | + end | ||
| 325 | + end | ||
| 326 | + | ||
| 327 | + def write_hook(name, content) | ||
| 328 | + hook_file = File.join(path_to_repo, 'hooks', name) | ||
| 329 | + | ||
| 330 | + File.open(hook_file, 'w') do |f| | ||
| 331 | + f.write(content) | ||
| 332 | + end | ||
| 333 | + | ||
| 334 | + File.chmod(0775, hook_file) | ||
| 335 | + end | ||
| 336 | + | ||
| 337 | + def repo | ||
| 338 | + @repo ||= Grit::Repo.new(path_to_repo) | ||
| 339 | + end | ||
| 340 | + | ||
| 341 | + def url_to_repo | ||
| 342 | + Gitlabhq::GitHost.url_to_repo(path) | ||
| 343 | + end | ||
| 344 | + | ||
| 345 | + def path_to_repo | ||
| 346 | + File.join(GIT_HOST["base_path"], "#{path}.git") | ||
| 347 | + end | ||
| 348 | + | ||
| 349 | + def update_repository | ||
| 350 | + Gitlabhq::GitHost.system.update_project(path, self) | ||
| 351 | + | ||
| 352 | + write_hooks if File.exists?(path_to_repo) | ||
| 353 | + end | ||
| 354 | + | ||
| 355 | + def destroy_repository | ||
| 356 | + Gitlabhq::GitHost.system.destroy_project(self) | ||
| 357 | + end | ||
| 358 | + | ||
| 359 | + def repo_exists? | ||
| 360 | + @repo_exists ||= (repo && !repo.branches.empty?) | ||
| 361 | + rescue | ||
| 362 | + @repo_exists = false | ||
| 363 | + end | ||
| 364 | + | ||
| 365 | + def tags | ||
| 366 | + repo.tags.map(&:name).sort.reverse | ||
| 367 | + end | ||
| 368 | + | ||
| 369 | + def heads | ||
| 370 | + @heads ||= repo.heads | ||
| 371 | + end | ||
| 372 | + | ||
| 373 | + def tree(fcommit, path = nil) | ||
| 374 | + fcommit = commit if fcommit == :head | ||
| 375 | + tree = fcommit.tree | ||
| 376 | + path ? (tree / path) : tree | ||
| 377 | + end | ||
| 296 | end | 378 | end |
| 379 | + | ||
| 297 | # == Schema Information | 380 | # == Schema Information |
| 298 | # | 381 | # |
| 299 | # Table name: projects | 382 | # Table name: projects |
app/models/protected_branch.rb
| @@ -7,9 +7,7 @@ class ProtectedBranch < ActiveRecord::Base | @@ -7,9 +7,7 @@ class ProtectedBranch < ActiveRecord::Base | ||
| 7 | after_destroy :update_repository | 7 | after_destroy :update_repository |
| 8 | 8 | ||
| 9 | def update_repository | 9 | def update_repository |
| 10 | - Gitlabhq::GitHost.system.new.configure do |c| | ||
| 11 | - c.update_project(project.path, project) | ||
| 12 | - end | 10 | + Gitlabhq::GitHost.system.update_project(project.path, project) |
| 13 | end | 11 | end |
| 14 | 12 | ||
| 15 | def commit | 13 | def commit |
app/models/repository.rb
| @@ -10,135 +10,4 @@ class Repository | @@ -10,135 +10,4 @@ class Repository | ||
| 10 | def self.access_options | 10 | def self.access_options |
| 11 | {} | 11 | {} |
| 12 | end | 12 | end |
| 13 | - | ||
| 14 | - def initialize(project) | ||
| 15 | - @project = project | ||
| 16 | - end | ||
| 17 | - | ||
| 18 | - def path | ||
| 19 | - @path ||= project.path | ||
| 20 | - end | ||
| 21 | - | ||
| 22 | - def project_id | ||
| 23 | - project.id | ||
| 24 | - end | ||
| 25 | - | ||
| 26 | - def write_hooks | ||
| 27 | - %w(post-receive).each do |hook| | ||
| 28 | - write_hook(hook, File.read(File.join(Rails.root, 'lib', "#{hook}-hook"))) | ||
| 29 | - end | ||
| 30 | - end | ||
| 31 | - | ||
| 32 | - def write_hook(name, content) | ||
| 33 | - hook_file = File.join(project.path_to_repo, 'hooks', name) | ||
| 34 | - | ||
| 35 | - File.open(hook_file, 'w') do |f| | ||
| 36 | - f.write(content) | ||
| 37 | - end | ||
| 38 | - | ||
| 39 | - File.chmod(0775, hook_file) | ||
| 40 | - end | ||
| 41 | - | ||
| 42 | - def repo | ||
| 43 | - @repo ||= Grit::Repo.new(project.path_to_repo) | ||
| 44 | - end | ||
| 45 | - | ||
| 46 | - def url_to_repo | ||
| 47 | - Gitlabhq::GitHost.url_to_repo(path) | ||
| 48 | - end | ||
| 49 | - | ||
| 50 | - def path_to_repo | ||
| 51 | - File.join(GIT_HOST["base_path"], "#{path}.git") | ||
| 52 | - end | ||
| 53 | - | ||
| 54 | - def update_repository | ||
| 55 | - Gitlabhq::GitHost.system.new.configure do |c| | ||
| 56 | - c.update_project(path, project) | ||
| 57 | - end | ||
| 58 | - | ||
| 59 | - write_hooks if File.exists?(project.path_to_repo) | ||
| 60 | - end | ||
| 61 | - | ||
| 62 | - def destroy_repository | ||
| 63 | - Gitlabhq::GitHost.system.new.configure do |c| | ||
| 64 | - c.destroy_project(@project) | ||
| 65 | - end | ||
| 66 | - end | ||
| 67 | - | ||
| 68 | - def repo_exists? | ||
| 69 | - @repo_exists ||= (repo && !repo.branches.empty?) | ||
| 70 | - rescue | ||
| 71 | - @repo_exists = false | ||
| 72 | - end | ||
| 73 | - | ||
| 74 | - def tags | ||
| 75 | - repo.tags.map(&:name).sort.reverse | ||
| 76 | - end | ||
| 77 | - | ||
| 78 | - def heads | ||
| 79 | - @heads ||= repo.heads | ||
| 80 | - end | ||
| 81 | - | ||
| 82 | - def tree(fcommit, path = nil) | ||
| 83 | - fcommit = commit if fcommit == :head | ||
| 84 | - tree = fcommit.tree | ||
| 85 | - path ? (tree / path) : tree | ||
| 86 | - end | ||
| 87 | - | ||
| 88 | - def commit(commit_id = nil) | ||
| 89 | - commit = if commit_id | ||
| 90 | - repo.commits(commit_id).first | ||
| 91 | - else | ||
| 92 | - repo.commits.first | ||
| 93 | - end | ||
| 94 | - Commit.new(commit) if commit | ||
| 95 | - end | ||
| 96 | - | ||
| 97 | - def fresh_commits(n = 10) | ||
| 98 | - commits = heads.map do |h| | ||
| 99 | - repo.commits(h.name, n).map { |c| Commit.new(c, h) } | ||
| 100 | - end.flatten.uniq { |c| c.id } | ||
| 101 | - | ||
| 102 | - commits.sort! do |x, y| | ||
| 103 | - y.committed_date <=> x.committed_date | ||
| 104 | - end | ||
| 105 | - | ||
| 106 | - commits[0...n] | ||
| 107 | - end | ||
| 108 | - | ||
| 109 | - def commits_with_refs(n = 20) | ||
| 110 | - commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) } | ||
| 111 | - | ||
| 112 | - commits.sort! do |x, y| | ||
| 113 | - y.committed_date <=> x.committed_date | ||
| 114 | - end | ||
| 115 | - | ||
| 116 | - commits[0..n] | ||
| 117 | - end | ||
| 118 | - | ||
| 119 | - def commits_since(date) | ||
| 120 | - commits = heads.map do |h| | ||
| 121 | - repo.log(h.name, nil, :since => date).each { |c| Commit.new(c, h) } | ||
| 122 | - end.flatten.uniq { |c| c.id } | ||
| 123 | - | ||
| 124 | - commits.sort! do |x, y| | ||
| 125 | - y.committed_date <=> x.committed_date | ||
| 126 | - end | ||
| 127 | - | ||
| 128 | - commits | ||
| 129 | - end | ||
| 130 | - | ||
| 131 | - def commits(ref, path = nil, limit = nil, offset = nil) | ||
| 132 | - if path | ||
| 133 | - repo.log(ref, path, :max_count => limit, :skip => offset) | ||
| 134 | - elsif limit && offset | ||
| 135 | - repo.commits(ref, limit, offset) | ||
| 136 | - else | ||
| 137 | - repo.commits(ref) | ||
| 138 | - end.map{ |c| Commit.new(c) } | ||
| 139 | - end | ||
| 140 | - | ||
| 141 | - def commits_between(from, to) | ||
| 142 | - repo.commits_between(from, to).map { |c| Commit.new(c) } | ||
| 143 | - end | ||
| 144 | end | 13 | end |
app/models/users_project.rb
| @@ -18,7 +18,7 @@ class UsersProject < ActiveRecord::Base | @@ -18,7 +18,7 @@ class UsersProject < ActiveRecord::Base | ||
| 18 | 18 | ||
| 19 | delegate :name, :email, :to => :user, :prefix => true | 19 | delegate :name, :email, :to => :user, :prefix => true |
| 20 | 20 | ||
| 21 | - def self.bulk_import(project, user_ids, project_access, repo_access) | 21 | + def self.bulk_import(project, user_ids, project_access) |
| 22 | UsersProject.transaction do | 22 | UsersProject.transaction do |
| 23 | user_ids.each do |user_id| | 23 | user_ids.each do |user_id| |
| 24 | users_project = UsersProject.new( | 24 | users_project = UsersProject.new( |
| @@ -31,7 +31,7 @@ class UsersProject < ActiveRecord::Base | @@ -31,7 +31,7 @@ class UsersProject < ActiveRecord::Base | ||
| 31 | end | 31 | end |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | - def self.user_bulk_import(user, project_ids, project_access, repo_access) | 34 | + def self.user_bulk_import(user, project_ids, project_access) |
| 35 | UsersProject.transaction do | 35 | UsersProject.transaction do |
| 36 | project_ids.each do |project_id| | 36 | project_ids.each do |project_id| |
| 37 | users_project = UsersProject.new( | 37 | users_project = UsersProject.new( |
app/views/dashboard/_projects_feed.html.haml
app/views/projects/_feed.html.haml
| @@ -1,23 +0,0 @@ | @@ -1,23 +0,0 @@ | ||
| 1 | -- @activities.each do |update| | ||
| 2 | - .wll | ||
| 3 | - = link_to dashboard_feed_path(@project, update) do | ||
| 4 | - - if update.kind_of? Note | ||
| 5 | - %p | ||
| 6 | - %strong | ||
| 7 | - - if update.target | ||
| 8 | - = update.target.class.name.titleize | ||
| 9 | - = truncate update.target.id.to_s, :length => 10 | ||
| 10 | - commented | ||
| 11 | - - else | ||
| 12 | - Project wall | ||
| 13 | - – | ||
| 14 | - = image_tag gravatar_icon(update.author_email), :class => "", :width => 16 | ||
| 15 | - = truncate dashboard_feed_title(update), :length => 50 | ||
| 16 | - - else | ||
| 17 | - %p | ||
| 18 | - %strong | ||
| 19 | - = update.class.name.titleize | ||
| 20 | - = truncate update.id.to_s | ||
| 21 | - – | ||
| 22 | - = image_tag gravatar_icon(update.author_email), :class => "", :width => 16 | ||
| 23 | - = truncate dashboard_feed_title(update), :length => 50 |
app/views/projects/_list.html.haml
| @@ -1,26 +0,0 @@ | @@ -1,26 +0,0 @@ | ||
| 1 | -%table.round-borders#projects-list | ||
| 2 | - %tr | ||
| 3 | - %th Name | ||
| 4 | - %th Path | ||
| 5 | - %th Code | ||
| 6 | - %th Web | ||
| 7 | - %th Git | ||
| 8 | - %th Admin | ||
| 9 | - %th Actions | ||
| 10 | - | ||
| 11 | - - @projects.each do |project| | ||
| 12 | - %tr{ :class => "project", :url => project_path(project) } | ||
| 13 | - %td | ||
| 14 | - = project.name | ||
| 15 | - .small-tags= tag_list project | ||
| 16 | - | ||
| 17 | - %td= truncate project.url_to_repo | ||
| 18 | - %td= project.code | ||
| 19 | - %td= check_box_tag "read", 1, project.readers.include?(current_user), :disabled => :disabled | ||
| 20 | - %td= check_box_tag "commit", 1, project.writers.include?(current_user), :disabled => :disabled | ||
| 21 | - %td= check_box_tag "admin", 1, project.admins.include?(current_user), :disabled => :disabled | ||
| 22 | - %td | ||
| 23 | - -if can? current_user, :admin_project, project | ||
| 24 | - = link_to 'Edit', edit_project_path(project), :class => "lbutton positive" | ||
| 25 | -%br | ||
| 26 | - |
lib/gitlabhq/gitolite.rb
| @@ -6,6 +6,14 @@ module Gitlabhq | @@ -6,6 +6,14 @@ module Gitlabhq | ||
| 6 | class Gitolite | 6 | class Gitolite |
| 7 | class AccessDenied < StandardError; end | 7 | class AccessDenied < StandardError; end |
| 8 | 8 | ||
| 9 | + def self.update_project(path, project) | ||
| 10 | + self.new.configure { |git| git.update_project(path, project) } | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + def self.destroy_project(project) | ||
| 14 | + self.new.configure { |git| git.destroy_project(project) } | ||
| 15 | + end | ||
| 16 | + | ||
| 9 | def pull | 17 | def pull |
| 10 | # create tmp dir | 18 | # create tmp dir |
| 11 | @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}") | 19 | @local_dir = File.join(Dir.tmpdir,"gitlabhq-gitolite-#{Time.now.to_i}") |
lib/tasks/bulk_import.rake
| @@ -90,8 +90,7 @@ def create_repo_project(project_name, user_email) | @@ -90,8 +90,7 @@ def create_repo_project(project_name, user_email) | ||
| 90 | 90 | ||
| 91 | # Add user as admin for project | 91 | # Add user as admin for project |
| 92 | project.users_projects.create!( | 92 | project.users_projects.create!( |
| 93 | - :repo_access => Repository::REPO_RW, | ||
| 94 | - :project_access => Project::PROJECT_RWA, | 93 | + :project_access => UsersProject::MASTER, |
| 95 | :user => user | 94 | :user => user |
| 96 | ) | 95 | ) |
| 97 | 96 |
spec/models/project_spec.rb
| @@ -22,10 +22,7 @@ describe Project do | @@ -22,10 +22,7 @@ describe Project do | ||
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | describe "Respond to" do | 24 | describe "Respond to" do |
| 25 | - it { should respond_to(:readers) } | ||
| 26 | - it { should respond_to(:writers) } | ||
| 27 | it { should respond_to(:repository_writers) } | 25 | it { should respond_to(:repository_writers) } |
| 28 | - it { should respond_to(:admins) } | ||
| 29 | it { should respond_to(:add_access) } | 26 | it { should respond_to(:add_access) } |
| 30 | it { should respond_to(:reset_access) } | 27 | it { should respond_to(:reset_access) } |
| 31 | it { should respond_to(:update_repository) } | 28 | it { should respond_to(:update_repository) } |