Skip to content

Commit 4766c8a

Browse files
authored
Merge pull request #3836 from OpenNeuroOrg/CHANGES-preamble
fix(worker): Allow edits to CHANGES with preambles
2 parents 3e69eac + 677ef3c commit 4766c8a

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

services/datalad/datalad_service/tasks/snapshots.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,20 @@ def edit_changes(changes, new_changes, tag, date):
8181
changelog_lines = changes.rstrip().splitlines()
8282
(start, end) = find_version(changelog_lines, tag)
8383
if start is None:
84-
# add new version
85-
changelog_lines = [*formatted_new_changes, *changelog_lines]
84+
# add new version before the first existing heading
85+
insert_at = 0
86+
for i, line in enumerate(changelog_lines):
87+
if cpan_version_prog.match(line):
88+
insert_at = i
89+
break
90+
else:
91+
# no existing version headings, append after all existing lines
92+
insert_at = len(changelog_lines)
93+
changelog_lines = [
94+
*changelog_lines[:insert_at],
95+
*formatted_new_changes,
96+
*changelog_lines[insert_at:],
97+
]
8698
else:
8799
# update existing version
88100
changelog_lines = [

services/datalad/tests/test_snapshots.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from datalad.api import Dataset
77
import pygit2
88

9-
from datalad_service.tasks.snapshots import write_new_changes
9+
from datalad_service.tasks.snapshots import edit_changes, write_new_changes
1010
from datalad_service.common.git import git_show, git_show_content
1111

1212

@@ -167,6 +167,60 @@ async def test_write_new_changes(datalad_store, new_dataset):
167167
)
168168

169169

170+
def test_edit_changes_with_preamble():
171+
"""New version should be inserted after preamble text, not before it."""
172+
changes = """Preamble text that should be preserved
173+
174+
1.0.0 2025-09-24
175+
- Initial upload
176+
"""
177+
result = edit_changes(changes, ['Added data'], '2.0.0', '2026-03-24')
178+
assert (
179+
result
180+
== """Preamble text that should be preserved
181+
182+
2.0.0 2026-03-24
183+
- Added data
184+
1.0.0 2025-09-24
185+
- Initial upload
186+
"""
187+
)
188+
189+
190+
def test_edit_changes_update_existing_with_preamble():
191+
"""Updating an existing version should preserve preamble."""
192+
changes = """Preamble text that should be preserved
193+
194+
1.0.0 2025-09-24
195+
- Initial upload
196+
"""
197+
result = edit_changes(changes, ['Updated data'], '1.0.0', '2025-09-24')
198+
assert (
199+
result
200+
== """Preamble text that should be preserved
201+
202+
1.0.0 2025-09-24
203+
- Updated data
204+
"""
205+
)
206+
207+
208+
def test_edit_changes_no_preamble():
209+
"""Without preamble, new version is prepended."""
210+
changes = """1.0.0 2025-09-24
211+
- Initial upload
212+
"""
213+
result = edit_changes(changes, ['Added data'], '2.0.0', '2026-03-24')
214+
assert (
215+
result
216+
== """2.0.0 2026-03-24
217+
- Added data
218+
1.0.0 2025-09-24
219+
- Initial upload
220+
"""
221+
)
222+
223+
170224
async def test_write_with_empty_changes(datalad_store, new_dataset):
171225
ds_id = os.path.basename(new_dataset.path)
172226
new_dataset.remove('CHANGES')

0 commit comments

Comments
 (0)