Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace github-linguist gem with Node port Nixinova/Linguist #436

Merged
merged 9 commits into from
Aug 1, 2021
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
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ RUN chmod +x /metrics/source/app/action/index.mjs \
# Based on https://github.com/github/linguist and https://github.com/github/licensed
Nixinova marked this conversation as resolved.
Show resolved Hide resolved
&& apt-get install -y ruby-full \
&& apt-get install -y git g++ cmake pkg-config libicu-dev zlib1g-dev libcurl4-openssl-dev libssl-dev ruby-dev \
&& gem install github-linguist -v 7.15.0 \
&& gem install licensed \
# Install python for node-gyp
&& apt-get install -y python3 \
Expand All @@ -35,4 +34,4 @@ ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV PUPPETEER_BROWSER_PATH "google-chrome-stable"

# Execute GitHub action
ENTRYPOINT node /metrics/source/app/action/index.mjs
ENTRYPOINT node /metrics/source/app/action/index.mjs
52 changes: 44 additions & 8 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"faker": "^5.5.3",
"jimp": "^0.16.1",
"js-yaml": "^4.1.0",
"linguist-js": "^1.4.3",
"marked": "^2.1.3",
"memory-cache": "^0.2.0",
"node-chartist": "^1.0.5",
Expand Down
2 changes: 1 addition & 1 deletion source/plugins/habits/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default async function({login, data, rest, imports, q, account}, {enabled
if (charts) {
//Check if linguist exists
console.debug(`metrics/compute/${login}/plugins > habits > searching recently used languages using linguist`)
if ((patches.length) && (await imports.which("github-linguist"))) {
if (patches.length) {
//Call language analyzer (note: using content from other plugin is usually disallowed, this is mostly for legacy purposes)
habits.linguist.available = true
const {total, stats} = await recent_analyzer({login, data, imports, rest, account}, {days, load:from || 1000, tempdir:"habits"})
Expand Down
16 changes: 7 additions & 9 deletions source/plugins/languages/analyzers.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import linguist from "linguist-js"

/**Indepth analyzer */
export async function indepth({login, data, imports, repositories}, {skipped}) {
//Check prerequisites
if (!await imports.which("github-linguist"))
throw new Error("Feature requires github-linguist")

//Compute repositories stats from fetched repositories
const results = {total:0, lines:{}, stats:{}, commits:0, files:0, missed:0}
Expand Down Expand Up @@ -46,9 +45,6 @@ export async function indepth({login, data, imports, repositories}, {skipped}) {

/**Recent languages activity */
export async function recent({login, data, imports, rest, account}, {skipped = [], days = 0, load = 0, tempdir = "recent"}) {
//Check prerequisites
if (!await imports.which("github-linguist"))
throw new Error("Feature requires github-linguist")

//Get user recent activity
console.debug(`metrics/compute/${login}/plugins > languages > querying api`)
Expand Down Expand Up @@ -141,9 +137,9 @@ export async function recent({login, data, imports, rest, account}, {skipped = [

/**Analyze a single repository */
async function analyze({login, imports, data}, {results, path}) {
//Spawn linguist process and map files to languages
//Gather language data
console.debug(`metrics/compute/${login}/plugins > languages > indepth > running linguist`)
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])))
const {results:files, languages:languageResults} = await linguist(path)

//Processing diff
const per_page = 1
Expand All @@ -168,8 +164,10 @@ async function analyze({login, imports, data}, {results, path}) {
return
//File marker
if (/^[+]{3}\sb[/](?<file>[\s\S]+)$/.test(line)) {
file = line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file ?? null
file = line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file.replace(/^/, `${path}/`) ?? null
lang = files[file] ?? null
if (lang in languageResults.data || lang in languageResults.prose)
lang = null
edited.add(file)
return
}
Expand Down