Skip to content

Commit

Permalink
fix: enhance error log context and config path handling
Browse files Browse the repository at this point in the history
Refine error messages and config path management during push events.
  • Loading branch information
gentlementlegen committed Oct 12, 2024
1 parent 9435e12 commit 9a4bede
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/github/handlers/push-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ function constructErrorBody(
errors: Iterable<ValueError> | (YAML.YAMLError | ValueError)[],
rawData: string | null,
repository: GitHubContext<"push">["payload"]["repository"],
after: string
after: string,
configPath: string
) {
const body = [];
if (errors) {
for (const error of errors) {
body.push("> [!CAUTION]\n");
if (error instanceof YAMLError) {
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${error.linePos?.[0].line || 0}`);
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${configPath}#L${error.linePos?.[0].line || 0}`);
} else if (rawData) {
const lineCounter = new LineCounter();
const doc = YAML.parseDocument(rawData, { lineCounter });
Expand All @@ -28,7 +29,7 @@ function constructErrorBody(
}
const node = doc.getIn(path, true) as Node;
const linePosStart = lineCounter.linePos(node?.range?.[0] || 0);
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${CONFIG_FULL_PATH}#L${linePosStart.line}`);
body.push(`> https://github.com/${repository.owner?.login}/${repository.name}/blob/${after}/${configPath}#L${linePosStart.line}`);
}
const message = [];
if (error instanceof YAMLError) {
Expand Down Expand Up @@ -125,7 +126,17 @@ export default async function handlePushEvent(context: GitHubContext<"push">) {
const { payload } = context;
const { repository, commits, after } = payload;
const configPaths = [CONFIG_FULL_PATH, DEV_CONFIG_FULL_PATH];
const didConfigurationFileChange = commits.some((commit) => configPaths.some((path) => commit.modified?.includes(path) || commit.added?.includes(path)));
const didConfigurationFileChange = commits.some((commit) =>
configPaths.some((path) => {
if (commit.modified?.includes(path) || commit.added?.includes(path)) {
// Keeps only the config that matched the modified elements
configPaths.length = 0;
configPaths.push(path);
return true;
}
return false;
})
);

if (!didConfigurationFileChange || !repository.owner) {
return;
Expand All @@ -143,7 +154,7 @@ export default async function handlePushEvent(context: GitHubContext<"push">) {
try {
if (errors.length) {
const body = [];
body.push(...constructErrorBody(errors, rawData, repository, after));
body.push(...constructErrorBody(errors, rawData, repository, after, configPaths[0]));
await createCommitComment(
context,
{
Expand Down
4 changes: 2 additions & 2 deletions src/github/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function fetchActionManifest(context: GitHubContext<"issue_comment.created
return manifest;
}
} catch (e) {
console.warn(`Could not find a manifest for ${owner}/${repo}: ${e}`);
console.warn(`Could not find a manifest for Action ${owner}/${repo}: ${e}`);
}
return null;
}
Expand All @@ -52,7 +52,7 @@ async function fetchWorkerManifest(url: string): Promise<Manifest | null> {
_manifestCache[url] = manifest;
return manifest;
} catch (e) {
console.warn(`Could not find a manifest for ${manifestUrl}: ${e}`);
console.warn(`Could not find a manifest for Worker ${manifestUrl}: ${e}`);
}
return null;
}
Expand Down

0 comments on commit 9a4bede

Please sign in to comment.