summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-03 21:09:18 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-03 21:09:18 +0200
commit39ba934c0a65f571214998e056e925b61f389360 (patch)
treea1da1af6df76e1d49feffd48a6247ced3abd5684 /app/models
parente6c0673ef1108a93928c4d88ba273e12616b836b (diff)
downloadgitlab-ce-39ba934c0a65f571214998e056e925b61f389360.tar.gz
REpostiry, Team models
Diffstat (limited to 'app/models')
-rw-r--r--app/models/ability.rb10
-rw-r--r--app/models/commit.rb4
-rw-r--r--app/models/event.rb20
-rw-r--r--app/models/gitlab_ci_service.rb4
-rw-r--r--app/models/note.rb5
-rw-r--r--app/models/project.rb241
-rw-r--r--app/models/repository.rb163
-rw-r--r--app/models/team.rb49
-rw-r--r--app/models/tree.rb7
-rw-r--r--app/models/users_project.rb25
10 files changed, 264 insertions, 264 deletions
diff --git a/app/models/ability.rb b/app/models/ability.rb
index 75a7163c219..256af1e800b 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -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
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 07c5fbd7183..32d942a9e47 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -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)
diff --git a/app/models/event.rb b/app/models/event.rb
index 95075ffae71..a737bfb0626 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -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
diff --git a/app/models/gitlab_ci_service.rb b/app/models/gitlab_ci_service.rb
index 0b0b65e2863..4eb39c7ef4d 100644
--- a/app/models/gitlab_ci_service.rb
+++ b/app/models/gitlab_ci_service.rb
@@ -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
diff --git a/app/models/note.rb b/app/models/note.rb
index 28b3879239f..100f72b6eac 100644
--- a/app/models/note.rb
+++ b/app/models/note.rb
@@ -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
@@ -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'
@@ -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)) }
diff --git a/app/models/project.rb b/app/models/project.rb
index f60c24426aa..f2ad390b5b6 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -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
@@ -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) }
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/app/models/repository.rb b/app/models/repository.rb
new file mode 100644
index 00000000000..a0351ce28c7
--- /dev/null
+++ b/app/models/repository.rb
@@ -0,0 +1,163 @@
+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
diff --git a/app/models/team.rb b/app/models/team.rb
new file mode 100644
index 00000000000..894361d1273
--- /dev/null
+++ b/app/models/team.rb
@@ -0,0 +1,49 @@
+class Team
+ attr_accessor :project
+
+ def initialize(project)
+ @project = project
+ @roles = UsersProject.roles_hash
+ end
+
+ def add_user(user, access)
+ add_users_ids([user.id], access)
+ end
+
+ def add_users(users, access)
+ add_users_ids(users.map(&:id), access)
+ end
+
+ def add_users_ids(users_ids, access)
+ UsersProject.add_users_into_projects(
+ [project.id],
+ user_ids,
+ access
+ )
+ end
+
+ # Remove all users from project team
+ def truncate
+ UsersProject.truncate_team(project)
+ end
+
+ def members
+ project.users_projects
+ end
+
+ def guests
+ members.guests.map(&:user)
+ end
+
+ def reporters
+ members.reporters.map(&:user)
+ end
+
+ def developers
+ members.developers.map(&:user)
+ end
+
+ def masters
+ members.masters.map(&:user)
+ end
+end
diff --git a/app/models/tree.rb b/app/models/tree.rb
index c3dfd4c718c..96395a42394 100644
--- a/app/models/tree.rb
+++ b/app/models/tree.rb
@@ -1,12 +1,13 @@
class Tree
include Linguist::BlobHelper
- attr_accessor :path, :tree, :project, :ref
+
+ attr_accessor :path, :tree, :ref
delegate :contents, :basename, :name, :data, :mime_type,
:mode, :size, :text?, :colorize, to: :tree
- def initialize(raw_tree, project, ref = nil, path = nil)
- @project, @ref, @path = project, ref, path
+ def initialize(raw_tree, ref = nil, path = nil)
+ @ref, @path = ref, path
@tree = if path.present?
raw_tree / path
else
diff --git a/app/models/users_project.rb b/app/models/users_project.rb
index f8e0078400f..a8e14675cd7 100644
--- a/app/models/users_project.rb
+++ b/app/models/users_project.rb
@@ -42,7 +42,21 @@ class UsersProject < ActiveRecord::Base
scope :in_project, ->(project) { where(project_id: project.id) }
class << self
- def add_users_into_projects(project_ids, user_ids, project_access)
+
+ # Add users to project teams with passed access option
+ #
+ # access can be an integer representing a access code
+ # or symbol like :master representing role
+ #
+ def add_users_into_projects(project_ids, user_ids, access)
+ project_access = if @roles.has_key?(access)
+ @roles[access]
+ elsif @roles.values.include?(access)
+ access
+ else
+ raise "Non valid access"
+ end
+
UsersProject.transaction do
project_ids.each do |project_id|
user_ids.each do |user_id|
@@ -141,6 +155,15 @@ class UsersProject < ActiveRecord::Base
add_users_into_projects(project_ids, [user.id], project_access)
end
+ def roles_hash
+ {
+ guest: GUEST,
+ reporter: REPORTER,
+ developer: DEVELOPER,
+ master: MASTER
+ }
+ end
+
def access_roles
{
"Guest" => GUEST,