| @@ -87,8 +87,10 @@ class PullRequestsController < ApplicationController | |||||
| if @issue.update_attributes(@issue_params) | if @issue.update_attributes(@issue_params) | ||||
| if @pull_request.update_attributes(@local_params.compact) | if @pull_request.update_attributes(@local_params.compact) | ||||
| gitea_request = Gitea::PullRequest::UpdateService.new(@project.owner, @repository.try(:identifier), @requests_params, @pull_request.try(:gpid)).call | |||||
| if gitea_request | |||||
| gitea_pull = Gitea::PullRequest::UpdateService.call(@owner.login, @repository.identifier, | |||||
| @pull_request.gpid, @requests_params, current_user.gitea_token) | |||||
| if gitea_pull[:status] === :success | |||||
| if params[:issue_tag_ids].present? | if params[:issue_tag_ids].present? | ||||
| params[:issue_tag_ids].each do |tag| | params[:issue_tag_ids].each do |tag| | ||||
| IssueTagsRelate.create(issue_id: @issue.id, issue_tag_id: tag) | IssueTagsRelate.create(issue_id: @issue.id, issue_tag_id: tag) | ||||
| @@ -117,9 +119,8 @@ class PullRequestsController < ApplicationController | |||||
| def refuse_merge | def refuse_merge | ||||
| ActiveRecord::Base.transaction do | ActiveRecord::Base.transaction do | ||||
| begin | begin | ||||
| @pull_request.update(status: 2) | |||||
| @pull_request.issue.update(status_id: 5) | |||||
| normal_status(1, "已拒绝") | |||||
| colsed = PullRequests::CloseService.call(@owner, @repository, @pull_request, current_user) | |||||
| colsed === true ? normal_status(1, "已拒绝") : normal_status(-1, '合并失败') | |||||
| rescue => e | rescue => e | ||||
| normal_status(-1, e.message) | normal_status(-1, e.message) | ||||
| raise ActiveRecord::Rollback | raise ActiveRecord::Rollback | ||||
| @@ -145,15 +146,10 @@ class PullRequestsController < ApplicationController | |||||
| else | else | ||||
| ActiveRecord::Base.transaction do | ActiveRecord::Base.transaction do | ||||
| begin | begin | ||||
| requests_params = { | |||||
| Do: params[:do], | |||||
| MergeMessageField: params[:body], | |||||
| MergeTitleField: params[:title] | |||||
| } | |||||
| merge_pr = Gitea::PullRequest::MergeService.call(current_user.gitea_token, @project.owner.login, | |||||
| @repository.try(:identifier), @pull_request.try(:gpid), requests_params) | |||||
| if @pull_request.update_attribute(:status, 1) && merge_pr[:status].to_i == 200 | |||||
| @pull_request&.project_trends&.update_all(action_type: "close") | |||||
| result = PullRequests::MergeService.call(@owner, @repository, @pull_request, current_user, params) | |||||
| if result && @pull_request.merge! | |||||
| @pull_request.project_trend_status! | |||||
| @issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) | @issue&.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) | ||||
| normal_status(1, "合并成功") | normal_status(1, "合并成功") | ||||
| else | else | ||||
| @@ -17,6 +17,13 @@ | |||||
| # | # | ||||
| class IssueStatus < ApplicationRecord | class IssueStatus < ApplicationRecord | ||||
| ADD = 1 | |||||
| SOLVING = 2 | |||||
| SOLVED = 3 | |||||
| FEEDBACK = 4 | |||||
| CLOSED = 5 | |||||
| REJECTED = 6 | |||||
| has_many :issues | has_many :issues | ||||
| belongs_to :project, optional: true | belongs_to :project, optional: true | ||||
| end | end | ||||
| @@ -18,6 +18,9 @@ | |||||
| # | # | ||||
| class ProjectTrend < ApplicationRecord | class ProjectTrend < ApplicationRecord | ||||
| CLOSE = 'close' | |||||
| CREATE = 'create' | |||||
| belongs_to :project | belongs_to :project | ||||
| belongs_to :trend, polymorphic: true, optional: true | belongs_to :trend, polymorphic: true, optional: true | ||||
| belongs_to :user | belongs_to :user | ||||
| @@ -24,7 +24,11 @@ | |||||
| # | # | ||||
| class PullRequest < ApplicationRecord | class PullRequest < ApplicationRecord | ||||
| #status 0 默认未合并, 1表示合并, 2表示请求拒绝 | |||||
| #status 0 默认未合并, 1表示合并, 2表示请求拒绝(或已关闭) | |||||
| OPEN = 0 | |||||
| MERGED = 1 | |||||
| CLOSED = 2 | |||||
| belongs_to :issue | belongs_to :issue | ||||
| belongs_to :user | belongs_to :user | ||||
| belongs_to :project, :counter_cache => true | belongs_to :project, :counter_cache => true | ||||
| @@ -42,6 +46,14 @@ class PullRequest < ApplicationRecord | |||||
| update_column(:gpid, gitea_pull_number) | update_column(:gpid, gitea_pull_number) | ||||
| end | end | ||||
| def merge! | |||||
| update_column(:status, PullRequest::MERGED) | |||||
| end | |||||
| def project_trend_status! | |||||
| self&.project_trends&.update_all(action_type: ProjectTrend::CLOSE) | |||||
| end | |||||
| # TODO: sync educoder platform repo's for update some statistics count | # TODO: sync educoder platform repo's for update some statistics count | ||||
| def self.update_some_count | def self.update_some_count | ||||
| PullRequest.includes(:user, :project).select(:id, :user_id, :gpid, :project_id, :fork_project_id).each do |pr| | PullRequest.includes(:user, :project).select(:id, :user_id, :gpid, :project_id, :fork_project_id).each do |pr| | ||||
| @@ -0,0 +1,22 @@ | |||||
| class Gitea::PullRequest::CloseService < Gitea::PullRequest::UpdateService | |||||
| attr_reader :owner, :repo, :base, :number, :token | |||||
| # params: | |||||
| # owner: owner of the repo | |||||
| # repo: name of the repo | |||||
| # base: branch name of base | |||||
| # number: number of pull request | |||||
| # token: token of gitea user | |||||
| # eq: | |||||
| # Gitea::PullRequest::CloseService.call(owner.login, repo.identifier, pull.gpid, pull.base, current_user.gitea_token) | |||||
| def initialize(owner, repo, number, base,token=nil) | |||||
| colse_pull_params = Hash.new.merge(base: base, state: 'closed').compact | |||||
| super(owner, repo, number, colse_pull_params, token) | |||||
| end | |||||
| def call | |||||
| super | |||||
| end | |||||
| end | |||||
| @@ -18,7 +18,9 @@ class Gitea::PullRequest::MergeService < Gitea::ClientService | |||||
| end | end | ||||
| def call | def call | ||||
| post(url, request_params) | |||||
| response = post(url, request_params) | |||||
| render_200_no_body(response) | |||||
| end | end | ||||
| private | private | ||||
| @@ -27,7 +29,6 @@ class Gitea::PullRequest::MergeService < Gitea::ClientService | |||||
| end | end | ||||
| def request_params | def request_params | ||||
| Hash.new.merge(token: token, data: params) | |||||
| Hash.new.merge(token: token, data: params.compact) | |||||
| end | end | ||||
| end | end | ||||
| @@ -0,0 +1,22 @@ | |||||
| class Gitea::PullRequest::OpenService < Gitea::PullRequest::UpdateService | |||||
| attr_reader :owner, :repo, :base, :number, :token | |||||
| # params: | |||||
| # owner: owner of the repo | |||||
| # repo: name of the repo | |||||
| # base: branch name of base | |||||
| # number: number of pull request | |||||
| # token: token of gitea user | |||||
| # eq: | |||||
| # Gitea::PullRequest::OpenService.new(owner.login, repo.identifier, pr.gpid, pr.base, current_user.gitea_token) | |||||
| def initialize(owner, repo, number, base, token=nil) | |||||
| open_pull_params = Hash.new.merge(base: base, state: 'open').compact | |||||
| super(owner, repo, number, open_pull_params, token) | |||||
| end | |||||
| def call | |||||
| super | |||||
| end | |||||
| end | |||||
| @@ -1,26 +1,54 @@ | |||||
| class Gitea::PullRequest::UpdateService < Gitea::ClientService | class Gitea::PullRequest::UpdateService < Gitea::ClientService | ||||
| attr_reader :user, :repo, :params,:pull_request_id | |||||
| attr_reader :owner, :repo, :params, :number, :token | |||||
| def initialize(user, repo, params,pull_request_id) | |||||
| @user = user | |||||
| # params: | |||||
| # { | |||||
| # "assignee": "string", | |||||
| # "assignees": [ | |||||
| # "string" | |||||
| # ], | |||||
| # "base": "string", | |||||
| # "body": "string", | |||||
| # "due_date": "2021-01-11T10:11:52.074Z", | |||||
| # "labels": [ | |||||
| # 0 | |||||
| # ], | |||||
| # "milestone": 0, | |||||
| # "state": "string", | |||||
| # "title": "string", | |||||
| # "unset_due_date": true | |||||
| # } | |||||
| def initialize(owner, repo, number, params, token=nil) | |||||
| @owner = owner | |||||
| @repo = repo | @repo = repo | ||||
| @params = params | @params = params | ||||
| @pull_request_id = pull_request_id | |||||
| @number = number | |||||
| @token = token | |||||
| end | end | ||||
| def call | def call | ||||
| put(url, request_params) | |||||
| response = patch(url, request_params) | |||||
| status, message, body = render_response(response) | |||||
| json_format(status, message, body) | |||||
| end | end | ||||
| private | private | ||||
| def request_params | def request_params | ||||
| Hash.new.merge(token: @user.gitea_token, data: @params) | |||||
| Hash.new.merge(token: token, data: @params) | |||||
| end | end | ||||
| def url | def url | ||||
| "/repos/#{@user.try(:login)}/#{@repo}/pulls/#{@pull_request_id}".freeze | |||||
| "/repos/#{owner}/#{repo}/pulls/#{number}".freeze | |||||
| end | |||||
| def json_format(status, message, body) | |||||
| case status | |||||
| when 201 then success(body) | |||||
| else | |||||
| error(message, status) | |||||
| end | |||||
| end | end | ||||
| end | end | ||||
| @@ -0,0 +1,33 @@ | |||||
| class PullRequests::CloseService < ApplicationService | |||||
| attr_reader :owner, :repo, :pull, :current_user | |||||
| # eq: | |||||
| # PullRequests::CloseService.call(owner, repo, pull, current_user) | |||||
| def initialize(owner, repo, pull, current_user) | |||||
| @owner = owner | |||||
| @repo = repo | |||||
| @pull = pull | |||||
| @current_user = current_user | |||||
| end | |||||
| def call | |||||
| ActiveRecord::Base.transaction do | |||||
| return false if close_gitea_pull[:status] != :success | |||||
| update_pull_status! | |||||
| return true | |||||
| end | |||||
| end | |||||
| private | |||||
| def close_gitea_pull | |||||
| Gitea::PullRequest::CloseService.call(@owner.login, @repo.identifier, | |||||
| @pull.gpid, @pull.base, current_user.gitea_token) | |||||
| end | |||||
| def update_pull_status! | |||||
| @pull.update(status: PullRequest::CLOSED) | |||||
| @pull.issue.update(status_id: IssueStatus::CLOSED) | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,36 @@ | |||||
| class PullRequests::MergeService < ApplicationService | |||||
| attr_reader :owner, :repo, :pull, :current_user, :params | |||||
| # eq: | |||||
| # PullRequests::MergeService.call(owner, repo, pull, current_user, params) | |||||
| def initialize(owner, repo, pull, current_user, params) | |||||
| @owner = owner | |||||
| @repo = repo | |||||
| @pull = pull | |||||
| @current_user = current_user | |||||
| @params = params | |||||
| end | |||||
| def call | |||||
| ActiveRecord::Base.transaction do | |||||
| gitea_pull_merge! | |||||
| end | |||||
| end | |||||
| private | |||||
| def gitea_pull_merge! | |||||
| result = Gitea::PullRequest::MergeService.call(@current_user.gitea_token, @owner.login, | |||||
| @repo.identifier, @pull.gpid, gitea_merge_pull_params) | |||||
| result[:status] === 200 ? true : false | |||||
| end | |||||
| def gitea_merge_pull_params | |||||
| { | |||||
| Do: params[:do], | |||||
| MergeMessageField: params[:body], | |||||
| MergeTitleField: params[:title] | |||||
| } | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,33 @@ | |||||
| class PullRequests::OpenService < ApplicationService | |||||
| attr_reader :owner, :repo, :pull, :current_user | |||||
| # eq: | |||||
| # PullRequests::OpenService.call(owner, repo, pull, current_user) | |||||
| def initialize(owner, repo, pull, current_user) | |||||
| @owner = owner | |||||
| @repo = repo | |||||
| @pull = pull | |||||
| @current_user = current_user | |||||
| end | |||||
| def call | |||||
| ActiveRecord::Base.transaction do | |||||
| return false if open_gitea_pull[:status] != :success | |||||
| update_pull_status! | |||||
| return true | |||||
| end | |||||
| end | |||||
| private | |||||
| def open_gitea_pull | |||||
| Gitea::PullRequest::OpenService.call(@owner.login, @repo.identifier, | |||||
| @pull.gpid, @pull.base, @current_user.gitea_token) | |||||
| end | |||||
| def update_pull_status! | |||||
| @pull.update(status: PullRequest::OPEN) | |||||
| @pull.issue.update(status_id: IssueStatus::SOLVING) | |||||
| end | |||||
| end | |||||