GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lite/src/github-items.ts at main · ultralytics/lite

Expand file tree

/

github-items.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
169 lines (157 loc) · 8.64 KB
/

github-items.ts

Copy path

File metadata and controls

169 lines (157 loc) · 8.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
interface Candidate {
index: number;
priority: number;
url: string;
}
// What a session keeps so a reference redrawn away still resolves as it did when it was seen: its ambiguous
// references, as "kind number word" where the word before a reference may name its repository, and every
// repository it named.
export interface RememberedReferences {
mentions: string[];
repositories: string[];
}
export interface GitHubReferences {
explicit: string[];
// One entry per ambiguous reference: the item it would name in each repository the session has named.
inferred: string[][];
// The remembered references with this text's added: mentions it no longer shows, then its own.
remembered: RememberedReferences;
}
// Repository-qualified references are certain. A bare number from prose or an unqualified GitHub CLI command
// may belong to any repository the session has named, and a short name narrows it to that one: GitHub
// activity must confirm one before the panel shows it. A logged commit's trailing (#N) is history, not work,
// and so is the feedback link at the foot of every Codecov comment a command prints.
// biome-ignore lint/suspicious/noControlCharactersInRegex: a color code has to be named to be removed.
const COLOR = /\u001b\[[0-9;?]*[ -/]*[@-~]/g;
// biome-ignore lint/suspicious/noControlCharactersInRegex: OSC hyperlinks are terminal framing.
const HYPERLINK = /\u001b]8;[^;]*;([^\u0007\u001b]*)(?:\u0007|\u001b\\)/g;
const GITHUB_ITEM = /\bhttps:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/(pull|issues)\/([1-9]\d{0,8})(?!\w)/gi;
const GITHUB_REPOSITORY =
/\bhttps:\/\/github\.com\/([\w.-]+)\/([\w.-]+?)(?:\.git)?(?=\/(?:pulls?|issues)(?:[/?#\s]|$)|\/?(?:[?#\s]|$))/gi;
const QUALIFIED_ITEM = /(?:^|[^\w./-])(\w[\w.-]*)\/(\w[\w.-]*)#([1-9]\d{0,8})(?!\w)/g;
const ITEM_MENTION =
/(?:^|[^\w./-])(\w[\w.-]*\/\w[\w.-]*)[ \t]+(pull requests?|PRs?|issues?)[ \t]+#?([1-9]\d{0,8})(?!\w|\.\d)/gi;
const ITEM_REFERENCE =
/(?:^|[^\w./-])(?:(\w[\w.-]*)[ \t]+)?(?:(pull requests?|PRs?|issues?)[ \t]+#?|#)([1-9]\d{0,8})(?!\w|\.\d)/gi;
const LOGGED_COMMIT = /^([^\w\n]*[\da-f]{7,40} .*)\(#\d+\)[ \t]*$/gim;
const CODECOV_FOOTER = /Thoughts\s+on\s+this\s+report\?\s+\[Let\s+us\s+know!\]\([^\s)]*\)/gi;
const GH_COMMAND = /\bgh\s+([\w-]+)\s+([\w-]+)((?:(?!\bgh\s)[^;&|'"\\\r\n]|\\.|'[^']*'|"(?:\\.|[^"\\])*")*)/gi;
const GH_REPOSITORY =
/^((?:[^'"\\]|\\.|'[^']*'|"(?:\\.|[^"\\])*")*?\s)(?:--repo|-R)(?:=|\s+)(?:([\w.-]+\/[\w.-]+)|'([\w.-]+\/[\w.-]+)'|"([\w.-]+\/[\w.-]+)")/i;
const GH_API =
/\bgh\s+api\s+["']?(?:https:\/\/api\.github\.com\/)?\/?repos\/([\w.-]+)\/([\w.-]+)\/(issues|pulls)\/([1-9]\d{0,8})(?![\w/])/gi;
const itemKind = (word: string | undefined) => (word?.toLowerCase().startsWith("issue") ? "issues" : "pull");
export function githubItemReferences(
output: string,
remote: string,
terminalStream: string,
prose: string,
remembered: RememberedReferences = { mentions: [], repositories: [] },
): GitHubReferences {
const text = output.replace(COLOR, "").replace(CODECOV_FOOTER, "");
const userText = prose.replace(COLOR, "");
const candidates: Candidate[] = [];
const repositories = new Map<string, string>();
const nameRepository = (owner: string, name: string) => {
const repository = `${owner}/${name.replace(/\.+$/, "").replace(/\.git$/i, "")}`;
if (!repositories.has(repository.toLowerCase())) repositories.set(repository.toLowerCase(), repository);
};
const add = (match: RegExpMatchArray, repository: string, kind: string, number: string, priority: number) => {
const [owner, name] = repository.split("/");
nameRepository(owner, name);
candidates.push({ index: match.index ?? 0, priority, url: `https://github.com/${repository}/${kind}/${number}` });
};
const base = remote.match(/^https:\/\/github\.com\/([\w.-]+)\/([\w.-]+?)(?:\.git)?\/?$/i);
if (base) nameRepository(base[1], base[2]);
for (const repository of remembered.repositories) nameRepository(...(repository.split("/") as [string, string]));
for (const match of text.matchAll(GITHUB_REPOSITORY)) nameRepository(match[1], match[2]);
for (const match of text.matchAll(GITHUB_ITEM))
add(match, `${match[1]}/${match[2]}`, match[3].toLowerCase(), match[4], 4);
for (const hyperlink of terminalStream.matchAll(HYPERLINK)) {
for (const match of hyperlink[1].matchAll(GITHUB_ITEM))
add(match, `${match[1]}/${match[2]}`, match[3].toLowerCase(), match[4], 4);
}
for (const source of new Set([text, userText])) {
for (const match of source.matchAll(QUALIFIED_ITEM)) add(match, `${match[1]}/${match[2]}`, "issues", match[3], 1);
for (const match of source.matchAll(ITEM_MENTION)) add(match, match[1], itemKind(match[2]), match[3], 2);
}
// Ambiguous forms are read once every repository the session names is known.
const mentions: string[] = [];
for (const match of text.matchAll(GH_COMMAND)) {
const repository = match[3].match(GH_REPOSITORY)?.slice(2).find(Boolean);
// Any command names its repository, even one that names no item or whose number is a shell variable.
if (repository) nameRepository(...(repository.split("/") as [string, string]));
if (!/^(?:issue|pr)$/i.test(match[1]) || /^(?:create|list|status)$/i.test(match[2])) continue;
const numbers = match[3]
.replace(GH_REPOSITORY, "$1")
.replace(/'[^']*'|"(?:\\.|[^"\\])*"/g, "")
.match(/(?:^|\s)([1-9]\d{0,8})(?=[\s)]|$)/g);
if (numbers?.length !== 1) continue;
const kind = match[1].toLowerCase() === "pr" ? "pull" : "issues";
if (repository) add(match, repository, kind, numbers[0].trim(), 2);
else mentions.push(`${kind} ${numbers[0].trim()} `);
}
for (const match of text.matchAll(GH_API))
add(match, `${match[1]}/${match[2]}`, match[3].toLowerCase() === "pulls" ? "pull" : "issues", match[4], 3);
for (const source of new Set([text, userText])) {
for (const match of source.replace(LOGGED_COMMIT, "$1").matchAll(ITEM_REFERENCE))
mentions.push(`${itemKind(match[2])} ${match[3]} ${match[1]?.toLowerCase() ?? ""}`);
}
candidates.sort((left, right) => left.index - right.index);
const items = new Map<string, Candidate>();
for (const candidate of candidates) {
const key = itemKey(candidate.url);
const current = items.get(key);
if (!current || candidate.priority > current.priority) items.set(key, candidate);
}
const explicit = [...items.values()].map((candidate) => candidate.url);
const inferred = new Map<string, string[]>();
const names = new Set([...repositories.keys()].map((repository) => repository.split("/")[1]));
for (const mention of new Set([...mentions, ...remembered.mentions])) {
const [kind, number, word] = mention.split(" ");
const group = [...repositories.values()]
.filter((repository) => !names.has(word) || repository.split("/")[1].toLowerCase() === word)
.map((repository) => `https://github.com/${repository}/${kind}/${number}`);
if (group.length && !group.some((url) => items.has(itemKey(url)))) inferred.set(group.join(" "), group);
}
const shown = new Set(mentions);
return {
explicit,
inferred: [...inferred.values()],
remembered: {
mentions: [...remembered.mentions.filter((mention) => !shown.has(mention)), ...shown],
repositories: [...repositories.values()],
},
};
}
const RECENT_ACTIVITY_MS = 30 * 24 * 60 * 60 * 1000;
// Each ambiguous reference keeps at most one item: the most recently updated of its candidates, and only
// one GitHub confirms was active in the last 30 days.
export function likelyGitHubItems<T extends { updatedAt: string | null; url: string }>(
items: T[],
inferred: string[][],
now = Date.now(),
): T[] {
const updated = (item: T) => (item.updatedAt ? Date.parse(item.updatedAt) : Number.NaN);
const byKey = new Map(items.map((item) => [itemKey(item.url), item]));
const candidates = new Set(inferred.flat().map(itemKey));
const chosen = new Set<T>();
for (const group of inferred) {
const [best] = group
.map((url) => byKey.get(itemKey(url)))
.filter((item): item is T => !!item && now - updated(item) <= RECENT_ACTIVITY_MS)
.sort((left, right) => updated(right) - updated(left));
if (best) chosen.add(best);
}
return items.filter((item) => !candidates.has(itemKey(item.url)) || chosen.has(item));
}
export function mergeGitHubItems<T extends { url: string }>(current: T[], updates: T[]): T[] {
const items = new Map(current.map((item) => [itemKey(item.url), item]));
for (const item of updates) items.set(itemKey(item.url), item);
return [...items.values()];
}
export function itemKey(url: string) {
const [, , , owner, repository, , number] = url.split("/");
return `${owner}/${repository}#${number}`.toLowerCase();
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.