Skip to content

Commit

Permalink
refactor(ng-dev/release): use ReleaseNotes appendEntryToChangelog
Browse files Browse the repository at this point in the history
… to update changelogs during release.

Use the appendEntryToChangelog method to update the changelogs, this will enable us to have the logic
of managing the changelog file exist in the ReleaseNotes class rather than having the release tool
have to understand how to stitch the two together.
  • Loading branch information
josephperrott committed Sep 8, 2021
1 parent 85482f5 commit 405f2b3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 42 deletions.
8 changes: 4 additions & 4 deletions ng-dev/release/notes/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {ReleaseNotes} from './release-notes';
export interface Options {
from: string;
to: string;
updateChangelog: boolean;
prependToChangelog: boolean;
releaseVersion: SemVer;
type: 'github-release' | 'changelog';
}
Expand Down Expand Up @@ -45,19 +45,19 @@ function builder(argv: Argv): Argv<Options> {
choices: ['github-release', 'changelog'] as const,
default: 'changelog' as const,
})
.option('updateChangelog', {
.option('prependToChangelog', {
type: 'boolean',
default: false,
description: 'Whether to update the changelog with the newly created entry',
});
}

/** Yargs command handler for generating release notes. */
async function handler({releaseVersion, from, to, updateChangelog, type}: Arguments<Options>) {
async function handler({releaseVersion, from, to, prependToChangelog, type}: Arguments<Options>) {
/** The ReleaseNotes instance to generate release notes. */
const releaseNotes = await ReleaseNotes.forRange(releaseVersion, from, to);

if (updateChangelog) {
if (prependToChangelog) {
await releaseNotes.prependEntryToChangelog();
info(`Added release notes for "${releaseVersion}" to the changelog`);
return;
Expand Down
24 changes: 15 additions & 9 deletions ng-dev/release/notes/release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {CommitFromGitLog} from '../../commit-message/parse';
import {promptInput} from '../../utils/console';
import {formatFiles} from '../../format/format';
import {GitClient} from '../../utils/git/git-client';
import {assertValidReleaseConfig, ReleaseConfig, ReleaseNotesConfig} from '../config/index';
import {assertValidReleaseConfig, ReleaseConfig} from '../config/index';
import {RenderContext} from './context';

import changelogTemplate from './templates/changelog';
Expand All @@ -23,6 +23,9 @@ import {existsSync, readFileSync, writeFileSync} from 'fs';
import {join} from 'path';
import {assertValidFormatConfig} from '../../format/config';

/** Project-relative path for the changelog file. */
export const changelogPath = 'CHANGELOG.md';

/** Release note generation. */
export class ReleaseNotes {
static async forRange(version: semver.SemVer, baseRef: string, headRef: string) {
Expand All @@ -31,6 +34,8 @@ export class ReleaseNotes {
return new ReleaseNotes(version, commits, git);
}

/** The absolute path to the changelog file. */
private changelogPath = join(this.git.baseDir, changelogPath);
/** The RenderContext to be used during rendering. */
private renderContext: RenderContext | undefined;
/** The title to use for the release. */
Expand All @@ -39,7 +44,7 @@ export class ReleaseNotes {
private config: {release: ReleaseConfig} = getConfig([assertValidReleaseConfig]);
/** The configuration for the release notes. */
private get notesConfig() {
return this.config.release.releaseNotes || {};
return this.config.release.releaseNotes ?? {};
}

protected constructor(
Expand All @@ -60,23 +65,24 @@ export class ReleaseNotes {
return render(changelogTemplate, await this.generateRenderContext(), {rmWhitespace: true});
}

/** Prepends the generated release note to the CHANGELOG file. */
/**
* Prepend generated release note to the CHANGELOG.md file in the base directory of the repository
* provided by the GitClient.
*/
async prependEntryToChangelog() {
/** The fully path to the changelog file. */
const filePath = join(this.git.baseDir, 'CHANGELOG.md');
/** The changelog contents in the current changelog. */
let changelog = '';
if (existsSync(filePath)) {
changelog = readFileSync(filePath, {encoding: 'utf8'});
if (existsSync(this.changelogPath)) {
changelog = readFileSync(this.changelogPath, {encoding: 'utf8'});
}
/** The new changelog entry to add to the changelog. */
const entry = await this.getChangelogEntry();

writeFileSync(filePath, `${entry}\n\n${changelog}`);
writeFileSync(this.changelogPath, `${entry}\n\n${changelog}`);

try {
assertValidFormatConfig(this.config);
await formatFiles([filePath]);
await formatFiles([this.changelogPath]);
} catch {
// If the formatting is either unavailable or fails, continue on with the unformatted result.
}
Expand Down
14 changes: 3 additions & 11 deletions ng-dev/release/publish/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@ import {
} from '../../utils/git/github-urls';
import {createExperimentalSemver} from '../../utils/semver';
import {BuiltPackage, ReleaseConfig} from '../config/index';
import {ReleaseNotes} from '../notes/release-notes';
import {changelogPath, ReleaseNotes} from '../notes/release-notes';
import {NpmDistTag} from '../versioning';
import {ActiveReleaseTrains} from '../versioning/active-release-trains';
import {runNpmPublish} from '../versioning/npm-publish';

import {FatalReleaseActionError, UserAbortedReleaseActionError} from './actions-error';
import {getCommitMessageForRelease, getReleaseNoteCherryPickCommitMessage} from './commit-message';
import {
changelogPath,
githubReleaseBodyLimit,
packageJsonPath,
waitForPullRequestInterval,
} from './constants';
import {githubReleaseBodyLimit, packageJsonPath, waitForPullRequestInterval} from './constants';
import {invokeReleaseBuildCommand, invokeYarnInstallCommand} from './external-commands';
import {findOwnedForksOfRepoQuery} from './graphql-queries';
import {getPullRequestState} from './pull-request-state';
Expand Down Expand Up @@ -372,10 +367,7 @@ export abstract class ReleaseAction {
* @returns A boolean indicating whether the release notes have been prepended.
*/
protected async prependReleaseNotesToChangelog(releaseNotes: ReleaseNotes): Promise<void> {
const localChangelogPath = join(this.projectDir, changelogPath);
const localChangelog = await fs.readFile(localChangelogPath, 'utf8');
const releaseNotesEntry = await releaseNotes.getChangelogEntry();
await fs.writeFile(localChangelogPath, `${releaseNotesEntry}\n\n${localChangelog}`);
await releaseNotes.prependEntryToChangelog();
info(green(` ✓ Updated the changelog to capture changes for "${releaseNotes.version}".`));
}

Expand Down
4 changes: 2 additions & 2 deletions ng-dev/release/publish/actions/branch-off-next-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as semver from 'semver';

import {green, info, yellow} from '../../../utils/console';
import {semverInc} from '../../../utils/semver';
import {ReleaseNotes} from '../../notes/release-notes';
import {changelogPath, ReleaseNotes} from '../../notes/release-notes';
import {
computeNewPrereleaseVersionForNext,
getReleaseNotesCompareVersionForNext,
Expand All @@ -20,7 +20,7 @@ import {
getCommitMessageForExceptionalNextVersionBump,
getReleaseNoteCherryPickCommitMessage,
} from '../commit-message';
import {changelogPath, packageJsonPath} from '../constants';
import {packageJsonPath} from '../constants';

/**
* Base action that can be used to move the next release-train into the feature-freeze or
Expand Down
3 changes: 0 additions & 3 deletions ng-dev/release/publish/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
/** Project-relative path for the "package.json" file. */
export const packageJsonPath = 'package.json';

/** Project-relative path for the changelog file. */
export const changelogPath = 'CHANGELOG.md';

/** Default interval in milliseconds to check whether a pull request has been merged. */
export const waitForPullRequestInterval = 10000;

Expand Down
4 changes: 2 additions & 2 deletions ng-dev/release/publish/test/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import * as semver from 'semver';
import {SemVer} from 'semver';

import {getBranchPushMatcher, testTmpDir} from '../../../utils/testing';
import {ReleaseNotes} from '../../notes/release-notes';
import {changelogPath, ReleaseNotes} from '../../notes/release-notes';
import {NpmDistTag} from '../../versioning';
import {ActiveReleaseTrains} from '../../versioning/active-release-trains';
import * as npm from '../../versioning/npm-publish';
import {ReleaseTrain} from '../../versioning/release-trains';
import {ReleaseAction} from '../actions';
import {actions} from '../actions/index';
import {changelogPath, githubReleaseBodyLimit} from '../constants';
import {githubReleaseBodyLimit} from '../constants';
import {
changelogPattern,
fakeNpmPackageQueryRequest,
Expand Down
2 changes: 1 addition & 1 deletion ng-dev/release/publish/test/test-utils/sandbox-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class SandboxGitRepo {
const commitSha = this._commitShaById.get(commitId);

if (commitSha === undefined) {
throw Error('Unable to get determine SHA due to an unknown commit id.');
throw Error('Unable to retrieve SHA due to an unknown commit id.');
}

if (type === 'short') {
Expand Down
21 changes: 11 additions & 10 deletions tools/local-actions/changelog/main.js

Large diffs are not rendered by default.

0 comments on commit 405f2b3

Please sign in to comment.