Commit a139e6b8 authored by Alessio Caiazza's avatar Alessio Caiazza Committed by Mayra Cabrera

Annotate merge request cherry-picks

When picking a merge_commit, it will be annotated with a system note.

This feature is behind the :track_mr_picking feature flag
parent 1bd45663
......@@ -2,6 +2,7 @@
module SystemNoteHelper
ICON_NAMES_BY_ACTION = {
'cherry_pick' => 'link',
'commit' => 'commit',
'description' => 'pencil-square',
'merge' => 'git-merge',
......
......@@ -228,6 +228,9 @@ class MergeRequest < ApplicationRecord
scope :by_merge_commit_sha, -> (sha) do
where(merge_commit_sha: sha)
end
scope :by_cherry_pick_sha, -> (sha) do
joins(:notes).where(notes: { commit_id: sha })
end
scope :join_project, -> { joins(:target_project) }
scope :references_project, -> { references(:target_project) }
scope :with_api_entity_associations, -> {
......
......@@ -17,7 +17,7 @@ class SystemNoteMetadata < ApplicationRecord
commit description merge confidential visible label assignee cross_reference
title time_tracking branch milestone discussion task moved
opened closed merged duplicate locked unlocked
outdated tag due_date pinned_embed
outdated tag due_date pinned_embed cherry_pick
].freeze
validates :note, presence: true
......
......@@ -3,7 +3,24 @@
module Commits
class CherryPickService < ChangeService
def create_commit!
commit_change(:cherry_pick)
commit_change(:cherry_pick).tap do |sha|
track_mr_picking(sha)
end
end
private
def track_mr_picking(pick_sha)
return unless Feature.enabled?(:track_mr_picking, project)
merge_request = project.merge_requests.by_merge_commit_sha(@commit.sha).first
return unless merge_request
::SystemNotes::MergeRequestsService.new(
noteable: merge_request,
project: project,
author: current_user
).picked_into_branch(@branch_name, pick_sha)
end
end
end
......@@ -38,6 +38,8 @@ module Deployments
.commits_between(from, to)
.map(&:id)
track_mr_picking = Feature.enabled?(:track_mr_picking, project)
# For some projects the list of commits to deploy may be very large. To
# ensure we do not end up running SQL queries with thousands of WHERE IN
# values, we run one query per a certain number of commits.
......@@ -50,6 +52,13 @@ module Deployments
project.merge_requests.merged.by_merge_commit_sha(slice)
deployment.link_merge_requests(merge_requests)
next unless track_mr_picking
picked_merge_requests =
project.merge_requests.by_cherry_pick_sha(slice)
deployment.link_merge_requests(picked_merge_requests)
end
end
......
......@@ -139,6 +139,17 @@ module SystemNotes
create_note(NoteSummary.new(noteable, project, author, body, action: 'merge'))
end
def picked_into_branch(branch_name, pick_commit)
link = url_helpers.project_tree_path(project, branch_name)
body = "picked this merge request into branch [`#{branch_name}`](#{link}) with commit #{pick_commit}"
summary = NoteSummary.new(noteable, project, author, body, action: 'cherry_pick')
summary.note[:commit_id] = pick_commit
create_note(summary)
end
end
end
......
......@@ -29,6 +29,11 @@ FactoryBot.define do
end
end
factory :track_mr_picking_note, traits: [:on_merge_request, :system] do
association :system_note_metadata, action: 'cherry_pick'
commit_id { RepoHelpers.sample_commit.id }
end
factory :discussion_note_on_issue, traits: [:on_issue], class: 'DiscussionNote'
factory :discussion_note_on_commit, traits: [:on_commit], class: 'DiscussionNote'
......
......@@ -327,6 +327,16 @@ describe MergeRequest do
end
end
describe '.by_cherry_pick_sha' do
it 'returns merge requests that match the given merge commit' do
note = create(:track_mr_picking_note, commit_id: '456abc')
create(:track_mr_picking_note, commit_id: '456def')
expect(described_class.by_cherry_pick_sha('456abc')).to eq([note.noteable])
end
end
describe '.in_projects' do
it 'returns the merge requests for a set of projects' do
expect(described_class.in_projects(Project.all)).to eq([subject])
......
......@@ -122,7 +122,29 @@ describe API::Deployments do
describe 'POST /projects/:id/deployments' do
let!(:project) { create(:project, :repository) }
let(:sha) { 'b83d6e391c22777fca1ed3012fce84f633d7fed0' }
# * ddd0f15ae83993f5cb66a927a28673882e99100b (HEAD -> master, origin/master, origin/HEAD) Merge branch 'po-fix-test-en
# |\
# | * 2d1db523e11e777e49377cfb22d368deec3f0793 Correct test_env.rb path for adding branch
# |/
# * 1e292f8fedd741b75372e19097c76d327140c312 Merge branch 'cherry-pick-ce369011' into 'master'
let_it_be(:sha) { 'ddd0f15ae83993f5cb66a927a28673882e99100b' }
let_it_be(:first_deployment_sha) { '1e292f8fedd741b75372e19097c76d327140c312' }
before do
# Creating the first deployment is an edge-case that is already covered by unit testing,
# here we want to see the behavior of a running system so we create a first deployment
post(
api("/projects/#{project.id}/deployments", user),
params: {
environment: 'production',
sha: first_deployment_sha,
ref: 'master',
tag: false,
status: 'success'
}
)
end
context 'as a maintainer' do
it 'creates a new deployment' do
......@@ -163,6 +185,7 @@ describe API::Deployments do
mr = create(
:merge_request,
:merged,
merge_commit_sha: sha,
target_project: project,
source_project: project,
target_branch: 'master',
......@@ -215,6 +238,7 @@ describe API::Deployments do
mr = create(
:merge_request,
:merged,
merge_commit_sha: sha,
target_project: project,
source_project: project,
target_branch: 'master',
......@@ -236,6 +260,43 @@ describe API::Deployments do
expect(deploy.merge_requests).to eq([mr])
end
it 'links any picked merge requests to the deployment', :sidekiq_inline do
mr = create(
:merge_request,
:merged,
merge_commit_sha: sha,
target_project: project,
source_project: project,
target_branch: 'master',
source_branch: 'foo'
)
# we branch from the previous deployment and cherry-pick mr into the new branch
branch = project.repository.add_branch(developer, 'stable', first_deployment_sha)
expect(branch).not_to be_nil
result = ::Commits::CherryPickService
.new(project, developer, commit: mr.merge_commit, start_branch: 'stable', branch_name: 'stable')
.execute
expect(result[:status]).to eq(:success), result[:message]
pick_sha = result[:result]
post(
api("/projects/#{project.id}/deployments", developer),
params: {
environment: 'production',
sha: pick_sha,
ref: 'stable',
tag: false,
status: 'success'
}
)
deploy = project.deployments.last
expect(deploy.merge_requests).to eq([mr])
end
end
context 'as non member' do
......
# frozen_string_literal: true
require 'spec_helper'
describe Commits::CherryPickService do
let(:project) { create(:project, :repository) }
# * ddd0f15ae83993f5cb66a927a28673882e99100b (HEAD -> master, origin/master, origin/HEAD) Merge branch 'po-fix-test-en
# |\
# | * 2d1db523e11e777e49377cfb22d368deec3f0793 Correct test_env.rb path for adding branch
# |/
# * 1e292f8fedd741b75372e19097c76d327140c312 Merge branch 'cherry-pick-ce369011' into 'master'
let_it_be(:merge_commit_sha) { 'ddd0f15ae83993f5cb66a927a28673882e99100b' }
let_it_be(:merge_base_sha) { '1e292f8fedd741b75372e19097c76d327140c312' }
let_it_be(:branch_name) { 'stable' }
let(:repository) { project.repository }
let(:commit) { project.commit }
let(:user) { create(:user) }
before do
project.add_maintainer(user)
repository.add_branch(user, branch_name, merge_base_sha)
end
def cherry_pick(sha, branch_name)
commit = project.commit(sha)
described_class.new(
project,
user,
commit: commit,
start_branch: branch_name,
branch_name: branch_name
).execute
end
describe '#execute' do
shared_examples 'successful cherry-pick' do
it 'picks the commit into the branch' do
result = cherry_pick(merge_commit_sha, branch_name)
expect(result[:status]).to eq(:success), result[:message]
head = repository.find_branch(branch_name).target
expect(head).not_to eq(merge_base_sha)
end
end
it_behaves_like 'successful cherry-pick'
context 'when picking a merge-request' do
let!(:merge_request) { create(:merge_request, :simple, :merged, author: user, source_project: project, merge_commit_sha: merge_commit_sha) }
it_behaves_like 'successful cherry-pick'
it 'adds a system note' do
result = cherry_pick(merge_commit_sha, branch_name)
mr_notes = find_cherry_pick_notes(merge_request)
expect(mr_notes.length).to eq(1)
expect(mr_notes[0].commit_id).to eq(result[:result])
end
context 'when :track_mr_picking feature flag is disabled' do
before do
stub_feature_flags(track_mr_picking: false)
end
it 'does not add system notes' do
expect do
cherry_pick(merge_commit_sha, branch_name)
end.not_to change { Note.count }
end
end
end
def find_cherry_pick_notes(noteable)
noteable
.notes
.joins(:system_note_metadata)
.where(system_note_metadata: { action: 'cherry_pick' })
end
end
end
......@@ -5,6 +5,19 @@ require 'spec_helper'
describe Deployments::LinkMergeRequestsService do
let(:project) { create(:project, :repository) }
# * ddd0f15 Merge branch 'po-fix-test-env-path' into 'master'
# |\
# | * 2d1db52 Correct test_env.rb path for adding branch
# |/
# * 1e292f8 Merge branch 'cherry-pick-ce369011' into 'master'
# |\
# | * c1c67ab Add file with a _flattable_ path
# |/
# * 7975be0 Merge branch 'rd-add-file-larger-than-1-mb' into 'master'
let_it_be(:first_deployment_sha) { '7975be0116940bf2ad4321f79d02a55c5f7779aa' }
let_it_be(:mr1_merge_commit_sha) { '1e292f8fedd741b75372e19097c76d327140c312' }
let_it_be(:mr2_merge_commit_sha) { 'ddd0f15ae83993f5cb66a927a28673882e99100b' }
describe '#execute' do
context 'when the deployment is for a review environment' do
it 'does nothing' do
......@@ -25,7 +38,7 @@ describe Deployments::LinkMergeRequestsService do
:deployment,
:success,
project: project,
sha: '7975be0116940bf2ad4321f79d02a55c5f7779aa'
sha: first_deployment_sha
)
deploy2 = create(
......@@ -33,17 +46,14 @@ describe Deployments::LinkMergeRequestsService do
:success,
project: deploy1.project,
environment: deploy1.environment,
sha: 'ddd0f15ae83993f5cb66a927a28673882e99100b'
sha: mr2_merge_commit_sha
)
service = described_class.new(deploy2)
expect(service)
.to receive(:link_merge_requests_for_range)
.with(
'7975be0116940bf2ad4321f79d02a55c5f7779aa',
'ddd0f15ae83993f5cb66a927a28673882e99100b'
)
.with(first_deployment_sha, mr2_merge_commit_sha)
service.execute
end
......@@ -70,7 +80,7 @@ describe Deployments::LinkMergeRequestsService do
mr1 = create(
:merge_request,
:merged,
merge_commit_sha: '1e292f8fedd741b75372e19097c76d327140c312',
merge_commit_sha: mr1_merge_commit_sha,
source_project: project,
target_project: project
)
......@@ -78,18 +88,97 @@ describe Deployments::LinkMergeRequestsService do
mr2 = create(
:merge_request,
:merged,
merge_commit_sha: '2d1db523e11e777e49377cfb22d368deec3f0793',
merge_commit_sha: mr2_merge_commit_sha,
source_project: project,
target_project: project
)
described_class.new(deploy).link_merge_requests_for_range(
'7975be0116940bf2ad4321f79d02a55c5f7779aa',
'ddd0f15ae83993f5cb66a927a28673882e99100b'
first_deployment_sha,
mr2_merge_commit_sha
)
expect(deploy.merge_requests).to include(mr1, mr2)
end
it 'links picked merge requests' do
environment = create(:environment, project: project)
deploy =
create(:deployment, :success, project: project, environment: environment)
picked_mr = create(
:merge_request,
:merged,
merge_commit_sha: '123abc',
source_project: project,
target_project: project
)
mr1 = create(
:merge_request,
:merged,
merge_commit_sha: mr1_merge_commit_sha,
source_project: project,
target_project: project
)
# mr1 includes c1c67abba which is a cherry-pick of the fake picked_mr merge request
create(:track_mr_picking_note, noteable: picked_mr, project: project, commit_id: 'c1c67abbaf91f624347bb3ae96eabe3a1b742478')
described_class.new(deploy).link_merge_requests_for_range(
first_deployment_sha,
mr1_merge_commit_sha
)
expect(deploy.merge_requests).to include(mr1, picked_mr)
end
context 'when :track_mr_picking feature flag is disabled' do
before do
stub_feature_flags(track_mr_picking: false)
end
it 'does not link picked merge requests' do
environment = create(:environment, project: project)
deploy =
create(:deployment, :success, project: project, environment: environment)
picked_mr = create(
:merge_request,
:merged,
merge_commit_sha: '123abc',
source_project: project,
target_project: project
)
mr1 = create(
:merge_request,
:merged,
merge_commit_sha: mr1_merge_commit_sha,
source_project: project,
target_project: project
)
# mr1 includes c1c67abba which is a cherry-pick of the fake picked_mr merge request
create(:track_mr_picking_note, noteable: picked_mr, project: project, commit_id: 'c1c67abbaf91f624347bb3ae96eabe3a1b742478')
mr2 = create(
:merge_request,
:merged,
merge_commit_sha: mr2_merge_commit_sha,
source_project: project,
target_project: project
)
described_class.new(deploy).link_merge_requests_for_range(
first_deployment_sha,
mr2_merge_commit_sha
)
expect(deploy.merge_requests).to include(mr1, mr2)
expect(deploy.merge_requests).not_to include(picked_mr)
end
end
end
describe '#link_all_merged_merge_requests' do
......
......@@ -240,4 +240,25 @@ describe ::SystemNotes::MergeRequestsService do
expect(subject.note).to eq("created merge request #{merge_request.to_reference(project)} to address this issue")
end
end
describe '.picked_into_branch' do
let(:branch_name) { 'a-branch' }
let(:commit_sha) { project.commit.sha }
let(:merge_request) { noteable }
subject { service.picked_into_branch(branch_name, commit_sha) }
it_behaves_like 'a system note' do
let(:action) { 'cherry_pick' }
end
it "posts the 'picked merge request' system note" do
expect(subject.note).to eq("picked this merge request into branch [`#{branch_name}`](/#{project.full_path}/tree/#{branch_name}) with commit #{commit_sha}")
end
it 'links the merge request and the cherry-pick commit' do
expect(subject.noteable).to eq(merge_request)
expect(subject.commit_id).to eq(commit_sha)
end
end
end
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