From 4f1d23a2a4f2c75770e85f192321f75c6c626926 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Thu, 24 May 2012 23:10:49 +0600 Subject: [PATCH] [refs #90] branch, merge validation --- app/assets/stylesheets/design/custom.scss | 4 +++ .../projects/pull_requests_controller.rb | 28 +++++++------------ app/models/pull_request.rb | 15 ++++++++++ .../pull_requests/_commit_diff.html.haml | 2 +- .../projects/pull_requests/new.html.haml | 9 +++--- config/locales/models/pull_request.en.yml | 1 + config/locales/models/pull_request.ru.yml | 1 + 7 files changed, 36 insertions(+), 24 deletions(-) diff --git a/app/assets/stylesheets/design/custom.scss b/app/assets/stylesheets/design/custom.scss index 78a929845..22fd15f6d 100644 --- a/app/assets/stylesheets/design/custom.scss +++ b/app/assets/stylesheets/design/custom.scss @@ -947,3 +947,7 @@ div#git_help_data p { .atom_icon { vertical-align: top; } + +.tab-content.pull_diff_fix { + display: block; +} \ No newline at end of file diff --git a/app/controllers/projects/pull_requests_controller.rb b/app/controllers/projects/pull_requests_controller.rb index 904a194da..6abc37eab 100644 --- a/app/controllers/projects/pull_requests_controller.rb +++ b/app/controllers/projects/pull_requests_controller.rb @@ -10,26 +10,22 @@ class Projects::PullRequestsController < Projects::BaseController def new @pull = PullRequest.default_base_project(@project).pull_requests.new - #@pull.build_issue @pull.issue = @project.issues.new + if params[:pull_request] && params[:pull_request][:issue_attributes] + @pull.issue.title = params[:pull_request][:issue_attributes][:title].presence + @pull.issue.body = params[:pull_request][:issue_attributes][:body].presence + end @pull.head_project = @project - - @pull.base_ref = @pull.base_project.default_branch - @pull.head_ref = params[:treeish].presence || @pull.head_project.default_branch - if @pull.save - @pull.check - if @pull.state == 'already' - record.errors.add(:head_ref, I18n.t('projects.pull_requests.up_to_date', :base_ref => record.base_ref, :head_ref => record.head_ref)) - flash[:error] = t('flash.pull_request.create_error') - flash[:warning] = @pull.errors.full_messages.join('. ') - end + @pull.base_ref = (params[:pull_request][:base_ref].presence if params[:pull_request]) || @pull.base_project.default_branch + @pull.head_ref = params[:treeish].presence || (params[:pull_request][:head_ref].presence if params[:pull_request]) || @pull.head_project.default_branch + @pull.state = @pull.soft_check + if @pull.state == 'already' + flash[:warning] = I18n.t('projects.pull_requests.up_to_date', :base_ref => @pull.base_ref, :head_ref => @pull.head_ref) + else repo = Git::Repository.new(@pull.path) @base_commit = repo.commits(@pull.base_ref).first @head_commit = repo.commits(@pull.head_branch).first @diff = Grit::Repo.new(@pull.path).diff @base_commit, @head_commit - else - flash[:error] = t('flash.pull_request.create_error') - flash[:warning] = @pull.errors.full_messages.join('. ') end end @@ -37,13 +33,9 @@ class Projects::PullRequestsController < Projects::BaseController @pull = @project.pull_requests.new(params[:pull_request]) # FIXME need validation! @pull.issue.user, @pull.issue.project = current_user, @pull.base_project @pull.base_project, @pull.head_project = PullRequest.default_base_project(@project), @project - #@pull.base_ref = params[:base_ref] # FIXME need validation! - #@pull.head_ref = params[:head_ref] # FIXME need validation! if @pull.save @pull.check - puts "!!!!!!!!!!!!!!!!!!!!!!!!" - puts "pull state is #{@pull.state}" if @pull.state == 'already' @pull.destroy diff --git a/app/models/pull_request.rb b/app/models/pull_request.rb index a6bc3cd54..a82fa1999 100644 --- a/app/models/pull_request.rb +++ b/app/models/pull_request.rb @@ -74,6 +74,21 @@ class PullRequest < ActiveRecord::Base end end + def soft_check + ret = merge + if ret =~ /Already up-to-date/ + 'already' + elsif ret =~ /Merge made by the 'recursive' strategy/ + system("cd #{path} && git reset --hard HEAD^") # remove merge commit + 'ready' + elsif ret =~ /Automatic merge failed/ + system("cd #{path} && git reset --hard HEAD") + 'block' + else + raise ret + end + end + def merge!(who) return false unless can_merge? Dir.chdir(path) do diff --git a/app/views/projects/pull_requests/_commit_diff.html.haml b/app/views/projects/pull_requests/_commit_diff.html.haml index dc7a0c0b9..c566a6953 100644 --- a/app/views/projects/pull_requests/_commit_diff.html.haml +++ b/app/views/projects/pull_requests/_commit_diff.html.haml @@ -1,4 +1,4 @@ -- commit_id = commit_diff.deleted_file ? @head_commit.parents.try(:first).try(:id) : @head_commit.id +- commit_id = commit_diff.deleted_file ? @base_commit.id : @head_commit.id .file %a{:name => h(commit_diff.a_path)} .top diff --git a/app/views/projects/pull_requests/new.html.haml b/app/views/projects/pull_requests/new.html.haml index c23bc4130..7f42cd87c 100644 --- a/app/views/projects/pull_requests/new.html.haml +++ b/app/views/projects/pull_requests/new.html.haml @@ -5,9 +5,9 @@ -t('pull_requests.tabs').each do |base, title| %li %a{"data-toggle" => "tab", :href => "##{base}"}=title -.tab-content +.tab-content.pull_diff_fix #discussion.tab-pane.active - =form_for @pull, :url => project_pull_requests_path, :html => {:class => 'well well-large'} do |f| + =form_for @pull, :url => (@pull.state != 'already' ? project_pull_requests_path : new_project_pull_requests_path), :html => {:class => 'well well-large'} do |f| .leftlist=f.label :base_project, t('.base_project'), :class => :label .rightlist = @pull.base_project.full_name @@ -37,11 +37,10 @@ .leftlist for debug .rightlist=@pull.state .both - .leftlist - \  - .rightlist=f.submit t('.submit'), :class => 'btn btn-primary disabled', 'data-loading-text' => t('layout.processing'), :id => 'create_pull' + .rightlist=f.submit t(@pull.state != 'already' ? '.submit' : '.update'), :class => 'btn btn-primary disabled', 'data-loading-text' => t('layout.processing')#, :id => 'create_pull' .both + #diff.tab-pane -begin =# render_commit_stats(stats) diff --git a/config/locales/models/pull_request.en.yml b/config/locales/models/pull_request.en.yml index ec7cd24dd..aabe3c6e2 100644 --- a/config/locales/models/pull_request.en.yml +++ b/config/locales/models/pull_request.en.yml @@ -9,6 +9,7 @@ en: base_project: Base project head_project: Head project submit: Send pull request + update: Update commits merge: Merge duplicate: 'There is already a pull request for %{head_ref}' up_to_date: 'The %{base_ref} branch is already up-to-date with %{head_ref}' diff --git a/config/locales/models/pull_request.ru.yml b/config/locales/models/pull_request.ru.yml index 938d71588..5dd904079 100644 --- a/config/locales/models/pull_request.ru.yml +++ b/config/locales/models/pull_request.ru.yml @@ -9,6 +9,7 @@ ru: base_project: Базовый проект head_project: Проект-источник submit: Создать запрос на слияние + update: Обновить коммиты merge: Слияние duplicate: 'Уже существует запрос на слияние %{head_ref}' up_to_date: 'Ветка %{base_ref} на данный момент уже содержит последние изменения %{head_ref}'