Skip to content
Snippets Groups Projects
Commit dccd8b6e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Continue refactoring. Use repostory and team

parent 39ba934c
No related branches found
No related tags found
No related merge requests found
Showing
with 142 additions and 105 deletions
Loading
@@ -9,7 +9,7 @@ module Notes
Loading
@@ -9,7 +9,7 @@ module Notes
   
@notes = case target_type @notes = case target_type
when "commit" when "commit"
project.commit_notes(project.commit(target_id)).fresh.limit(20) project.commit_notes(project.repository.commit(target_id)).fresh.limit(20)
when "issue" when "issue"
project.issues.find(target_id).notes.inc_author.fresh.limit(20) project.issues.find(target_id).notes.inc_author.fresh.limit(20)
when "merge_request" when "merge_request"
Loading
Loading
class TestHookContext < BaseContext class TestHookContext < BaseContext
def execute def execute
hook = project.hooks.find(params[:id]) hook = project.hooks.find(params[:id])
commits = project.commits(project.default_branch, nil, 3) commits = project.repository.commits(project.default_branch, nil, 3)
data = project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", current_user) data = project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", current_user)
hook.execute(data) hook.execute(data)
end end
Loading
Loading
Loading
@@ -10,6 +10,7 @@ class Admin::ProjectsController < AdminController
Loading
@@ -10,6 +10,7 @@ class Admin::ProjectsController < AdminController
end end
   
def show def show
@repository = @project.repository
@users = User.active @users = User.active
@users = @users.not_in_project(@project) if @project.users.present? @users = @users.not_in_project(@project) if @project.users.present?
@users = @users.all @users = @users.all
Loading
@@ -19,7 +20,7 @@ class Admin::ProjectsController < AdminController
Loading
@@ -19,7 +20,7 @@ class Admin::ProjectsController < AdminController
end end
   
def team_update def team_update
@project.add_users_ids_to_team(params[:user_ids], params[:project_access]) @project.team.add_users_ids(params[:user_ids], params[:project_access])
   
redirect_to [:admin, @project], notice: 'Project was successfully updated.' redirect_to [:admin, @project], notice: 'Project was successfully updated.'
end end
Loading
@@ -36,7 +37,7 @@ class Admin::ProjectsController < AdminController
Loading
@@ -36,7 +37,7 @@ class Admin::ProjectsController < AdminController
   
def destroy def destroy
# Delete team first in order to prevent multiple gitolite calls # Delete team first in order to prevent multiple gitolite calls
@project.truncate_team @project.team.truncate
   
@project.destroy @project.destroy
   
Loading
Loading
Loading
@@ -83,12 +83,12 @@ class MergeRequestsController < ProjectResourceController
Loading
@@ -83,12 +83,12 @@ class MergeRequestsController < ProjectResourceController
end end
   
def branch_from def branch_from
@commit = project.commit(params[:ref]) @commit = @repository.commit(params[:ref])
@commit = CommitDecorator.decorate(@commit) @commit = CommitDecorator.decorate(@commit)
end end
   
def branch_to def branch_to
@commit = project.commit(params[:ref]) @commit = @repository.commit(params[:ref])
@commit = CommitDecorator.decorate(@commit) @commit = CommitDecorator.decorate(@commit)
end end
   
Loading
Loading
Loading
@@ -2,6 +2,7 @@ require Rails.root.join('lib', 'gitlab', 'graph', 'json_builder')
Loading
@@ -2,6 +2,7 @@ require Rails.root.join('lib', 'gitlab', 'graph', 'json_builder')
   
class ProjectsController < ProjectResourceController class ProjectsController < ProjectResourceController
skip_before_filter :project, only: [:new, :create] skip_before_filter :project, only: [:new, :create]
skip_before_filter :repository, only: [:new, :create]
   
# Authorize # Authorize
before_filter :authorize_read_project!, except: [:index, :new, :create] before_filter :authorize_read_project!, except: [:index, :new, :create]
Loading
@@ -58,7 +59,7 @@ class ProjectsController < ProjectResourceController
Loading
@@ -58,7 +59,7 @@ class ProjectsController < ProjectResourceController
   
respond_to do |format| respond_to do |format|
format.html do format.html do
unless @project.empty_repo? if @project.repository && !@project.repository.empty?
@last_push = current_user.recent_push(@project.id) @last_push = current_user.recent_push(@project.id)
render :show render :show
else else
Loading
Loading
Loading
@@ -26,7 +26,7 @@ class ServicesController < ProjectResourceController
Loading
@@ -26,7 +26,7 @@ class ServicesController < ProjectResourceController
end end
   
def test def test
commits = project.commits(project.default_branch, nil, 3) commits = project.repository.commits(project.default_branch, nil, 3)
data = project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", current_user) data = project.post_receive_data(commits.last.id, commits.first.id, "refs/heads/#{project.default_branch}", current_user)
   
@service = project.gitlab_ci_service @service = project.gitlab_ci_service
Loading
Loading
Loading
@@ -16,10 +16,9 @@ class TeamMembersController < ProjectResourceController
Loading
@@ -16,10 +16,9 @@ class TeamMembersController < ProjectResourceController
end end
   
def create def create
@project.add_users_ids_to_team( users = User.where(id: params[:user_ids])
params[:user_ids],
params[:project_access] @project.team << [users, params[:project_access]]
)
   
if params[:redirect_to] if params[:redirect_to]
redirect_to params[:redirect_to] redirect_to params[:redirect_to]
Loading
@@ -50,7 +49,7 @@ class TeamMembersController < ProjectResourceController
Loading
@@ -50,7 +49,7 @@ class TeamMembersController < ProjectResourceController
   
def apply_import def apply_import
giver = Project.find(params[:source_project_id]) giver = Project.find(params[:source_project_id])
status = UsersProject.import_team(giver, project) status = @project.team.import(giver)
notice = status ? "Succesfully imported" : "Import failed" notice = status ? "Succesfully imported" : "Import failed"
   
redirect_to project_team_members_path(project), notice: notice redirect_to project_team_members_path(project), notice: notice
Loading
Loading
Loading
@@ -53,7 +53,7 @@ module ApplicationHelper
Loading
@@ -53,7 +53,7 @@ module ApplicationHelper
   
def last_commit(project) def last_commit(project)
if project.repo_exists? if project.repo_exists?
time_ago_in_words(project.commit.committed_date) + " ago" time_ago_in_words(project.repository.commit.committed_date) + " ago"
else else
"Never" "Never"
end end
Loading
@@ -102,7 +102,7 @@ module ApplicationHelper
Loading
@@ -102,7 +102,7 @@ module ApplicationHelper
] ]
   
project_nav = [] project_nav = []
if @project && !@project.new_record? if @project && @project.repository && @project.repository.root_ref
project_nav = [ project_nav = [
{ label: "#{@project.name} Issues", url: project_issues_path(@project) }, { label: "#{@project.name} Issues", url: project_issues_path(@project) },
{ label: "#{@project.name} Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) }, { label: "#{@project.name} Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) },
Loading
@@ -142,6 +142,7 @@ module ApplicationHelper
Loading
@@ -142,6 +142,7 @@ module ApplicationHelper
event.last_push_to_non_root? && event.last_push_to_non_root? &&
!event.rm_ref? && !event.rm_ref? &&
event.project && event.project &&
event.project.repository &&
event.project.merge_requests_enabled event.project.merge_requests_enabled
end end
   
Loading
Loading
Loading
@@ -4,7 +4,7 @@ module MergeRequestsHelper
Loading
@@ -4,7 +4,7 @@ module MergeRequestsHelper
event.project, event.project,
merge_request: { merge_request: {
source_branch: event.branch_name, source_branch: event.branch_name,
target_branch: event.project.root_ref, target_branch: event.project.repository.root_ref,
title: event.branch_name.titleize title: event.branch_name.titleize
} }
) )
Loading
Loading
Loading
@@ -204,7 +204,7 @@ class Event < ActiveRecord::Base
Loading
@@ -204,7 +204,7 @@ class Event < ActiveRecord::Base
   
# Max 20 commits from push DESC # Max 20 commits from push DESC
def commits def commits
@commits ||= data[:commits].map { |commit| project.commit(commit[:id]) }.reverse @commits ||= data[:commits].map { |commit| repository.commit(commit[:id]) }.reverse
end end
   
def commits_count def commits_count
Loading
@@ -225,14 +225,18 @@ class Event < ActiveRecord::Base
Loading
@@ -225,14 +225,18 @@ class Event < ActiveRecord::Base
end end
end end
   
def repository
project.repository
end
def parent_commit def parent_commit
project.commit(commit_from) repository.commit(commit_from)
rescue => ex rescue => ex
nil nil
end end
   
def last_commit def last_commit
project.commit(commit_to) repository.commit(commit_to)
rescue => ex rescue => ex
nil nil
end end
Loading
Loading
Loading
@@ -71,7 +71,7 @@ class Note < ActiveRecord::Base
Loading
@@ -71,7 +71,7 @@ class Note < ActiveRecord::Base
# override to return commits, which are not active record # override to return commits, which are not active record
def noteable def noteable
if for_commit? if for_commit?
project.commit(commit_id) project.repository.commit(commit_id)
else else
super super
end end
Loading
Loading
Loading
@@ -166,7 +166,13 @@ class Project < ActiveRecord::Base
Loading
@@ -166,7 +166,13 @@ class Project < ActiveRecord::Base
end end
   
def repository def repository
@repository ||= Repository.new(path_with_namespace, default_branch) if path
@repository ||= Repository.new(path_with_namespace, default_branch)
else
nil
end
rescue Grit::NoSuchPathError
nil
end end
   
def git_error? def git_error?
Loading
@@ -279,39 +285,6 @@ class Project < ActiveRecord::Base
Loading
@@ -279,39 +285,6 @@ class Project < ActiveRecord::Base
users_projects.find_by_user_id(user_id) users_projects.find_by_user_id(user_id)
end end
   
# Update multiple project users
# to same access role by user ids
def update_users_ids_to_role(users_ids, access_role)
UsersProject.bulk_update(self, users_ids, access_role)
end
# Delete multiple users from project by user ids
def delete_users_ids_from_team(users_ids)
UsersProject.bulk_delete(self, users_ids)
end
def repository_readers
repository_members[UsersProject::REPORTER]
end
def repository_writers
repository_members[UsersProject::DEVELOPER]
end
def repository_masters
repository_members[UsersProject::MASTER]
end
def repository_members
keys = Hash.new {|h,k| h[k] = [] }
UsersProject.select("keys.identifier, project_access").
joins(user: :keys).where(project_id: id).
each {|row| keys[row.project_access] << [row.identifier] }
keys[UsersProject::REPORTER] += deploy_keys.pluck(:identifier)
keys
end
def transfer(new_namespace) def transfer(new_namespace)
Project.transaction do Project.transaction do
old_namespace = namespace old_namespace = namespace
Loading
@@ -441,7 +414,7 @@ class Project < ActiveRecord::Base
Loading
@@ -441,7 +414,7 @@ class Project < ActiveRecord::Base
# #
def post_receive_data(oldrev, newrev, ref, user) def post_receive_data(oldrev, newrev, ref, user)
   
push_commits = commits_between(oldrev, newrev) push_commits = repository.commits_between(oldrev, newrev)
   
# Total commits count # Total commits count
push_commits_count = push_commits.size push_commits_count = push_commits.size
Loading
@@ -488,7 +461,7 @@ class Project < ActiveRecord::Base
Loading
@@ -488,7 +461,7 @@ class Project < ActiveRecord::Base
def update_merge_requests(oldrev, newrev, ref, user) def update_merge_requests(oldrev, newrev, ref, user)
return true unless ref =~ /heads/ return true unless ref =~ /heads/
branch_name = ref.gsub("refs/heads/", "") branch_name = ref.gsub("refs/heads/", "")
c_ids = self.commits_between(oldrev, newrev).map(&:id) c_ids = self.repository.commits_between(oldrev, newrev).map(&:id)
   
# Update code for merge requests # Update code for merge requests
mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
Loading
@@ -510,7 +483,7 @@ class Project < ActiveRecord::Base
Loading
@@ -510,7 +483,7 @@ class Project < ActiveRecord::Base
end end
   
def empty_repo? def empty_repo?
!repository || repository.empty_repo? !repository || repository.empty?
end end
   
def satellite def satellite
Loading
Loading
Loading
@@ -26,6 +26,6 @@ class ProtectedBranch < ActiveRecord::Base
Loading
@@ -26,6 +26,6 @@ class ProtectedBranch < ActiveRecord::Base
end end
   
def commit def commit
project.commit(self.name) project.repository.commit(self.name)
end end
end end
Loading
@@ -13,9 +13,11 @@ class Repository
Loading
@@ -13,9 +13,11 @@ class Repository
attr_accessor :root_ref attr_accessor :root_ref
   
def initialize(path_with_namespace, root_ref = 'master') def initialize(path_with_namespace, root_ref = 'master')
@root_ref = root_ref @root_ref = root_ref || "master"
@path_with_namespace = path_with_namespace @path_with_namespace = path_with_namespace
@repo = Grit::Repo.new(path_to_repo)
# Init grit repo object
repo
end end
   
def raw def raw
Loading
@@ -26,6 +28,10 @@ class Repository
Loading
@@ -26,6 +28,10 @@ class Repository
@path_to_repo ||= File.join(Gitlab.config.gitolite.repos_path, "#{path_with_namespace}.git") @path_to_repo ||= File.join(Gitlab.config.gitolite.repos_path, "#{path_with_namespace}.git")
end end
   
def repo
@repo ||= Grit::Repo.new(path_to_repo)
end
def commit(commit_id = nil) def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id, root_ref) Commit.find_or_first(repo, commit_id, root_ref)
end end
Loading
@@ -114,7 +120,7 @@ class Repository
Loading
@@ -114,7 +120,7 @@ class Repository
false false
end end
   
def empty_repo? def empty?
!has_commits? !has_commits?
end end
   
Loading
Loading
Loading
@@ -3,7 +3,22 @@ class Team
Loading
@@ -3,7 +3,22 @@ class Team
   
def initialize(project) def initialize(project)
@project = project @project = project
@roles = UsersProject.roles_hash end
# Shortcut to add users
#
# Use:
# @team << [@user, :master]
# @team << [@users, :master]
#
def << args
users = args.first
if users.respond_to?(:each)
add_users(users, args.second)
else
add_user(users, args.second)
end
end end
   
def add_user(user, access) def add_user(user, access)
Loading
@@ -14,7 +29,7 @@ class Team
Loading
@@ -14,7 +29,7 @@ class Team
add_users_ids(users.map(&:id), access) add_users_ids(users.map(&:id), access)
end end
   
def add_users_ids(users_ids, access) def add_users_ids(user_ids, access)
UsersProject.add_users_into_projects( UsersProject.add_users_into_projects(
[project.id], [project.id],
user_ids, user_ids,
Loading
@@ -46,4 +61,58 @@ class Team
Loading
@@ -46,4 +61,58 @@ class Team
def masters def masters
members.masters.map(&:user) members.masters.map(&:user)
end end
def repository_readers
repository_members[UsersProject::REPORTER]
end
def repository_writers
repository_members[UsersProject::DEVELOPER]
end
def repository_masters
repository_members[UsersProject::MASTER]
end
def repository_members
keys = Hash.new {|h,k| h[k] = [] }
UsersProject.select("keys.identifier, project_access").
joins(user: :keys).where(project_id: project.id).
each {|row| keys[row.project_access] << [row.identifier] }
keys[UsersProject::REPORTER] += project.deploy_keys.pluck(:identifier)
keys
end
def import(source_project)
target_project = project
source_team = source_project.users_projects.all
target_team = target_project.users_projects.all
target_user_ids = target_team.map(&:user_id)
source_team.reject! do |tm|
# Skip if user already present in team
target_user_ids.include?(tm.user_id)
end
source_team.map! do |tm|
new_tm = tm.dup
new_tm.id = nil
new_tm.project_id = target_project.id
new_tm.skip_git = true
new_tm
end
UsersProject.transaction do
source_team.each do |tm|
tm.save
end
target_project.update_repository
end
true
rescue
false
end
end end
Loading
@@ -188,7 +188,7 @@ class User < ActiveRecord::Base
Loading
@@ -188,7 +188,7 @@ class User < ActiveRecord::Base
   
# Team membership in personal projects # Team membership in personal projects
def tm_in_personal_projects def tm_in_personal_projects
personal_projects.users_projects.where(user_id: self.id) UsersProject.where(project_id: personal_projects.map(&:id), user_id: self.id)
end end
   
# Returns a string for use as a Gitolite user identifier # Returns a string for use as a Gitolite user identifier
Loading
Loading
Loading
@@ -48,10 +48,23 @@ class UsersProject < ActiveRecord::Base
Loading
@@ -48,10 +48,23 @@ class UsersProject < ActiveRecord::Base
# access can be an integer representing a access code # access can be an integer representing a access code
# or symbol like :master representing role # or symbol like :master representing role
# #
# Ex.
# add_users_into_projects(
# project_ids,
# user_ids,
# UsersProject::MASTER
# )
#
# add_users_into_projects(
# project_ids,
# user_ids,
# :master
# )
#
def add_users_into_projects(project_ids, user_ids, access) def add_users_into_projects(project_ids, user_ids, access)
project_access = if @roles.has_key?(access) project_access = if roles_hash.has_key?(access)
@roles[access] roles_hash[access]
elsif @roles.values.include?(access) elsif roles_hash.values.include?(access.to_i)
access access
else else
raise "Non valid access" raise "Non valid access"
Loading
@@ -93,36 +106,6 @@ class UsersProject < ActiveRecord::Base
Loading
@@ -93,36 +106,6 @@ class UsersProject < ActiveRecord::Base
truncate_teams [project.id] truncate_teams [project.id]
end end
   
def import_team(source_project, target_project)
source_team = source_project.users_projects.all
target_team = target_project.users_projects.all
target_user_ids = target_team.map(&:user_id)
source_team.reject! do |tm|
# Skip if user already present in team
target_user_ids.include?(tm.user_id)
end
source_team.map! do |tm|
new_tm = tm.dup
new_tm.id = nil
new_tm.project_id = target_project.id
new_tm.skip_git = true
new_tm
end
UsersProject.transaction do
source_team.each do |tm|
tm.save
end
target_project.update_repository
end
true
rescue
false
end
def bulk_delete(project, user_ids) def bulk_delete(project, user_ids)
UsersProject.transaction do UsersProject.transaction do
UsersProject.where(user_id: user_ids, project_id: project.id).each do |users_project| UsersProject.where(user_id: user_ids, project_id: project.id).each do |users_project|
Loading
Loading
Loading
@@ -22,7 +22,7 @@
Loading
@@ -22,7 +22,7 @@
- if project.repo_exists? - if project.repo_exists?
.clearfix .clearfix
= f.label :default_branch, "Default Branch" = f.label :default_branch, "Default Branch"
.input= f.select(:default_branch, project.heads.map(&:name), {}, style: "width:210px;") .input= f.select(:default_branch, repository.heads.map(&:name), {}, style: "width:210px;")
   
%fieldset.adv_settings %fieldset.adv_settings
%legend Features: %legend Features:
Loading
Loading
Loading
@@ -4,15 +4,15 @@
Loading
@@ -4,15 +4,15 @@
%i.icon-edit %i.icon-edit
Edit Edit
   
- if @project.has_commits? - if @repository.has_commits?
- if !@project.has_post_receive_file? - if !@repository.has_post_receive_file?
%br %br
.alert.alert-error .alert.alert-error
%span %span
%strong Project has commits but missing post-receive file. %strong Project has commits but missing post-receive file.
%br %br
If you exported project manually - make a link of post-receive hook file from gitolite to project repository If you exported project manually - make a link of post-receive hook file from gitolite to project repository
- elsif !@project.valid_post_receive_file? - elsif !@repository.valid_post_receive_file?
%br %br
.alert.alert-error .alert.alert-error
%span %span
Loading
@@ -76,7 +76,7 @@
Loading
@@ -76,7 +76,7 @@
%b %b
FS Path: FS Path:
%td %td
%code= @project.path_to_repo %code= @repository.path_to_repo
%tr %tr
%td %td
%b %b
Loading
@@ -100,7 +100,7 @@
Loading
@@ -100,7 +100,7 @@
%b %b
Post Receive File: Post Receive File:
%td %td
= check_box_tag :post_receive_file, 1, @project.has_post_receive_file?, disabled: true = check_box_tag :post_receive_file, 1, @repository.has_post_receive_file?, disabled: true
   
%br %br
%h5 %h5
Loading
Loading
Loading
@@ -13,7 +13,7 @@
Loading
@@ -13,7 +13,7 @@
.mr_branch_box .mr_branch_box
%h5 From (Head Branch) %h5 From (Head Branch)
.body .body
.padded= f.select(:source_branch, @project.heads.map(&:name), { include_blank: "Select branch" }, {class: 'chosen span4'}) .padded= f.select(:source_branch, @repository.heads.map(&:name), { include_blank: "Select branch" }, {class: 'chosen span4'})
.mr_source_commit .mr_source_commit
   
.span2 .span2
Loading
@@ -22,7 +22,7 @@
Loading
@@ -22,7 +22,7 @@
.mr_branch_box .mr_branch_box
%h5 To (Base Branch) %h5 To (Base Branch)
.body .body
.padded= f.select(:target_branch, @project.heads.map(&:name), { include_blank: "Select branch" }, {class: 'chosen span4'}) .padded= f.select(:target_branch, @repository.heads.map(&:name), { include_blank: "Select branch" }, {class: 'chosen span4'})
.mr_target_commit .mr_target_commit
   
%h4.cdark 2. Fill info %h4.cdark 2. Fill info
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment