Commit 5a7d3be2 authored by Yorick Peterse's avatar Yorick Peterse

Support custom messages in the cherry-pick API

The API used for cherry-picking commits into a branch now supports the
use of a custom commit message. Leaving out a custom message results in
one being generated for you (as before).

See https://gitlab.com/gitlab-org/release-tools/-/issues/500 for more
information.

Changelog: added
parent 50022004
...@@ -6,6 +6,7 @@ module Commits ...@@ -6,6 +6,7 @@ module Commits
super super
@commit = params[:commit] @commit = params[:commit]
@message = params[:message]
end end
private private
...@@ -14,7 +15,9 @@ module Commits ...@@ -14,7 +15,9 @@ module Commits
raise NotImplementedError unless repository.respond_to?(action) raise NotImplementedError unless repository.respond_to?(action)
# rubocop:disable GitlabSecurity/PublicSend # rubocop:disable GitlabSecurity/PublicSend
message = @commit.public_send(:"#{action}_message", current_user) message =
@message || @commit.public_send(:"#{action}_message", current_user)
repository.public_send( repository.public_send(
action, action,
current_user, current_user,
......
...@@ -305,6 +305,7 @@ Parameters: ...@@ -305,6 +305,7 @@ Parameters:
| `sha` | string | yes | The commit hash | | `sha` | string | yes | The commit hash |
| `branch` | string | yes | The name of the branch | | `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) | | `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 ```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" 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: ...@@ -100,6 +100,11 @@ To disable it:
Feature.disable(:pick_into_project) 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 <!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues Include any troubleshooting steps that you can foresee. If you know beforehand what issues
......
...@@ -203,6 +203,7 @@ module API ...@@ -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 :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 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 :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 end
post ':id/repository/commits/:sha/cherry_pick', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do post ':id/repository/commits/:sha/cherry_pick', requirements: API::COMMIT_ENDPOINT_REQUIREMENTS do
authorize_push_to_branch!(params[:branch]) authorize_push_to_branch!(params[:branch])
...@@ -216,7 +217,8 @@ module API ...@@ -216,7 +217,8 @@ module API
commit: commit, commit: commit,
start_branch: params[:branch], start_branch: params[:branch],
branch_name: params[:branch], branch_name: params[:branch],
dry_run: params[:dry_run] dry_run: params[:dry_run],
message: params[:message]
} }
result = ::Commits::CherryPickService result = ::Commits::CherryPickService
......
...@@ -1503,6 +1503,13 @@ RSpec.describe API::Commits do ...@@ -1503,6 +1503,13 @@ RSpec.describe API::Commits do
expect(json_response).to eq("dry_run" => "success") expect(json_response).to eq("dry_run" => "success")
expect(project.commit(branch)).to eq(head) expect(project.commit(branch)).to eq(head)
end 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 end
context 'when repository is disabled' do context 'when repository is disabled' do
......
...@@ -24,7 +24,7 @@ RSpec.describe Commits::CherryPickService do ...@@ -24,7 +24,7 @@ RSpec.describe Commits::CherryPickService do
repository.add_branch(user, branch_name, merge_base_sha) repository.add_branch(user, branch_name, merge_base_sha)
end end
def cherry_pick(sha, branch_name) def cherry_pick(sha, branch_name, message: nil)
commit = project.commit(sha) commit = project.commit(sha)
described_class.new( described_class.new(
...@@ -32,7 +32,8 @@ RSpec.describe Commits::CherryPickService do ...@@ -32,7 +32,8 @@ RSpec.describe Commits::CherryPickService do
user, user,
commit: commit, commit: commit,
start_branch: branch_name, start_branch: branch_name,
branch_name: branch_name branch_name: branch_name,
message: message
).execute ).execute
end end
...@@ -45,6 +46,14 @@ RSpec.describe Commits::CherryPickService do ...@@ -45,6 +46,14 @@ RSpec.describe Commits::CherryPickService do
head = repository.find_branch(branch_name).target head = repository.find_branch(branch_name).target
expect(head).not_to eq(merge_base_sha) expect(head).not_to eq(merge_base_sha)
end 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 end
it_behaves_like 'successful cherry-pick' 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