buildsight-collector 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +50 -0
- package/index.js +160 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 BuildSight.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# BuildSight Collector
|
|
2
|
+
|
|
3
|
+
CLI para coletar commits e métricas de repositórios locais e enviar para a API do BuildSight.
|
|
4
|
+
|
|
5
|
+
Uso rápido
|
|
6
|
+
|
|
7
|
+
Instalar globalmente (após publicar no npm):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g buildsight-collector
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Executar:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
buildsight-collector <token>
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Ou usar com npx (sem instalar globalmente):
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx buildsight-collector <token>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Parâmetros
|
|
26
|
+
|
|
27
|
+
- <token>: token de autenticação gerado pela API do BuildSight.
|
|
28
|
+
|
|
29
|
+
Publicação no npm
|
|
30
|
+
|
|
31
|
+
1. Verifique o `name` em `package.json` e confirme que está disponível no npm (se já estiver em uso, escolha um nome distinto ou um scope como `@seu-usuario/buildsight-collector`).
|
|
32
|
+
2. Faça login no npm:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm login
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
3. Publicar (público):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm publish --access public
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Se usar um scoped package e quiser publicação pública, precisa `--access public`.
|
|
45
|
+
|
|
46
|
+
Observações
|
|
47
|
+
|
|
48
|
+
- Substitua `author` e `repository.url` em `package.json` pelos seus valores reais antes de publicar.
|
|
49
|
+
- Este pacote já inclui o `bin` apontando para `index.js`, que tem shebang para execução como CLI.
|
|
50
|
+
- Node.js mínimo sugerido: 14+.
|
package/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import ora from "ora";
|
|
6
|
+
import simpleGit from "simple-git";
|
|
7
|
+
|
|
8
|
+
const spinner = ora();
|
|
9
|
+
|
|
10
|
+
async function main() {
|
|
11
|
+
const token = process.argv[3];
|
|
12
|
+
|
|
13
|
+
if (!token) {
|
|
14
|
+
console.log(chalk.red("❌ Token de autenticação ausente."));
|
|
15
|
+
console.log(chalk.yellow("Uso: npx buildsight-collector <token>"));
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log(chalk.cyan.bold("\n🔗 Iniciando BuildSight Collector..."));
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
spinner.start("Buscando paths de repositórios configurados...");
|
|
23
|
+
const { data } = await axios.get(
|
|
24
|
+
`https://buildsight.app/api/collector/paths?token=${token}`
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
if (!data?.data?.length) {
|
|
28
|
+
spinner.fail("Nenhum path configurado encontrado.");
|
|
29
|
+
process.exit(0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
spinner.succeed(`Foram encontrados ${data.data.length} paths.`);
|
|
33
|
+
|
|
34
|
+
for (const repo of data.data) {
|
|
35
|
+
console.log(chalk.blue(`\n📂 Processando repositório:`), chalk.white(repo.name));
|
|
36
|
+
const git = simpleGit(repo.path);
|
|
37
|
+
const DAYS_RANGE = repo.periodDays || 1;
|
|
38
|
+
const sinceDate = new Date();
|
|
39
|
+
sinceDate.setDate(sinceDate.getDate() - DAYS_RANGE);
|
|
40
|
+
|
|
41
|
+
console.log(chalk.gray(`Buscando commits desde: ${sinceDate.toISOString()}`));
|
|
42
|
+
|
|
43
|
+
// --- 🔹 1. Commits detalhados ---
|
|
44
|
+
const log = await git.log({
|
|
45
|
+
'--since': sinceDate.toISOString(),
|
|
46
|
+
'--stat': null, // inclui dados de arquivos alterados
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// --- 🔹 2. Branches ---
|
|
50
|
+
const branches = await git.branchLocal();
|
|
51
|
+
const branchList = Object.keys(branches.branches);
|
|
52
|
+
|
|
53
|
+
// --- 🔹 3. Merges locais ---
|
|
54
|
+
const merges = await git.log({ '--merges': null });
|
|
55
|
+
|
|
56
|
+
// --- 🔹 4. Arquivos mais modificados ---
|
|
57
|
+
const fileChangeCounts = {};
|
|
58
|
+
for (const commit of log.all) {
|
|
59
|
+
try {
|
|
60
|
+
const show = await git.raw(['show', '--stat', '--oneline', commit.hash]);
|
|
61
|
+
const files = Array.from(show.matchAll(/ ([^\s]+)\s+\|\s+\d+/g)).map(m => m[1]);
|
|
62
|
+
for (const file of files) {
|
|
63
|
+
fileChangeCounts[file] = (fileChangeCounts[file] || 0) + 1;
|
|
64
|
+
}
|
|
65
|
+
} catch { }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// --- 🔹 5. Montar dados crus por commit ---
|
|
69
|
+
const commits = await Promise.all(
|
|
70
|
+
log.all.map(async (c) => {
|
|
71
|
+
const isMerge = c.message.startsWith("Merge");
|
|
72
|
+
const isRevert = /revert:/i.test(c.message);
|
|
73
|
+
const isFix = /^fix:/i.test(c.message);
|
|
74
|
+
const isFeat = /^feat:/i.test(c.message);
|
|
75
|
+
const isRefactor = /^refactor:/i.test(c.message);
|
|
76
|
+
const isHotfix = /hotfix|urgent/i.test(c.message);
|
|
77
|
+
|
|
78
|
+
let filesChanged = [];
|
|
79
|
+
try {
|
|
80
|
+
const show = await git.raw([
|
|
81
|
+
"show",
|
|
82
|
+
"--pretty=",
|
|
83
|
+
"--name-only",
|
|
84
|
+
c.hash,
|
|
85
|
+
]);
|
|
86
|
+
filesChanged = show
|
|
87
|
+
.split("\n")
|
|
88
|
+
.filter((line) => line.trim() !== "");
|
|
89
|
+
} catch { }
|
|
90
|
+
|
|
91
|
+
// Branches que contêm o commit
|
|
92
|
+
let branchesContaining = [];
|
|
93
|
+
try {
|
|
94
|
+
const branchOutput = await git.raw(['branch', '--all', '--contains', c.hash]);
|
|
95
|
+
branchesContaining = branchOutput
|
|
96
|
+
.split("\n")
|
|
97
|
+
.map(b => b.trim())
|
|
98
|
+
.filter(Boolean);
|
|
99
|
+
} catch { }
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
hash: c.hash,
|
|
103
|
+
author: c.author_name,
|
|
104
|
+
email: c.author_email,
|
|
105
|
+
date: c.date,
|
|
106
|
+
message: c.message,
|
|
107
|
+
isMerge,
|
|
108
|
+
isRevert,
|
|
109
|
+
isFix,
|
|
110
|
+
isFeat,
|
|
111
|
+
isRefactor,
|
|
112
|
+
isHotfix,
|
|
113
|
+
filesChanged,
|
|
114
|
+
branchesContaining,
|
|
115
|
+
};
|
|
116
|
+
})
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// --- 🔹 6. Dividir em lotes de 500 commits ---
|
|
120
|
+
const batchSize = 500;
|
|
121
|
+
const totalBatches = Math.ceil(commits.length / batchSize);
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < totalBatches; i++) {
|
|
124
|
+
const batch = commits.slice(i * batchSize, (i + 1) * batchSize);
|
|
125
|
+
|
|
126
|
+
const payload = {
|
|
127
|
+
token: token,
|
|
128
|
+
repoName: repo.name,
|
|
129
|
+
part: i + 1,
|
|
130
|
+
totalParts: totalBatches,
|
|
131
|
+
metadata: {
|
|
132
|
+
branches: branchList,
|
|
133
|
+
merges: merges.all.map(m => ({
|
|
134
|
+
hash: m.hash,
|
|
135
|
+
author: m.author_name,
|
|
136
|
+
date: m.date,
|
|
137
|
+
message: m.message,
|
|
138
|
+
})),
|
|
139
|
+
fileChangeCounts, // 🔥 arquivos mais alterados
|
|
140
|
+
},
|
|
141
|
+
commits: batch,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
spinner.start(
|
|
145
|
+
`Enviando lote ${i + 1}/${totalBatches} (${batch.length} commits) de ${repo.name}...`
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
await axios.post(`https://buildsight.app/api/collector/records`, payload);
|
|
149
|
+
spinner.succeed(`Lote ${i + 1}/${totalBatches} de ${repo.name} enviado com sucesso!`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
console.log(chalk.green.bold("\n✅ Coleta concluída com sucesso!"));
|
|
154
|
+
} catch (err) {
|
|
155
|
+
spinner.fail("Erro durante o processo de coleta.");
|
|
156
|
+
console.error(chalk.red(err.message));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "buildsight-collector",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "CLI do BuildSight para coleta automatizada de commits e métricas de repositórios.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"buildsight-collector": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "node index.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"buildsight",
|
|
15
|
+
"collector",
|
|
16
|
+
"git",
|
|
17
|
+
"ci",
|
|
18
|
+
"metrics"
|
|
19
|
+
],
|
|
20
|
+
"author": "Marcos Oliveira <marcosoliveira.ti9@gmail.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/dev-MarcosVinicius/buildsight-collector.git"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"index.js"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"axios": "^1.6.8",
|
|
34
|
+
"chalk": "^5.3.0",
|
|
35
|
+
"ora": "^8.0.0",
|
|
36
|
+
"simple-git": "^3.22.0"
|
|
37
|
+
}
|
|
38
|
+
}
|