forgeplus/app/models/pull_request.rb

70 lines
2.2 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
# pull_request_id :integer
# gpid :integer
# 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(65535)
# head :string(255)
# base :string(255)
# issue_id :integer
#
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
2020-06-30 09:44:28 +08:00
# belongs_to :fork_project, foreign_key: :fork_project_id
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
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
def bind_gitea_pull_request!(gitea_pull_number)
update_column(:gpid, gitea_pull_number)
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, :gpid, :project_id, :fork_project_id).each do |pr|
puts pr.id
next if pr.gpid.blank?
project = pr.project
next if project.blank?
user = project.owner
next if pr.gpid === 6 || pr.gpid === 7
files_result = Gitea::PullRequest::FilesService.call(user.login, project.identifier, pr.gpid)
pr.update_column(:files_count, files_result['NumFiles']) unless files_result.blank?
commits_result = Gitea::PullRequest::CommitsService.call(user.login, project.identifier, pr.gpid)
pr.update_column(:commits_count, commits_result.size) unless commits_result.blank?
end
end
2020-03-09 00:40:16 +08:00
end