@notes = case target_type
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"
project.issues.find(target_id).notes.inc_author.fresh.limit(20)
when "merge_request"
class TestHookContext < BaseContext
def execute
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)
hook.execute(data)
end
end
def show
+ @repository = @project.repository
@users = User.active
@users = @users.not_in_project(@project) if @project.users.present?
@users = @users.all
end
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.'
end
def destroy
# Delete team first in order to prevent multiple gitolite calls
- @project.truncate_team
+ @project.team.truncate
@project.destroy
end
def branch_from
- @commit = project.commit(params[:ref])
+ @commit = @repository.commit(params[:ref])
@commit = CommitDecorator.decorate(@commit)
end
def branch_to
- @commit = project.commit(params[:ref])
+ @commit = @repository.commit(params[:ref])
@commit = CommitDecorator.decorate(@commit)
end
class ProjectsController < ProjectResourceController
skip_before_filter :project, only: [:new, :create]
+ skip_before_filter :repository, only: [:new, :create]
# Authorize
before_filter :authorize_read_project!, except: [:index, :new, :create]
respond_to do |format|
format.html do
- unless @project.empty_repo?
+ if @project.repository && !@project.repository.empty?
@last_push = current_user.recent_push(@project.id)
render :show
else
end
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)
@service = project.gitlab_ci_service
end
def create
- @project.add_users_ids_to_team(
- params[:user_ids],
- params[:project_access]
- )
+ users = User.where(id: params[:user_ids])
+
+ @project.team << [users, params[:project_access]]
if params[:redirect_to]
redirect_to params[:redirect_to]
def apply_import
giver = Project.find(params[:source_project_id])
- status = UsersProject.import_team(giver, project)
+ status = @project.team.import(giver)
notice = status ? "Succesfully imported" : "Import failed"
redirect_to project_team_members_path(project), notice: notice
def last_commit(project)
if project.repo_exists?
- time_ago_in_words(project.commit.committed_date) + " ago"
+ time_ago_in_words(project.repository.commit.committed_date) + " ago"
else
"Never"
end
]
project_nav = []
- if @project && !@project.new_record?
+ if @project && @project.repository && @project.repository.root_ref
project_nav = [
{ label: "#{@project.name} Issues", url: project_issues_path(@project) },
{ label: "#{@project.name} Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) },
event.last_push_to_non_root? &&
!event.rm_ref? &&
event.project &&
+ event.project.repository &&
event.project.merge_requests_enabled
end
event.project,
merge_request: {
source_branch: event.branch_name,
- target_branch: event.project.root_ref,
+ target_branch: event.project.repository.root_ref,
title: event.branch_name.titleize
}
)
# Max 20 commits from push DESC
def commits
- @commits ||= data[:commits].map { |commit| project.commit(commit[:id]) }.reverse
+ @commits ||= data[:commits].map { |commit| repository.commit(commit[:id]) }.reverse
end
def commits_count
end
end
+ def repository
+ project.repository
+ end
+
def parent_commit
- project.commit(commit_from)
+ repository.commit(commit_from)
rescue => ex
nil
end
def last_commit
- project.commit(commit_to)
+ repository.commit(commit_to)
rescue => ex
nil
end
# override to return commits, which are not active record
def noteable
if for_commit?
- project.commit(commit_id)
+ project.repository.commit(commit_id)
else
super
end
end
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
def git_error?
users_projects.find_by_user_id(user_id)
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)
Project.transaction do
old_namespace = namespace
#
def post_receive_data(oldrev, newrev, ref, user)
- push_commits = commits_between(oldrev, newrev)
+ push_commits = repository.commits_between(oldrev, newrev)
# Total commits count
push_commits_count = push_commits.size
def update_merge_requests(oldrev, newrev, ref, user)
return true unless ref =~ /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
mrs = self.merge_requests.opened.find_all_by_branch(branch_name).all
end
def empty_repo?
- !repository || repository.empty_repo?
+ !repository || repository.empty?
end
def satellite
end
def commit
- project.commit(self.name)
+ project.repository.commit(self.name)
end
end
attr_accessor :root_ref
def initialize(path_with_namespace, root_ref = 'master')
- @root_ref = root_ref
+ @root_ref = root_ref || "master"
@path_with_namespace = path_with_namespace
- @repo = Grit::Repo.new(path_to_repo)
+
+ # Init grit repo object
+ repo
end
def raw
@path_to_repo ||= File.join(Gitlab.config.gitolite.repos_path, "#{path_with_namespace}.git")
end
+ def repo
+ @repo ||= Grit::Repo.new(path_to_repo)
+ end
+
def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id, root_ref)
end
false
end
- def empty_repo?
+ def empty?
!has_commits?
end
def initialize(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
def add_user(user, access)
add_users_ids(users.map(&:id), access)
end
- def add_users_ids(users_ids, access)
+ def add_users_ids(user_ids, access)
UsersProject.add_users_into_projects(
[project.id],
user_ids,
def masters
members.masters.map(&:user)
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
# Team membership 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
# Returns a string for use as a Gitolite user identifier
# access can be an integer representing a access code
# 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)
- project_access = if @roles.has_key?(access)
- @roles[access]
- elsif @roles.values.include?(access)
+ project_access = if roles_hash.has_key?(access)
+ roles_hash[access]
+ elsif roles_hash.values.include?(access.to_i)
access
else
raise "Non valid access"
truncate_teams [project.id]
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)
UsersProject.transaction do
UsersProject.where(user_id: user_ids, project_id: project.id).each do |users_project|
- if project.repo_exists?
.clearfix
= 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
%legend Features:
%i.icon-edit
Edit
-- if @project.has_commits?
- - if !@project.has_post_receive_file?
+- if @repository.has_commits?
+ - if !@repository.has_post_receive_file?
%br
.alert.alert-error
%span
%strong Project has commits but missing post-receive file.
%br
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
.alert.alert-error
%span
%b
FS Path:
%td
- %code= @project.path_to_repo
+ %code= @repository.path_to_repo
%tr
%td
%b
%b
Post Receive File:
%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
%h5
.mr_branch_box
%h5 From (Head Branch)
.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
.span2
.mr_branch_box
%h5 To (Base Branch)
.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
%h4.cdark 2. Fill info
= f.label :path do
Repository
.controls
- = text_field_tag :ppath, @project.path_to_repo, class: "xxlarge", readonly: true
+ = text_field_tag :ppath, @repository.path_to_repo, class: "xxlarge", readonly: true
- - unless @project.heads.empty?
+ - unless @repository.heads.empty?
.clearfix
= 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.features
%legend Features:
# Ignore push from non-gitlab users
user = if identifier.eql? Gitlab.config.gitolite.admin_key
- email = project.commit(newrev).author.email rescue nil
+ email = project.repository.commit(newrev).author.email rescue nil
User.find_by_email(email) if email
elsif /^[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}$/.match(identifier)
User.find_by_email(identifier)
@project = create(:project, group: @group)
@event = create(:closed_issue_event, project: @project)
- @project.add_access current_user, :admin
+ @project.team << [current_user, :master]
end
And 'Create gitlab user "John"' do
And 'I own project "Shop"' do
@project = create :project, name: 'Shop'
- @project.add_access(@user, :admin)
+ @project.team << [@user, :master]
end
And 'I have group with projects' do
@project = create(:project, group: @group)
@event = create(:closed_issue_event, project: @project)
- @project.add_access current_user, :admin
+ @project.team << [current_user, :master]
end
And 'project "Shop" has push event' do
And 'I have assigned issues' do
project = create :project
- project.add_access(@user, :read, :write)
+ project.team << [@user, :master]
2.times { create :issue, author: @user, assignee: @user, project: project }
end
project1 = create :project
project2 = create :project
- project1.add_access(@user, :read, :write)
- project2.add_access(@user, :read, :write)
+ project1.team << [@user, :master]
+ project2.team << [@user, :master]
merge_request1 = create :merge_request, author: @user, project: project1
merge_request2 = create :merge_request, author: @user, project: project2
class DashboardSearch < Spinach::FeatureSteps
include SharedAuthentication
include SharedPaths
+ include SharedProject
Given 'I search for "Sho"' do
fill_in "dashboard_search", with: "Sho"
page.should have_link "Shop"
end
- And 'I own project "Shop"' do
- @project = create(:project, :name => "Shop")
- @project.add_access(@user, :admin)
- end
-
Given 'I search for "Contibuting"' do
fill_in "dashboard_search", with: "Contibuting"
click_button "Search"
@project = create(:project, group: @group)
@event = create(:closed_issue_event, project: @project)
- @project.add_access current_user, :admin
+ @project.team << [current_user, :master]
end
And 'I should see projects activity feed' do
And '"Sam" is "Shop" developer' do
user = User.find_by_name("Sam")
project = Project.find_by_name("Shop")
- project.add_access(user, :write)
+ project.team << [user, :developer]
end
Given 'I own project "Website"' do
@project = create(:project, :name => "Website")
- @project.add_access(@user, :admin)
+ @project.team << [@user, :master]
end
And '"Mike" is "Website" reporter' do
user = User.find_by_name("Mike")
project = Project.find_by_name("Website")
- project.add_access(user, :read)
+ project.team << [user, :reporter]
end
And 'I click link "Import team from another project"' do
end
Given "I visit my project's files page" do
- visit project_tree_path(@project, @project.root_ref)
+ visit project_tree_path(@project, root_ref)
end
Given "I visit my project's commits page" do
- visit project_commits_path(@project, @project.root_ref, {limit: 5})
+ visit project_commits_path(@project, root_ref, {limit: 5})
end
Given "I visit my project's commits page for a specific path" do
- visit project_commits_path(@project, @project.root_ref + "/app/models/project.rb", {limit: 5})
+ visit project_commits_path(@project, root_ref + "/app/models/project.rb", {limit: 5})
end
Given 'I visit my project\'s commits stats page' do
end
Given 'I visit project commits page' do
- visit project_commits_path(@project, @project.root_ref, {limit: 5})
+ visit project_commits_path(@project, root_ref, {limit: 5})
end
Given 'I visit project commits page for stable branch' do
end
Given 'I visit project source page' do
- visit project_tree_path(@project, @project.root_ref)
+ visit project_tree_path(@project, root_ref)
end
Given 'I visit blob file from repo' do
Given 'I visit project wiki page' do
visit project_wiki_path(@project, :index)
end
+
+ def root_ref
+ @project.repository.root_ref
+ end
end
# Create a project without caring about what it's called
And "I own a project" do
@project = create(:project)
- @project.add_access(@user, :admin)
+ @project.team << [@user, :master]
end
# Create a specific project called "Shop"
And 'I own project "Shop"' do
- @project = create(:project, :name => "Shop")
- @project.add_access(@user, :admin)
+ @project = create(:project, name: "Shop")
+ @project.team << [@user, :master]
end
def current_project
end
def destroy_project(project)
- FileUtils.rm_rf(project.path_to_repo)
+ FileUtils.rm_rf(project.repository.path_to_repo)
conf.rm_repo(project.path_with_namespace)
end
::Gitolite::Config::Repo.new(repo_name)
end
- name_readers = project.repository_readers
- name_writers = project.repository_writers
- name_masters = project.repository_masters
+ name_readers = project.team.repository_readers
+ name_writers = project.team.repository_writers
+ name_masters = project.team.repository_masters
pr_br = project.protected_branches.map(&:name).join("$ ")
end
def reference_commit(identifier)
- if @project.valid_repo? && commit = @project.commit(identifier)
+ if @project.valid_repo? && commit = @project.repository.commit(identifier)
link_to(identifier, project_commit_path(@project, commit), html_options.merge(title: CommitDecorator.new(commit).link_title, class: "gfm gfm-commit #{html_options[:class]}"))
end
end
merge_repo.git.push({raise: true, timeout: true}, :origin, merge_request.target_branch)
# remove source branch
- if merge_request.should_remove_source_branch && !project.root_ref?(merge_request.source_branch)
+ if merge_request.should_remove_source_branch && !project.repository.root_ref?(merge_request.source_branch)
# will raise CommandFailed when push fails
merge_repo.git.push({raise: true, timeout: true}, :origin, ":#{merge_request.source_branch}")
end
end
describe "Respond to" do
- it { should respond_to(:public?) }
- it { should respond_to(:private?) }
it { should respond_to(:url_to_repo) }
it { should respond_to(:path_to_repo) }
it { should respond_to(:valid_repo?) }
it { should respond_to(:repo_exists?) }
# Repository Role
- it { should respond_to(:tree) }
- it { should respond_to(:root_ref) }
- it { should respond_to(:repo) }
- it { should respond_to(:tags) }
- it { should respond_to(:commit) }
- it { should respond_to(:commits) }
- it { should respond_to(:commits_between) }
- it { should respond_to(:commits_with_refs) }
- it { should respond_to(:commits_since) }
- it { should respond_to(:commits_between) }
it { should respond_to(:satellite) }
it { should respond_to(:update_repository) }
it { should respond_to(:destroy_repository) }
it { should respond_to(:archive_repo) }
# Authority Role
- it { should respond_to(:add_access) }
it { should respond_to(:reset_access) }
- it { should respond_to(:repository_writers) }
- it { should respond_to(:repository_masters) }
- it { should respond_to(:repository_readers) }
it { should respond_to(:allow_read_for?) }
it { should respond_to(:guest_access_for?) }
it { should respond_to(:report_access_for?) }
--- /dev/null
+describe Repository do
+ describe "Respond to" do
+ it { should respond_to(:repo) }
+ it { should respond_to(:tree) }
+ it { should respond_to(:root_ref) }
+ it { should respond_to(:tags) }
+ it { should respond_to(:commit) }
+ it { should respond_to(:commits) }
+ it { should respond_to(:commits_between) }
+ it { should respond_to(:commits_with_refs) }
+ it { should respond_to(:commits_since) }
+ it { should respond_to(:commits_between) }
+ end
+end
user = create(:user)
project = create(:project)
with_resque do
- project.add_access(user, :admin)
+ project.team << [user, :master]
end
WebMock.should have_requested(:post, @system_hook.url).with(body: /user_add_to_team/).once
end
it "project_destroy hook" do
user = create(:user)
project = create(:project)
- project.add_access(user, :admin)
+ project.team << [user, :master]
with_resque do
project.users_projects.clear
end
--- /dev/null
+describe Team do
+ describe "Respond to" do
+ it { should respond_to(:developers) }
+ it { should respond_to(:masters) }
+ it { should respond_to(:reporters) }
+ it { should respond_to(:guests) }
+ it { should respond_to(:repository_writers) }
+ it { should respond_to(:repository_masters) }
+ it { should respond_to(:repository_readers) }
+ end
+end
+
@user_1 = create :user
@user_2 = create :user
- @project_1.add_access @user_1, :write
- @project_2.add_access @user_2, :read
+ @project_1.team << [ @user_1, :developer ]
+ @project_2.team << [ @user_2, :reporter ]
- @status = UsersProject.import_team(@project_1, @project_2)
+ @status = @project_2.team.import(@project_1)
end
it { @status.should be_true }
@user_1 = create :user
@user_2 = create :user
- @project_1.add_access @user_1, :write
- @project_2.add_access @user_2, :read
+ @project_1.team << [ @user_1, :developer]
+ @project_2.team << [ @user_2, :reporter]
UsersProject.truncate_teams([@project_1.id, @project_2.id])
end
login_as :user
user2 = create(:user)
- project.add_access(@user, :read, :write)
- project.add_access(user2, :read, :write)
+ project.team << [[@user, user2], :developer]
end
describe "Edit issue" do
describe "GET /projects/show" do
before do
@project = create(:project, namespace: @user.namespace)
- @project.add_access(@user, :read)
+ @project.team << [@user, :reporter]
visit project_path(@project)
end
describe "GET /projects/:id/edit" do
before do
@project = create(:project)
- @project.add_access(@user, :admin, :read)
+ @project.team << [@user, :master]
visit edit_project_path(@project)
end
describe "PUT /projects/:id" do
before do
@project = create(:project, namespace: @user.namespace)
- @project.add_access(@user, :admin, :read)
+ @project.team << [@user, :master]
visit edit_project_path(@project)
describe "DELETE /projects/:id" do
before do
@project = create(:project, namespace: @user.namespace)
- @project.add_access(@user, :read, :admin)
+ @project.team << [@user, :master]
visit edit_project_path(@project)
end
# Stubs out all Git repository access done by models so that specs can run
# against fake repositories without Grit complaining that they don't exist.
class Project
- def path_to_repo
- if new_record? || path == 'newproject'
- # There are a couple Project specs and features that expect the Project's
- # path to be in the returned path, so let's patronize them.
- Rails.root.join('tmp', 'repositories', path)
- else
- # For everything else, just give it the path to one of our real seeded
- # repos.
- Rails.root.join('tmp', 'repositories', 'gitlabhq')
- end
- end
-
def satellite
FakeSatellite.new
end
end
end
end
+
+class Repository
+ def repo
+ @repo ||= Grit::Repo.new(Rails.root.join('tmp', 'repositories', 'gitlabhq'))
+ end
+end