Skip to content

Commit f2b5154

Browse files
committed
Improve path regex to only match on the relevant paths
1 parent c18fc86 commit f2b5154

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/gha_logs.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::handlers::Context;
33
use anyhow::Context as _;
44
use hyper::header::{CACHE_CONTROL, CONTENT_SECURITY_POLICY, CONTENT_TYPE};
55
use hyper::{Body, Response, StatusCode};
6+
use itertools::Itertools;
67
use std::collections::VecDeque;
78
use std::str::FromStr;
89
use std::sync::Arc;
@@ -134,13 +135,11 @@ async fn process_logs(
134135
.await
135136
.context("unable to fetch git tree for the repository")?;
136137

137-
let tree_roots: Vec<_> = trees
138+
let tree_roots = trees
138139
.tree
139140
.iter()
140141
.filter_map(|t| (t.object_type == "tree").then_some(&t.path))
141-
.collect();
142-
let tree_roots =
143-
serde_json::to_string(&tree_roots).context("unable to serialize tree roots")?;
142+
.join("|");
144143

145144
anyhow::Result::<_>::Ok((job, tree_roots))
146145
};
@@ -212,8 +211,6 @@ async fn process_logs(
212211
import {{ AnsiUp }} from '{ANSI_UP_URL}'
213212
214213
var logs = {logs};
215-
var tree_roots = {tree_roots};
216-
217214
var ansi_up = new AnsiUp();
218215
219216
// 1. Tranform the ANSI escape codes to HTML
@@ -242,20 +239,10 @@ async fn process_logs(
242239
);
243240
244241
// 5. Add anchors to GitHub around some paths
245-
const pathRegex = /((?:[A-Za-z]:)?[a-zA-Z0-9_.$-]*(?:[\\/][a-zA-Z0-9_$.-]+)+)(?::([0-9]*):([0-9]*))?/g;
246-
html = html.replace(pathRegex, (match, path, line, col) => {{
247-
const removePrefix = (value, prefix) =>
248-
value.startsWith(prefix) ? value.slice(prefix.length) : value;
249-
250-
var path = removePrefix(removePrefix(path, "/checkout"), "/");
251-
var root = path.substring(0, path.indexOf("/"));
252-
253-
if (tree_roots.includes(root)) {{
254-
const pos = (line !== undefined) ? `#L${{line}}` : "";
255-
return `<a href="https://github.com/{owner}/{repo}/blob/{sha}/${{path}}${{pos}}" class="path-marker">${{match}}</a>`;
256-
}}
257-
258-
return match;
242+
const pathRegex = /(?<boundary>[^a-zA-Z0-9.\\/])(?<inner>(?:[\\\/]?(?:checkout[\\\/])?(?<path>(?:{tree_roots})[\\\/][a-zA-Z0-9_$\-.\\\/]+))(?::(?<line>[0-9]+):(?<col>[0-9]+))?)/g;
243+
html = html.replace(pathRegex, (match, boundary, inner, path, line, col) => {{
244+
const pos = (line !== undefined) ? `#L${{line}}` : "";
245+
return `${{boundary}}<a href="https://github.com/{owner}/{repo}/blob/{sha}/${{path}}${{pos}}" class="path-marker">${{inner}}</a>`;
259246
}});
260247
261248
// 6. Add the html to the DOM

0 commit comments

Comments
 (0)