Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/update-changelog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import _outdent from 'outdent';

import { updateChangelog } from './update-changelog';

const outdent = _outdent({ trimTrailingNewline: false });

describe('updateChangelog', () => {
it('should call git fetch by default', () => {
const cmdMock = jest.fn();
updateChangelog({
changelogContent: outdent`
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

[Unreleased]:https://github.com/ExampleUsernameOrOrganization/ExampleRepository/
`,
isReleaseCandidate: true,
repoUrl:
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
run: cmdMock,
});
expect(cmdMock).toHaveBeenCalledWith('git', ['fetch', '--tags'])
});
it('should not call git fetch when ', () => {
const cmdMock = jest.fn();
updateChangelog({
changelogContent: outdent`
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

[Unreleased]:https://github.com/ExampleUsernameOrOrganization/ExampleRepository/
`,
isReleaseCandidate: true,
repoUrl:
'https://github.com/ExampleUsernameOrOrganization/ExampleRepository',
fetchRemote: false,
run: cmdMock,
});
expect(cmdMock).not.toHaveBeenCalledWith(['git'], ['fetch', '--tags'])
});
});
18 changes: 17 additions & 1 deletion src/update-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ import { PackageRename } from './shared-types';
* Get the most recent tag for a project.
*
* @param options - Options.
* @param options.fetchRemote - Whether to synchronize local tags with remote.
* @param options.tagPrefixes - A list of tag prefixes to look for, where the first is the intended
* prefix and each subsequent prefix is a fallback in case the previous tag prefixes are not found.
* @param options.run - Optional alternative shell command interpreter
* @returns The most recent tag.
*/
async function getMostRecentTag({
fetchRemote,
tagPrefixes,
run = runCommand,
}: {
fetchRemote?: boolean;
tagPrefixes: [string, ...string[]];
run?: (cmd: string, args: string[]) => Promise<(string|null)[]>;
}) {
// Ensure we have all tags on remote
await runCommand('git', ['fetch', '--tags']);
if (typeof fetchRemote !== 'boolean' || fetchRemote) {
await run('git', ['fetch', '--tags']);
}

let mostRecentTagCommitHash: string | null = null;
for (const tagPrefix of tagPrefixes) {
Expand Down Expand Up @@ -219,6 +227,8 @@ export type UpdateChangelogOptions = {
* The package rename properties, used in case of package is renamed
*/
packageRename?: PackageRename;
fetchRemote?: boolean;
run?: (cmd: string, args: string[]) => Promise<(string|null)[]>;
};

/**
Expand All @@ -243,6 +253,8 @@ export type UpdateChangelogOptions = {
* @param options.formatter - A custom Markdown formatter to use.
* @param options.packageRename - The package rename properties.
* An optional, which is required only in case of package renamed.
* @param options.fetchRemote - Whether to synchronize local tags with remote.
* @param options.run - Optional alternative shell command interpreter
* @returns The updated changelog text.
*/
export async function updateChangelog({
Expand All @@ -254,6 +266,8 @@ export async function updateChangelog({
tagPrefixes = ['v'],
formatter = undefined,
packageRename,
fetchRemote = true,
run,
}: UpdateChangelogOptions): Promise<string | undefined> {
const changelog = parseChangelog({
changelogContent,
Expand All @@ -264,6 +278,8 @@ export async function updateChangelog({
});

const mostRecentTag = await getMostRecentTag({
fetchRemote,
run,
tagPrefixes,
});

Expand Down