Commit 9a5c22a2 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'changelog-api-to-optional' into 'master'

Make `to` in the changelog API optional

See merge request gitlab-org/gitlab!54229
parents 5aecf245 f5045d8c
......@@ -39,10 +39,10 @@ module Repositories
project,
user,
version:,
to:,
branch: project.default_branch_or_master,
from: nil,
to: branch,
date: DateTime.now,
branch: project.default_branch_or_master,
trailer: DEFAULT_TRAILER,
file: DEFAULT_FILE,
message: "Add changelog for version #{version}"
......
---
title: Make `to` in the changelog API optional
merge_request: 54229
author:
type: added
......@@ -303,7 +303,7 @@ Supported attributes:
| :-------- | :------- | :--------- | :---------- |
| `version` | string | yes | The version to generate the changelog for. The format must follow [semantic versioning](https://semver.org/). |
| `from` | string | no | The start of the range of commits (as a SHA) to use for generating the changelog. This commit itself isn't included in the list. |
| `to` | string | yes | The end of the range of commits (as a SHA) to use for the changelog. This commit _is_ included in the list. |
| `to` | string | no | The end of the range of commits (as a SHA) to use for the changelog. This commit _is_ included in the list. Defaults to the branch specified in the `branch` attribute. |
| `date` | datetime | no | The date and time of the release, defaults to the current time. |
| `branch` | string | no | The branch to commit the changelog changes to, defaults to the project's default branch. |
| `trailer` | string | no | The Git trailer to use for including commits, defaults to `Changelog`. |
......
......@@ -184,7 +184,7 @@ module API
type: String,
desc: 'The first commit in the range of commits to use for the changelog'
requires :to,
optional :to,
type: String,
desc: 'The last commit in the range of commits to use for the changelog'
......
......@@ -650,6 +650,40 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok)
end
it 'supports leaving out the from and to attribute' do
spy = instance_spy(Repositories::ChangelogService)
allow(Repositories::ChangelogService)
.to receive(:new)
.with(
project,
user,
version: '1.0.0',
date: DateTime.new(2020, 1, 1),
branch: 'kittens',
trailer: 'Foo',
file: 'FOO.md',
message: 'Commit message'
)
.and_return(spy)
expect(spy).to receive(:execute)
post(
api("/projects/#{project.id}/repository/changelog", user),
params: {
version: '1.0.0',
date: '2020-01-01',
branch: 'kittens',
trailer: 'Foo',
file: 'FOO.md',
message: 'Commit message'
}
)
expect(response).to have_gitlab_http_status(:ok)
end
it 'produces an error when generating the changelog fails' do
spy = instance_spy(Repositories::ChangelogService)
......
......@@ -4,17 +4,12 @@ require 'spec_helper'
RSpec.describe Repositories::ChangelogService do
describe '#execute' do
it 'generates and commits a changelog section' do
project = create(:project, :empty_repo)
creator = project.creator
author1 = create(:user)
author2 = create(:user)
project.add_maintainer(author1)
project.add_maintainer(author2)
mr1 = create(:merge_request, :merged, target_project: project)
mr2 = create(:merge_request, :merged, target_project: project)
let!(:project) { create(:project, :empty_repo) }
let!(:creator) { project.creator }
let!(:author1) { create(:user) }
let!(:author2) { create(:user) }
let!(:mr1) { create(:merge_request, :merged, target_project: project) }
let!(:mr2) { create(:merge_request, :merged, target_project: project) }
# The range of commits ignores the first commit, but includes the last
# commit. To ensure both the commits below are included, we must create an
......@@ -22,30 +17,51 @@ RSpec.describe Repositories::ChangelogService do
#
# In the real world, the start commit of the range will be the last commit
# of the previous release, so ignoring that is expected and desired.
sha1 = create_commit(
let!(:sha1) do
create_commit(
project,
creator,
commit_message: 'Initial commit',
actions: [{ action: 'create', content: 'test', file_path: 'README.md' }]
)
end
let!(:sha2) do
project.add_maintainer(author1)
sha2 = create_commit(
create_commit(
project,
author1,
commit_message: "Title 1\n\nChangelog: feature",
actions: [{ action: 'create', content: 'foo', file_path: 'a.txt' }]
)
end
sha3 = create_commit(
let!(:sha3) do
project.add_maintainer(author2)
create_commit(
project,
author2,
commit_message: "Title 2\n\nChangelog: feature",
actions: [{ action: 'create', content: 'bar', file_path: 'b.txt' }]
)
end
commit1 = project.commit(sha2)
commit2 = project.commit(sha3)
let!(:sha4) do
create_commit(
project,
author2,
commit_message: "Title 3\n\nChangelog: feature",
actions: [{ action: 'create', content: 'bar', file_path: 'c.txt' }]
)
end
let!(:commit1) { project.commit(sha2) }
let!(:commit2) { project.commit(sha3) }
let!(:commit3) { project.commit(sha4) }
it 'generates and commits a changelog section' do
allow(MergeRequestDiffCommit)
.to receive(:oldest_merge_request_id_per_commit)
.with(project.id, [commit2.id, commit1.id])
......@@ -54,17 +70,33 @@ RSpec.describe Repositories::ChangelogService do
{ sha: sha3, merge_request_id: mr2.id }
])
recorder = ActiveRecord::QueryRecorder.new do
described_class
service = described_class
.new(project, creator, version: '1.0.0', from: sha1, to: sha3)
.execute
end
recorder = ActiveRecord::QueryRecorder.new { service.execute }
changelog = project.repository.blob_at('master', 'CHANGELOG.md')&.data
expect(recorder.count).to eq(10)
expect(changelog).to include('Title 1', 'Title 2')
end
it 'uses the target branch when "to" is unspecified' do
allow(MergeRequestDiffCommit)
.to receive(:oldest_merge_request_id_per_commit)
.with(project.id, [commit3.id, commit2.id, commit1.id])
.and_return([
{ sha: sha2, merge_request_id: mr1.id },
{ sha: sha3, merge_request_id: mr2.id }
])
described_class
.new(project, creator, version: '1.0.0', from: sha1)
.execute
changelog = project.repository.blob_at('master', 'CHANGELOG.md')&.data
expect(changelog).to include('Title 1', 'Title 2', 'Title 3')
end
end
describe '#start_of_commit_range' 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