From 386b41d08e223eaf1fe696b4f28942ccc8a3850d Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 17 Nov 2016 11:32:39 +0800 Subject: [PATCH] =?UTF-8?q?pull=20requst=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/pull_requests_controller.rb | 57 ++++++++++++++----- .../quality_analysis_controller.rb | 8 +-- app/helpers/application_helper.rb | 5 +- app/views/pull_requests/_form.html.erb | 23 ++++++-- 4 files changed, 71 insertions(+), 22 deletions(-) diff --git a/app/controllers/pull_requests_controller.rb b/app/controllers/pull_requests_controller.rb index bfc2ab9f7..932cdeacd 100644 --- a/app/controllers/pull_requests_controller.rb +++ b/app/controllers/pull_requests_controller.rb @@ -1,9 +1,10 @@ - +# 如果你对改模块任何功能不清楚,请不要随便改 +# @Hjqreturn class PullRequestsController < ApplicationController before_filter :authorize_logged before_filter :find_project_and_repository before_filter :connect_gitlab, :only => [:index, :show, :create, :accept_pull_request, :pull_request_commits, :pull_request_changes, :new, - :update_pull_request, :pull_request_comments, :create_pull_request_comment] + :update_pull_request, :pull_request_comments, :create_pull_request_comment, :compare_pull_request] layout "base_projects" include PullRequestsHelper @@ -85,16 +86,25 @@ class PullRequestsController < ApplicationController source_branch = params[:source_branch] target_branch = params[:target_branch] begin - # 如果传送了目标项目ID,则PR请求发至目标项目 - if params[:forked_project_id] && params[:source_project] == "forked_project_name" - target_project_id = params[:forked_project_id].to_i - request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch, :target_project_id => target_project_id) - @fork_project_name = Project.find(@project.forked_from_project_id).try(:name) - @fork_pr_message = true if @fork_project_name - else - request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch) + # 如果分支没有改动 + if compare_pull_request(source_branch, target_project, target_branch).blank? + @no_updates = 1 respond_to do |format| - format.js{redirect_to project_pull_request_path(request.id, :project_id => @project.id)} + format.html{new_project_pull_request_path()} + end + else + @no_updates = 0 + # 如果传送了目标项目ID即向fork源项目发送请求 + if params[:forked_project_id] && params[:source_project] == "forked_project_name" + target_project_id = params[:forked_project_id].to_i + request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch, :target_project_id => target_project_id) + @fork_project_name = Project.find(@project.forked_from_project_id).try(:name) + @fork_pr_message = true if @fork_project_name + else + request = @g.create_merge_request(@project.gpid, title, User.current.gid, :description => description, :source_branch => source_branch, :target_branch => target_branch) + respond_to do |format| + format.js{redirect_to project_pull_request_path(request.id, :project_id => @project.id)} + end end end rescue Exception => e @@ -102,8 +112,29 @@ class PullRequestsController < ApplicationController end end + # Compare branch for MR + # 判断源分支和目标分支是否有改动 + def compare_pull_request source_branch, target_project, target_branch + user_source_name = @project.owner.try(:login) + identifier = @repository.identifier.downcase + git_source_tree = '--git-dir=/home/git/repositories/' + user_name + '/' + identifier + '.git' + if target_project + git_target_tree = '--git-dir=/home/git/repositories/' + user_name + '/' + identifier + '.git' + else + + end + # def compare_pull_request + user_name = @project.owner.try(:login) + identifier = @repository.identifier.downcase + git_tree = '--git-dir=/home/git/repositories/' + user_name + '/' + identifier + '.git' + status = `git #{git_tree} log --pretty=oneline #{source_branch} ^#{target_branch}` + end + + # @project_menu_type 为了控制base顶部导航 + # merge_when_succeeds def show - # project_menu_type 为了控制base顶部导航 + # compare_pull_request source_project, source_branch, target_project, target_branch + # compare_pull_request @project_menu_type = 6 @type = params[:type] @request = @g.merge_request(@project.gpid, params[:id]) @@ -266,7 +297,7 @@ class PullRequestsController < ApplicationController def find_project_and_repository @project = Project.find(params[:project_id]) render_404 if @project.gpid.blank? - @repository = Repository.where(:project_id => @project.id, :type => "Repository::Gitlab") + @repository = Repository.where(:project_id => @project.id, :type => "Repository::Gitlab").first rescue ActiveRecord::RecordNotFound render_404 end diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index c7cfeefe5..33b0d4f72 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -6,10 +6,10 @@ class QualityAnalysisController < ApplicationController layout "base_projects" include ApplicationHelper include QualityAnalysisHelper - # require 'jenkins_api_client' - # require 'nokogiri' - # require 'json' - # require 'open-uri' + require 'jenkins_api_client' + require 'nokogiri' + require 'json' + require 'open-uri' def show diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c4746ff29..dff739343 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -931,7 +931,10 @@ module ApplicationHelper def allow_pull_request project return 0 if project.gpid.nil? g = Gitlab.client - count = g.user_static(project.gpid, :rev => "master").count + # 之所以这样比较是为了解决gitlab本身的bug + commit_count = g.project(project.gpid).try(:commit_count).to_i + git_commit_cout = g.user_static(project.gpid, :rev => "master").count + count = commit_count > git_commit_cout ? commit_count : git_commit_cout count end diff --git a/app/views/pull_requests/_form.html.erb b/app/views/pull_requests/_form.html.erb index a2a07d8c9..a79f8c374 100644 --- a/app/views/pull_requests/_form.html.erb +++ b/app/views/pull_requests/_form.html.erb @@ -12,17 +12,19 @@ <% if @forked_project.nil? %> <%= select_tag :branch, options_for_select(@source_rev), :id => "pull_request_branch", :name => "target_branch", :value => "target_branch",:class => "fl PullReques_minselect ml5" %> <% else %> - + + + <%= hidden_field_tag 'target_project_id', '' %> <% end %> +