From 4212d46b3fb19b9dcbcc2787dac3ba5cf2a845f8 Mon Sep 17 00:00:00 2001 From: "sylor_huang@126.com" Date: Mon, 29 Jun 2020 17:06:46 +0800 Subject: [PATCH] change pull-request --- app/controllers/pull_requests_controller.rb | 43 ++++++++++++++----- app/models/project.rb | 4 ++ .../pull_requests/branches_service.rb | 34 +++++++++++++++ .../create_merge_infos.json.jbuilder | 2 + app/views/pull_requests/edit.json.jbuilder | 8 +++- app/views/pull_requests/index.json.jbuilder | 3 ++ app/views/pull_requests/new.json.jbuilder | 6 ++- app/views/pull_requests/show.json.jbuilder | 4 +- config/routes.rb | 2 + ...1_add_origin_project_id_to_pull_request.rb | 6 +++ 10 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 app/services/pull_requests/branches_service.rb create mode 100644 app/views/pull_requests/create_merge_infos.json.jbuilder create mode 100644 db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index b961f9f02..a4d802e09 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -2,15 +2,15 @@ class PullRequestsController < ApplicationController before_action :require_login, except: [:index, :show] 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] + before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos] + # before_action :get_relatived, only: [:edit] include TagChosenHelper include ApplicationHelper def index # @issues = Gitea::PullRequest::ListService.new(@user,@repository.try(:identifier)).call #通过gitea获取 - issues = @project.issues.issue_pull_request.issue_index_includes.includes(:pull_request) + issues = @project.issues.issue_pull_request.issue_index_includes.includes(pull_request: :user) issues = issues.where(is_private: false) unless current_user.present? && (current_user.admin? || @project.member?(current_user)) @all_issues_size = issues.size @open_issues_size = issues.joins(:pull_request).where(pull_requests: {status: 0}).size @@ -24,15 +24,28 @@ class PullRequestsController < ApplicationController end def new - @all_branches = [] - get_all_branches = Gitea::Repository::Branches::ListService.new(@user, @repository.try(:identifier)).call - if get_all_branches && get_all_branches.size > 0 - get_all_branches.each do |b| - @all_branches.push(b["name"]) - end + @all_branches = PullRequests::BranchesService.new(@user, @project).call + @is_fork = @project.forked_from_project_id.present? + @projects_names = [{ + project_name: "#{@user.try(:show_real_name)}/#{@repository.try(:identifier)}", + project_id: @project.id + }] + @merge_projects = @projects_names + fork_project = @project.fork_project if @is_fork + if fork_project.present? + @merge_projects.push({ + project_name: "#{fork_project.owner.try(:show_real_name)}/#{fork_project.repository.try(:identifier)}", + project_id: fork_project.id + }) end end + def get_branches + branch_result = PullRequests::BranchesService.new(@user, @project).call + render json: branch_result + # return json: branch_result + end + def create if params[:title].nil? normal_status(-1, "名称不能为空") @@ -81,6 +94,8 @@ class PullRequestsController < ApplicationController end def edit + @fork_project_user = @project&.fork_project&.owner.try(:show_real_name) + @fork_project_identifier = @project&.fork_project&.repository.try(:identifier) end @@ -102,7 +117,7 @@ class PullRequestsController < ApplicationController end if @issue.update_attributes(@issue_params) - if @pull_request.update_attributes(@local_params) + if @pull_request.update_attributes(@local_params.compact) gitea_request = Gitea::PullRequest::UpdateService.new(current_user, @repository.try(:identifier), @requests_params, @pull_request.try(:gpid)).call if gitea_request if params[:issue_tag_ids].present? @@ -143,6 +158,10 @@ class PullRequestsController < ApplicationController end end + def create_merge_infos + get_relatived + end + def show @user_permission = current_user.present? && current_user.logged? && (@issue.assigned_to_id == current_user.id || current_user.admin? ) @issue_user = @issue.user @@ -237,7 +256,9 @@ class PullRequestsController < ApplicationController assignee: current_user.try(:login), assignees: ["#{params[:assigned_login].to_s}"], labels: params[:issue_tag_ids], - due_date: Time.now + due_date: Time.now, + fork_project_id: params[:fork_project_id], + is_original: params[:is_original] }) @issue_params = { author_id: current_user.id, diff --git a/app/models/project.rb b/app/models/project.rb index 09f1705be..1d6fd0327 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -158,4 +158,8 @@ class Project < ApplicationRecord member&.roles&.last&.name || permission end + def fork_project + Project.find_by(id: self.forked_from_project_id) + end + end diff --git a/app/services/pull_requests/branches_service.rb b/app/services/pull_requests/branches_service.rb new file mode 100644 index 000000000..117f17729 --- /dev/null +++ b/app/services/pull_requests/branches_service.rb @@ -0,0 +1,34 @@ +class PullRequests::BranchesService < ApplicationService + + attr_reader :user, :project + + def initialize(user, project) + @user = user + @project = project + end + + def call + all_branches = [] + user_name = user.try(:show_real_name) + identifier = project.repository.try(:identifier) + get_all_branches = Gitea::Repository::Branches::ListService.new(user, identifier).call + all_branches = branch_lists(user_name,user.try(:login), identifier, get_all_branches) if get_all_branches && get_all_branches.size > 0 + return all_branches + end + + def branch_lists(user_name,user_login, identifier, branches) + branches_array = [] + branches.each do |b| + branch_params = { + user_name: user_name, + user_login: user_login, + identifier: identifier, + name: b["name"], + can_merge: b["user_can_merge"], + } + branches_array.push(branch_params) + end + return branches_array + end + +end \ No newline at end of file diff --git a/app/views/pull_requests/create_merge_infos.json.jbuilder b/app/views/pull_requests/create_merge_infos.json.jbuilder new file mode 100644 index 000000000..1df3b7e97 --- /dev/null +++ b/app/views/pull_requests/create_merge_infos.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! "commons/success" +json.partial! "pull_requests/merge_item" \ 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 373c88cd4..aad5c26b0 100644 --- a/app/views/pull_requests/edit.json.jbuilder +++ b/app/views/pull_requests/edit.json.jbuilder @@ -1,5 +1,9 @@ json.partial! "commons/success" -json.partial! "pull_requests/merge_item" -json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base +# json.partial! "pull_requests/merge_item" +json.fork_project_user @fork_project_user +json.fork_project_identifier @fork_project_identifier +json.project_author @project.owner.try(:show_real_name) +json.project_name @project.repository.try(:identifier) +json.extract! @pull_request, :id, :title, :body, :milestone,:head,:base,:is_original 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 34b84dbff..5b46c77e3 100644 --- a/app/views/pull_requests/index.json.jbuilder +++ b/app/views/pull_requests/index.json.jbuilder @@ -16,6 +16,9 @@ json.issues do 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.is_original pr.is_original + json.fork_project_id pr.fork_project_id + json.pull_request_user pr.user.try(:show_real_name) json.id issue.id json.name issue.subject diff --git a/app/views/pull_requests/new.json.jbuilder b/app/views/pull_requests/new.json.jbuilder index b2ab0c150..ab0e87135 100644 --- a/app/views/pull_requests/new.json.jbuilder +++ b/app/views/pull_requests/new.json.jbuilder @@ -1,3 +1,7 @@ json.partial! "commons/success" -json.partial! "pull_requests/merge_item" +json.project_id @project.id json.branches @all_branches +json.is_fork @is_fork +json.projects_names @projects_names +json.merge_projects @merge_projects +# json.merge_branches @merge_branches \ No newline at end of file diff --git a/app/views/pull_requests/show.json.jbuilder b/app/views/pull_requests/show.json.jbuilder index 31dec7d86..a294b91c3 100644 --- a/app/views/pull_requests/show.json.jbuilder +++ b/app/views/pull_requests/show.json.jbuilder @@ -1,9 +1,11 @@ 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 + json.extract! @pull_request, :id,:base, :head, :status,:fork_project_id, :is_original json.pull_request_staus @pull_request.status == 1 ? "merged" : (@pull_request.status == 2 ? "closed" : "open") + json.pull_request_user @pull_request.user.try(:show_real_name) end json.issue do diff --git a/config/routes.rb b/config/routes.rb index 24ae81176..a53334a6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -69,6 +69,8 @@ Rails.application.routes.draw do end collection do post :check_can_merge + get :create_merge_infos + get :get_branches end end resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy] diff --git a/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb b/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb new file mode 100644 index 000000000..fa02f66ae --- /dev/null +++ b/db/migrate/20200629081111_add_origin_project_id_to_pull_request.rb @@ -0,0 +1,6 @@ +class AddOriginProjectIdToPullRequest < ActiveRecord::Migration[5.2] + def change + add_column :pull_requests, :fork_project_id, :integer + add_column :pull_requests, :is_original, :boolean, default: false + end +end