Skip to content

Commit 7eb9afc

Browse files
authored
Add plugin_languages_sections option and "recently-used" section (lowlighter#327)
1 parent 2fd7452 commit 7eb9afc

File tree

5 files changed

+203
-131
lines changed

5 files changed

+203
-131
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**Indepth analyzer */
2+
export async function indepth({login, data, imports}, {skipped}) {
3+
//Check prerequisites
4+
if (!await imports.which("github-linguist"))
5+
throw new Error("Feature requires github-linguist")
6+
7+
//Compute repositories stats from fetched repositories
8+
const results = {total:0, stats:{}}
9+
for (const repository of data.user.repositories.nodes) {
10+
//Skip repository if asked
11+
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
12+
console.debug(`metrics/compute/${login}/plugins > languages > skipped repository ${repository.owner.login}/${repository.name}`)
13+
continue
14+
}
15+
16+
//Repository handle
17+
const repo = `${repository.owner.login}/${repository.name}`
18+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > checking ${repo}`)
19+
20+
//Temporary directory
21+
const path = imports.paths.join(imports.os.tmpdir(), `${data.user.databaseId}-${repo.replace(/[^\w]/g, "_")}`)
22+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > cloning ${repo} to temp dir ${path}`)
23+
24+
//Process
25+
try {
26+
//Git clone into temporary directory
27+
await imports.fs.rmdir(path, {recursive:true})
28+
await imports.fs.mkdir(path, {recursive:true})
29+
const git = await imports.git(path)
30+
await git.clone(`https://github.com/${repo}`, ".").status()
31+
32+
//Analyze repository
33+
await analyze(arguments[0], {results, path})
34+
}
35+
catch {
36+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > an error occured while processing ${repo}, skipping...`)
37+
}
38+
finally {
39+
//Cleaning
40+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > cleaning temp dir ${path}`)
41+
await imports.fs.rmdir(path, {recursive:true})
42+
}
43+
}
44+
return results
45+
}
46+
47+
/**Recent languages activity */
48+
export async function recent({login, data, imports, rest, account}, {skipped}) {
49+
//Check prerequisites
50+
if (!await imports.which("github-linguist"))
51+
throw new Error("Feature requires github-linguist")
52+
53+
//Get user recent activity
54+
console.debug(`metrics/compute/${login}/plugins > languages > querying api`)
55+
const commits = [], days = 14, pages = 3, results = {total:0, stats:{}}
56+
try {
57+
for (let page = 1; page <= pages; page++) {
58+
console.debug(`metrics/compute/${login}/plugins > languages > loading page ${page}`)
59+
commits.push(...(await rest.activity.listEventsForAuthenticatedUser({username:login, per_page:100, page})).data
60+
.filter(({type}) => type === "PushEvent")
61+
.filter(({actor}) => account === "organization" ? true : actor.login === login)
62+
.filter(({repo:{name:repo}}) => (!skipped.includes(repo.toLocaleLowerCase())) && (!skipped.includes(repo.toLocaleLowerCase().split("/").pop())))
63+
.filter(({created_at}) => new Date(created_at) > new Date(Date.now() - days * 24 * 60 * 60 * 1000))
64+
)
65+
}
66+
}
67+
catch {
68+
console.debug(`metrics/compute/${login}/plugins > languages > no more page to load`)
69+
}
70+
console.debug(`metrics/compute/${login}/plugins > languages > ${commits.length} commits loaded`)
71+
72+
//Retrieve edited files and filter edited lines (those starting with +/-) from patches
73+
console.debug(`metrics/compute/${login}/plugins > languages > loading patches`)
74+
const patches = [
75+
...await Promise.allSettled(
76+
commits
77+
.flatMap(({payload}) => payload.commits).map(commit => commit.url)
78+
.map(async commit => (await rest.request(commit)).data.files),
79+
),
80+
]
81+
.filter(({status}) => status === "fulfilled")
82+
.map(({value}) => value)
83+
.flatMap(files => files.map(file => ({name:imports.paths.basename(file.filename), patch:file.patch ?? ""})))
84+
.map(({name, patch}) => ({name, patch:patch.split("\n").filter(line => /^[+]/.test(line)).map(line => line.substring(1)).join("\n")}))
85+
86+
//Temporary directory
87+
const path = imports.paths.join(imports.os.tmpdir(), `${data.user.databaseId}`)
88+
console.debug(`metrics/compute/${login}/plugins > languages > creating temp dir ${path} with ${patches.length} files`)
89+
90+
//Process
91+
try {
92+
//Save patches in temporary directory
93+
await imports.fs.rmdir(path, {recursive:true})
94+
await imports.fs.mkdir(path, {recursive:true})
95+
await Promise.all(patches.map(({name, patch}, i) => imports.fs.writeFile(imports.paths.join(path, `${i}${imports.paths.extname(name)}`), patch)))
96+
97+
//Create temporary git repository
98+
console.debug(`metrics/compute/${login}/plugins > languages > creating temp git repository`)
99+
const git = await imports.git(path)
100+
await git.init().add(".").addConfig("user.name", login).addConfig("user.email", "<>").commit("linguist").status()
101+
102+
//Analyze repository
103+
await analyze(arguments[0], {results, path})
104+
}
105+
catch {
106+
console.debug(`metrics/compute/${login}/plugins > languages > an error occured while processing recently used languages`)
107+
}
108+
finally {
109+
//Cleaning
110+
console.debug(`metrics/compute/${login}/plugins > languages > cleaning temp dir ${path}`)
111+
await imports.fs.rmdir(path, {recursive:true})
112+
}
113+
114+
console.log(results)
115+
return results
116+
}
117+
118+
/**Analyze a single repository */
119+
async function analyze({login, imports}, {results, path}) {
120+
//Spawn linguist process and map files to languages
121+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > running linguist`)
122+
const files = Object.fromEntries(Object.entries(JSON.parse(await imports.run("github-linguist --json", {cwd:path}, {log:false}))).flatMap(([lang, files]) => files.map(file => [file, lang])))
123+
124+
console.log(files)
125+
126+
//Processing diff
127+
const per_page = 10
128+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > checking git log`)
129+
for (let page = 0; ; page++) {
130+
try {
131+
const stdout = await imports.run(`git log --author="${login}" --format="" --patch --max-count=${per_page} --skip=${page*per_page}`, {cwd:path}, {log:false})
132+
let file = null, lang = null
133+
if (!stdout.trim().length) {
134+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > no more commits`)
135+
break
136+
}
137+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > processing commits ${page*per_page} from ${(page+1)*per_page}`)
138+
for (const line of stdout.split("\n").map(line => line.trim())) {
139+
//Ignore empty lines or unneeded lines
140+
if ((!/^[+]/.test(line))||(!line.length))
141+
continue
142+
//File marker
143+
if (/^[+]{3}\sb[/](?<file>[\s\S]+)$/.test(line)) {
144+
file = line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file ?? null
145+
lang = files[file] ?? null
146+
continue
147+
}
148+
//Ignore unkonwn languages
149+
if (!lang)
150+
continue
151+
//Added line marker
152+
if (/^[+]\s(?<line>[\s\S]+)$/.test(line)) {
153+
const size = Buffer.byteLength(line.match(/^[+]\s(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8")
154+
results.stats[lang] = (results.stats[lang] ?? 0) + size
155+
results.total += size
156+
}
157+
}
158+
}
159+
catch {
160+
console.debug(`metrics/compute/${login}/plugins > languages > indepth > an error occured on page ${page}, skipping...`)
161+
}
162+
}
163+
164+
}

source/plugins/languages/indepth.mjs

Lines changed: 0 additions & 105 deletions
This file was deleted.

source/plugins/languages/index.mjs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//Imports
2-
import indepth_analyzer from "./indepth.mjs"
2+
import { indepth as indepth_analyzer, recent as recent_analyzer } from "./analyzers.mjs"
33

44
//Setup
5-
export default async function({login, data, imports, q, account}, {enabled = false} = {}) {
5+
export default async function({login, data, imports, q, rest, account}, {enabled = false} = {}) {
66
//Plugin execution
77
try {
88
//Check if plugin is enabled and requirements are met
99
if ((!enabled) || (!q.languages))
1010
return null
1111

1212
//Load inputs
13-
let {ignored, skipped, colors, details, threshold, limit, indepth} = imports.metadata.plugins.languages.inputs({data, account, q})
13+
let {ignored, skipped, colors, details, threshold, limit, indepth, sections} = imports.metadata.plugins.languages.inputs({data, account, q})
1414
threshold = (Number(threshold.replace(/%$/, "")) || 0) / 100
1515
skipped.push(...data.shared["repositories.skipped"])
1616
if (!limit)
@@ -25,7 +25,7 @@ export default async function({login, data, imports, q, account}, {enabled = fal
2525

2626
//Iterate through user's repositories and retrieve languages data
2727
console.debug(`metrics/compute/${login}/plugins > languages > processing ${data.user.repositories.nodes.length} repositories`)
28-
const languages = {details, colors:{}, total:0, stats:{}}
28+
const languages = {sections, details, colors:{}, total:0, stats:{}, "stats.recent":{}}
2929
for (const repository of data.user.repositories.nodes) {
3030
//Skip repository if asked
3131
if ((skipped.includes(repository.name.toLocaleLowerCase())) || (skipped.includes(`${repository.owner.login}/${repository.name}`.toLocaleLowerCase()))) {
@@ -34,33 +34,35 @@ export default async function({login, data, imports, q, account}, {enabled = fal
3434
}
3535
//Process repository languages
3636
for (const {size, node:{color, name}} of Object.values(repository.languages.edges)) {
37-
//Ignore language if asked
38-
if (ignored.includes(name.toLocaleLowerCase())) {
39-
console.debug(`metrics/compute/${login}/plugins > languages > ignored language ${name}`)
40-
continue
41-
}
42-
//Update language stats
4337
languages.stats[name] = (languages.stats[name] ?? 0) + size
4438
languages.colors[name] = colors[name.toLocaleLowerCase()] ?? color ?? "#ededed"
4539
languages.total += size
4640
}
4741
}
4842

43+
//Recently used languages
44+
if ((sections.includes("recently-used"))&&(["user", "organization"].includes(account))) {
45+
console.debug(`metrics/compute/${login}/plugins > languages > using recent analyzer`)
46+
languages["stats.recent"] = await recent_analyzer({login, data, imports, rest, account}, {skipped})
47+
}
48+
4949
//Indepth mode
5050
if (indepth) {
5151
console.debug(`metrics/compute/${login}/plugins > languages > switching to indepth mode (this may take some time)`)
52-
Object.assign(languages, await indepth_analyzer({login, data, imports}, {skipped, ignored}))
52+
Object.assign(languages, await indepth_analyzer({login, data, imports}, {skipped}))
5353
}
5454

5555
//Compute languages stats
56-
console.debug(`metrics/compute/${login}/plugins > languages > computing stats`)
57-
languages.favorites = Object.entries(languages.stats).sort(([_an, a], [_bn, b]) => b - a).slice(0, limit).map(([name, value]) => ({name, value, size:value, color:languages.colors[name], x:0})).filter(({value}) => value / languages.total > threshold)
58-
const visible = {total:Object.values(languages.favorites).map(({size}) => size).reduce((a, b) => a + b, 0)}
59-
for (let i = 0; i < languages.favorites.length; i++) {
60-
languages.favorites[i].value /= visible.total
61-
languages.favorites[i].x = (languages.favorites[i - 1]?.x ?? 0) + (languages.favorites[i - 1]?.value ?? 0)
62-
if ((colors[i]) && (!colors[languages.favorites[i].name.toLocaleLowerCase()]))
63-
languages.favorites[i].color = colors[i]
56+
for (const {section, stats = {}, total = 0} of [{section:"favorites", stats:languages.stats, total:languages.total}, {section:"recent", ...languages["stats.recent"]}]) {
57+
console.debug(`metrics/compute/${login}/plugins > languages > computing stats ${section}`)
58+
languages[section] = Object.entries(stats).filter(([name]) => !ignored.includes(name.toLocaleLowerCase())).sort(([_an, a], [_bn, b]) => b - a).slice(0, limit).map(([name, value]) => ({name, value, size:value, color:languages.colors[name], x:0})).filter(({value}) => value / total > threshold)
59+
const visible = {total:Object.values(languages[section]).map(({size}) => size).reduce((a, b) => a + b, 0)}
60+
for (let i = 0; i < languages[section].length; i++) {
61+
languages[section][i].value /= visible.total
62+
languages[section][i].x = (languages[section][i - 1]?.x ?? 0) + (languages[section][i - 1]?.value ?? 0)
63+
if ((colors[i]) && (!colors[languages[section][i].name.toLocaleLowerCase()]))
64+
languages[section][i].color = colors[i]
65+
}
6466
}
6567

6668
//Results

source/plugins/languages/metadata.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ inputs:
3939
min: 0
4040
max: 8
4141

42+
# Sections to display
43+
plugin_languages_sections:
44+
description: Sections to display
45+
type: array
46+
format: comma-separated
47+
default: most-used
48+
example: most-used, recently-used
49+
values:
50+
- most-used # Most used languages
51+
- recently-used # Recently used languages
52+
4253
# Overrides default languages colors
4354
# Use `${n}:${color}` to change the color of the n-th most used language (e.g. "0:red" to make your most used language red)
4455
# Use `${language}:${color}` to change the color of named language (e.g. "javascript:red" to make JavaScript language red, language case is ignored)

0 commit comments

Comments
 (0)