Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/changelog-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: MetaMask/github-tools
ref: main
ref: ${{ github.sha }}
path: github-tools

- name: Enable Corepack
Expand Down
68 changes: 68 additions & 0 deletions src/changelog-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,57 @@ async function getChangedFiles(
}
}

/**
* Checks if a package.json file only has version changes by comparing the diff output.
* Returns true if the diff contains exactly two lines (one addition and one removal)
* and both lines are version changes with the same format (with or without trailing comma).
*
* @param repoPath - The path to the repository.
* @param filePath - The path to the package.json file.
* @param baseRef - The base reference to compare against.
* @returns Promise that resolves to true if only version was changed, false otherwise.
*/
async function isVersionOnlyChange(
repoPath: string,
filePath: string,
baseRef: string,
): Promise<boolean> {
try {
const { stdout } = await execa(
'git',
['diff', `origin/${baseRef}...HEAD`, '--', filePath],
{
cwd: repoPath,
},
);

if (!stdout) {
return false;
}

// Split the diff into lines and filter out the diff header lines (+++ and ---)
const lines = stdout
.split('\n')
.filter((line) => line.startsWith('+') || line.startsWith('-'))
.filter((line) => !line.startsWith('+++') && !line.startsWith('---'));

// If we have exactly 2 lines (one addition and one removal) and they both contain version changes
if (lines.length === 2) {
const versionRegex = /^[+-]\s*"version":\s*"[^"]+"\s*,?\s*$/mu;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this would fail to match a line starting with --- or +++, is that important?

Copy link
Copy Markdown
Contributor Author

@cryptodev-2s cryptodev-2s Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 144, I’m excluding header lines that usually start with +++ or ---, which I believe typically indicate filenames for the old and new files. But I might be mistaken—let me know if that’s not the case.

Line changes usually have only + or - ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, you're right. I didn't read the code above correctly. This seems right.

return lines.every((line) => versionRegex.test(line));
}

return false;
} catch (error) {
logError(
`Failed to check ${filePath} changes: ${
error instanceof Error ? error.message : String(error)
}`,
);
return false;
}
}

/**
* Reads and validates a changelog file.
*
Expand Down Expand Up @@ -153,11 +204,15 @@ async function checkChangelogFile(
*
* @param files - The list of changed files.
* @param workspacePatterns - The workspace patterns.
* @param repoPath - The path to the repository.
* @param baseRef - The base reference to compare against.
* @returns Array of changed package information.
*/
async function getChangedPackages(
files: string[],
workspacePatterns: string[],
repoPath: string,
baseRef: string,
): Promise<
{
base: string;
Expand All @@ -181,6 +236,17 @@ async function getChangedPackages(
!file.includes('/docs/') &&
!file.endsWith('CHANGELOG.md')
) {
// If the file is package.json, check if it's only a version change
if (file.endsWith('package.json')) {
const isVersionOnly = await isVersionOnlyChange(
repoPath,
file,
baseRef,
);
if (isVersionOnly) {
continue;
}
}
changedPackages.set(packageInfo.package, packageInfo);
}
}
Expand Down Expand Up @@ -229,6 +295,8 @@ async function main() {
const changedPackages = await getChangedPackages(
changedFiles,
workspacePatterns,
fullRepoPath,
baseRef,
);
if (!changedPackages.length) {
console.log(
Expand Down
Loading