forgeplus/app/models/pull_request.rb

120 lines
4.1 KiB
Ruby
Raw Normal View History

2020-11-23 15:35:03 +08:00
# == Schema Information
#
# Table name: pull_requests
#
# id :integer not null, primary key
# gitea_id :integer
# gitea_number :integer
2020-11-23 15:35:03 +08:00
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# status :integer default("0")
# project_id :integer
# title :string(255)
# milestone :integer
# body :text(4294967295)
2020-11-23 15:35:03 +08:00
# head :string(255)
# base :string(255)
# issue_id :integer
2021-06-09 16:53:09 +08:00
# fork_project_id :integer
# is_original :boolean default("0")
# comments_count :integer default("0")
# commits_count :integer default("0")
# files_count :integer default("0")
2020-11-23 15:35:03 +08:00
#
2020-03-09 00:40:16 +08:00
class PullRequest < ApplicationRecord
2021-01-12 15:45:18 +08:00
#status 0 默认未合并, 1表示合并, 2表示请求拒绝(或已关闭)
OPEN = 0
MERGED = 1
CLOSED = 2
2020-03-09 00:40:16 +08:00
belongs_to :issue
belongs_to :user
belongs_to :project, counter_cache: true, touch: true
belongs_to :fork_project, class_name: 'Project', foreign_key: :fork_project_id, optional: true
2020-03-09 00:40:16 +08:00
has_many :pull_request_assigns, foreign_key: :pull_request_id
has_many :pull_request_tags, foreign_key: :pull_request_id
has_many :project_trends, as: :trend, dependent: :destroy
has_many :attachments, as: :container, dependent: :destroy
has_one :gitea_pull, foreign_key: :id, primary_key: :gitea_number, class_name: 'Gitea::Pull'
has_many :journals, :as => :journalized, :dependent => :destroy
has_many :journal_details, through: :journals
has_many :reviews, dependent: :destroy
has_many :pull_requests_reviewers, dependent: :destroy
has_many :reviewers, through: :pull_requests_reviewers
2020-06-30 09:44:28 +08:00
2021-06-16 09:49:08 +08:00
scope :merged_and_closed, ->{where.not(status: 0)}
scope :opening, -> {where(status: 0)}
2021-10-26 15:22:42 +08:00
after_create :incre_project_common, :incre_user_statistic, :incre_platform_statistic
after_destroy :decre_project_common, :decre_user_statistic, :decre_platform_statistic
2021-06-08 11:51:46 +08:00
2021-10-26 15:22:42 +08:00
def incre_project_common
CacheAsyncSetJob.perform_later("project_common_service", {pullrequests: 1}, self.project_id)
end
def decre_project_common
CacheAsyncSetJob.perform_later("project_common_service", {pullrequests: -1}, self.project_id)
end
def incre_user_statistic
CacheAsyncSetJob.perform_later("user_statistic_service", {pullrequest_count: 1}, self.user_id)
end
def decre_user_statistic
CacheAsyncSetJob.perform_later("user_statistic_service", {pullrequest_count: -1}, self.user_id)
end
def incre_platform_statistic
CacheAsyncSetJob.perform_later("platform_statistic_service", {pullrequest_count: 1})
end
def decre_platform_statistic
CacheAsyncSetJob.perform_later("platform_statistic_service", {pullrequest_count: -1})
2021-06-08 11:51:46 +08:00
end
2020-06-30 09:44:28 +08:00
def fork_project
Project.find_by(id: self.fork_project_id)
end
2020-11-23 15:35:03 +08:00
2021-09-28 15:59:36 +08:00
def bind_gitea_pull_request!(gitea_pull_number, gitea_pull_id)
update_columns(
gitea_number: gitea_pull_number,
gitea_id: gitea_pull_id)
end
2021-01-12 15:45:18 +08:00
def merge!
update_column(:status, PullRequest::MERGED)
end
def project_trend_status!
self&.project_trends&.update_all(action_type: ProjectTrend::CLOSE)
end
2020-11-23 15:35:03 +08:00
# TODO: sync educoder platform repo's for update some statistics count
def self.update_some_count
PullRequest.includes(:user, :project).select(:id, :user_id, :gitea_number, :project_id, :fork_project_id).each do |pr|
2020-11-23 15:35:03 +08:00
puts pr.id
next if pr.gitea_number.blank?
2020-11-23 15:35:03 +08:00
project = pr.project
next if project.blank?
user = project.owner
next if pr.gitea_number === 6 || pr.gitea_number === 7
files_result = Gitea::PullRequest::FilesService.call(user.login, project.identifier, pr.gitea_number)
2020-11-23 15:35:03 +08:00
pr.update_column(:files_count, files_result['NumFiles']) unless files_result.blank?
commits_result = Gitea::PullRequest::CommitsService.call(user.login, project.identifier, pr.gitea_number)
2020-11-23 15:35:03 +08:00
pr.update_column(:commits_count, commits_result.size) unless commits_result.blank?
end
end
2021-06-24 16:08:32 +08:00
def conflict_files
file_names = self&.gitea_pull&.conflicted_files
return [] if file_names.blank?
JSON.parse file_names
end
2020-03-09 00:40:16 +08:00
end