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
65 changes: 33 additions & 32 deletions action/dist/index.js

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

2 changes: 1 addition & 1 deletion action/dist/index.js.map

Large diffs are not rendered by default.

39 changes: 5 additions & 34 deletions action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { readFileSync } from "node:fs";
import { Octokit } from "@octokit/rest";
import { graphql } from "@octokit/graphql";

import { decideCommentAction, statuses } from "./comment.ts";
import { decideCommentAction } from "./comment.ts";
import { scanJobLog } from "./scan.ts";
import { headSha, renderTrailer } from "./trailer.ts";

if (!process.env.GITHUB_REF?.startsWith("refs/pull/")) {
Expand Down Expand Up @@ -51,9 +52,6 @@ interface Row {
[field: string]: string;
}

const warningRegex = /warning( .\d+)?:/;
const errorRegex = /error( .\d+)?:/;

const rows: Row[] = [];

const { data: jobList } = await octokit.actions.listJobsForWorkflowRun({
Expand All @@ -79,36 +77,9 @@ for (const job of jobList.jobs) {
}
const jobLog = await response.text();

const lines = jobLog.split("\n");
console.log(`total lines: ${lines.length}`);

let offset = 0;
const offsetIdx = lines.findIndex((line) => line.match("CPPWARNINGNOTIFIER_LOG_MARKER"));
if (offsetIdx !== -1) {
offset = offsetIdx;
} else {
if (ignoreNoMarker) {
continue;
}
}

let compileResult: string = statuses.success;
let firstIssueLine = 1;
const warningIdx = lines.findIndex((line) => line.match(warningRegex));
console.log(`warningIdx: ${warningIdx}`);
if (warningIdx !== -1) {
compileResult = statuses.warning;
firstIssueLine = warningIdx - offset + 1;
console.log(`matched warning line: ${lines[warningIdx]}`);
} else {
const errorIdx = lines.findIndex((line) => line.match(errorRegex));
console.log(`errorIdx: ${errorIdx}`);
if (errorIdx !== -1) {
compileResult = statuses.error;
firstIssueLine = errorIdx - offset + 1;
console.log(`matched error line: ${lines[errorIdx]}`);
}
}
const { hasMarker, status: compileResult, firstIssueLine, issue } = scanJobLog(jobLog);
if (!hasMarker && ignoreNoMarker) continue;
console.log(`${compileResult} at line ${firstIssueLine}${issue === undefined ? "" : `: ${issue}`}`);

const steps = job.steps ?? [];
const stepIndex = steps.findIndex(
Expand Down
76 changes: 76 additions & 0 deletions action/src/scan.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";

import { statuses } from "./comment.ts";
import { scanJobLog } from "./scan.ts";

// Shaped like a GitHub Actions job log: the runner echoes each run step's
// script before its output, and prefixes every line with a timestamp.
const log = (...lines: string[]) =>
lines.map((line) => `2026-11-19T01:00:00.0000000Z ${line}`).join("\n");

const buildStep = [
'##[group]Run echo "CPPWARNINGNOTIFIER_LOG_MARKER"',
'echo "CPPWARNINGNOTIFIER_LOG_MARKER"',
"cmake --build build",
"##[endgroup]",
"CPPWARNINGNOTIFIER_LOG_MARKER",
];

describe("scanJobLog", () => {
it("ignores matches before the marker's output", () => {
const result = scanJobLog(
log(
"##[group]Run if (-not $match) {",
'Write-Output "::error::No installed MSVC toolset"',
"foo.cpp(1): warning C4100: unreferenced parameter",
"##[endgroup]",
...buildStep,
"[100%] Built target test",
),
);
assert.deepEqual(result, { hasMarker: true, status: statuses.success, firstIssueLine: 1, issue: undefined });
});

it("reports an error after the marker by its line in the step", () => {
const result = scanJobLog(
log("setup", ...buildStep, "compiling", "foo.cpp(2,3): error C2131: expression did not evaluate to a constant"),
);
assert.deepEqual(result, {
hasMarker: true,
status: statuses.error,
firstIssueLine: 7,
issue: "2026-11-19T01:00:00.0000000Z foo.cpp(2,3): error C2131: expression did not evaluate to a constant",
});
});

it("prefers a warning over an error", () => {
const result = scanJobLog(log(...buildStep, "a.cpp:1:1: error: x", "b.cpp:2:2: warning: y"));
assert.deepEqual(result, {
hasMarker: true,
status: statuses.warning,
firstIssueLine: 7,
issue: "2026-11-19T01:00:00.0000000Z b.cpp:2:2: warning: y",
});
});

it("scans the whole log when there is no marker", () => {
const result = scanJobLog(log("compiling", "a.cpp:1:1: warning: y"));
assert.deepEqual(result, {
hasMarker: false,
status: statuses.warning,
firstIssueLine: 2,
issue: "2026-11-19T01:00:00.0000000Z a.cpp:1:1: warning: y",
});
});

it("scans from the step when the marker's output is missing", () => {
const result = scanJobLog(log("setup", ...buildStep.slice(0, 4), "a.cpp:1:1: error: x"));
assert.deepEqual(result, {
hasMarker: true,
status: statuses.error,
firstIssueLine: 5,
issue: "2026-11-19T01:00:00.0000000Z a.cpp:1:1: error: x",
});
});
});
43 changes: 43 additions & 0 deletions action/src/scan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { statuses } from "./comment.ts";

const marker = "CPPWARNINGNOTIFIER_LOG_MARKER";
// The marker printed by the step, as opposed to the runner's echo of the step's script that contains it.
const markerOutputRegex = new RegExp(`^(\\S+ )?${marker}\\r?$`);

const warningRegex = /warning( .\d+)?:/;
const errorRegex = /error( .\d+)?:/;

export interface LogScan {
readonly hasMarker: boolean;
readonly status: string;
// 1-based, counted from the first line that mentions the marker, which starts the step that prints it.
readonly firstIssueLine: number;
readonly issue: string | undefined;
}

// Looks for compiler warnings and errors after the marker's output, so that earlier steps, including the runner's
// echo of their scripts, are not taken for compiler output.
export function scanJobLog(jobLog: string): LogScan {
const lines = jobLog.split("\n");

const stepStart = lines.findIndex((line) => line.includes(marker));
const hasMarker = stepStart !== -1;
const offset = hasMarker ? stepStart : 0;
const markerOutput = hasMarker ? lines.findIndex((line, i) => i > stepStart && markerOutputRegex.test(line)) : -1;
const searchStart = markerOutput !== -1 ? markerOutput + 1 : offset;

const find = (regex: RegExp) => {
const i = lines.slice(searchStart).findIndex((line) => regex.test(line));
return i === -1 ? -1 : searchStart + i;
};

const warningIdx = find(warningRegex);
if (warningIdx !== -1) {
return { hasMarker, status: statuses.warning, firstIssueLine: warningIdx - offset + 1, issue: lines[warningIdx] };
}
const errorIdx = find(errorRegex);
if (errorIdx !== -1) {
return { hasMarker, status: statuses.error, firstIssueLine: errorIdx - offset + 1, issue: lines[errorIdx] };
}
return { hasMarker, status: statuses.success, firstIssueLine: 1, issue: undefined };
}