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

@aws-amplify/backend should inherit version bump of same kind from dependencies #2042

Merged
merged 17 commits into from
Sep 24, 2024
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@aws-sdk/client-ssm": "^3.624.0",
"@changesets/cli": "^2.26.1",
"@changesets/get-release-plan": "^4.0.0",
"@changesets/types": "^6.0.0",
"@microsoft/api-extractor": "7.43.8",
"@octokit/webhooks-types": "^7.5.1",
"@shopify/eslint-plugin": "^43.0.0",
Expand Down
155 changes: 120 additions & 35 deletions scripts/check_changeset_completeness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,136 @@ import getReleasePlan from '@changesets/get-release-plan';
import { GitClient } from './components/git_client.js';
import { readPackageJson } from './components/package-json/package_json.js';
import { EOL } from 'os';
import { ReleasePlan, VersionType } from '@changesets/types';

const gitClient = new GitClient();

const baseRef = process.argv[2];
if (baseRef === undefined) {
throw new Error('No base ref specified for changeset completeness check');
enum VersionTypeEnum {
'NONE' = 0,
'PATCH' = 1,
'MINOR' = 2,
'MAJOR' = 3,
}

const releasePlan = await getReleasePlan(process.cwd());
const checkForMissingChangesets = async (
releasePlan: ReleasePlan,
gitClient: GitClient,
baseRef: string
) => {
const packagesWithChangeset = new Set(
releasePlan.releases.map((release) => release.name)
);

const packagesWithChangeset = new Set(
releasePlan.releases.map((release) => release.name)
);
const changedFiles = await gitClient.getChangedFiles(baseRef);
const modifiedPackageDirs = new Set<string>();

const changedFiles = await gitClient.getChangedFiles(baseRef);
changedFiles
.filter(
(changedFile) =>
changedFile.startsWith('packages/') && !changedFile.endsWith('test.ts')
)
.forEach((changedPackageFile) => {
modifiedPackageDirs.add(
changedPackageFile.split('/').slice(0, 2).join('/')
);
});

const modifiedPackageDirs = new Set<string>();
const packagesMissingChangesets = [];
for (const modifiedPackageDir of modifiedPackageDirs) {
const { name: modifiedPackageName, private: isPrivate } =
await readPackageJson(modifiedPackageDir);
if (isPrivate) {
continue;
}
if (!packagesWithChangeset.has(modifiedPackageName)) {
packagesMissingChangesets.push(modifiedPackageName);
}
}

changedFiles
.filter(
(changedFile) =>
changedFile.startsWith('packages/') && !changedFile.endsWith('test.ts')
)
.forEach((changedPackageFile) => {
modifiedPackageDirs.add(
changedPackageFile.split('/').slice(0, 2).join('/')
if (packagesMissingChangesets.length > 0) {
throw new Error(
`The following packages have changes but are not included in any changeset:${EOL}${EOL}${packagesMissingChangesets.join(
EOL
)}${EOL}${EOL}Add a changeset using 'npx changeset add'.`
);
});

const packagesMissingChangesets = [];
for (const modifiedPackageDir of modifiedPackageDirs) {
const { name: modifiedPackageName, private: isPrivate } =
await readPackageJson(modifiedPackageDir);
if (isPrivate) {
continue;
}
if (!packagesWithChangeset.has(modifiedPackageName)) {
packagesMissingChangesets.push(modifiedPackageName);
};

const convertVersionType = (version: VersionType): VersionTypeEnum => {
switch (version) {
case 'major':
return VersionTypeEnum.MAJOR;
case 'minor':
return VersionTypeEnum.MINOR;
case 'patch':
return VersionTypeEnum.PATCH;
case 'none':
return VersionTypeEnum.NONE;
}
}
};

if (packagesMissingChangesets.length > 0) {
throw new Error(
`The following packages have changes but are not included in any changeset:${EOL}${EOL}${packagesMissingChangesets.join(
EOL
)}${EOL}${EOL}Add a changeset using 'npx changeset add'.`
const findEffectiveVersion = (
releasePlan: ReleasePlan,
packageName: string
): VersionTypeEnum => {
let effectiveVersion: VersionTypeEnum = VersionTypeEnum.NONE;

for (const changeset of releasePlan.changesets) {
for (const release of changeset.releases) {
if (release.name === packageName) {
const releaseVersionType = convertVersionType(release.type);
if (releaseVersionType > effectiveVersion) {
effectiveVersion = releaseVersionType;
}
}
}
}
return effectiveVersion;
};

const checkBackendDependenciesVersion = (releasePlan: ReleasePlan) => {
const backendVersion: VersionTypeEnum = findEffectiveVersion(
releasePlan,
'@aws-amplify/backend'
);
const backendAuthVersion: VersionTypeEnum = findEffectiveVersion(
releasePlan,
'@aws-amplify/backend-auth'
);
const backendDataVersion: VersionTypeEnum = findEffectiveVersion(
releasePlan,
'@aws-amplify/backend-data'
);
const backendFunctionVersion: VersionTypeEnum = findEffectiveVersion(
releasePlan,
'@aws-amplify/backend-function'
);
const backendStorageVersion: VersionTypeEnum = findEffectiveVersion(
releasePlan,
'@aws-amplify/backend-storage'
);

if (
backendVersion <
Math.max(
backendAuthVersion,
backendDataVersion,
backendFunctionVersion,
backendStorageVersion
)
) {
throw new Error(
`@aws-amplify/backend has a version bump of a different kind from its dependencies (@aws-amplify/backend-auth, @aws-amplify/backend-data, @aws-amplify/backend-function, or @aws-amplify/backend-storage) but is expected to have a version bump of the same kind.${EOL}`
);
}
};

const gitClient = new GitClient();

const baseRef = process.argv[2];
if (baseRef === undefined) {
throw new Error('No base ref specified for changeset completeness check');
}

const releasePlan = await getReleasePlan(process.cwd());

await checkForMissingChangesets(releasePlan, gitClient, baseRef);
checkBackendDependenciesVersion(releasePlan);