From 20ac5e6d4b1e1b6807e13b11daaedd2fb47bd3fc Mon Sep 17 00:00:00 2001 From: Armin Hohenegger Date: Sat, 10 Aug 2019 19:11:20 +0200 Subject: fix handling of empty ref_name parameter string in commits api when params[:ref_name] is set to "" by passing an empty query parameter to the api it is evaluated as false by the || operator. The use of active support core extensions presence method fixes the original implemantation. https://guides.rubyonrails.org/active_support_core_extensions.html#presence --- changelogs/unreleased/fix-commits-api-empty-refname.yml | 5 +++++ lib/api/commits.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/fix-commits-api-empty-refname.yml diff --git a/changelogs/unreleased/fix-commits-api-empty-refname.yml b/changelogs/unreleased/fix-commits-api-empty-refname.yml new file mode 100644 index 00000000000..efdb950e45d --- /dev/null +++ b/changelogs/unreleased/fix-commits-api-empty-refname.yml @@ -0,0 +1,5 @@ +--- +title: Fix 500 errors in commits api caused by empty ref_name parameter +merge_request: +author: +type: fixed diff --git a/lib/api/commits.rb b/lib/api/commits.rb index e4f4e79cd46..a2f3e87ebd2 100644 --- a/lib/api/commits.rb +++ b/lib/api/commits.rb @@ -43,7 +43,7 @@ module API path = params[:path] before = params[:until] after = params[:since] - ref = params[:ref_name] || user_project.try(:default_branch) || 'master' unless params[:all] + ref = params[:ref_name].presence || user_project.try(:default_branch) || 'master' unless params[:all] offset = (params[:page] - 1) * params[:per_page] all = params[:all] with_stats = params[:with_stats] -- cgit v1.2.1 From e70cfdebc70e4bf12f6d8393e1d36e92b3de2219 Mon Sep 17 00:00:00 2001 From: Armin Hohenegger Date: Tue, 13 Aug 2019 23:00:14 +0200 Subject: add a test for empty ref_name to api commits spec --- spec/requests/api/commits_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb index e8e17228523..1b139bfa6f2 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require 'spec_helper' require 'mime/types' @@ -126,6 +127,12 @@ describe API::Commits do end end + context "with empty ref_name parameter" do + let(:route) { "/projects/#{project_id}/repository/commits?ref_name=" } + + it_behaves_like 'project commits' + end + context "path optional parameter" do it "returns project commits matching provided path parameter" do path = 'files/ruby/popen.rb' -- cgit v1.2.1 From eb35728201b7f7c36979c314b86fdaafc564e190 Mon Sep 17 00:00:00 2001 From: Armin Hohenegger Date: Tue, 13 Aug 2019 23:36:22 +0200 Subject: remove encoding comment created by emacs ruby-mode --- spec/requests/api/commits_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/requests/api/commits_spec.rb b/spec/requests/api/commits_spec.rb index 1b139bfa6f2..5e6ff40e8cf 100644 --- a/spec/requests/api/commits_spec.rb +++ b/spec/requests/api/commits_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 require 'spec_helper' require 'mime/types' -- cgit v1.2.1