-
Notifications
You must be signed in to change notification settings - Fork 369
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
fix: Use strategies for workspace plugins. #2112
Changes from 1 commit
277821b
39a2415
6f9af54
5648538
cc25227
f9b38c1
3707bb8
0d5d844
cd9666f
9ca04c5
c8a9503
8459bc8
c8e1e15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,9 @@ import { | |
addPath, | ||
} from './workspace'; | ||
import {PatchVersionUpdate} from '../versioning-strategy'; | ||
import {Strategy} from '../strategy'; | ||
import {Commit} from '../commit'; | ||
import {Release} from '../release'; | ||
|
||
class Package extends LernaPackage { | ||
constructor( | ||
|
@@ -69,6 +72,10 @@ interface NodeWorkspaceOptions extends WorkspacePluginOptions { | |
export class NodeWorkspace extends WorkspacePlugin<Package> { | ||
private alwaysLinkLocal: boolean; | ||
private packageGraph?: PackageGraph; | ||
|
||
private strategiesByPath: Record<string, Strategy> = {}; | ||
private releasesByPath: Record<string, Release> = {}; | ||
|
||
constructor( | ||
github: GitHub, | ||
targetBranch: string, | ||
|
@@ -232,10 +239,10 @@ export class NodeWorkspace extends WorkspacePlugin<Package> { | |
} | ||
return existingCandidate; | ||
} | ||
protected newCandidate( | ||
protected async newCandidate( | ||
pkg: Package, | ||
updatedVersions: VersionsMap | ||
): CandidateReleasePullRequest { | ||
): Promise<CandidateReleasePullRequest> { | ||
const graphPackage = this.packageGraph?.get(pkg.name); | ||
if (!graphPackage) { | ||
throw new Error(`Could not find graph package for ${pkg.name}`); | ||
|
@@ -271,20 +278,31 @@ export class NodeWorkspace extends WorkspacePlugin<Package> { | |
); | ||
const packageJson = updatedPackage.toJSON() as PackageJson; | ||
const version = Version.parse(packageJson.version); | ||
|
||
const strategy = this.strategiesByPath[updatedPackage.location]; | ||
const latestRelease = this.releasesByPath[updatedPackage.location]; | ||
|
||
const basePullRequest = strategy | ||
? await strategy.buildReleasePullRequest([], latestRelease, false, [], { | ||
newVersion: version, | ||
}) | ||
: undefined; | ||
|
||
const pullRequest: ReleasePullRequest = { | ||
title: PullRequestTitle.ofTargetBranch(this.targetBranch), | ||
body: new PullRequestBody([ | ||
{ | ||
component: updatedPackage.name, | ||
version, | ||
notes: appendDependenciesSectionToChangelog( | ||
'', | ||
basePullRequest?.body.notes() ?? '', | ||
dependencyNotes, | ||
this.logger | ||
), | ||
}, | ||
]), | ||
updates: [ | ||
...(basePullRequest ? basePullRequest?.updates : []), | ||
{ | ||
path: addPath(updatedPackage.location, 'package.json'), | ||
createIfMissing: false, | ||
|
@@ -297,16 +315,17 @@ export class NodeWorkspace extends WorkspacePlugin<Package> { | |
createIfMissing: false, | ||
updater: new Changelog({ | ||
version, | ||
changelogEntry: appendDependenciesSectionToChangelog( | ||
'', | ||
dependencyNotes, | ||
this.logger | ||
), | ||
versionHeaderRegex: `\n###? v?[${version | ||
kinyoklion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.toString() | ||
.replace('.', '.')}]`, | ||
changelogEntry: dependencyNotes, | ||
}), | ||
}, | ||
], | ||
labels: [], | ||
headRefName: BranchName.ofTargetBranch(this.targetBranch).toString(), | ||
headRefName: | ||
basePullRequest?.headRefName ?? | ||
BranchName.ofTargetBranch(this.targetBranch).toString(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For #2109 |
||
version, | ||
draft: false, | ||
}; | ||
|
@@ -375,6 +394,18 @@ export class NodeWorkspace extends WorkspacePlugin<Package> { | |
...(packageJson.optionalDependencies ?? {}), | ||
}; | ||
} | ||
|
||
async preconfigure( | ||
strategiesByPath: Record<string, Strategy>, | ||
_commitsByPath: Record<string, Commit[]>, | ||
_releasesByPath: Record<string, Release> | ||
): Promise<Record<string, Strategy>> { | ||
// Using preconfigure to siphon releases and strategies. | ||
this.strategiesByPath = strategiesByPath; | ||
this.releasesByPath = _releasesByPath; | ||
|
||
return strategiesByPath; | ||
} | ||
} | ||
|
||
enum SUPPORTED_RANGE_PREFIXES { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Strategy, BuildReleaseOptions} from '../strategy'; | ||
import {Strategy, BuildReleaseOptions, BumpReleaseOptions} from '../strategy'; | ||
import {GitHub} from '../github'; | ||
import {VersioningStrategy} from '../versioning-strategy'; | ||
import {Repository} from '../repository'; | ||
|
@@ -254,19 +254,19 @@ export abstract class BaseStrategy implements Strategy { | |
commits: ConventionalCommit[], | ||
latestRelease?: Release, | ||
draft?: boolean, | ||
labels: string[] = [] | ||
labels: string[] = [], | ||
bumpOnlyOptions?: BumpReleaseOptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed some indicator to bypass all the early out conditions associated with no commits or changelog entries. An alternate approach to this whole thing may be to generate all the PRs always, and then prune ones we don't want. |
||
): Promise<ReleasePullRequest | undefined> { | ||
const conventionalCommits = await this.postProcessCommits(commits); | ||
this.logger.info(`Considering: ${conventionalCommits.length} commits`); | ||
if (conventionalCommits.length === 0) { | ||
if (!bumpOnlyOptions && conventionalCommits.length === 0) { | ||
this.logger.info(`No commits for path: ${this.path}, skipping`); | ||
return undefined; | ||
} | ||
|
||
const newVersion = await this.buildNewVersion( | ||
conventionalCommits, | ||
latestRelease | ||
); | ||
const newVersion = | ||
bumpOnlyOptions?.newVersion ?? | ||
(await this.buildNewVersion(conventionalCommits, latestRelease)); | ||
const versionsMap = await this.updateVersionsMap( | ||
await this.buildVersionsMap(conventionalCommits), | ||
conventionalCommits, | ||
|
@@ -302,7 +302,7 @@ export abstract class BaseStrategy implements Strategy { | |
latestRelease, | ||
commits | ||
); | ||
if (this.changelogEmpty(releaseNotesBody)) { | ||
if (!bumpOnlyOptions && this.changelogEmpty(releaseNotesBody)) { | ||
this.logger.info( | ||
`No user facing commits found since ${ | ||
latestRelease ? latestRelease.sha : 'beginning of time' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,10 @@ export interface BuildReleaseOptions { | |
groupPullRequestTitlePattern?: string; | ||
} | ||
|
||
export interface BumpReleaseOptions { | ||
newVersion: Version; | ||
} | ||
|
||
/** | ||
* A strategy is responsible for determining which files are | ||
* necessary to update in a release pull request. | ||
|
@@ -47,7 +51,8 @@ export interface Strategy { | |
commits: Commit[], | ||
latestRelease?: Release, | ||
draft?: boolean, | ||
labels?: string[] | ||
labels?: string[], | ||
bumpOnlyOptions?: BumpReleaseOptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit unclear on what these options are for. Perhaps a docstring is good enough to describe what it's use is for. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will add some documentation. I wasn't 100% sure on this, but basically these serve as an indicator that you are creating a release with a version number update, even if there were no associated conventional commits. So it is: |
||
): Promise<ReleasePullRequest | undefined>; | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make a base pull request that contains the "extra-files" updaters as well as the changelog header.