2016-03-08 02:40:23 +00:00
|
|
|
require 'base64'
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
class Project < ActiveRecord::Base
|
2014-03-11 08:58:36 +00:00
|
|
|
include Autostart
|
|
|
|
include Owner
|
|
|
|
include UrlHelper
|
2014-03-11 07:39:25 +00:00
|
|
|
include EventLoggable
|
2015-02-19 22:03:04 +00:00
|
|
|
include Project::DefaultBranch
|
2015-03-12 22:43:13 +00:00
|
|
|
include Project::Finders
|
2015-12-15 06:27:57 +00:00
|
|
|
include Project::GithubApi
|
2015-12-16 18:36:38 +00:00
|
|
|
include Project::MassCreate
|
2014-02-16 20:51:41 +00:00
|
|
|
|
2011-10-23 22:39:44 +01:00
|
|
|
VISIBILITIES = ['open', 'hidden']
|
2012-01-27 00:16:07 +00:00
|
|
|
MAX_OWN_PROJECTS = 32000
|
2013-07-08 15:44:07 +01:00
|
|
|
NAME_REGEXP = /[\w\-\+\.]+/
|
2014-03-13 16:35:00 +00:00
|
|
|
OWNER_AND_NAME_REGEXP = /#{User::NAME_REGEXP.source}\/#{NAME_REGEXP.source}/
|
2014-06-26 13:07:22 +01:00
|
|
|
self.per_page = 25
|
2011-10-23 22:39:44 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
belongs_to :owner, polymorphic: true, counter_cache: :own_projects_count
|
2014-03-11 07:39:25 +00:00
|
|
|
belongs_to :maintainer, class_name: 'User'
|
2014-01-21 04:51:49 +00:00
|
|
|
|
|
|
|
has_many :project_to_repositories, dependent: :destroy
|
|
|
|
has_many :repositories, through: :project_to_repositories
|
|
|
|
has_many :project_statistics, dependent: :destroy
|
|
|
|
|
|
|
|
has_many :build_lists, dependent: :destroy
|
|
|
|
|
|
|
|
has_many :relations, as: :target, dependent: :destroy
|
|
|
|
has_many :groups, through: :relations, source: :actor, source_type: 'Group'
|
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
has_many :packages, class_name: 'BuildList::Package', dependent: :destroy
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2014-03-11 12:40:44 +00:00
|
|
|
validates :name, uniqueness: { scope: [:owner_id, :owner_type], case_sensitive: false },
|
2014-01-21 04:51:49 +00:00
|
|
|
presence: true,
|
2014-03-13 16:35:00 +00:00
|
|
|
format: { with: /\A#{NAME_REGEXP.source}\z/,
|
2014-11-28 16:19:06 +00:00
|
|
|
message: I18n.t("activerecord.errors.project.uname") },
|
|
|
|
length: { maximum: 100 }
|
2014-11-25 16:42:02 +00:00
|
|
|
validates :maintainer, presence: true, unless: :new_record?
|
2015-12-16 18:36:38 +00:00
|
|
|
validates :url, presence: true, format: { with: /\Ahttps?:\/\/[\S]+\z/ }, if: :mass_import or :mass_create
|
|
|
|
validates :add_to_repository_id, presence: true, if: :mass_import or :mass_create
|
2014-03-11 12:40:44 +00:00
|
|
|
validates :visibility, presence: true, inclusion: { in: VISIBILITIES }
|
2014-01-21 04:51:49 +00:00
|
|
|
validate { errors.add(:base, :can_have_less_or_equal, count: MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS }
|
2013-04-09 11:21:17 +01:00
|
|
|
# throws validation error message from ProjectToRepository model into Project model
|
2013-04-08 15:27:50 +01:00
|
|
|
validate do |project|
|
|
|
|
project.project_to_repositories.each do |p_to_r|
|
|
|
|
next if p_to_r.valid?
|
|
|
|
p_to_r.errors.full_messages.each{ |msg| errors[:base] << msg }
|
|
|
|
end
|
|
|
|
errors.delete :project_to_repositories
|
|
|
|
end
|
2012-04-04 13:13:10 +01:00
|
|
|
|
2013-08-13 14:56:50 +01:00
|
|
|
attr_readonly :owner_id, :owner_type
|
2011-03-10 12:38:42 +00:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
before_validation :truncate_name, on: :create
|
2014-03-11 07:39:25 +00:00
|
|
|
before_save -> { self.owner_uname = owner.uname if owner_uname.blank? || owner_id_changed? || owner_type_changed? }
|
2012-08-28 16:00:04 +01:00
|
|
|
before_create :set_maintainer
|
2012-09-12 16:06:34 +01:00
|
|
|
after_save :attach_to_personal_repository
|
2011-11-23 15:52:33 +00:00
|
|
|
|
2015-12-16 18:36:38 +00:00
|
|
|
attr_accessor :url, :srpms_list, :mass_import, :add_to_repository_id, :mass_create
|
2013-11-13 22:01:12 +00:00
|
|
|
|
2013-11-14 21:26:05 +00:00
|
|
|
def init_mass_import
|
2014-04-15 22:26:12 +01:00
|
|
|
Project.perform_later :low, :run_mass_import, url, srpms_list, visibility, owner, add_to_repository_id
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
2011-11-24 21:46:19 +00:00
|
|
|
|
2015-12-16 18:36:38 +00:00
|
|
|
def init_mass_create
|
|
|
|
Project.perform_later :low, :run_mass_create, url, visibility, owner, add_to_repository_id
|
|
|
|
end
|
|
|
|
|
2013-04-03 22:06:13 +01:00
|
|
|
def name_with_owner
|
2013-04-03 22:16:20 +01:00
|
|
|
"#{owner_uname || owner.uname}/#{name}"
|
2013-04-03 22:06:13 +01:00
|
|
|
end
|
|
|
|
|
2012-05-02 10:18:07 +01:00
|
|
|
def to_param
|
2014-03-13 16:46:21 +00:00
|
|
|
name_with_owner
|
2012-05-02 10:18:07 +01:00
|
|
|
end
|
2012-04-19 20:45:50 +01:00
|
|
|
|
2014-03-19 22:01:42 +00:00
|
|
|
def all_members(*includes)
|
|
|
|
members(includes) | (owner_type == 'User' ? [owner] : owner.members.includes(includes))
|
2012-09-17 17:17:43 +01:00
|
|
|
end
|
|
|
|
|
2014-03-19 22:01:42 +00:00
|
|
|
def members(*includes)
|
2016-03-20 09:24:23 +00:00
|
|
|
groups.map{ |g| g.members.includes(includes) }.flatten
|
2012-10-17 14:46:16 +01:00
|
|
|
end
|
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
def platforms
|
|
|
|
@platforms ||= repositories.map(&:platform).uniq
|
|
|
|
end
|
|
|
|
|
2012-12-13 18:18:22 +00:00
|
|
|
def admins
|
|
|
|
admins = self.collaborators.where("relations.role = 'admin'")
|
|
|
|
grs = self.groups.where("relations.role = 'admin'")
|
|
|
|
if self.owner.is_a? Group
|
|
|
|
grs = grs.where("relations.actor_id != ?", self.owner.id)
|
|
|
|
admins = admins | owner.members.where("relations.role = 'admin'")
|
|
|
|
end
|
|
|
|
admins = admins | grs.map(&:members).flatten # member of the admin group is admin
|
2012-04-19 20:45:50 +01:00
|
|
|
end
|
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
def public?
|
|
|
|
visibility == 'open'
|
|
|
|
end
|
|
|
|
|
|
|
|
def owner?(user)
|
|
|
|
owner == user
|
2012-04-19 20:45:50 +01:00
|
|
|
end
|
|
|
|
|
2015-12-29 19:22:17 +00:00
|
|
|
def git_project_address
|
|
|
|
"git://github.com/" + github_get_organization + "/" + name + ".git"
|
2012-08-09 14:56:45 +01:00
|
|
|
end
|
|
|
|
|
2016-03-08 02:40:23 +00:00
|
|
|
def build_for(mass_build, repository_id, project_version, arch = Arch.find_by(name: 'i586'), priority = 0)
|
2013-09-18 20:01:02 +01:00
|
|
|
build_for_platform = mass_build.build_for_platform
|
|
|
|
save_to_platform = mass_build.save_to_platform
|
|
|
|
user = mass_build.user
|
2012-04-14 14:46:39 +01:00
|
|
|
# Select main and project platform repository(contrib, non-free and etc)
|
2012-04-14 17:10:18 +01:00
|
|
|
# If main does not exist, will connect only project platform repository
|
|
|
|
# If project platform repository is main, only main will be connect
|
2014-02-17 21:02:48 +00:00
|
|
|
main_rep_id = build_for_platform.repositories.main.first.try(:id)
|
2013-06-03 16:44:55 +01:00
|
|
|
include_repos = ([main_rep_id] << (save_to_platform.main? ? repository_id : nil)).compact.uniq
|
|
|
|
|
2012-12-25 16:01:44 +00:00
|
|
|
build_list = build_lists.build do |bl|
|
2014-06-30 21:56:13 +01:00
|
|
|
bl.save_to_platform = save_to_platform
|
|
|
|
bl.build_for_platform = build_for_platform
|
|
|
|
bl.arch = arch
|
|
|
|
bl.project_version = project_version
|
|
|
|
bl.user = user
|
|
|
|
bl.auto_publish_status = mass_build.auto_publish_status
|
2014-07-01 18:32:44 +01:00
|
|
|
bl.auto_create_container = mass_build.auto_create_container
|
2014-06-30 21:56:13 +01:00
|
|
|
bl.include_repos = include_repos
|
|
|
|
bl.extra_repositories = mass_build.extra_repositories
|
|
|
|
bl.extra_build_lists = mass_build.extra_build_lists
|
|
|
|
bl.priority = priority
|
|
|
|
bl.mass_build_id = mass_build.id
|
|
|
|
bl.save_to_repository_id = repository_id
|
|
|
|
bl.include_testing_subrepository = mass_build.include_testing_subrepository?
|
|
|
|
bl.use_cached_chroot = mass_build.use_cached_chroot?
|
|
|
|
bl.use_extra_tests = mass_build.use_extra_tests?
|
2014-10-15 20:54:15 +01:00
|
|
|
bl.external_nodes = mass_build.external_nodes
|
2011-11-30 21:43:01 +00:00
|
|
|
end
|
2012-12-25 16:01:44 +00:00
|
|
|
build_list.save
|
2011-11-30 21:43:01 +00:00
|
|
|
end
|
|
|
|
|
2013-02-14 21:49:18 +00:00
|
|
|
def archive_by_treeish_and_format(treeish, format)
|
|
|
|
@archive ||= create_archive treeish, format
|
|
|
|
end
|
|
|
|
|
2013-09-19 13:07:49 +01:00
|
|
|
# Finds release tag and increase its:
|
|
|
|
# 'Release: %mkrel 4mdk' => 'Release: 5mdk'
|
|
|
|
# 'Release: 4' => 'Release: 5'
|
|
|
|
# Finds release macros and increase it:
|
|
|
|
# '%define release %mkrel 4mdk' => '%define release 5mdk'
|
|
|
|
# '%define release 4' => '%define release 5'
|
|
|
|
def self.replace_release_tag(content)
|
|
|
|
|
|
|
|
build_new_release = Proc.new do |release, combine_release|
|
|
|
|
if combine_release.present?
|
|
|
|
r = combine_release.split('.').last.to_i
|
|
|
|
release << combine_release.gsub(/.[\d]+$/, '') << ".#{r + 1}"
|
|
|
|
else
|
|
|
|
release = release.to_i + 1
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
release
|
2013-09-19 13:07:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
content.gsub(/^Release:(\s+)(%mkrel\s+)?(\d+)([.\d]+)?(mdk)?$/) do |line|
|
|
|
|
tab, mkrel, mdk = $1, $2, $5
|
|
|
|
"Release:#{tab}#{build_new_release.call($3, $4)}#{mdk}"
|
|
|
|
end.gsub(/^%define\s+release:?(\s+)(%mkrel\s+)?(\d+)([.\d]+)?(mdk)?$/) do |line|
|
|
|
|
tab, mkrel, mdk = $1, $2, $5
|
|
|
|
"%define release#{tab}#{build_new_release.call($3, $4)}#{mdk}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-16 20:51:41 +00:00
|
|
|
class << self
|
2014-03-11 08:58:36 +00:00
|
|
|
Autostart::HUMAN_AUTOSTART_STATUSES.each do |autostart_status, human_autostart_status|
|
2014-02-16 20:51:41 +00:00
|
|
|
define_method "autostart_build_lists_#{human_autostart_status}" do
|
2014-02-17 21:02:48 +00:00
|
|
|
autostart_build_lists autostart_status
|
2014-02-16 20:51:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.autostart_build_lists(autostart_status)
|
|
|
|
Project.where(autostart_status: autostart_status).find_each do |p|
|
2014-02-17 21:02:48 +00:00
|
|
|
p.project_to_repositories.autostart_enabled.includes(repository: :platform).each do |p_to_r|
|
|
|
|
repository = p_to_r.repository
|
2014-02-17 21:32:47 +00:00
|
|
|
user = User.find(p_to_r.user_id)
|
|
|
|
if repository.platform.personal?
|
|
|
|
platforms = Platform.availables_main_platforms(user)
|
|
|
|
else
|
|
|
|
platforms = [repository.platform]
|
|
|
|
end
|
|
|
|
platforms.each do |platform|
|
|
|
|
platform.platform_arch_settings.by_default.pluck(:arch_id).each do |arch_id|
|
|
|
|
build_list = p.build_lists.build do |bl|
|
|
|
|
bl.save_to_platform = repository.platform
|
|
|
|
bl.build_for_platform = platform
|
|
|
|
bl.arch_id = arch_id
|
2014-03-12 20:49:36 +00:00
|
|
|
bl.project_version = p.project_version_for(repository.platform, platform)
|
2014-02-17 21:32:47 +00:00
|
|
|
bl.user = user
|
2014-02-19 21:19:49 +00:00
|
|
|
bl.auto_publish_status = p_to_r.auto_publish? ? BuildList::AUTO_PUBLISH_STATUS_DEFAULT : BuildList::AUTO_PUBLISH_STATUS_NONE
|
2014-02-17 21:32:47 +00:00
|
|
|
bl.save_to_repository = repository
|
2014-03-12 20:49:36 +00:00
|
|
|
bl.include_repos = [platform.repositories.main.first.try(:id)].compact
|
|
|
|
if repository.platform.personal?
|
|
|
|
bl.extra_repositories = [repository.id]
|
|
|
|
else
|
|
|
|
bl.include_repos |= [repository.id]
|
|
|
|
end
|
2014-02-17 21:32:47 +00:00
|
|
|
end
|
2014-03-12 20:49:36 +00:00
|
|
|
build_list.save
|
2014-02-16 20:51:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-08 02:40:23 +00:00
|
|
|
def increase_release_tag(project_version, message)
|
2018-10-30 08:03:28 +00:00
|
|
|
begin
|
|
|
|
file = Github_blobs_api.contents github_get_organization + '/' + name, path: '/' + name + '.spec', ref: project_version
|
|
|
|
rescue => e
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
return false
|
|
|
|
end
|
2016-03-28 10:56:04 +01:00
|
|
|
if file
|
2018-10-30 08:03:28 +00:00
|
|
|
begin
|
2018-10-30 08:09:36 +00:00
|
|
|
decoded_content = Base64.decode64(file.content)
|
|
|
|
new_content = Project.replace_release_tag decoded_content
|
|
|
|
return if new_content == decoded_content
|
2018-10-30 08:03:28 +00:00
|
|
|
Github_blobs_api.update_contents github_get_organization + '/' + name, name + '.spec',\
|
|
|
|
message, file.sha, new_content, branch: project_version
|
|
|
|
rescue => e
|
|
|
|
Raven.capture_message(e)
|
|
|
|
return false
|
|
|
|
end
|
2016-03-28 10:56:04 +01:00
|
|
|
end
|
2018-10-30 08:03:28 +00:00
|
|
|
return true
|
2013-09-18 20:01:02 +01:00
|
|
|
end
|
|
|
|
|
2014-06-16 20:26:14 +01:00
|
|
|
protected
|
2013-09-19 13:07:49 +01:00
|
|
|
|
2013-02-14 21:49:18 +00:00
|
|
|
def create_archive(treeish, format)
|
|
|
|
file_name = "#{name}-#{treeish}"
|
|
|
|
fullname = "#{file_name}.#{tag_file_format(format)}"
|
2014-01-17 17:27:44 +00:00
|
|
|
file = Tempfile.new fullname, File.join(Rails.root, 'tmp')
|
2013-02-14 21:49:18 +00:00
|
|
|
system("cd #{path}; git archive --format=#{format == 'zip' ? 'zip' : 'tar'} --prefix=#{file_name}/ #{treeish} #{format == 'zip' ? '' : ' | gzip -9'} > #{file.path}")
|
|
|
|
file.close
|
|
|
|
{
|
2014-01-21 04:51:49 +00:00
|
|
|
path: file.path,
|
|
|
|
fullname: fullname
|
2013-02-14 21:49:18 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-14 12:46:25 +00:00
|
|
|
def tag_file_format(format)
|
|
|
|
format == 'zip' ? 'zip' : 'tar.gz'
|
|
|
|
end
|
|
|
|
|
2012-10-15 09:28:18 +01:00
|
|
|
def truncate_name
|
|
|
|
self.name = name.strip if name
|
|
|
|
end
|
|
|
|
|
2012-01-27 15:35:18 +00:00
|
|
|
def attach_to_personal_repository
|
2013-04-09 12:08:49 +01:00
|
|
|
owner_repos = self.owner.personal_platform.repositories
|
2012-09-12 16:06:34 +01:00
|
|
|
if is_package
|
2014-01-21 04:51:49 +00:00
|
|
|
repositories << self.owner.personal_repository unless repositories.exists?(id: owner_repos.pluck(:id))
|
2012-09-12 16:06:34 +01:00
|
|
|
else
|
2013-04-09 12:08:49 +01:00
|
|
|
repositories.delete owner_repos
|
2012-09-12 16:06:34 +01:00
|
|
|
end
|
2012-01-27 15:35:18 +00:00
|
|
|
end
|
2012-08-24 16:19:26 +01:00
|
|
|
|
2012-08-28 16:00:04 +01:00
|
|
|
def set_maintainer
|
2012-10-17 13:18:52 +01:00
|
|
|
if maintainer_id.blank?
|
|
|
|
self.maintainer_id = (owner_type == 'User') ? self.owner_id : self.owner.owner_id
|
|
|
|
end
|
2012-08-28 16:00:04 +01:00
|
|
|
end
|
|
|
|
|
2011-03-09 17:38:21 +00:00
|
|
|
end
|