Commit ef776df2 authored by Alejandro Rodríguez's avatar Alejandro Rodríguez

Merge branch 'sh-fix-wrong-commit-count-in-push' into 'master'

Fix wrong commit count in push event payload

Closes #49971

See merge request gitlab-org/gitlab-ce!21338
parents 0e9dc23d bd91f644
......@@ -158,7 +158,7 @@ class GitPushService < BaseService
end
def process_default_branch
offset = [push_commits_count - PROCESS_COMMIT_LIMIT, 0].max
offset = [push_commits_count_for_ref - PROCESS_COMMIT_LIMIT, 0].max
@push_commits = project.repository.commits(params[:newrev], offset: offset, limit: PROCESS_COMMIT_LIMIT)
project.after_create_default_branch
......@@ -172,7 +172,7 @@ class GitPushService < BaseService
params[:newrev],
params[:ref],
@push_commits,
commits_count: push_commits_count)
commits_count: commits_count)
end
def push_to_existing_branch?
......@@ -213,8 +213,14 @@ class GitPushService < BaseService
end
end
def push_commits_count
strong_memoize(:push_commits_count) do
def commits_count
return push_commits_count_for_ref if default_branch? && push_to_new_branch?
Array(@push_commits).size
end
def push_commits_count_for_ref
strong_memoize(:push_commits_count_for_ref) do
project.repository.commit_count_for_ref(params[:ref])
end
end
......
---
title: Fix wrong commit count in push event payload
merge_request: 21338
author:
type: fixed
......@@ -204,16 +204,37 @@ describe GitPushService, services: true do
end
describe "Push Event" do
let!(:push_data) { push_data_from_service(project, user, oldrev, newrev, ref) }
let(:event) { Event.find_by_action(Event::PUSHED) }
context "with an existing branch" do
let!(:push_data) { push_data_from_service(project, user, oldrev, newrev, ref) }
let(:event) { Event.find_by_action(Event::PUSHED) }
it { expect(event).to be_an_instance_of(PushEvent) }
it { expect(event.project).to eq(project) }
it { expect(event.action).to eq(Event::PUSHED) }
it { expect(event.push_event_payload).to be_an_instance_of(PushEventPayload) }
it { expect(event.push_event_payload.commit_from).to eq(oldrev) }
it { expect(event.push_event_payload.commit_to).to eq(newrev) }
it { expect(event.push_event_payload.ref).to eq('master') }
it 'generates a push event with one commit' do
expect(event).to be_an_instance_of(PushEvent)
expect(event.project).to eq(project)
expect(event.action).to eq(Event::PUSHED)
expect(event.push_event_payload).to be_an_instance_of(PushEventPayload)
expect(event.push_event_payload.commit_from).to eq(oldrev)
expect(event.push_event_payload.commit_to).to eq(newrev)
expect(event.push_event_payload.ref).to eq('master')
expect(event.push_event_payload.commit_count).to eq(1)
end
end
context "with a new branch" do
let!(:new_branch_data) { push_data_from_service(project, user, Gitlab::Git::BLANK_SHA, newrev, ref) }
let(:event) { Event.find_by_action(Event::PUSHED) }
it 'generates a push event with more than one commit' do
expect(event).to be_an_instance_of(PushEvent)
expect(event.project).to eq(project)
expect(event.action).to eq(Event::PUSHED)
expect(event.push_event_payload).to be_an_instance_of(PushEventPayload)
expect(event.push_event_payload.commit_from).to be_nil
expect(event.push_event_payload.commit_to).to eq(newrev)
expect(event.push_event_payload.ref).to eq('master')
expect(event.push_event_payload.commit_count).to be > 1
end
end
context "Updates merge requests" do
it "when pushing a new branch for the first time" do
......@@ -224,6 +245,7 @@ describe GitPushService, services: true do
end
describe 'system hooks' do
let!(:push_data) { push_data_from_service(project, user, oldrev, newrev, ref) }
let(:system_hooks_service) { SystemHooksService.new }
it "sends a system hook after pushing a branch" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment