From eee699300c78f57f333cc3fbc5cf1f69cdf1f28f Mon Sep 17 00:00:00 2001 From: "sylor_huang@126.com" Date: Fri, 12 Jun 2020 18:22:42 +0800 Subject: [PATCH] change --- app/controllers/pull_requests_controller.rb | 289 +++++++----------- app/services/issues/list_query_service.rb | 2 + .../pull_requests/_merge_item.json.jbuilder | 14 + app/views/pull_requests/edit.json.jbuilder | 21 +- app/views/pull_requests/index.json.jbuilder | 28 +- app/views/pull_requests/new.json.jbuilder | 12 +- app/views/pull_requests/show.json.jbuilder | 24 +- config/routes.rb | 1 + dump.rdb | Bin 662 -> 808 bytes 9 files changed, 155 insertions(+), 236 deletions(-) create mode 100644 app/views/pull_requests/_merge_item.json.jbuilder diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index 1bc67a402..51988b35e 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -3,6 +3,7 @@ class PullRequestsController < ApplicationController before_action :find_project_with_id before_action :set_repository before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge] + before_action :get_relatived, only: [:new, :edit] include TagChosenHelper include ApplicationHelper @@ -14,8 +15,7 @@ class PullRequestsController < ApplicationController @all_issues_size = issues.size @open_issues_size = issues.where.not(status_id: 5).size @close_issues_size = issues.where(status_id: 5).size - @assign_to_me_size = issues.where(assigned_to_id: current_user&.id).size - @my_published_size = issues.where(author_id: current_user&.id).size + @merged_issues_size = issues.joins(:pull_request).where(pull_requests: {status: 1}).size @user_admin_or_member = current_user.present? && (current_user.admin || @project.member?(current_user)) scopes = Issues::ListQueryService.call(issues,params.delete_if{|k,v| v.blank?}) @@ -31,13 +31,9 @@ class PullRequestsController < ApplicationController @all_branches.push(b["name"]) end end - @project_tags = @project.issue_tags&.select(:id,:name, :color).as_json - @project_versions = @project.versions&.select(:id,:name, :status).as_json - @project_members = @project.members_user_infos end def create - if params[:title].nil? normal_status(-1, "名称不能为空") elsif params[:issue_tag_ids].nil? @@ -45,55 +41,18 @@ class PullRequestsController < ApplicationController else ActiveRecord::Base.transaction do begin - local_params = { - title: params[:title], #标题 - body: params[:body], #内容 - head: params[:head], #源分支 - base: params[:base], #目标分支 - milestone: 0, #里程碑,未与本地的里程碑关联 - } - requests_params = local_params.merge({ - assignee: current_user.try(:login), - assignees: ["#{params[:assigned_login].to_s}"], - labels: params[:issue_tag_ids], - due_date: Time.now - }) - issue_params = { - author_id: current_user.id, - project_id: @project.id, - subject: params[:title], - description: params[:body], - assigned_to_id: params[:assigned_to_id], - fixed_version_id: params[:fixed_version_id], - issue_tags_value: params[:issue_tag_ids].present? ? params[:issue_tag_ids].join(",") : "", - issue_classify: "pull_request", - issue_type: params[:issue_type] || "1", - tracker_id: 2, - status_id: 1, - priority_id: 1 - } - pull_issue = Issue.new(issue_params) + merge_params + pull_issue = Issue.new(@issue_params) if pull_issue.save! - local_requests = PullRequest.new(local_params.merge(user_id: current_user.try(:id), project_id: @project.id, issue_id: pull_issue.id)) + local_requests = PullRequest.new(@local_params.merge(user_id: current_user.try(:id), project_id: @project.id, issue_id: pull_issue.id)) if local_requests.save - gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @project.owner, @repository.try(:identifier), requests_params).call + gitea_request = Gitea::PullRequest::CreateService.new(current_user.try(:gitea_token), @project.owner, @repository.try(:identifier), @requests_params).call if gitea_request && local_requests.update_attributes(gpid: gitea_request["number"]) if params[:issue_tag_ids].present? params[:issue_tag_ids].each do |tag| IssueTagsRelate.create!(issue_id: pull_issue.id, issue_tag_id: tag) end end - if params[:attachment_ids].present? - params[:attachment_ids].each do |id| - attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) - unless attachment.blank? - attachment.container = pull_issue - attachment.author_id = current_user.id - attachment.description = "" - attachment.save - end - end - end if params[:assigned_to_id].present? Tiding.create!(user_id: params[:assigned_to_id], trigger_user_id: current_user.id, @@ -122,8 +81,7 @@ class PullRequestsController < ApplicationController end def edit - @issue_chosen = issue_left_chosen(@project, @issue.id) - @issue_attachments = @issue.attachments + end def update @@ -134,25 +92,7 @@ class PullRequestsController < ApplicationController else ActiveRecord::Base.transaction do begin - local_params = { - title: params[:title], #标题 - body: params[:body], #内容 - head: params[:head], #源分支 - base: params[:base], #目标分支 - milestone: 0, #里程碑,未与本地的里程碑关联 - } - requests_params = local_params.merge({ - assignee: current_user.try(:login), - assignees: ["#{params[:assigned_login].to_s}"], - labels: params[:issue_tag_ids] - }) - issue_params = { - subject: params[:title], - description: params[:body], - assigned_to_id: params[:assigned_to_id].to_s, - fixed_version_id: params[:fixed_version_id], - issue_tags_value: params[:issue_tag_ids].present? ? params[:issue_tag_ids].join(",") : "", - } + merge_params if params[:issue_tag_ids].present? && !@issue&.issue_tags_relates.where(issue_tag_id: params[:issue_tag_ids]).exists? @issue&.issue_tags_relates&.destroy_all @@ -161,26 +101,10 @@ class PullRequestsController < ApplicationController end end - if @issue.update_attributes(issue_params) - if @pull_request.update_attributes(local_params) - gitea_request = Gitea::PullRequest::UpdateService.new(current_user, @repository.try(:identifier), requests_params, @pull_request.try(:gpid)).call + if @issue.update_attributes(@issue_params) + if @pull_request.update_attributes(@local_params) + gitea_request = Gitea::PullRequest::UpdateService.new(current_user, @repository.try(:identifier), @requests_params, @pull_request.try(:gpid)).call if gitea_request - issue_files = params[:attachment_ids] - change_files = false - issue_file_ids = [] - - if issue_files.present? - change_files = true - issue_files.each do |id| - attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) - unless attachment.blank? - attachment.container = @issue - attachment.author_id = current_user.id - attachment.description = "" - attachment.save - end - end - end if params[:issue_tag_ids].present? params[:issue_tag_ids].each do |tag| IssueTagsRelate.create(issue_id: @issue.id, issue_tag_id: tag) @@ -189,7 +113,6 @@ class PullRequestsController < ApplicationController if params[:status_id].to_i == 5 @issue.issue_times.update_all(end_time: Time.now) end - @issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id) normal_status(0, "PullRequest更新成功") else normal_status(-1, "PullRequest更新失败") @@ -208,51 +131,36 @@ class PullRequestsController < ApplicationController end def simple_update + # ActiveRecord::Base.transaction do + # begin + # @pull_request.update(status: 2) + # @pull_request.issue.update(status_id: 5) + # normal_status(1, "已拒绝") + # rescue => e + # normal_status(-1, e.message) + # raise ActiveRecord::Rollback + # end + # end + end + + def refuse_merge ActiveRecord::Base.transaction do begin - issue_params = { - assigned_to_id: params[:assigned_to_id].to_s, - fixed_version_id: params[:fixed_version_id], - issue_tags_value: params[:issue_tag_ids].present? ? params[:issue_tag_ids].join(",") : "", - } - if params[:issue_tag_ids].blank? - @issue&.issue_tags_relates&.destroy_all - end - - if params[:issue_tag_ids].present? && !@issue&.issue_tags_relates.where(issue_tag_id: params[:issue_tag_ids]).exists? - @issue&.issue_tags_relates&.destroy_all - params[:issue_tag_ids].each do |tag| - IssueTagsRelate.create(issue_id: @issue.id, issue_tag_id: tag) - end - end - - if @issue.update_attributes(issue_params) - normal_status(0, "PullRequest更新成功") - else - normal_status(-1, "PullRequest更新成功") - end + @pull_request.update(status: 2) + @pull_request.issue.update(status_id: 5) + normal_status(1, "已拒绝") rescue => e normal_status(-1, e.message) raise ActiveRecord::Rollback end end - end def show - @user_permission = current_user.present? && current_user.logged? && (!@issue.is_lock || @project.member?(current_user) || current_user.admin? || @issue.user == current_user) - @issue_attachments = @issue.attachments + @user_permission = current_user.present? && current_user.logged? && (@issue.assigned_to_id == current_user.id || current_user.admin? ) @issue_user = @issue.user @issue_assign_to = @issue.get_assign_user - @join_users = join_users(@issue) - #总耗时 - # cost_time(@issue) - # #被依赖 - # @be_depended_issues_array = be_depended_issues(@issue) - - # #依赖于 - # depended_issues(@issue) end def pr_merge @@ -286,71 +194,69 @@ class PullRequestsController < ApplicationController #评审 def check_merge - notes = params[:content] - pull_request_status = params[:status] - if notes.blank? - normal_status(-1, "评论内容不能为空") - else - if @pull_request.status > 0 - normal_status(-1, "已合并,不能评审") - else - if pull_request_status.to_i == 1 - message = "评审通过:" - elsif pull_request_status.to_i == 2 - message = "评审请求变更:" - else - message = "" - end - journal_params = { - journalized_id: @issue.id , - journalized_type: "Issue", - user_id: current_user.id , - notes: message + notes.to_s.strip - } - journal = Journal.new journal_params - if journal.save - if pull_request_status.present? - @pull_request.update_attribute(:status, pull_request_status.to_i) - end - if pull_request_status.to_i == 1 - requests_params = { - do: "merge", - MergeMessageField: notes, - MergeTitleField: "Merge PullRequest ##{@pull_request.gpid}" - } - merge_pr = Gitea::PullRequest::MergeService.new(current_user, @repository.try(:identifier), @pull_request.try(:gpid), requests_params).call - if merge_pr - @pull_request&.project_trends&.update_all(action_type: "close") - # @pull_request.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "merge") - @issue.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) - normal_status(1, "评审成功") - else - normal_status(-1, "评审失败") - end - end - normal_status(0, "评审成功") - else - normal_status(-1, "评审失败") - end - end - end + # notes = params[:content] + # pull_request_status = params[:status] + # if notes.blank? + # normal_status(-1, "评论内容不能为空") + # else + # if @pull_request.status > 0 + # normal_status(-1, "已合并,不能评审") + # else + # if pull_request_status.to_i == 1 + # message = "评审通过:" + # elsif pull_request_status.to_i == 2 + # message = "评审请求变更:" + # else + # message = "" + # end + # journal_params = { + # journalized_id: @issue.id , + # journalized_type: "Issue", + # user_id: current_user.id , + # notes: message + notes.to_s.strip + # } + # journal = Journal.new journal_params + # if journal.save + # if pull_request_status.present? + # @pull_request.update_attribute(:status, pull_request_status.to_i) + # end + # if pull_request_status.to_i == 1 + # requests_params = { + # do: "merge", + # MergeMessageField: notes, + # MergeTitleField: "Merge PullRequest ##{@pull_request.gpid}" + # } + # merge_pr = Gitea::PullRequest::MergeService.new(current_user, @repository.try(:identifier), @pull_request.try(:gpid), requests_params).call + # if merge_pr + # @pull_request&.project_trends&.update_all(action_type: "close") + # # @pull_request.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "merge") + # @issue.custom_journal_detail("merge", "", "该合并请求已被合并", current_user&.id) + # normal_status(1, "评审成功") + # else + # normal_status(-1, "评审失败") + # end + # end + # normal_status(0, "评审成功") + # else + # normal_status(-1, "评审失败") + # end + # end + # end end def check_can_merge target_head = params[:head] #源分支 target_base = params[:base] #目标分支 if target_head.blank? || target_base.blank? - normal_status(-1, "请选择分支。") + normal_status(-2, "请选择分支") elsif target_head === target_base - normal_status(-1, "分支内容相同,无需创建合并请求。") + normal_status(-2, "分支内容相同,无需创建合并请求") else can_merge = @project&.pull_requests.where(user_id: current_user&.id, head: target_head, base: target_base, status: 0) if can_merge.present? render json: { status: -2, - message: "在这些分支之间的合并请求已存在", - pull_request_id: can_merge.first.id, - pull_request_name: can_merge.first.try(:title) + message: "在这些分支之间的合并请求已存在:#{can_merge.first.try(:title)}", } else normal_status(0, "可以合并") @@ -379,4 +285,41 @@ class PullRequestsController < ApplicationController normal_status(-1, "您没有权限") end end + + def get_relatived + @project_tags = @project.issue_tags&.select(:id,:name, :color).as_json + @project_versions = @project.versions&.select(:id,:name, :status).as_json + @project_members = @project.members_user_infos + @project_priories = IssuePriority&.select(:id,:name, :position).as_json + end + + def merge_params + @local_params = { + title: params[:title], #标题 + body: params[:body], #内容 + head: params[:head], #源分支 + base: params[:base], #目标分支 + milestone: 0, #里程碑,未与本地的里程碑关联 + } + @requests_params = @local_params.merge({ + assignee: current_user.try(:login), + assignees: ["#{params[:assigned_login].to_s}"], + labels: params[:issue_tag_ids], + due_date: Time.now + }) + @issue_params = { + author_id: current_user.id, + project_id: @project.id, + subject: params[:title], + description: params[:body], + assigned_to_id: params[:assigned_to_id], + fixed_version_id: params[:fixed_version_id], + issue_tags_value: params[:issue_tag_ids].present? ? params[:issue_tag_ids].join(",") : "", + priority_id: params[:priority_id] || "2", + issue_classify: "pull_request", + issue_type: params[:issue_type] || "1", + tracker_id: 2, + status_id: 1, + } + end end \ No newline at end of file diff --git a/app/services/issues/list_query_service.rb b/app/services/issues/list_query_service.rb index 0e2432ea0..17045705a 100644 --- a/app/services/issues/list_query_service.rb +++ b/app/services/issues/list_query_service.rb @@ -18,6 +18,8 @@ class Issues::ListQueryService < ApplicationService issues = issues.where(status_id: 5) elsif status_type.to_s == "1" issues = issues.where.not(status_id: 5) #默认显示开启中的 + elsif status_type.to_s == "11" #表示pr的已关闭 + issues = issues.joins(:pull_request).where(pull_requests: {status: 1}) end if search_name.present? diff --git a/app/views/pull_requests/_merge_item.json.jbuilder b/app/views/pull_requests/_merge_item.json.jbuilder new file mode 100644 index 000000000..8475c468d --- /dev/null +++ b/app/views/pull_requests/_merge_item.json.jbuilder @@ -0,0 +1,14 @@ + +json.issue_tags @project_tags +json.issue_versions @project_versions +json.issue_priories @project_priories +json.project_author @project.owner.try(:show_real_name) +json.project_name @project.try(:name) +json.members do + json.array! @project_members.to_a.each do |member| + json.id member.user_id + json.login member.user.try(:login) + json.name member.user.try(:show_real_name) + json.avatar_url url_to_avatar(member.user) + end +end \ No newline at end of file diff --git a/app/views/pull_requests/edit.json.jbuilder b/app/views/pull_requests/edit.json.jbuilder index d2c815fc1..373c88cd4 100644 --- a/app/views/pull_requests/edit.json.jbuilder +++ b/app/views/pull_requests/edit.json.jbuilder @@ -1,18 +1,5 @@ json.partial! "commons/success" -json.pull_request do - json.extract! @pull_request, :id,:base, :head, :status -end - -json.issue do - json.extract! @issue, :id,:subject,:description,:is_private,:assigned_to_id,:tracker_id,:status_id,:priority_id,:fixed_version_id, - :start_date,:due_date,:estimated_hours, :issue_type, :token,:issue_classify, :branch_name - json.done_ratio @issue.done_ratio.to_s + "%" - json.issue_tags @issue.get_issue_tags - json.issue_chosen @issue_chosen -end - -json.attachments do - json.array! @issue_attachments do |attachment| - json.partial! "attachments/attachment_simple", locals: {attachment: attachment} - end -end \ No newline at end of file +json.partial! "pull_requests/merge_item" +json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base +json.extract! @issue, :assigned_to_id, :fixed_version_id, :priority_id +json.issue_tag_ids @issue.issue_tags_value.split(",") diff --git a/app/views/pull_requests/index.json.jbuilder b/app/views/pull_requests/index.json.jbuilder index 85ca9e761..34b84dbff 100644 --- a/app/views/pull_requests/index.json.jbuilder +++ b/app/views/pull_requests/index.json.jbuilder @@ -2,39 +2,33 @@ json.partial! "commons/success" json.all_count @all_issues_size json.open_count @open_issues_size json.close_count @close_issues_size -json.assign_me_count @assign_to_me_size -json.my_published_count @my_published_size +json.merged_issues_size @merged_issues_size json.search_count @issues_size json.limit @limit json.user_admin_or_member @user_admin_or_member +json.project_name @project.name json.issues do json.array! @issues.to_a do |issue| - # cost_time(issue) - json.pull_request_id issue.pull_request.id - json.pull_request_status issue.pull_request.status + pr = issue.pull_request + json.pull_request_id pr.id + json.pull_request_status pr.status + json.pull_request_head pr.head + json.pull_request_base pr.base + json.pull_request_staus pr.status == 1 ? "merged" : (pr.status == 2 ? "closed" : "open") + json.id issue.id json.name issue.subject - json.format_time format_time(issue.created_on) - json.created_at time_from_now(issue.created_on) - json.updated_at format_time(issue.updated_on) + json.pr_time time_from_now(pr.status == 1 ? pr.updated_at : issue.updated_on) json.assign_user_name issue.get_assign_user.try(:show_real_name) json.assign_user_login issue.get_assign_user.try(:login) json.author_name issue.user.try(:show_real_name) json.author_login issue.user.try(:login) - json.tracker issue.tracker.try(:name) - json.issue_status issue.issue_status.try(:name) + json.avatar_url url_to_avatar(issue.user) json.priority issue.priority.try(:name) json.version issue.version.try(:name) - json.done_ratio issue.done_ratio.to_s + "%" json.journals_count issue.get_journals_size json.issue_tags issue.get_issue_tags - json.issue_type issue.issue_type == "1" ? "普通" : "悬赏" - json.token issue.issue_type == "2" ? issue.token : "" - json.issue_classify issue.issue_classify - json.branch_name issue.branch_name - # json.cost_time @all_cost_time end end -# json.issues @issues diff --git a/app/views/pull_requests/new.json.jbuilder b/app/views/pull_requests/new.json.jbuilder index 4c8543461..b2ab0c150 100644 --- a/app/views/pull_requests/new.json.jbuilder +++ b/app/views/pull_requests/new.json.jbuilder @@ -1,13 +1,3 @@ json.partial! "commons/success" +json.partial! "pull_requests/merge_item" json.branches @all_branches -json.issue_tags @project_tags -json.issue_versions @project_versions - -json.members do - json.array! @project_members.to_a.each do |member| - json.id member.user_id - json.login member.user.try(:login) - json.name member.user.try(:show_real_name) - json.avatar_url url_to_avatar(member.user) - end -end diff --git a/app/views/pull_requests/show.json.jbuilder b/app/views/pull_requests/show.json.jbuilder index d16ac6b7f..31dec7d86 100644 --- a/app/views/pull_requests/show.json.jbuilder +++ b/app/views/pull_requests/show.json.jbuilder @@ -1,11 +1,13 @@ json.partial! "commons/success" +json.project_name @project.name +json.pr_time time_from_now(@pull_request.updated_at) json.pull_request do - json.extract! @pull_request, :id,:base, :head, :status, :gpid + json.extract! @pull_request, :id,:base, :head, :status + json.pull_request_staus @pull_request.status == 1 ? "merged" : (@pull_request.status == 2 ? "closed" : "open") end json.issue do - json.extract! @issue, :id,:subject,:is_lock,:description,:is_private, :start_date,:due_date,:estimated_hours,:issue_classify, :branch_name - + json.extract! @issue, :id,:subject,:description,:is_private, :branch_name json.user_permission @user_permission json.closed_on @issue.closed_on.present? ? format_time(@issue.closed_on) : "" json.created_at format_time(@issue.created_on) @@ -14,27 +16,13 @@ json.issue do json.author_name @issue_user.try(:show_real_name) json.author_login @issue_user.try(:login) json.author_picture url_to_avatar(@issue_user) - json.tracker @issue.tracker.try(:name) json.issue_status @issue.issue_status.try(:name) json.priority @issue.priority.try(:name) json.version @issue.version.try(:name) json.issue_tags @issue.get_issue_tags - json.done_ratio @issue.done_ratio.to_s + "%" - json.issue_type @issue.issue_type == "1" ? "普通" : "悬赏" - json.token @issue.issue_type == "2" ? @issue.token : "" - json.join_users @join_users - # json.cost_time @cost_time_array - # json.total_cost_time Time.at(@all_cost_time).utc.strftime('%H h %M min %S s') - # json.be_depended_issues @be_depended_issues_array - # json.depended_issues @depended_issues_array -end - -json.attachments do - json.array! @issue_attachments do |attachment| - json.partial! "attachments/attachment_simple", locals: {attachment: attachment} - end end + diff --git a/config/routes.rb b/config/routes.rb index 3ff0d0dd7..750796055 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,6 +66,7 @@ Rails.application.routes.draw do post :pr_merge post :check_merge post :simple_update + post :refuse_merge end collection do post :check_can_merge diff --git a/dump.rdb b/dump.rdb index c0d1da747ab3df830e34a4db27cbceba88543532..3bcfe7a80bf9bf205f88e2c792e8182b68a9ba9f 100644 GIT binary patch delta 169 zcmbQnx`J(jfuMN!%Q-PZh2@+DU(Cb?IS#`EjDMWw zJPMoG5XSmv;h)AhprE+X0R{%{;*!J?tF*+-oYWMc8n9HX6sI9j?hg|;?}~}9TzM@` z&5g{=^h_;H3`~tC+c17-0UHHUW&qR!wgbd51nOD;>QLas2HDBwOrl)o|Bo_SU1iOV GG6Mi-_&?tO delta 124 zcmZ3%HjQ2XVhRN{drdbaPX44=DsqbQERcWStnH!t%7iFk~_#quj(J79tGX z#U+U)R%wZuIjJcJ82`BS@0mL}lF^md+``1j&{EIB+}zT_a`I}%?_g~cW2Gh^U}9xi b|LRa6&