Skip to content

Commit 2edab4f

Browse files
authored
package json lint error as github annotation (#357)
* package json lint error as github annotation * path fixes * undo lint violation * correct test violation * css test violation * fixes * fix
1 parent 252c101 commit 2edab4f

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

.github/bin/format-package-json.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { promises as fsp, constants } from 'fs';
2+
import path from 'path';
23

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

201202
if (process.env.GITHUB_ACTIONS && JSON.stringify(formatted, null, '\t') !== packageJSONInfoCopy) {
202-
console.error('package.json has an incorrect field order. Run "npm run lint" to resolve.')
203+
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.`);
203204
process.exit(1);
204205
}
205206

packages/postcss-tape/src/github-annotations.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
import path from 'path';
2+
13
export function formatGitHubActionAnnotation(message, level = 'error', options = {}) {
24
let output = '::' + level;
35

46
const outputOptions = Object.keys(options).map((key) => {
5-
return `${key}=${escapeValue(String(options[key]))}`;
7+
let value = String(options[key]);
8+
9+
if (key === 'file' && process.env.GITHUB_WORKSPACE) {
10+
// make file paths relative to the workspace root.
11+
value = path.relative(process.env.GITHUB_WORKSPACE, path.resolve(value));
12+
}
13+
14+
return `${key}=${escapeValue(value)}`;
615
}).join(',');
716

817
if (outputOptions) {

packages/postcss-tape/src/index.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
248248
console.log(formatGitHubActionAnnotation(
249249
formatCSSAssertError(testCaseLabel, testCaseOptions, err, true),
250250
'error',
251-
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
251+
{ file: expectFilePath, line: 1, col: 1 },
252252
));
253253
} else {
254254
failureSummary.add(testCaseLabel);
@@ -344,7 +344,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
344344
console.log(formatGitHubActionAnnotation(
345345
'testing older PostCSS:\n' + formatCSSAssertError(testCaseLabel, testCaseOptions, err, true),
346346
'error',
347-
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
347+
{ file: expectFilePath, line: 1, col: 1 },
348348
));
349349
} else {
350350
failureSummary.add(testCaseLabel);
@@ -366,7 +366,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
366366
console.log(formatGitHubActionAnnotation(
367367
formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings(), testCaseOptions.warnings, true),
368368
'error',
369-
{ file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 },
369+
{ file: expectFilePath, line: 1, col: 1 },
370370
));
371371
} else {
372372
failureSummary.add(testCaseLabel);
@@ -390,22 +390,3 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
390390
console.warn('pass ' + (currentPlugin() as Plugin).postcssPlugin);
391391
};
392392
}
393-
394-
function normalizeFilePathForGithubAnnotation(filePath: string) {
395-
// Any plugin or packages is located in `<workspace>/<package>`;
396-
const parts = process.cwd().split('/').slice(-2);
397-
398-
const workspaces = [
399-
'packages',
400-
'plugins',
401-
'plugin-packs',
402-
'cli',
403-
'experimental',
404-
];
405-
406-
if (!workspaces.includes(parts[0])) {
407-
throw new Error('PostCSS Tape was intended to be run from <workspace>/<package>');
408-
}
409-
410-
return path.join(parts.join('/'), filePath);
411-
}

0 commit comments

Comments
 (0)