/
amesk
/
gitlab-analyzer
Обзор
Документация
Войти
/
amesk
/
gitlab-analyzer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
gitlab.js
115 строк
4 KB
amesk
Added "Time Spent" and "Estimation" fields to the table.
06 фев 2025, 15:26
06 фев 2025, 15:26
f05739a
Код
Авторство
О чём код?
// gitlab.js const { Gitlab } = require('@gitbeaker/node'); // Конфигурация GitLab const GITLAB_URL = 'http://gitlab.evomarine.local'; const ACCESS_TOKEN = 'A1tnvhx67Vm5ryBGJtgw'; // Замените на ваш токен // Инициализация клиента GitLab api = new Gitlab({ host: GITLAB_URL, token: ACCESS_TOKEN, }); function setupApiToken(token) { console.log(`Setting up GitLab API with token: ${token}`); api = new Gitlab({ host: GITLAB_URL, token: token, }); } /** * Обновляет метку у задачи в GitLab * @param {string} projectId - ID проекта * @param {number} issueId - ID задачи * @param {string} label - Метка * @param {string} action - Действие (add или remove) * @returns {Promise<{success: boolean, error?: string}>} */ async function updateLabel(projectId, issueId, label, action) { try { // Получаем текущие метки задачи const issue = await api.Issues.show(projectId, issueId); const currentLabels = issue.labels; // Обновляем метки let newLabels; if (action === 'add') { if (!currentLabels.includes(label)) { newLabels = [...currentLabels, label]; } else { return { success: true }; // Метка уже существует, ничего не делаем } } else if (action === 'remove') { if (currentLabels.includes(label)) { newLabels = currentLabels.filter(l => l !== label); } else { return { success: true }; // Метки нет, ничего не делаем } } else { return { success: false, error: 'Invalid action' }; } // Обновляем задачу с новыми метками await api.Issues.edit(projectId, issueId, { labels: newLabels.join(','), }); return { success: true }; } catch (error) { console.error('Error updating label:', error); return { success: false, error: error.message }; } } /** * Получает список открытых задач для проекта * @param {string} projectId - ID проекта * @returns {Promise<{success: boolean, issues?: Array, error?: string}>} */ async function getOpenIssues(projectId) { try { // Получаем список открытых задач const issues = await api.Issues.all({ projectId, state: 'opened', }); // Получаем метки проекта const labels = await api.Labels.all(projectId); const labelColors = labels.reduce((acc, label) => { acc[label.name] = label.color; return acc; }, {}); // Формируем список задач с метками, assignee, milestone, estimation и spent time const issuesList = await Promise.all(issues.map(async (issue) => { // Получаем информацию о времени (estimation и spent time) const timeStats = await api.Issues.timeStats(projectId, issue.iid); return { iid: issue.iid, title: issue.title, url: issue.web_url, labels: issue.labels.map(label => [label, labelColors[label] || "#CCCCCC"]), assignee: issue.assignee ? issue.assignee.name : null, // Добавляем assignee milestone: issue.milestone ? issue.milestone.title : null, // Добавляем milestone estimation: timeStats.time_estimate, // Оценка времени total_time_spent: timeStats.total_time_spent // Суммарное время, списанное на пункт }; })); return { success: true, issues: issuesList }; } catch (error) { console.error('Error fetching open issues:', error); return { success: false, error: error.message }; } } module.exports = { setupApiToken, updateLabel, getOpenIssues, GITLAB_URL };