104 lines
3.8 KiB
C#
104 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Atlassian.Jira;
|
|
using JiraTimeBotForm.Configuration;
|
|
using JiraTimeBotForm.TaskTime;
|
|
|
|
namespace JiraTimeBotForm.JiraIntegration
|
|
{
|
|
public class JiraApi
|
|
{
|
|
private readonly Settings _settings;
|
|
private readonly ILog _log;
|
|
private Jira _jira;
|
|
|
|
public JiraApi(Settings settings, ILog log = null)
|
|
{
|
|
_settings = settings;
|
|
_log = log;
|
|
_jira = Jira.CreateRestClient(_settings.JiraUrl, _settings.JiraUserName, _settings.JiraPassword);
|
|
}
|
|
|
|
public string GetTaskName(string branch)
|
|
{
|
|
try
|
|
{
|
|
var issue = _jira.Issues.Queryable.FirstOrDefault(f => f.Key == branch);
|
|
return issue?.Summary;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void SetTodayWorklog(List<TaskTimeItem> taskTimeItems, DateTime? date = null, bool dummy = false, bool addCommentsToWorklog = false)
|
|
{
|
|
if (date == null)
|
|
{
|
|
date = DateTime.Now.Date;
|
|
}
|
|
date = date.Value.Date;
|
|
|
|
foreach (var taskTimeItem in taskTimeItems)
|
|
{
|
|
Issue issue = null;
|
|
try
|
|
{
|
|
issue = _jira.Issues.Queryable.FirstOrDefault(f => f.Key == taskTimeItem.Branch);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
if (issue == null)
|
|
{
|
|
_log.Error($"[!] Не могу найти ветку {taskTimeItem.Branch} в JIRA, пропускаю!");
|
|
continue;
|
|
}
|
|
|
|
var hasTodayWorklog = false;
|
|
var workLogs = issue.GetWorklogsAsync().Result;
|
|
foreach (var workLog in workLogs)
|
|
{
|
|
var timeSpent = TimeSpan.FromSeconds(workLog.TimeSpentInSeconds);
|
|
if (workLog.CreateDate.GetValueOrDefault().Date == date && workLog.Author == _settings.JiraUserName)
|
|
{
|
|
var timeDiff = Math.Abs((timeSpent - taskTimeItem.Time).TotalMinutes);
|
|
if (timeDiff > 1)
|
|
{
|
|
_log.Trace($"Время отличается на {timeDiff} минут, удаляю worklog: {taskTimeItem.Branch} {workLog.Author} {workLog.CreateDate}: {workLog.TimeSpent}");
|
|
if (!dummy)
|
|
{
|
|
issue.DeleteWorklogAsync(workLog);
|
|
}
|
|
hasTodayWorklog = false;
|
|
}
|
|
else
|
|
{
|
|
_log.Trace($"По задаче {taskTimeItem.Branch} уже есть аналогичный worklog. Пропускаю.");
|
|
hasTodayWorklog = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!hasTodayWorklog)
|
|
{
|
|
var timeSpentJira = $"{taskTimeItem.Time.TotalMinutes}m";
|
|
Worklog workLogToAdd = new Worklog(timeSpentJira, date.Value);
|
|
if (addCommentsToWorklog)
|
|
{
|
|
workLogToAdd = new Worklog(timeSpentJira, date.Value, taskTimeItem.Description);
|
|
}
|
|
if (!dummy)
|
|
{
|
|
workLogToAdd = issue.AddWorklogAsync(workLogToAdd).Result;
|
|
}
|
|
_log.Trace($"Добавили Worklog для {taskTimeItem.Branch}: {workLogToAdd.Author} {workLogToAdd.CreateDate}: {workLogToAdd.TimeSpent}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|