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

REpostiry, Team models

parent e6c0673e
No related branches found
No related tags found
No related merge requests found
Showing
with 232 additions and 296 deletions
Loading
Loading
@@ -24,3 +24,4 @@ db/data.yml
.DS_Store
.chef
vendor/bundle/*
rails_best_practices_output.html
Loading
Loading
@@ -123,6 +123,8 @@ group :development do
gem 'better_errors'
gem 'binding_of_caller'
 
gem 'rails_best_practices'
# Docs generator
gem "sdoc"
end
Loading
Loading
Loading
Loading
@@ -132,6 +132,8 @@ GEM
chosen-rails (0.9.8)
railties (~> 3.0)
thor (~> 0.14)
code_analyzer (0.3.1)
sexp_processor
coderay (1.0.8)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
Loading
Loading
@@ -302,6 +304,7 @@ GEM
pg (0.14.1)
polyglot (0.3.3)
posix-spawn (0.3.6)
progressbar (0.12.0)
pry (0.9.10)
coderay (~> 1.0.5)
method_source (~> 0.8)
Loading
Loading
@@ -335,6 +338,14 @@ GEM
rails-dev-tweaks (0.6.1)
actionpack (~> 3.1)
railties (~> 3.1)
rails_best_practices (1.13.2)
activesupport
awesome_print
code_analyzer
colored
erubis
i18n
progressbar
railties (3.2.9)
actionpack (= 3.2.9)
activesupport (= 3.2.9)
Loading
Loading
@@ -393,6 +404,7 @@ GEM
multi_json (~> 1.0)
rubyzip
settingslogic (2.0.8)
sexp_processor (4.1.3)
shoulda-matchers (1.3.0)
activesupport (>= 3.0.0)
simplecov (0.7.1)
Loading
Loading
@@ -512,6 +524,7 @@ DEPENDENCIES
rack-mini-profiler
rails (= 3.2.9)
rails-dev-tweaks
rails_best_practices
raphael-rails (= 1.5.2)
rb-fsevent
rb-inotify
Loading
Loading
Loading
Loading
@@ -9,7 +9,7 @@ class CommitLoadContext < BaseContext
status: :ok
}
 
commit = project.commit(params[:id])
commit = project.repository.commit(params[:id])
 
if commit
commit = CommitDecorator.decorate(commit)
Loading
Loading
Loading
Loading
@@ -18,7 +18,7 @@ module Notes
project.snippets.find(target_id).notes.fresh
when "wall"
# this is the only case, where the order is DESC
project.common_notes.order("created_at DESC, id DESC").limit(50)
project.notes.common.inc_author_project.order("created_at DESC, id DESC").limit(50)
end
 
@notes = if after_id
Loading
Loading
Loading
Loading
@@ -76,6 +76,12 @@ class ApplicationController < ActionController::Base
end
end
 
def repository
@repository ||= project.repository
rescue Grit::NoSuchPathError
nil
end
def add_abilities
abilities << Ability
end
Loading
Loading
Loading
Loading
@@ -9,10 +9,10 @@ class CommitsController < ProjectResourceController
before_filter :require_non_empty_project
 
def show
@repo = @project.repo
@repo = @project.repository
@limit, @offset = (params[:limit] || 40), (params[:offset] || 0)
 
@commits = @project.commits(@ref, @path, @limit, @offset)
@commits = @repo.commits(@ref, @path, @limit, @offset)
@commits = CommitDecorator.decorate(@commits)
 
respond_to do |format|
Loading
Loading
class ProjectResourceController < ApplicationController
before_filter :project
before_filter :repository
end
Loading
Loading
@@ -31,7 +31,7 @@ class RefsController < ProjectResourceController
contents = @tree.contents
@logs = contents.map do |content|
file = params[:path] ? File.join(params[:path], content.name) : content.name
last_commit = @project.commits(@commit.id, file, 1).last
last_commit = @repo.commits(@commit.id, file, 1).last
last_commit = CommitDecorator.decorate(last_commit)
{
file_name: content.name,
Loading
Loading
@@ -45,10 +45,10 @@ class RefsController < ProjectResourceController
def define_tree_vars
params[:path] = nil if params[:path].blank?
 
@repo = project.repo
@commit = project.commit(@ref)
@repo = project.repository
@commit = @repo.commit(@ref)
@commit = CommitDecorator.decorate(@commit)
@tree = Tree.new(@commit.tree, project, @ref, params[:path])
@tree = Tree.new(@commit.tree, @ref, params[:path])
@tree = TreeDecorator.new(@tree)
@hex_path = Digest::SHA1.hexdigest(params[:path] || "")
 
Loading
Loading
Loading
Loading
@@ -5,19 +5,19 @@ class RepositoriesController < ProjectResourceController
before_filter :require_non_empty_project
 
def show
@activities = @project.commits_with_refs(20)
@activities = @repository.commits_with_refs(20)
end
 
def branches
@branches = @project.branches
@branches = @repository.branches
end
 
def tags
@tags = @project.tags
@tags = @repository.tags
end
 
def stats
@stats = Gitlab::GitStats.new(@project.repo, @project.root_ref)
@stats = Gitlab::GitStats.new(@repository.raw, @repository.root_ref)
@graph = @stats.graph
end
 
Loading
Loading
@@ -27,7 +27,7 @@ class RepositoriesController < ProjectResourceController
end
 
 
file_path = @project.archive_repo(params[:ref])
file_path = @repository.archive_repo(params[:ref])
 
if file_path
# Send file to user
Loading
Loading
Loading
Loading
@@ -6,16 +6,14 @@ class TreeDecorator < ApplicationDecorator
part_path = ""
parts = path.split("\/")
 
#parts = parts[0...-1] if is_blob?
yield(h.link_to("..", "#")) if parts.count > max_links
yield('..', nil) if parts.count > max_links
 
parts.each do |part|
part_path = File.join(part_path, part) unless part_path.empty?
part_path = part if part_path.empty?
 
next unless parts.last(2).include?(part) if parts.count > max_links
yield(h.link_to(h.truncate(part, length: 40), h.project_tree_path(project, h.tree_join(ref, part_path))))
yield(part, h.tree_join(ref, part_path))
end
end
end
Loading
Loading
@@ -26,7 +24,7 @@ class TreeDecorator < ApplicationDecorator
 
def up_dir_path
file = File.join(path, "..")
h.project_tree_path(project, h.tree_join(ref, file))
h.tree_join(ref, file)
end
 
def readme
Loading
Loading
Loading
Loading
@@ -62,9 +62,11 @@ module ApplicationHelper
end
 
def grouped_options_refs(destination = :tree)
repository = @project.repository
options = [
["Branch", @project.branch_names ],
[ "Tag", @project.tag_names ]
["Branch", repository.branch_names ],
[ "Tag", repository.tag_names ]
]
 
# If reference is commit id -
Loading
Loading
@@ -103,12 +105,12 @@ module ApplicationHelper
if @project && !@project.new_record?
project_nav = [
{ label: "#{@project.name} Issues", url: project_issues_path(@project) },
{ label: "#{@project.name} Commits", url: project_commits_path(@project, @ref || @project.root_ref) },
{ label: "#{@project.name} Commits", url: project_commits_path(@project, @ref || @project.repository.root_ref) },
{ label: "#{@project.name} Merge Requests", url: project_merge_requests_path(@project) },
{ label: "#{@project.name} Milestones", url: project_milestones_path(@project) },
{ label: "#{@project.name} Snippets", url: project_snippets_path(@project) },
{ label: "#{@project.name} Team", url: project_team_index_path(@project) },
{ label: "#{@project.name} Tree", url: project_tree_path(@project, @ref || @project.root_ref) },
{ label: "#{@project.name} Tree", url: project_tree_path(@project, @ref || @project.repository.root_ref) },
{ label: "#{@project.name} Wall", url: wall_project_path(@project) },
{ label: "#{@project.name} Wiki", url: project_wikis_path(@project) },
]
Loading
Loading
Loading
Loading
@@ -20,20 +20,6 @@ module EventsHelper
[event.action_name, target].join(" ")
end
 
def event_image event
event_image_path = if event.push?
"event_push.png"
elsif event.merged?
"event_mr_merged.png"
end
return nil unless event_image_path
content_tag :div, class: 'event_icon' do
image_tag event_image_path
end
end
def event_filter_link key, tooltip
key = key.to_s
 
Loading
Loading
Loading
Loading
@@ -15,17 +15,19 @@ class Ability
def project_abilities(user, project)
rules = []
 
team = project.team
# Rules based on role in project
if project.master_access_for?(user)
if team.masters.include?(user)
rules << project_master_rules
 
elsif project.dev_access_for?(user)
elsif team.developers.include?(user)
rules << project_dev_rules
 
elsif project.report_access_for?(user)
elsif team.reporters.include?(user)
rules << project_report_rules
 
elsif project.guest_access_for?(user)
elsif team.guests.include?(user)
rules << project_guest_rules
end
 
Loading
Loading
Loading
Loading
@@ -83,8 +83,8 @@ class Commit
 
return result unless from && to
 
first = project.commit(to.try(:strip))
last = project.commit(from.try(:strip))
first = project.repository.commit(to.try(:strip))
last = project.repository.commit(from.try(:strip))
 
if first && last
result[:same] = (first.id == last.id)
Loading
Loading
Loading
Loading
@@ -110,26 +110,6 @@ class Event < ActiveRecord::Base
target_type == "MergeRequest"
end
 
def new_issue?
target_type == "Issue" &&
action == Created
end
def new_merge_request?
target_type == "MergeRequest" &&
action == Created
end
def changed_merge_request?
target_type == "MergeRequest" &&
[Closed, Reopened].include?(action)
end
def changed_issue?
target_type == "Issue" &&
[Closed, Reopened].include?(action)
end
def joined?
action == Joined
end
Loading
Loading
Loading
Loading
@@ -29,10 +29,6 @@ class GitlabCiService < Service
hook.save
end
 
def commit_badge_path sha
project_url + "/status?sha=#{sha}"
end
def commit_status_path sha
project_url + "/builds/#{sha}/status.json?token=#{token}"
end
Loading
Loading
Loading
Loading
@@ -4,7 +4,6 @@
#
# id :integer not null, primary key
# note :text
# noteable_id :string(255)
# noteable_type :string(255)
# author_id :integer
# created_at :datetime not null
Loading
Loading
@@ -12,6 +11,8 @@
# project_id :integer
# attachment :string(255)
# line_code :string(255)
# commit_id :string(255)
# noteable_id :integer
#
 
require 'carrierwave/orm/activerecord'
Loading
Loading
@@ -42,7 +43,7 @@ class Note < ActiveRecord::Base
 
# Scopes
scope :for_commits, ->{ where(noteable_type: "Commit") }
scope :common, ->{ where(noteable_id: nil, commit_id: nil) }
scope :common, ->{ where(noteable_type: ["", nil]) }
scope :today, ->{ where("created_at >= :date", date: Date.today) }
scope :last_week, ->{ where("created_at >= :date", date: (Date.today - 7.days)) }
scope :since, ->(day) { where("created_at >= :date", date: (day)) }
Loading
Loading
Loading
Loading
@@ -9,7 +9,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# private_flag :boolean default(TRUE), not null
# owner_id :integer
# creator_id :integer
# default_branch :string(255)
# issues_enabled :boolean default(TRUE), not null
# wall_enabled :boolean default(TRUE), not null
Loading
Loading
@@ -75,7 +75,6 @@ class Project < ActiveRecord::Base
validate :check_limit, :repo_name
 
# Scopes
scope :public_only, where(private_flag: false)
scope :without_user, ->(user) { where("id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) }
scope :not_in_group, ->(group) { where("id NOT IN (:ids)", ids: group.project_ids ) }
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
Loading
Loading
@@ -162,6 +161,14 @@ class Project < ActiveRecord::Base
end
end
 
def team
@team ||= Team.new(self)
end
def repository
@repository ||= Repository.new(path_with_namespace, default_branch)
end
def git_error?
error_code == :gitolite
end
Loading
Loading
@@ -198,10 +205,6 @@ class Project < ActiveRecord::Base
[Gitlab.config.gitlab.url, path_with_namespace].join("/")
end
 
def common_notes
notes.where(noteable_type: ["", nil]).inc_author_project
end
def build_commit_note(commit)
notes.new(commit_id: commit.id, noteable_type: "Commit")
end
Loading
Loading
@@ -214,14 +217,6 @@ class Project < ActiveRecord::Base
notes.where(commit_id: commit.id, noteable_type: "Commit").where("line_code IS NOT NULL")
end
 
def public?
!private_flag
end
def private?
private_flag
end
def last_activity
last_event
end
Loading
Loading
@@ -284,33 +279,6 @@ class Project < ActiveRecord::Base
users_projects.find_by_user_id(user_id)
end
 
# Add user to project
# with passed access role
def add_user_to_team(user, access_role)
add_user_id_to_team(user.id, access_role)
end
# Add multiple users to project
# with same access role
def add_users_to_team(users, access_role)
add_users_ids_to_team(users.map(&:id), access_role)
end
# Add user to project
# with passed access role by user id
def add_user_id_to_team(user_id, access_role)
users_projects.create(
user_id: user_id,
project_access: access_role
)
end
# Add multiple users to project
# with same access role by user ids
def add_users_ids_to_team(users_ids, access_role)
UsersProject.bulk_import(self, users_ids, access_role)
end
# Update multiple project users
# to same access role by user ids
def update_users_ids_to_role(users_ids, access_role)
Loading
Loading
@@ -322,30 +290,6 @@ class Project < ActiveRecord::Base
UsersProject.bulk_delete(self, users_ids)
end
 
# Remove all users from project team
def truncate_team
UsersProject.truncate_team(self)
end
# Compatible with all access rights
# Should be rewrited for new access rights
def add_access(user, *access)
access = if access.include?(:admin)
{ project_access: UsersProject::MASTER }
elsif access.include?(:write)
{ project_access: UsersProject::DEVELOPER }
else
{ project_access: UsersProject::REPORTER }
end
opts = { user: user }
opts.merge!(access)
users_projects.create(opts)
end
def reset_access(user)
users_projects.where(project_id: self.id, user_id: user.id).destroy if self.id
end
def repository_readers
repository_members[UsersProject::REPORTER]
end
Loading
Loading
@@ -368,26 +312,6 @@ class Project < ActiveRecord::Base
keys
end
 
def allow_read_for?(user)
!users_projects.where(user_id: user.id).empty?
end
def guest_access_for?(user)
!users_projects.where(user_id: user.id).empty?
end
def report_access_for?(user)
!users_projects.where(user_id: user.id, project_access: [UsersProject::REPORTER, UsersProject::DEVELOPER, UsersProject::MASTER]).empty?
end
def dev_access_for?(user)
!users_projects.where(user_id: user.id, project_access: [UsersProject::DEVELOPER, UsersProject::MASTER]).empty?
end
def master_access_for?(user)
!users_projects.where(user_id: user.id, project_access: [UsersProject::MASTER]).empty?
end
def transfer(new_namespace)
Project.transaction do
old_namespace = namespace
Loading
Loading
@@ -586,97 +510,21 @@ class Project < ActiveRecord::Base
end
 
def empty_repo?
!repo_exists? || !has_commits?
end
def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id, root_ref)
end
def fresh_commits(n = 10)
Commit.fresh_commits(repo, n)
end
def commits_with_refs(n = 20)
Commit.commits_with_refs(repo, n)
end
def commits_since(date)
Commit.commits_since(repo, date)
end
def commits(ref, path = nil, limit = nil, offset = nil)
Commit.commits(repo, ref, path, limit, offset)
end
def last_commit_for(ref, path = nil)
commits(ref, path, 1).first
end
def commits_between(from, to)
Commit.commits_between(repo, from, to)
!repository || repository.empty_repo?
end
 
def satellite
@satellite ||= Gitlab::Satellite::Satellite.new(self)
end
 
def has_post_receive_file?
!!hook_file
end
def valid_post_receive_file?
valid_hook_file == hook_file
end
def valid_hook_file
@valid_hook_file ||= File.read(Rails.root.join('lib', 'hooks', 'post-receive'))
end
def hook_file
@hook_file ||= begin
hook_path = File.join(path_to_repo, 'hooks', 'post-receive')
File.read(hook_path) if File.exists?(hook_path)
end
end
# Returns an Array of branch names
def branch_names
repo.branches.collect(&:name).sort
end
# Returns an Array of Branches
def branches
repo.branches.sort_by(&:name)
end
# Returns an Array of tag names
def tag_names
repo.tags.collect(&:name).sort.reverse
end
# Returns an Array of Tags
def tags
repo.tags.sort_by(&:name).reverse
end
# Returns an Array of branch and tag names
def ref_names
[branch_names + tag_names].flatten
end
def repo
@repo ||= Grit::Repo.new(path_to_repo)
repository.raw
end
 
def url_to_repo
gitolite.url_to_repo(path_with_namespace)
end
 
def path_to_repo
File.join(Gitlab.config.gitolite.repos_path, "#{path_with_namespace}.git")
end
def namespace_dir
namespace.try(:path) || ''
end
Loading
Loading
@@ -690,21 +538,11 @@ class Project < ActiveRecord::Base
end
 
def repo_exists?
@repo_exists ||= (repo && !repo.branches.empty?)
@repo_exists ||= (repository && repository.branches.present?)
rescue
@repo_exists = false
end
 
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
def open_branches
if protected_branches.empty?
self.repo.heads
Loading
Loading
@@ -714,61 +552,8 @@ class Project < ActiveRecord::Base
end.sort_by(&:name)
end
 
# Discovers the default branch based on the repository's available branches
#
# - If no branches are present, returns nil
# - If one branch is present, returns its name
# - If two or more branches are present, returns the one that has a name
# matching root_ref (default_branch or 'master' if default_branch is nil)
def discover_default_branch
if branch_names.length == 0
nil
elsif branch_names.length == 1
branch_names.first
else
branch_names.select { |v| v == root_ref }.first
end
end
def has_commits?
!!commit
rescue Grit::NoSuchPathError
false
end
def root_ref
default_branch || "master"
end
def root_ref?(branch)
root_ref == branch
end
# Archive Project to .tar.gz
#
# Already packed repo archives stored at
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
#
def archive_repo(ref)
ref = ref || self.root_ref
commit = self.commit(ref)
return nil unless commit
# Build file path
file_name = self.path + "-" + commit.id.to_s + ".tar.gz"
storage_path = Rails.root.join("tmp", "repositories", self.path_with_namespace)
file_path = File.join(storage_path, file_name)
# Put files into a directory before archiving
prefix = self.path + "/"
# Create file if not exists
unless File.exists?(file_path)
FileUtils.mkdir_p storage_path
file = self.repo.archive_to_file(ref, prefix, file_path)
end
file_path
repository.root_ref == branch
end
 
def ssh_url_to_repo
Loading
Loading
class Repository
# Repository directory name with namespace direcotry
# Examples:
# gitlab/gitolite
# diaspora
#
attr_accessor :path_with_namespace
# Grit repo object
attr_accessor :repo
# Default branch in the repository
attr_accessor :root_ref
def initialize(path_with_namespace, root_ref = 'master')
@root_ref = root_ref
@path_with_namespace = path_with_namespace
@repo = Grit::Repo.new(path_to_repo)
end
def raw
repo
end
def path_to_repo
@path_to_repo ||= File.join(Gitlab.config.gitolite.repos_path, "#{path_with_namespace}.git")
end
def commit(commit_id = nil)
Commit.find_or_first(repo, commit_id, root_ref)
end
def fresh_commits(n = 10)
Commit.fresh_commits(repo, n)
end
def commits_with_refs(n = 20)
Commit.commits_with_refs(repo, n)
end
def commits_since(date)
Commit.commits_since(repo, date)
end
def commits(ref, path = nil, limit = nil, offset = nil)
Commit.commits(repo, ref, path, limit, offset)
end
def last_commit_for(ref, path = nil)
commits(ref, path, 1).first
end
def commits_between(from, to)
Commit.commits_between(repo, from, to)
end
def has_post_receive_file?
!!hook_file
end
def valid_post_receive_file?
valid_hook_file == hook_file
end
def valid_hook_file
@valid_hook_file ||= File.read(Rails.root.join('lib', 'hooks', 'post-receive'))
end
def hook_file
@hook_file ||= begin
hook_path = File.join(path_to_repo, 'hooks', 'post-receive')
File.read(hook_path) if File.exists?(hook_path)
end
end
# Returns an Array of branch names
def branch_names
repo.branches.collect(&:name).sort
end
# Returns an Array of Branches
def branches
repo.branches.sort_by(&:name)
end
# Returns an Array of tag names
def tag_names
repo.tags.collect(&:name).sort.reverse
end
# Returns an Array of Tags
def tags
repo.tags.sort_by(&:name).reverse
end
# Returns an Array of branch and tag names
def ref_names
[branch_names + tag_names].flatten
end
def heads
@heads ||= repo.heads
end
def tree(fcommit, path = nil)
fcommit = commit if fcommit == :head
tree = fcommit.tree
path ? (tree / path) : tree
end
def has_commits?
!!commit
rescue Grit::NoSuchPathError
false
end
def empty_repo?
!has_commits?
end
# Discovers the default branch based on the repository's available branches
#
# - If no branches are present, returns nil
# - If one branch is present, returns its name
# - If two or more branches are present, returns the one that has a name
# matching root_ref (default_branch or 'master' if default_branch is nil)
def discover_default_branch
if branch_names.length == 0
nil
elsif branch_names.length == 1
branch_names.first
else
branch_names.select { |v| v == root_ref }.first
end
end
# Archive Project to .tar.gz
#
# Already packed repo archives stored at
# app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
#
def archive_repo(ref)
ref = ref || self.root_ref
commit = self.commit(ref)
return nil unless commit
# Build file path
file_name = self.path + "-" + commit.id.to_s + ".tar.gz"
storage_path = Rails.root.join("tmp", "repositories", self.path_with_namespace)
file_path = File.join(storage_path, file_name)
# Put files into a directory before archiving
prefix = self.path + "/"
# Create file if not exists
unless File.exists?(file_path)
FileUtils.mkdir_p storage_path
file = self.repo.archive_to_file(ref, prefix, file_path)
end
file_path
end
end
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