Skip to content

package json lint error as github annotation #357

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

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
3 changes: 2 additions & 1 deletion .github/bin/format-package-json.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { promises as fsp, constants } from 'fs';
import path from 'path';

const packageJSONInfo = JSON.parse(await fsp.readFile('./package.json', 'utf8'));
const packageJSONInfoCopy = JSON.stringify(packageJSONInfo, null, '\t');
Expand Down Expand Up @@ -199,7 +200,7 @@ const formatted = {};
Object.assign(formatted, packageJSONInfo);

if (process.env.GITHUB_ACTIONS && JSON.stringify(formatted, null, '\t') !== packageJSONInfoCopy) {
console.error('package.json has an incorrect field order. Run "npm run lint" to resolve.')
console.error(`::error file=${path.relative(process.env.GITHUB_WORKSPACE, path.resolve('./package.json'))},line=1,col=1::package.json has an incorrect field order. Run "npm run lint" to resolve.`);
process.exit(1);
}

Expand Down
11 changes: 10 additions & 1 deletion packages/postcss-tape/src/github-annotations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import path from 'path';

export function formatGitHubActionAnnotation(message, level = 'error', options = {}) {
let output = '::' + level;

const outputOptions = Object.keys(options).map((key) => {
return `${key}=${escapeValue(String(options[key]))}`;
let value = String(options[key]);

if (key === 'file' && process.env.GITHUB_WORKSPACE) {
// make file paths relative to the workspace root.
value = path.relative(process.env.GITHUB_WORKSPACE, path.resolve(value));
}

return `${key}=${escapeValue(value)}`;
}).join(',');

if (outputOptions) {
Expand Down
25 changes: 3 additions & 22 deletions packages/postcss-tape/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
console.log(formatGitHubActionAnnotation(
formatCSSAssertError(testCaseLabel, testCaseOptions, err, true),
'error',
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
{ file: expectFilePath, line: 1, col: 1 },
));
} else {
failureSummary.add(testCaseLabel);
Expand Down Expand Up @@ -344,7 +344,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
console.log(formatGitHubActionAnnotation(
'testing older PostCSS:\n' + formatCSSAssertError(testCaseLabel, testCaseOptions, err, true),
'error',
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
{ file: expectFilePath, line: 1, col: 1 },
));
} else {
failureSummary.add(testCaseLabel);
Expand All @@ -366,7 +366,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
console.log(formatGitHubActionAnnotation(
formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings(), testCaseOptions.warnings, true),
'error',
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
{ file: expectFilePath, line: 1, col: 1 },
));
} else {
failureSummary.add(testCaseLabel);
Expand All @@ -390,22 +390,3 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
console.warn('pass ' + (currentPlugin() as Plugin).postcssPlugin);
};
}

function normalizeFilePathForGithubAnnotation(filePath: string) {
// Any plugin or packages is located in `<workspace>/<package>`;
const parts = process.cwd().split('/').slice(-2);

const workspaces = [
'packages',
'plugins',
'plugin-packs',
'cli',
'experimental',
];

if (!workspaces.includes(parts[0])) {
throw new Error('PostCSS Tape was intended to be run from <workspace>/<package>');
}

return path.join(parts.join('/'), filePath);
}