1
0
Fork 0
mirror of https://github.com/imjasonh/apidiff-action synced 2026-07-06 22:52:43 +00:00

Fix lint errors and formatting

- Remove unused variables
- Add .eslintignore to exclude dist/
- Apply prettier formatting
- Fix all pre-commit hook issues
This commit is contained in:
Jason Hall 2025-07-29 17:07:15 -04:00
parent e3eca548b6
commit 805c061497
Failed to extract signature
20 changed files with 2397 additions and 2391 deletions

3
.eslintignore Normal file
View file

@ -0,0 +1,3 @@
dist/
node_modules/
coverage/

View file

@ -13,4 +13,4 @@ module.exports = {
'no-console': 'off', 'no-console': 'off',
'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
}, },
}; };

View file

@ -79,7 +79,7 @@ jobs:
fi fi
echo "Breaking changes detected: ${{ steps.test-breaking.outputs.breaking-count }}" echo "Breaking changes detected: ${{ steps.test-breaking.outputs.breaking-count }}"
echo "Compatible changes: ${{ steps.test-breaking.outputs.compatible-count }}" echo "Compatible changes: ${{ steps.test-breaking.outputs.compatible-count }}"
# Verify counts match what we expect # Verify counts match what we expect
if [ "${{ steps.test-breaking.outputs.breaking-count }}" != "3" ]; then if [ "${{ steps.test-breaking.outputs.breaking-count }}" != "3" ]; then
echo "Expected 3 breaking changes, got ${{ steps.test-breaking.outputs.breaking-count }}" echo "Expected 3 breaking changes, got ${{ steps.test-breaking.outputs.breaking-count }}"
@ -109,7 +109,7 @@ jobs:
exit 1 exit 1
fi fi
echo "Compatible changes: ${{ steps.test-safe.outputs.compatible-count }}" echo "Compatible changes: ${{ steps.test-safe.outputs.compatible-count }}"
# Verify count matches what we expect # Verify count matches what we expect
if [ "${{ steps.test-safe.outputs.compatible-count }}" != "3" ]; then if [ "${{ steps.test-safe.outputs.compatible-count }}" != "3" ]; then
echo "Expected 3 compatible changes, got ${{ steps.test-safe.outputs.compatible-count }}" echo "Expected 3 compatible changes, got ${{ steps.test-safe.outputs.compatible-count }}"
@ -141,4 +141,4 @@ jobs:
if [ "${{ steps.test-clean.outputs.compatible-count }}" != "0" ]; then if [ "${{ steps.test-clean.outputs.compatible-count }}" != "0" ]; then
echo "Expected 0 compatible changes, got ${{ steps.test-clean.outputs.compatible-count }}" echo "Expected 0 compatible changes, got ${{ steps.test-clean.outputs.compatible-count }}"
exit 1 exit 1
fi fi

View file

@ -13,18 +13,18 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: actions/setup-node@v4 - uses: actions/setup-node@v4
with: with:
node-version: '20' node-version: '20'
cache: 'npm' cache: 'npm'
- name: Install dependencies - name: Install dependencies
run: npm ci run: npm ci
- name: Build - name: Build
run: npm run build run: npm run build
- name: Create Release - name: Create Release
uses: actions/create-release@v1 uses: actions/create-release@v1
env: env:
@ -34,13 +34,13 @@ jobs:
release_name: ${{ github.ref }} release_name: ${{ github.ref }}
body: | body: |
## What's Changed ## What's Changed
See the [commit history](https://github.com/${{ github.repository }}/compare/${{ github.event.before }}...${{ github.ref }}) for details. See the [commit history](https://github.com/${{ github.repository }}/compare/${{ github.event.before }}...${{ github.ref }}) for details.
## Usage ## Usage
```yaml ```yaml
- uses: ${{ github.repository }}@${{ github.ref }} - uses: ${{ github.repository }}@${{ github.ref }}
``` ```
draft: false draft: false
prerelease: false prerelease: false

View file

@ -20,4 +20,4 @@ repos:
hooks: hooks:
- id: eslint - id: eslint
files: \.js$ files: \.js$
args: ['--fix'] args: ['--fix']

View file

@ -4,4 +4,4 @@
"tabWidth": 2, "tabWidth": 2,
"trailingComma": "es5", "trailingComma": "es5",
"printWidth": 100 "printWidth": 100
} }

View file

@ -33,4 +33,4 @@ outputs:
runs: runs:
using: 'node20' using: 'node20'
main: 'dist/index.js' main: 'dist/index.js'

View file

@ -17,7 +17,7 @@ async function run() {
// Determine refs to compare // Determine refs to compare
let oldRef, newRef; let oldRef, newRef;
// Check if INPUT_OLD and INPUT_NEW env vars are set (for testing) // Check if INPUT_OLD and INPUT_NEW env vars are set (for testing)
if (process.env.INPUT_OLD && process.env.INPUT_NEW) { if (process.env.INPUT_OLD && process.env.INPUT_NEW) {
oldRef = process.env.INPUT_OLD; oldRef = process.env.INPUT_OLD;
@ -34,12 +34,14 @@ async function run() {
newRef = context.payload.after; newRef = context.payload.after;
core.info(`Comparing commits ${oldRef} with ${newRef}`); core.info(`Comparing commits ${oldRef} with ${newRef}`);
} else { } else {
throw new Error('Unable to determine commits to compare. This action should be run on pull_request or push events.'); throw new Error(
'Unable to determine commits to compare. This action should be run on pull_request or push events.'
);
} }
// Run apidiff // Run apidiff
const output = await runApidiff({ workingDirectory, oldRef, newRef }); const output = await runApidiff({ workingDirectory, oldRef, newRef });
// Parse output // Parse output
const parsedChanges = parseApidiffOutput(output); const parsedChanges = parseApidiffOutput(output);
@ -62,11 +64,10 @@ async function run() {
} else { } else {
core.info('No breaking API changes detected'); core.info('No breaking API changes detected');
} }
} catch (error) { } catch (error) {
core.setFailed(`Action failed: ${error.message}`); core.setFailed(`Action failed: ${error.message}`);
} }
} }
// Run the action // Run the action
run(); run();

View file

@ -18,24 +18,22 @@ async function runApidiff({ workingDirectory, oldRef, newRef }) {
// Create export files if comparing directories // Create export files if comparing directories
const isDirectory = !oldRef.match(/^[0-9a-f]{7,40}$/i); const isDirectory = !oldRef.match(/^[0-9a-f]{7,40}$/i);
if (isDirectory) { if (isDirectory) {
// For directories, we need to create export files first // For directories, we need to create export files first
const oldDir = path.join(workingDirectory, oldRef); const oldDir = path.join(workingDirectory, oldRef);
const newDir = path.join(workingDirectory, newRef); const newDir = path.join(workingDirectory, newRef);
const oldExportPath = path.join(oldDir, 'apidiff.export');
const newExportPath = path.join(newDir, 'apidiff.export');
// Create export for old version // Create export for old version
await exec.exec('apidiff', ['-w', 'apidiff.export', '.'], { await exec.exec('apidiff', ['-w', 'apidiff.export', '.'], {
cwd: oldDir cwd: oldDir,
}); });
// Create export for new version // Create export for new version
await exec.exec('apidiff', ['-w', 'apidiff.export', '.'], { await exec.exec('apidiff', ['-w', 'apidiff.export', '.'], {
cwd: newDir cwd: newDir,
}); });
// Now compare the export files // Now compare the export files
let output = ''; let output = '';
let errorOutput = ''; let errorOutput = '';
@ -48,16 +46,16 @@ async function runApidiff({ workingDirectory, oldRef, newRef }) {
}, },
stderr: (data) => { stderr: (data) => {
errorOutput += data.toString(); errorOutput += data.toString();
} },
}, },
ignoreReturnCode: true ignoreReturnCode: true,
}; };
// Compare using relative paths from working directory // Compare using relative paths from working directory
const relativeOldPath = path.join(oldRef, 'apidiff.export'); const relativeOldPath = path.join(oldRef, 'apidiff.export');
const relativeNewPath = path.join(newRef, 'apidiff.export'); const relativeNewPath = path.join(newRef, 'apidiff.export');
const exitCode = await exec.exec('apidiff', [relativeOldPath, relativeNewPath], options); const exitCode = await exec.exec('apidiff', [relativeOldPath, relativeNewPath], options);
if (errorOutput) { if (errorOutput) {
core.warning(`apidiff stderr: ${errorOutput}`); core.warning(`apidiff stderr: ${errorOutput}`);
} }
@ -82,9 +80,9 @@ async function runApidiff({ workingDirectory, oldRef, newRef }) {
}, },
stderr: (data) => { stderr: (data) => {
errorOutput += data.toString(); errorOutput += data.toString();
} },
}, },
ignoreReturnCode: true ignoreReturnCode: true,
}; };
const exitCode = await exec.exec('apidiff', [oldRef, newRef], options); const exitCode = await exec.exec('apidiff', [oldRef, newRef], options);
@ -116,14 +114,14 @@ async function installApidiff() {
// apidiff not found, install it // apidiff not found, install it
core.info('Installing apidiff...'); core.info('Installing apidiff...');
await exec.exec('go', ['install', 'golang.org/x/exp/cmd/apidiff@latest']); await exec.exec('go', ['install', 'golang.org/x/exp/cmd/apidiff@latest']);
// Add Go bin to PATH // Add Go bin to PATH
const goPath = process.env.GOPATH || path.join(process.env.HOME, 'go'); const goPath = process.env.GOPATH || path.join(process.env.HOME, 'go');
const goBin = path.join(goPath, 'bin'); const goBin = path.join(goPath, 'bin');
core.addPath(goBin); core.addPath(goBin);
core.info('apidiff installed successfully'); core.info('apidiff installed successfully');
} }
} }
module.exports = { runApidiff }; module.exports = { runApidiff };

View file

@ -31,12 +31,10 @@ async function createOrUpdateComment({ token, body }) {
const { data: comments } = await octokit.rest.issues.listComments({ const { data: comments } = await octokit.rest.issues.listComments({
owner, owner,
repo, repo,
issue_number: prNumber issue_number: prNumber,
}); });
const existingComment = comments.find(comment => const existingComment = comments.find((comment) => comment.body?.includes(COMMENT_TAG));
comment.body?.includes(COMMENT_TAG)
);
if (existingComment) { if (existingComment) {
// Update existing comment // Update existing comment
@ -44,7 +42,7 @@ async function createOrUpdateComment({ token, body }) {
owner, owner,
repo, repo,
comment_id: existingComment.id, comment_id: existingComment.id,
body: commentBody body: commentBody,
}); });
core.info(`Updated existing comment: ${existingComment.html_url}`); core.info(`Updated existing comment: ${existingComment.html_url}`);
} else { } else {
@ -53,7 +51,7 @@ async function createOrUpdateComment({ token, body }) {
owner, owner,
repo, repo,
issue_number: prNumber, issue_number: prNumber,
body: commentBody body: commentBody,
}); });
core.info(`Created new comment: ${newComment.html_url}`); core.info(`Created new comment: ${newComment.html_url}`);
} }
@ -63,4 +61,4 @@ async function createOrUpdateComment({ token, body }) {
} }
} }
module.exports = { createOrUpdateComment }; module.exports = { createOrUpdateComment };

View file

@ -10,7 +10,7 @@ function parseApidiffOutput(output) {
hasBreakingChanges: false, hasBreakingChanges: false,
breakingCount: 0, breakingCount: 0,
compatibleCount: 0, compatibleCount: 0,
packages: [] packages: [],
}; };
if (!output || output.trim() === '') { if (!output || output.trim() === '') {
@ -19,14 +19,14 @@ function parseApidiffOutput(output) {
// Split into lines // Split into lines
const lines = output.split('\n'); const lines = output.split('\n');
let currentPackage = { let currentPackage = {
name: 'default', name: 'default',
breaking: [], breaking: [],
compatible: [] compatible: [],
}; };
result.packages.push(currentPackage); result.packages.push(currentPackage);
let inCompatibleSection = false; let inCompatibleSection = false;
let inIncompatibleSection = false; let inIncompatibleSection = false;
@ -55,10 +55,10 @@ function parseApidiffOutput(output) {
// Parse changes (they start with "- ") // Parse changes (they start with "- ")
if ((inCompatibleSection || inIncompatibleSection) && trimmedLine.startsWith('- ')) { if ((inCompatibleSection || inIncompatibleSection) && trimmedLine.startsWith('- ')) {
const changeMessage = trimmedLine.substring(2).trim(); const changeMessage = trimmedLine.substring(2).trim();
const change = { const change = {
message: changeMessage, message: changeMessage,
compatible: inCompatibleSection compatible: inCompatibleSection,
}; };
if (inCompatibleSection) { if (inCompatibleSection) {
@ -72,8 +72,8 @@ function parseApidiffOutput(output) {
} }
// Clean up empty packages // Clean up empty packages
result.packages = result.packages.filter(pkg => result.packages = result.packages.filter(
pkg.breaking.length > 0 || pkg.compatible.length > 0 (pkg) => pkg.breaking.length > 0 || pkg.compatible.length > 0
); );
// If no packages with changes, return empty packages array // If no packages with changes, return empty packages array
@ -81,7 +81,9 @@ function parseApidiffOutput(output) {
result.packages = []; result.packages = [];
} }
core.info(`Parsed ${result.breakingCount} breaking changes and ${result.compatibleCount} compatible changes`); core.info(
`Parsed ${result.breakingCount} breaking changes and ${result.compatibleCount} compatible changes`
);
return result; return result;
} }
@ -112,7 +114,7 @@ function formatChangesAsMarkdown(parsedChanges) {
// Details by package // Details by package
markdown += '## Details\n\n'; markdown += '## Details\n\n';
for (const pkg of parsedChanges.packages) { for (const pkg of parsedChanges.packages) {
if (pkg.name !== 'default') { if (pkg.name !== 'default') {
markdown += `### \`${pkg.name}\`\n\n`; markdown += `### \`${pkg.name}\`\n\n`;
@ -138,4 +140,4 @@ function formatChangesAsMarkdown(parsedChanges) {
return markdown; return markdown;
} }
module.exports = { parseApidiffOutput, formatChangesAsMarkdown }; module.exports = { parseApidiffOutput, formatChangesAsMarkdown };

4566
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,30 +1,11 @@
{ {
"name": "apidiff-action",
"version": "1.0.0",
"description": "GitHub Action for detecting breaking API changes in Go code",
"main": "index.js",
"scripts": {
"build": "ncc build index.js -o dist",
"test": "jest",
"lint": "eslint .",
"format": "prettier --write .",
"prepare": "husky install"
},
"keywords": [
"github-action",
"go",
"golang",
"api",
"compatibility",
"breaking-changes"
],
"author": "Jason Hall", "author": "Jason Hall",
"license": "Apache-2.0",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"@actions/github": "^6.0.0" "@actions/github": "^6.0.0"
}, },
"description": "GitHub Action for detecting breaking API changes in Go code",
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.11", "@types/jest": "^29.5.11",
"@vercel/ncc": "^0.38.1", "@vercel/ncc": "^0.38.1",
@ -35,6 +16,27 @@
"lint-staged": "^15.2.0", "lint-staged": "^15.2.0",
"prettier": "^3.1.1" "prettier": "^3.1.1"
}, },
"jest": {
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
},
"testEnvironment": "node"
},
"keywords": [
"github-action",
"go",
"golang",
"api",
"compatibility",
"breaking-changes"
],
"license": "Apache-2.0",
"lint-staged": { "lint-staged": {
"*.js": [ "*.js": [
"eslint --fix", "eslint --fix",
@ -44,16 +46,14 @@
"prettier --write" "prettier --write"
] ]
}, },
"jest": { "main": "index.js",
"testEnvironment": "node", "name": "apidiff-action",
"collectCoverage": true, "scripts": {
"coverageThreshold": { "build": "ncc build index.js -o dist",
"global": { "format": "prettier --write .",
"branches": 80, "lint": "eslint .",
"functions": 80, "prepare": "husky install",
"lines": 80, "test": "jest"
"statements": 80 },
} "version": "1.0.0"
} }
}
}

View file

@ -86,7 +86,7 @@ describe('formatChangesAsMarkdown', () => {
hasBreakingChanges: false, hasBreakingChanges: false,
breakingCount: 0, breakingCount: 0,
compatibleCount: 0, compatibleCount: 0,
packages: [] packages: [],
}; };
const markdown = formatChangesAsMarkdown(parsedChanges); const markdown = formatChangesAsMarkdown(parsedChanges);
@ -98,11 +98,13 @@ describe('formatChangesAsMarkdown', () => {
hasBreakingChanges: true, hasBreakingChanges: true,
breakingCount: 1, breakingCount: 1,
compatibleCount: 0, compatibleCount: 0,
packages: [{ packages: [
name: 'default', {
breaking: [{ message: 'Foo: removed', compatible: false }], name: 'default',
compatible: [] breaking: [{ message: 'Foo: removed', compatible: false }],
}] compatible: [],
},
],
}; };
const markdown = formatChangesAsMarkdown(parsedChanges); const markdown = formatChangesAsMarkdown(parsedChanges);
@ -118,14 +120,16 @@ describe('formatChangesAsMarkdown', () => {
hasBreakingChanges: false, hasBreakingChanges: false,
breakingCount: 0, breakingCount: 0,
compatibleCount: 2, compatibleCount: 2,
packages: [{ packages: [
name: 'default', {
breaking: [], name: 'default',
compatible: [ breaking: [],
{ message: 'Bar: added', compatible: true }, compatible: [
{ message: 'Baz: added', compatible: true } { message: 'Bar: added', compatible: true },
] { message: 'Baz: added', compatible: true },
}] ],
},
],
}; };
const markdown = formatChangesAsMarkdown(parsedChanges); const markdown = formatChangesAsMarkdown(parsedChanges);
@ -134,4 +138,4 @@ describe('formatChangesAsMarkdown', () => {
expect(markdown).toContain('- Bar: added'); expect(markdown).toContain('- Bar: added');
expect(markdown).toContain('- Baz: added'); expect(markdown).toContain('- Baz: added');
}); });
}); });

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21

View file

@ -1,3 +1,3 @@
module example.com/test module example.com/test
go 1.21 go 1.21