public void DeleteWorklog() { var summary = "Test issue with worklogs" + _random.Next(int.MaxValue); var issue = new Issue(_jira, "TST") { Type = "1", Summary = summary, Assignee = "admin" }; issue.SaveChanges(); var worklog = issue.AddWorklogAsync("1h").Result; Assert.Single(issue.GetWorklogsAsync().Result); issue.DeleteWorklogAsync(worklog).Wait(); Assert.Empty(issue.GetWorklogsAsync().Result); }
public void SetTodayWorklog(List <TaskTimeItem> taskTimeItems, Settings settings, DateTime?date = null, bool dummy = false, bool addCommentsToWorklog = false, CancellationToken cancellationToken = default) { date = date.GetValueOrDefault(DateTime.Now.Date).Date; var jira = GetClient(settings); if (settings.RemoveManuallyAddedWorklogs) { //Удаляем добавленные вручную пользователем данные. RemoveWorklogsAddedByUser(taskTimeItems, settings, date, dummy, cancellationToken); } foreach (TaskTimeItem taskTimeItem in taskTimeItems) { Issue issue = null; try { issue = jira.Issues.Queryable.FirstOrDefault(f => f.Key == taskTimeItem.Branch); } catch (Exception ex) { _log.Error($"Ошибка получения задачи из JIRA: {ex.Message} - {ex.InnerException?.Message}"); } if (issue == null) { _log.Error($"[!] Не могу найти ветку {taskTimeItem.Branch} в JIRA, пропускаю!"); continue; } var hasTodayWorklog = false; var workLogs = issue.GetWorklogsAsync(cancellationToken).Result; var userWorklogs = workLogs.Where(w => w.StartDate.GetValueOrDefault().Date == date && w.Author.Equals(settings.JiraUserName, StringComparison.InvariantCultureIgnoreCase)).ToList(); var comment = _descriptionSource.GetDescription(taskTimeItem, addCommentsToWorklog, settings); foreach (var workLog in userWorklogs) { var timeSpent = TimeSpan.FromSeconds(workLog.TimeSpentInSeconds); var timeDiff = Math.Abs((timeSpent - taskTimeItem.TimeSpent).TotalMinutes); if (timeDiff > 1 || userWorklogs.Count > 1 || userWorklogs.First().Comment != comment) { if (timeDiff > 1) { _log.Trace($"Время отличается на {timeDiff} минут, удаляю worklog: {taskTimeItem.Branch} {workLog.Author} {workLog.CreateDate}: {workLog.TimeSpent}"); } else if (userWorklogs.Count > 1) { _log.Trace($"Найдено более одного ворклога на задачу, удаляю worklog: {taskTimeItem.Branch} {workLog.Author} {workLog.CreateDate}: {workLog.TimeSpent}"); } else { _log.Trace($"Описание отличается, удаляю worklog: {taskTimeItem.Branch} {workLog.Author} {workLog.CreateDate}: {workLog.TimeSpent}"); } if (!dummy) { try { issue.DeleteWorklogAsync(workLog, WorklogStrategy.AutoAdjustRemainingEstimate, token: cancellationToken); } catch (Exception ex) { _log.Error($"Не могу удалить Worklog по задаче {taskTimeItem.Branch}: {ex.Message}."); continue; } } hasTodayWorklog = false; } else { _log.Trace($"По задаче {taskTimeItem.Branch} уже есть аналогичный worklog. Пропускаю."); hasTodayWorklog = true; } } if (!hasTodayWorklog) { var timeSpentJira = $"{taskTimeItem.TimeSpent.TotalMinutes}m"; Worklog workLogToAdd = new Worklog(timeSpentJira, date.Value, comment); if (!dummy) { try { workLogToAdd = issue.AddWorklogAsync(workLogToAdd, WorklogStrategy.AutoAdjustRemainingEstimate, token: cancellationToken).Result; } catch (Exception ex) { _log.Error($"Не могу добавить Worklog по задаче {taskTimeItem.Branch}: {ex.Message} ({ex.InnerException?.Message})."); continue; } } _log.Trace($"Добавили Worklog для {taskTimeItem.Branch}: {workLogToAdd.Author} {workLogToAdd.CreateDate}: {workLogToAdd.TimeSpent}"); } } }