Skip to content

Commit

Permalink
fix(release): validate releaseTagPattern version placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Oct 23, 2023
1 parent fccfd31 commit 9016379
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
3 changes: 0 additions & 3 deletions packages/nx/src/command-line/release/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ export async function changelogHandler(args: ChangelogOptions): Promise<void> {
process.env.NX_VERBOSE_LOGGING = 'true';
}

/**
* TODO: validate releaseTagPattern contains exactly one instance of {version}
*/
// Apply default configuration to any optional user configuration
const { error: configError, nxReleaseConfig } = await createNxReleaseConfig(
projectGraph,
Expand Down
44 changes: 44 additions & 0 deletions packages/nx/src/command-line/release/config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,5 +945,49 @@ describe('createNxReleaseConfig()', () => {
}
`);
});

it("should return an error if a group's releaseTagPattern has no {version} placeholder", async () => {
const res = await createNxReleaseConfig(projectGraph, {
groups: {
'group-1': {
projects: '*',
releaseTagPattern: 'v',
},
},
});
expect(res).toMatchInlineSnapshot(`
{
"error": {
"code": "RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE",
"data": {
"releaseGroupName": "group-1",
},
},
"nxReleaseConfig": null,
}
`);
});

it("should return an error if a group's releaseTagPattern has more than one {version} placeholder", async () => {
const res = await createNxReleaseConfig(projectGraph, {
groups: {
'group-1': {
projects: '*',
releaseTagPattern: '{version}v{version}',
},
},
});
expect(res).toMatchInlineSnapshot(`
{
"error": {
"code": "RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE",
"data": {
"releaseGroupName": "group-1",
},
},
"nxReleaseConfig": null,
}
`);
});
});
});
44 changes: 44 additions & 0 deletions packages/nx/src/command-line/release/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type NxReleaseConfig = DeepRequired<
export interface CreateNxReleaseConfigError {
code:
| 'RELEASE_GROUP_MATCHES_NO_PROJECTS'
| 'RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE'
| 'PROJECT_MATCHES_MULTIPLE_GROUPS'
| 'PROJECTS_MISSING_TARGET';
data: Record<string, string | string[]>;
Expand Down Expand Up @@ -194,6 +195,20 @@ export async function createNxReleaseConfig(
}
}

// If provided, ensure release tag pattern is valid
if (releaseGroup.releaseTagPattern) {
const error = ensureReleaseTagPatternIsValid(
releaseGroup.releaseTagPattern,
releaseGroupName
);
if (error) {
return {
error,
nxReleaseConfig: null,
};
}
}

for (const project of matchingProjects) {
if (alreadyMatchedProjects.has(project)) {
return {
Expand Down Expand Up @@ -290,13 +305,42 @@ export async function handleNxReleaseConfigError(
});
}
break;
case 'RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE':
{
const nxJsonMessage = await resolveNxJsonConfigErrorMessage([
'release',
'groups',
error.data.releaseGroupName as string,
'releaseTagPattern',
]);
output.error({
title: `Release group "${error.data.releaseGroupName}" has an invalid releaseTagPattern. Please ensure the pattern contains exactly one instance of the "{version}" placeholder`,
bodyLines: [nxJsonMessage],
});
}
break;
default:
throw new Error(`Unhandled error code: ${error.code}`);
}

process.exit(1);
}

function ensureReleaseTagPatternIsValid(
releaseTagPattern: string,
releaseGroupName: string
): null | CreateNxReleaseConfigError {
// ensure that any provided releaseTagPattern contains exactly one instance of {version}
return releaseTagPattern.split('{version}').length === 2
? null
: {
code: 'RELEASE_GROUP_RELEASE_TAG_PATTERN_VERSION_PLACEHOLDER_MISSING_OR_EXCESSIVE',
data: {
releaseGroupName,
},
};
}

function ensureProjectsConfigIsArray(
groups: NxJsonConfiguration['release']['groups']
): NxReleaseConfig['groups'] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export async function resolveNxJsonConfigErrorMessage(
joinPathFragments(workspaceRoot, 'nx.json')
)}`;
if (errorLines) {
nxJsonMessage += `, lines ${errorLines.startLine}-${errorLines.endLine}`;
nxJsonMessage +=
errorLines.startLine === errorLines.endLine
? `, line ${errorLines.startLine}`
: `, lines ${errorLines.startLine}-${errorLines.endLine}`;
}
return nxJsonMessage;
}
Expand Down

0 comments on commit 9016379

Please sign in to comment.