UNPKG

@tshio/jira-worklog

Version:
80 lines (70 loc) 2.52 kB
const JiraClient = require('jira-connector'); const jiraDateTime = day => new Date(day).toISOString().replace('Z', '+0000'); const dayStart = day => `${day} 00:00`; const dayEnd = day => `${day} 23:59`; const findWorklogs = async (client, project, username, day) => client .search .search({ jql: `project = ${project} AND worklogAuthor = currentUser() AND worklogDate = ${day} ORDER BY updated ASC`, fields: ['worklog'], }) .then(res => res.issues.map((task) => { const spentSeconds = task.fields.worklog .worklogs .filter( worklog => (worklog.author.emailAddress === username || worklog.author.name === username) && new Date(worklog.started) >= new Date(dayStart(day)) && new Date(worklog.started) <= new Date(dayEnd(day)) ) .reduce((sum, worklog) => sum + worklog.timeSpentSeconds, 0); return { key: task.key, hours: Math.floor(spentSeconds / 3600), }; })); module.exports = { checkCredentials: async (host, account, password) => { try { const client = new JiraClient({ host, basic_auth: { username: account, password }, }); return await client.myself.getMyself().then(() => true).catch(() => false); } catch (err) { return false; } }, initialize: (host, account, password) => { const username = account; const client = new JiraClient({ host, basic_auth: { username: account, password }, }); return { getWorklogs: (project, day) => findWorklogs(client, project, username, day), getSuggestedTaskKeys: (project, day) => client .search .search({ jql: `project = ${project} AND assignee was currentUser() ON ${day} AND status was "In progress" ON ${day} ORDER BY updated ASC` }) .then(result => result.issues.map(task => ({ key: task.key, name: task.fields.summary }))) .then(tasks => tasks.map(task => task.key)), findTasksWithKeys: (keys) => { if (keys.length === 0) { return []; } return client .search .search({ jql: `key in (${keys.join(', ')})` }) .then(result => result.issues.map( task => ({ key: task.key, name: task.fields.summary }) )); }, sendWorklog: (day, task, hours) => client .issue .addWorkLog({ issueKey: task, notifyUsers: false, worklog: { timeSpent: `${hours}h`, started: jiraDateTime(day) }, }), }; }, };