Commit dfa1c397 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'cherry-pick-custom-commit-message' into 'master'

Add support for a custom commit message when cherry-picking commits using the API

See merge request gitlab-org/gitlab!62481
parents 45f9f5e8 5a7d3be2
......@@ -6,6 +6,7 @@ module Commits
super
@commit = params[:commit]
@message = params[:message]
end
private
......@@ -14,7 +15,9 @@ module Commits
raise NotImplementedError unless repository.respond_to?(action)
# rubocop:disable GitlabSecurity/PublicSend
message = @commit.public_send(:"#{action}_message", current_user)
message =
@message || @commit.public_send(:"#{action}_message", current_user)
repository.public_send(
action,
current_user,
......
......@@ -305,6 +305,7 @@ Parameters:
| `sha` | string | yes | The commit hash |
| `branch` | string | yes | The name of the branch |
| `dry_run` | boolean | no | Does not commit any changes. Default is false. [Introduced in GitLab 13.3](https://gitlab.com/gitlab-org/gitlab/-/issues/231032) |
| `message` | string | no | A custom commit message to use for the new commit. [Introduced in GitLab 14.0](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/62481)
```shell
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --form "branch=master" "https://gitlab.example.com/api/v4/projects/5/repository/commits/master/cherry_pick"
......
......@@ -100,6 +100,11 @@ To disable it:
Feature.disable(:pick_into_project)
```
## Related links
- The [Commits API](../../../api/commits.md) enables you to add custom messages
to changes you cherry-pick through the API.
<!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
......
......@@ -203,6 +203,7 @@ module API
requires :sha, type: String, desc: 'A commit sha, or the name of a branch or tag to be cherry picked'
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
optional :dry_run, type: Boolean, default: false, desc: "Does not commit any changes"
optional :message, type: String, desc: 'A custom commit message to use for the picked commit'
end
post ':id/repository/commits/:sha/cherry_pick', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
authorize_push_to_branch!(params[:branch])
......@@ -216,7 +217,8 @@ module API
commit: commit,
start_branch: params[:branch],
branch_name: params[:branch],
dry_run: params[:dry_run]
dry_run: params[:dry_run],
message: params[:message]
}
result = ::Commits::CherryPickService
......
......@@ -1503,6 +1503,13 @@ RSpec.describe API::Commits do
expect(json_response).to eq("dry_run" => "success")
expect(project.commit(branch)).to eq(head)
end
it 'supports the use of a custom commit message' do
post api(route, user), params: { branch: branch, message: 'foo' }
expect(response).to have_gitlab_http_status(:created)
expect(json_response["message"]).to eq('foo')
end
end
context 'when repository is disabled' do
......
......@@ -24,7 +24,7 @@ RSpec.describe Commits::CherryPickService do
repository.add_branch(user, branch_name, merge_base_sha)
end
def cherry_pick(sha, branch_name)
def cherry_pick(sha, branch_name, message: nil)
commit = project.commit(sha)
described_class.new(
......@@ -32,7 +32,8 @@ RSpec.describe Commits::CherryPickService do
user,
commit: commit,
start_branch: branch_name,
branch_name: branch_name
branch_name: branch_name,
message: message
).execute
end
......@@ -45,6 +46,14 @@ RSpec.describe Commits::CherryPickService do
head = repository.find_branch(branch_name).target
expect(head).not_to eq(merge_base_sha)
end
it 'supports a custom commit message' do
result = cherry_pick(merge_commit_sha, branch_name, message: 'foo')
branch = repository.find_branch(branch_name)
expect(result[:status]).to eq(:success)
expect(branch.dereferenced_target.message).to eq('foo')
end
end
it_behaves_like 'successful cherry-pick'
......
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