Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(getMostRecentTags): add fetchRemote option to allow disabling remote fetching #199

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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({

Check failure on line 10 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 10 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
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'])

Check failure on line 27 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Insert `;`

Check failure on line 27 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Insert `;`
});
it('should not call git fetch when ', () => {

Check failure on line 29 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

should not have leading or trailing spaces

Check failure on line 29 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

should not have leading or trailing spaces
const cmdMock = jest.fn();
updateChangelog({

Check failure on line 31 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 31 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
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'])

Check failure on line 49 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Insert `;`

Check failure on line 49 in src/update-changelog.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Insert `;`
});
});
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 @@
* 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

Check failure on line 17 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

JSDoc description does not satisfy the regex pattern

Check failure on line 17 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

JSDoc description does not satisfy the regex pattern
* @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)[]>;

Check failure on line 27 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Replace `|` with `·|·`

Check failure on line 27 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Replace `|` with `·|·`
}) {
// 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 @@
* The package rename properties, used in case of package is renamed
*/
packageRename?: PackageRename;
fetchRemote?: boolean;
run?: (cmd: string, args: string[]) => Promise<(string|null)[]>;

Check failure on line 231 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Replace `|` with `·|·`

Check failure on line 231 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Replace `|` with `·|·`
};

/**
Expand All @@ -243,6 +253,8 @@
* @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

Check failure on line 257 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

JSDoc description does not satisfy the regex pattern

Check failure on line 257 in src/update-changelog.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

JSDoc description does not satisfy the regex pattern
* @returns The updated changelog text.
*/
export async function updateChangelog({
Expand All @@ -254,6 +266,8 @@
tagPrefixes = ['v'],
formatter = undefined,
packageRename,
fetchRemote = true,
run,
}: UpdateChangelogOptions): Promise<string | undefined> {
const changelog = parseChangelog({
changelogContent,
Expand All @@ -264,6 +278,8 @@
});

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

Expand Down
Loading