|
| 1 | +//Imports |
| 2 | +import fs from "fs/promises" |
| 3 | +import os from "os" |
| 4 | +import paths from "path" |
| 5 | +import git from "simple-git" |
| 6 | +import {filters} from "../../../app/metrics/utils.mjs" |
| 7 | + |
| 8 | +/**Analyzer */ |
| 9 | +export class Analyzer { |
| 10 | + |
| 11 | + /**Constructor */ |
| 12 | + constructor(login, {account = "bypass", authoring = [], uid = Math.random(), shell, rest = null, context = {mode:"user"}, skipped = [], categories = ["programming", "markup"], timeout = {global:NaN, repositories:NaN}}) { |
| 13 | + //User informations |
| 14 | + this.login = login |
| 15 | + this.account = account |
| 16 | + this.authoring = authoring |
| 17 | + this.uid = uid |
| 18 | + this.gpg = [] |
| 19 | + |
| 20 | + //Utilities |
| 21 | + this.shell = shell |
| 22 | + this.rest = rest |
| 23 | + this.context = context |
| 24 | + this.markers = { |
| 25 | + hash:/\b[0-9a-f]{40}\b/, |
| 26 | + file:/^[+]{3}\sb[/](?<file>[\s\S]+)$/, |
| 27 | + line:/^(?<op>[-+])\s*(?<content>[\s\S]+)$/, |
| 28 | + } |
| 29 | + this.parser = /^(?<login>[\s\S]+?)\/(?<name>[\s\S]+?)(?:@(?<branch>[\s\S]+?)(?::(?<ref>[\s\S]+))?)?$/ |
| 30 | + this.consumed = false |
| 31 | + |
| 32 | + //Options |
| 33 | + this.skipped = skipped |
| 34 | + this.categories = categories |
| 35 | + this.timeout = timeout |
| 36 | + |
| 37 | + //Results |
| 38 | + this.results = {partial: {global:false, repositories:false}, total: 0, lines: {}, stats: {}, colors: {}, commits: 0, files: 0, missed: {lines: 0, bytes: 0, commits: 0}, elapsed:0} |
| 39 | + this.debug(`instantiated a new ${this.constructor.name}`) |
| 40 | + } |
| 41 | + |
| 42 | + /**Run analyzer */ |
| 43 | + async run(runner) { |
| 44 | + if (this.consumed) |
| 45 | + throw new Error("This analyzer has already been consumed, another instance needs to be created to perform a new analysis") |
| 46 | + this.consumed = true |
| 47 | + const results = await new Promise(async solve => { |
| 48 | + let completed = false |
| 49 | + if (Number.isFinite(this.timeout.global)) { |
| 50 | + this.debug(`timeout set to ${this.timeout.global}m`) |
| 51 | + setTimeout(() => { |
| 52 | + if (!completed) { |
| 53 | + try { |
| 54 | + this.debug(`reached maximum execution time of ${this.timeout.global}m for analysis`) |
| 55 | + this.results.partial.global = true |
| 56 | + solve(this.results) |
| 57 | + } |
| 58 | + catch { |
| 59 | + //Ignore errors |
| 60 | + } |
| 61 | + } |
| 62 | + }, this.timeout.global * 60 * 1000) |
| 63 | + } |
| 64 | + await runner() |
| 65 | + completed = true |
| 66 | + solve(this.results) |
| 67 | + }) |
| 68 | + results.partial = (results.partial.global)||(results.partial.repositories) |
| 69 | + return results |
| 70 | + } |
| 71 | + |
| 72 | + /**Parse repository */ |
| 73 | + parse(repository) { |
| 74 | + let branch = null, ref = null |
| 75 | + if (typeof repository === "string") { |
| 76 | + if (!this.parser.test(repository)) |
| 77 | + throw new TypeError(`"${repository}" pattern is not supported`) |
| 78 | + const {login, name, ...groups} = repository.match(this.parser)?.groups ?? {} |
| 79 | + repository = {owner:{login}, name} |
| 80 | + branch = groups.branch ?? null |
| 81 | + ref = groups.ref ?? null |
| 82 | + } |
| 83 | + const repo = `${repository.owner.login}/${repository.name}` |
| 84 | + const path = paths.join(os.tmpdir(), `${this.uid}-${repo.replace(/[^\w]/g, "_")}`) |
| 85 | + return {repo, path, branch, ref} |
| 86 | + } |
| 87 | + |
| 88 | + /**Clone a repository */ |
| 89 | + async clone(repository) { |
| 90 | + const {repo, branch, path} = this.parse(repository) |
| 91 | + let url = /^https?:\/\//.test(repo) ? repo : `https://github.com/${repo}` |
| 92 | + try { |
| 93 | + this.debug(`cloning ${url} to ${path}`) |
| 94 | + await fs.rm(path, {recursive: true, force: true}) |
| 95 | + await fs.mkdir(path, {recursive: true}) |
| 96 | + await git(path).clone(url, ".", ["--single-branch"]).status() |
| 97 | + this.debug(`cloned ${url} to ${path}`) |
| 98 | + if (branch) { |
| 99 | + this.debug(`switching to branch ${branch} for ${repo}`) |
| 100 | + await git(path).branch(branch) |
| 101 | + } |
| 102 | + return true |
| 103 | + } |
| 104 | + catch (error) { |
| 105 | + this.debug(`failed to clone ${url} (${error})`) |
| 106 | + this.clean(path) |
| 107 | + return false |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /**Analyze a repository */ |
| 112 | + async analyze(path, {commits = []} = {}) { |
| 113 | + const cache = {files:{}, languages:{}} |
| 114 | + const start = Date.now() |
| 115 | + let elapsed = 0, processed = 0 |
| 116 | + if (this.timeout.repositories) |
| 117 | + this.debug(`timeout for repository analysis set to ${this.timeout.repositories}m`) |
| 118 | + for (const commit of commits) { |
| 119 | + elapsed = (Date.now() - start)/1000/60 |
| 120 | + if ((this.timeout.repositories)&&(elapsed > this.timeout.repositories)) { |
| 121 | + this.results.partial.repositories = true |
| 122 | + this.debug(`reached maximum execution time of ${this.timeout.repositories}m for repository analysis (${elapsed}m elapsed)`) |
| 123 | + break |
| 124 | + } |
| 125 | + try { |
| 126 | + const {total, files, missed, lines, stats} = await this.linguist(path, {commit, cache}) |
| 127 | + this.results.commits++ |
| 128 | + this.results.total += total |
| 129 | + this.results.files += files |
| 130 | + this.results.missed.lines += missed.lines |
| 131 | + this.results.missed.bytes += missed.bytes |
| 132 | + for (const language in lines) { |
| 133 | + if (this.categories.includes(cache.languages[language]?.type)) |
| 134 | + this.results.lines[language] = (this.results.lines[language] ?? 0) + lines[language] |
| 135 | + } |
| 136 | + for (const language in stats) { |
| 137 | + if (this.categories.includes(cache.languages[language]?.type)) |
| 138 | + this.results.stats[language] = (this.results.stats[language] ?? 0) + stats[language] |
| 139 | + } |
| 140 | + } |
| 141 | + catch (error) { |
| 142 | + this.debug(`skipping commit ${commit.sha} (${error})`) |
| 143 | + this.results.missed.commits++ |
| 144 | + } |
| 145 | + finally { |
| 146 | + this.results.elapsed += elapsed |
| 147 | + processed++ |
| 148 | + if ((processed%50 === 0)||(processed === commits.length)) |
| 149 | + this.debug(`at commit ${processed}/${commits.length} (${(100*processed/commits.length).toFixed(2)}%, ${elapsed.toFixed(2)}m elapsed)`) |
| 150 | + } |
| 151 | + } |
| 152 | + this.results.colors = Object.fromEntries(Object.entries(cache.languages).map(([lang, {color}]) => [lang, color])) |
| 153 | + } |
| 154 | + |
| 155 | + /**Clean a path */ |
| 156 | + async clean(path) { |
| 157 | + try { |
| 158 | + this.debug(`cleaning ${path}`) |
| 159 | + await fs.rm(path, {recursive: true, force: true}) |
| 160 | + this.debug(`cleaned ${path}`) |
| 161 | + return true |
| 162 | + } |
| 163 | + catch (error) { |
| 164 | + this.debug(`failed to clean (${error})`) |
| 165 | + return false |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /**Whether to skip a repository or not */ |
| 170 | + ignore(repository) { |
| 171 | + const ignored = !filters.repo(repository, this.skipped) |
| 172 | + if (ignored) |
| 173 | + this.debug(`skipping ${typeof repository === "string" ? repository : `${repository?.owner?.login}/${repository?.name}`} as it matches skipped repositories`) |
| 174 | + return ignored |
| 175 | + } |
| 176 | + |
| 177 | + /**Debug log */ |
| 178 | + debug(message) { |
| 179 | + return console.debug(`metrics/compute/${this.login}/plugins > languages > ${this.constructor.name.replace(/([a-z])([A-Z])/, (_, a, b) => `${a} ${b.toLocaleLowerCase()}`).toLocaleLowerCase()} > ${message}`) |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments