Skip to content

Commit e74aaa5

Browse files
authored
Gather commits no release contains under an Unreleased heading (#17)
1 parent c8d3b2b commit e74aaa5

4 files changed

Lines changed: 139 additions & 23 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ This works across release lines. For example, with 7.x on `master` and 6.x on a
145145
Merge one release line into another with a merge commit. A squash merge hides the commits, and their pull requests, that it brings in.
146146

147147
The version being prepared (from `package.json`) gets a section for the commits on `--head` that no tag contains yet.
148+
Once that version is tagged those commits are its own, and anything the branch gains afterwards is gathered under an `Unreleased` heading until the next version is prepared, which `template.unreleased` renames.
148149

149150
Without `--tags`, `gren` writes that section and the latest tag `--head` contains, so on a maintenance branch it works on that line's latest release rather than on the highest version in the repository.
150151
`--tags=<new-tag>..<old-tag>` puts everything since the old tag in the new tag's section, taking in the releases between them.

‎lib/src/Gren.js‎

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ class Gren {
905905
return `${rangeBody}\n`;
906906
}
907907

908-
return "*No changelog for this release.*\n";
908+
return `${this.options.template.noChangelog}\n`;
909909
}
910910

911911
/**
@@ -1914,17 +1914,30 @@ class Gren {
19141914
}
19151915

19161916
return entries
1917-
.map(({ release, frozen, verbatim }) =>
1918-
frozen
1919-
? verbatim !== undefined && {
1917+
.map(({ release, frozen, verbatim }) => {
1918+
if (frozen) {
1919+
return (
1920+
verbatim !== undefined && {
19201921
id: release.id,
19211922
release: release.name,
19221923
name: this.options.prefix + release.name,
19231924
published_at: release.date,
19241925
verbatim,
19251926
}
1926-
: blocks.shift(),
1927-
)
1927+
);
1928+
}
1929+
1930+
const block = blocks.shift();
1931+
1932+
if (release.version) {
1933+
return block;
1934+
}
1935+
1936+
// There is no version for the prefix to belong to in an unreleased section, and no
1937+
// reason to write the heading at all when the commits under it have nothing to say:
1938+
// they are in the report either way.
1939+
return block.body !== this._templateBody([]) && { ...block, name: release.name };
1940+
})
19281941
.filter(Boolean);
19291942
}
19301943

@@ -2016,30 +2029,53 @@ class Gren {
20162029
const releases = [];
20172030
const unreleasedVersion = parseVersion(version);
20182031

2019-
if (head && unreleasedVersion && !tags.some((tag) => tag.version === unreleasedVersion)) {
2032+
if (head) {
20202033
if (!git.refExists(head)) {
20212034
throw chalk.red(`\nThe branch "${head}" is not in the local repository.`);
20222035
}
20232036

20242037
const merged = git.tagsMergedInto(head);
2025-
const newer = tags.find(
2026-
(tag) => merged.has(tag.name) && semver.gte(tag.version, unreleasedVersion),
2027-
);
2028-
2029-
if (newer) {
2038+
const taken = unreleasedVersion && tags.some((tag) => tag.version === unreleasedVersion);
2039+
// A version equal to a tag is the ordinary state between releases. A version behind one
2040+
// is a package.json somebody forgot to bump, which is worth saying out loud.
2041+
const behind =
2042+
unreleasedVersion &&
2043+
!taken &&
2044+
tags.find((tag) => merged.has(tag.name) && semver.gt(tag.version, unreleasedVersion));
2045+
2046+
if (behind) {
20302047
console.warn(
20312048
chalk.yellow(
2032-
`\nSkipping the unreleased ${version} section: ${head} already contains ${newer.name}.`,
2049+
`\nThe version being prepared, ${version}, is older than ${behind.name}, which ` +
2050+
`${head} already contains. Commits no tag holds are listed as ` +
2051+
`${this.options.template.unreleased}.`,
20332052
),
20342053
);
2035-
} else {
2054+
}
2055+
2056+
if (unreleasedVersion && !taken && !behind) {
20362057
releases.push({
20372058
id: null,
20382059
name: version,
20392060
version: unreleasedVersion,
20402061
ref: head,
20412062
date: new Date().toISOString(),
20422063
});
2064+
} else {
2065+
// The version being prepared has been released already, so anything the branch has that
2066+
// no tag contains belongs to a release that does not have a name yet.
2067+
const excludeRefs = tags.map(({ ref }) => ref);
2068+
2069+
if (git.releaseCommits(head, excludeRefs).length) {
2070+
releases.push({
2071+
id: null,
2072+
name: this.options.template.unreleased,
2073+
version: null,
2074+
ref: head,
2075+
date: new Date().toISOString(),
2076+
excludeRefs,
2077+
});
2078+
}
20432079
}
20442080
}
20452081

‎lib/src/templates.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ export default {
88
group: "\n### {{heading}}\n",
99
changelogTitle: "# Changelog\n\n",
1010
release: "## {{release}} ({{date}})\n\n{{body}}",
11+
unreleased: "Unreleased",
12+
noChangelog: "*No changelog for this release.*",
1113
releaseSeparator: "\n---\n\n",
1214
};

‎test/Gren.membership.spec.js‎

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,88 @@ describe("Gren release membership", () => {
412412
assert.deepEqual(numbers(byRelease(blocks)["v1.2.0"]), [6, 5, 4, 3]);
413413
});
414414

415-
it("Should skip an unreleased version the branch has already passed", async () => {
415+
it("Should gather commits no tag contains under an unreleased heading", async () => {
416+
// 2.0.0 is released, and master carries c12 beyond every tag.
417+
const blocks = await createGren({ version: "2.0.0" })._getReleaseBlocks();
418+
const unreleased = byRelease(blocks)["Unreleased"];
419+
420+
assert.deepEqual(
421+
blocks.map(({ release }) => release),
422+
["Unreleased", "v2.0.0", "v1.2.1", "v1.2.0", "v1.1.1", "v1.1.0", "v1.0.0"],
423+
);
424+
assert.deepEqual(numbers(unreleased), [9]);
425+
assert.equal(
426+
unreleased.name,
427+
"Unreleased",
428+
"The prefix belongs to a version, and it has none",
429+
);
430+
});
431+
432+
it("Should take the unreleased heading and the empty body from the templates", async () => {
433+
const gren = createGren({
434+
version: "2.0.0",
435+
template: { issue: "{{text}} {{name}} {{url}}", unreleased: "Nog niet uitgebracht" },
436+
});
437+
const blocks = await gren._getReleaseBlocks();
438+
439+
assert.include(
440+
blocks.map(({ release }) => release),
441+
"Nog niet uitgebracht",
442+
);
443+
assert.equal(
444+
createGren({ template: { noChangelog: "_Niets te melden._" } })._templateBody([]),
445+
"_Niets te melden._\n",
446+
);
447+
});
448+
449+
it("Should leave out the unreleased heading when its commits have no pull request", async () => {
450+
// c12 is beyond every tag, but without pull request 9 there is nothing to say about it.
451+
const blocks = await createGren({
452+
version: "2.0.0",
453+
overridePrs: (prs) => prs.filter(({ number }) => number !== 9),
454+
})._getReleaseBlocks();
455+
456+
assert.notInclude(
457+
blocks.map(({ release }) => release),
458+
"Unreleased",
459+
);
460+
assert.include(warnings.join("\n"), repo.sha.c12.slice(0, 10), "It is reported instead");
461+
});
462+
463+
it("Should leave out the unreleased heading when the branch is level with a tag", async () => {
464+
const blocks = await createGren({ head: "1.x", version: "1.2.1" })._getReleaseBlocks();
465+
466+
assert.deepEqual(
467+
blocks.map(({ release }) => release),
468+
["v2.0.0", "v1.2.1", "v1.2.0", "v1.1.1", "v1.1.0", "v1.0.0"],
469+
"Every tag still has a section; 1.x simply has nothing beyond them",
470+
);
471+
});
472+
473+
it("Should still gather them when the branch has passed the version being prepared", async () => {
474+
const blocks = await createGren({ version: "1.1.5" })._getReleaseBlocks();
475+
476+
assert.include(
477+
blocks.map(({ release }) => release),
478+
"Unreleased",
479+
"The work is there whatever package.json says",
480+
);
481+
});
482+
483+
it("Should say when the version being prepared is older than the branch", async () => {
416484
const blocks = await createGren({ version: "1.1.5" })._getReleaseBlocks();
417485

418486
assert.notInclude(
419487
blocks.map(({ release }) => release),
420488
"1.1.5",
421489
);
422-
assert.include(warnings.join("\n"), "Skipping the unreleased 1.1.5 section");
490+
assert.include(warnings.join("\n"), "The version being prepared, 1.1.5, is older than");
491+
});
492+
493+
it("Should say nothing when the version being prepared is simply released", async () => {
494+
await createGren({ version: "2.0.0" })._getReleaseBlocks();
495+
496+
assert.notInclude(warnings.join("\n"), "version being prepared");
423497
});
424498

425499
it("Should generate only the tag given with --tags, or the latest tag", async () => {
@@ -428,7 +502,8 @@ describe("Gren release membership", () => {
428502

429503
assert.deepEqual(
430504
selected.map(({ release }) => release),
431-
["v1.1.1"],
505+
["Unreleased", "v1.1.1"],
506+
"2.0.0 is released, so master's later commits have no version yet",
432507
);
433508
assert.deepEqual(
434509
latest.map(({ release }) => release),
@@ -523,16 +598,18 @@ describe("Gren release membership", () => {
523598
}
524599
});
525600

526-
it("Should not ask about the branch when the version is already tagged", async () => {
527-
// Nothing is read from the branch, so it does not matter where GitHub has it.
601+
it("Should not ask about a branch it has nothing to read from", async () => {
602+
// 1.x is level with v1.2.1, so nothing is read from it and where GitHub has it does not
603+
// matter.
528604
const blocks = await createGren({
529605
headSha: "0".repeat(40),
530-
version: "2.0.0",
606+
head: "1.x",
607+
version: "1.2.1",
531608
})._getReleaseBlocks();
532609

533-
assert.deepEqual(
610+
assert.notInclude(
534611
blocks.map(({ release }) => release),
535-
["v2.0.0", "v1.2.1", "v1.2.0", "v1.1.1", "v1.1.0", "v1.0.0"],
612+
"Unreleased",
536613
);
537614
});
538615

@@ -563,7 +640,7 @@ describe("Gren release membership", () => {
563640

564641
try {
565642
// Every tag is here and points where GitHub says, but the commits between them are not.
566-
await createGren({ tags: "v1.2.0", version: "1.2.0" })._getReleaseBlocks();
643+
await createGren({ tags: "v1.2.0", version: "1.2.0", head: "HEAD" })._getReleaseBlocks();
567644
assert.fail("A shallow clone should stop the changelog");
568645
} catch (error) {
569646
assert.include(String(error), "truncated history");

0 commit comments

Comments
 (0)