-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
65 lines (58 loc) · 2.02 KB
/
github-api.js
File metadata and controls
65 lines (58 loc) · 2.02 KB
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
const axios = require('axios');
/**
* Busca informações de um repositório no GitHub
* @param {string} repoPath - Caminho do repositório no formato "usuario/repositorio"
* @returns {Promise<string>} - Mensagem formatada com informações do repositório
*/
async function getRepositoryInfo(repoPath) {
try {
// Busca informações do repositório
const repoResponse = await axios.get(
`https://api.github.com/repos/${repoPath}`
);
// Busca as linguagens do repositório
const languagesResponse = await axios.get(repoResponse.data.languages_url);
// Extrair informações relevantes
const {
name,
description,
html_url,
stargazers_count,
forks_count,
open_issues_count,
owner,
created_at,
updated_at
} = repoResponse.data;
// Formatar as linguagens
const languages = Object.keys(languagesResponse.data);
const languagesText =
languages.length > 0 ? languages.join(', ') : 'Não especificado';
// Formatar datas
const createDate = new Date(created_at).toLocaleDateString('pt-BR');
const updateDate = new Date(updated_at).toLocaleDateString('pt-BR');
// Construir mensagem formatada
return (
`📂 *${name}*\n` +
`${description ? `_${description}_\n\n` : '\n'}` +
`👤 *Autor:* ${owner.login}\n` +
`⭐ *Stars:* ${stargazers_count}\n` +
`🔄 *Forks:* ${forks_count}\n` +
`🐞 *Issues:* ${open_issues_count}\n` +
`💻 *Linguagens:* ${languagesText}\n` +
`📅 *Criado em:* ${createDate}\n` +
`🔄 *Última atualização:* ${updateDate}\n\n` +
`🔗 *Link:* ${html_url}\n\n` +
`_Informações fornecidas pela API do GitHub_`
);
} catch (error) {
console.error('Erro na API do GitHub:', error.message);
if (error.response && error.response.status === 404) {
return `❌ Repositório "${repoPath}" não encontrado.`;
}
throw new Error('Falha ao obter informações do repositório.');
}
}
module.exports = {
getRepositoryInfo
};