public void TranslateItemToJira(WorkItem workItem, Jira jira, Session session) { var key = workItem.Fields["ExternalAccountId"].Value.ToString(); var issue = jira.GetIssue(key); var correspondings = jira.GetCorrespondings(issue); var revisions = workItem.Revisions.Cast <Revision>() .Where(elem => !_store.IsTfsRevisionInChanges(elem.WorkItem.Id, elem.Index)).OrderBy(elem => elem.Index); foreach (Revision revision in revisions) { try { TranslateRevisionToJira(revision, jira, session); } catch (Exception ex) { _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, //JiraChangelog = jira.GetLastChangelogId(key), Operation = string.Format(ex.ToString()), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Fail }); } } Logger.WriteInfo(p => p.JMessage(Site.Jira, Operation.Sended, Artifact.WorkItem, workItem.Fields["ExternalAccountId"].Value.ToString(), partnerId: workItem.Id.ToString())); }
public void TranslateRevisionToJira(Revision revision, Jira jira, Session session) { //bug разобраться с удалением вложения на сторонеtfs var key = revision.Fields["ExternalAccountId"].Value.ToString(); var issue = jira.GetIssue(key); var currentComments = jira.GetComments(issue); var correspondings = jira.GetCorrespondings(issue); var previousRevision = GetRevisionPreviousTo(revision); IEnumerable <TfsAttachment> attachsToDelete = Enumerable.Empty <TfsAttachment>(); IEnumerable <TfsAttachment> attachsToAdd; if (previousRevision != null) { attachsToDelete = previousRevision.Attachments .Cast <TfsAttachment>() .Where(elem => !revision.Attachments.Contains(elem)); attachsToAdd = revision.Attachments .Cast <TfsAttachment>() .Where(elem => !previousRevision.Attachments.Contains(elem)); } else { attachsToAdd = revision.Attachments.Cast <TfsAttachment>(); } foreach (var attachment in attachsToDelete) { var jiraAttachment = issue.Fields.Attachment.FirstOrDefault(elem => elem.Filename == attachment.Name); if (jiraAttachment != null) { jira.DeleteAttachment(key, jiraAttachment); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(key), Operation = string.Format("Удалено вложение {0} элемента {1}", attachment.Name, key), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); } } var currentAttachments = issue.Fields.Attachment; foreach (var attachment in attachsToAdd) { using (var client = new WebClient()) { client.Credentials = NetCred; if (!Directory.Exists(globalConfig.AttachmentsFolder)) { Directory.CreateDirectory(globalConfig.AttachmentsFolder); } client.DownloadFile(attachment.Uri, globalConfig.AttachmentsFolder + attachment.Name); } var attachmentToSend = new Attachment { Content = attachment.Uri.AbsoluteUri, Filename = attachment.Name }; if (currentAttachments.All(elem => elem.Filename != attachmentToSend.Filename)) { jira.CreateAttachment(key, attachmentToSend); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(key), Operation = string.Format("Добавлено вложение {0} для элемента {1}", attachment.Name, key), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); Logger.WriteInfo( p => p.JMessage(Site.Jira, Operation.Create, Artifact.Attachment, attachmentToSend.Filename, key, revision.WorkItem.Id.ToString())); } } SyncFields(revision.WorkItem, jira); var text = revision.Fields["History"].Value.ToString(); if (!string.IsNullOrWhiteSpace(text) && !text.StartsWith("Jira") && !currentComments.Any(comment => comment.renderedBody.RemakeBadTags().EqualToComment(text))) { var newComment = new Comment { Author = new JiraUser { Name = revision.Fields["Authorized As"].Value.ToString() }, body = converter.Html2Wiki(revision.Fields["History"].Value.ToString()), Updated = revision.Fields["Changed Date"].Value.ToString() }; text = string.Format("TFS | [{3}] {0} пишет:{1}{2}", newComment.Author.Name, Environment.NewLine, newComment.body, newComment.UpdatedTime); jira.SendComment(key, text); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = text, Operation = string.Format("Добавлен комменатрий для элемента {0}", key), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); Logger.WriteInfo( p => p.JMessage(Site.Jira, Operation.Sended, Artifact.Comment, text, key, revision.WorkItem.Id.ToString())); } var toState = revision.Fields["State"].Value.ToString(); string fromState = null; if (previousRevision != null) { fromState = previousRevision.Fields["State"].Value.ToString(); } var stateChanges = _store.ReadProgram(Tracker.TFS, fromState, toState); IEnumerable <StateChangedCommand> stateChangedCommands = Enumerable.Empty <StateChangedCommand>(); if (stateChanges.Count() == 1) { stateChangedCommands = _store.ReadCommands(stateChanges.FirstOrDefault().Id); } foreach (var stateChangedCommand in stateChangedCommands) { switch (stateChangedCommand.Command) { //todo: если же статус в жире не нужно менять case Command.ChangeState: jira.ChangeIssueStateBy(issue, stateChangedCommand.ChangeTo); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(issue.Key), Operation = string.Format("Измение статуса элемента {1} на {0}", stateChangedCommand.ChangeTo, key), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); break; case Command.AssignToAuthor: jira.AssignIssueToAuthor(issue); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(issue.Key), Operation = string.Format("Изменение исполнителя на автора у {0}", key), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); break; case Command.SendFieldAsComment: if (!string.IsNullOrWhiteSpace(revision.Fields[stateChangedCommand.FieldName].Value.ToString())) { var field = revision.Fields[stateChangedCommand.FieldName]; jira.SendComment(key, string.Format("TFS | {0}:\n{1}", field.Name, field.Value)); _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(issue.Key), Operation = string.Format("Перенос поля {0} ({1})в качестве комментария ", field.Name, converter.Html2Wiki(field.Value.ToString())), TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); } break; } } if (!_store.IsTfsRevisionInChanges(revision.WorkItem.Id, revision.Index)) { _store.AddSessionChange(new SessionChange { Bug = correspondings, Session = session, Date = DateTime.Now, JiraChangelog = jira.GetLastChangelogId(issue.Key), Operation = "Отсутствуют значимые изменения", TFSRevision = revision.WorkItem.Id + "-" + revision.Index, SyncState = SyncState.Success }); } var attachmentsFolder = new DirectoryInfo(globalConfig.AttachmentsFolder); attachmentsFolder.ClearFolder(); }