public async Task <List <RevisionAction <JiraAttachment> > > DownloadAttachments(JiraRevision rev)
        {
            var attChanges = rev.AttachmentActions;

            if (attChanges != null && attChanges.Any(a => a.ChangeType == RevisionChangeType.Added))
            {
                var downloadedAtts = new List <JiraAttachment>();
                using (var web = new WebClientWrapper(this))
                {
                    foreach (var remoteAtt in attChanges)
                    {
                        var jiraAtt = await DownloadAttachmentAsync(remoteAtt.Value, web);

                        if (jiraAtt != null && !string.IsNullOrWhiteSpace(jiraAtt.LocalPath))
                        {
                            downloadedAtts.Add(jiraAtt);
                        }
                    }
                }

                // of added attachments, leave only attachments that have been successfully downloaded
                attChanges.RemoveAll(ac => ac.ChangeType == RevisionChangeType.Added);
                attChanges.AddRange(downloadedAtts.Select(da => new RevisionAction <JiraAttachment>()
                {
                    ChangeType = RevisionChangeType.Added, Value = da
                }));
            }

            return(attChanges);
        }
Пример #2
0
        private static List <JiraRevision> BuildRevisions(JiraItem jiraItem, JiraProvider jiraProvider)
        {
            string issueKey    = jiraItem.Key;
            var    remoteIssue = jiraItem.RemoteIssue;
            Dictionary <string, object> fields      = ExtractFields(issueKey, (JObject)remoteIssue, jiraProvider);
            List <JiraAttachment>       attachments = ExtractAttachments(remoteIssue.SelectTokens("$.fields.attachment[*]").Cast <JObject>()) ?? new List <JiraAttachment>();
            List <JiraLink>             links       = ExtractLinks(issueKey, remoteIssue.SelectTokens("$.fields.issuelinks[*]").Cast <JObject>()) ?? new List <JiraLink>();

            // save these field since these might be removed in the loop
            string reporter  = GetAuthor(fields);
            var    createdOn = fields.TryGetValue("created", out object crdate) ? (DateTime)crdate : default(DateTime);

            if (createdOn == DateTime.MinValue)
            {
                Logger.Log(LogLevel.Debug, "created key was not found, using DateTime default value");
            }


            var changelog = jiraProvider.DownloadChangelog(issueKey).ToList();

            Logger.Log(LogLevel.Debug, $"Downloaded issue: {issueKey} changelog.");
            changelog.Reverse();

            Stack <JiraRevision> revisions = new Stack <JiraRevision>();

            foreach (var change in changelog)
            {
                DateTime created = change.ExValue <DateTime>("$.created");
                string   author  = GetAuthor(change);

                List <RevisionAction <JiraLink> >       linkChanges       = new List <RevisionAction <JiraLink> >();
                List <RevisionAction <JiraAttachment> > attachmentChanges = new List <RevisionAction <JiraAttachment> >();
                Dictionary <string, object>             fieldChanges      = new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase);

                var items = change.SelectTokens("$.items[*]")?.Cast <JObject>()?.Select(i => new JiraChangeItem(i));

                foreach (var item in items)
                {
                    if (item.Field == "Link")
                    {
                        var linkChange = TransformLinkChange(item, issueKey, jiraProvider);
                        if (linkChange == null)
                        {
                            continue;
                        }

                        linkChanges.Add(linkChange);

                        UndoLinkChange(linkChange, links);
                    }
                    else if (item.Field == "Attachment")
                    {
                        var attachmentChange = TransformAttachmentChange(item);
                        if (attachmentChange == null)
                        {
                            continue;
                        }

                        attachmentChanges.Add(attachmentChange);

                        UndoAttachmentChange(attachmentChange, attachments);
                    }
                    else
                    {
                        var(fieldref, from, to) = TransformFieldChange(item, jiraProvider);

                        fieldChanges[fieldref] = to;

                        // undo field change
                        if (string.IsNullOrEmpty(from))
                        {
                            fields.Remove(fieldref);
                        }
                        else
                        {
                            fields[fieldref] = from;
                        }
                    }
                }

                var revision = new JiraRevision(jiraItem)
                {
                    Time = created, Author = author, AttachmentActions = attachmentChanges, LinkActions = linkChanges, Fields = fieldChanges
                };
                revisions.Push(revision);
            }

            // what is left after undoing all changes is first revision
            var attActions = attachments.Select(a => new RevisionAction <JiraAttachment>()
            {
                ChangeType = RevisionChangeType.Added, Value = a
            }).ToList();
            var linkActions = links.Select(l => new RevisionAction <JiraLink>()
            {
                ChangeType = RevisionChangeType.Added, Value = l
            }).ToList();
            var fieldActions = fields;

            var firstRevision = new JiraRevision(jiraItem)
            {
                Time = createdOn, Author = reporter, AttachmentActions = attActions, Fields = fieldActions, LinkActions = linkActions
            };

            revisions.Push(firstRevision);
            var listOfRevisions = revisions.ToList();

            List <JiraRevision> commentRevisions = BuildCommentRevisions(jiraItem, jiraProvider);

            listOfRevisions.AddRange(commentRevisions);
            listOfRevisions.Sort();

            foreach (var revAndI in listOfRevisions.Select((r, i) => (r, i)))
            {
                revAndI.Item1.Index = revAndI.Item2;
            }

            return(listOfRevisions);
        }
        private static List <JiraRevision> BuildRevisions(JiraItem jiraItem, JiraProvider jiraProvider)
        {
            string issueKey    = jiraItem.Key;
            var    remoteIssue = jiraItem.RemoteIssue;
            Dictionary <string, object> fields      = ExtractFields(issueKey, (JObject)remoteIssue.SelectToken("$.fields"), jiraProvider);
            List <JiraAttachment>       attachments = ExtractAttachments(remoteIssue.SelectTokens("$.fields.attachment[*]").Cast <JObject>()) ?? new List <JiraAttachment>();
            List <JiraLink>             links       = ExtractLinks(issueKey, remoteIssue.SelectTokens("$.fields.issuelinks[*]").Cast <JObject>()) ?? new List <JiraLink>();


            var changelog = jiraProvider.DownloadChangelog(issueKey).ToList();

            changelog.Reverse();

            Stack <JiraRevision> revisions = new Stack <JiraRevision>();

            foreach (var change in changelog)
            {
                DateTime created = change.ExValue <DateTime>("$.created");
                string   author  = change.ExValue <string>("$.author.name");

                List <RevisionAction <JiraLink> >       linkChanges       = new List <RevisionAction <JiraLink> >();
                List <RevisionAction <JiraAttachment> > attachmentChanges = new List <RevisionAction <JiraAttachment> >();
                Dictionary <string, object>             fieldChanges      = new Dictionary <string, object>();

                var items = change.SelectTokens("$.items[*]").Cast <JObject>().Select(i => new JiraChangeItem(i));
                foreach (var item in items)
                {
                    if (item.Field == "Link")
                    {
                        var linkChange = TransformLinkChange(item, issueKey, jiraProvider);
                        if (linkChange == null)
                        {
                            continue;
                        }

                        linkChanges.Add(linkChange);

                        UndoLinkChange(linkChange, links);
                    }
                    else if (item.Field == "Attachment")
                    {
                        var attachmentChange = TransformAttachmentChange(item);
                        if (attachmentChange == null)
                        {
                            continue;
                        }

                        attachmentChanges.Add(attachmentChange);

                        UndoAttachmentChange(attachmentChange, attachments);
                    }
                    else
                    {
                        var(fieldref, from, to) = TransformFieldChange(item, jiraProvider);

                        fieldChanges[fieldref] = to;

                        // undo field change
                        if (string.IsNullOrEmpty(from))
                        {
                            fields.Remove(fieldref);
                        }
                        else
                        {
                            fields[fieldref] = from;
                        }
                    }
                }

                var revision = new JiraRevision(jiraItem)
                {
                    Time = created, Author = author, AttachmentActions = attachmentChanges, LinkActions = linkChanges, Fields = fieldChanges
                };
                revisions.Push(revision);
            }

            // what is left after undoing all changes is first revision
            var attActions = attachments.Select(a => new RevisionAction <JiraAttachment>()
            {
                ChangeType = RevisionChangeType.Added, Value = a
            }).ToList();
            var linkActions = links.Select(l => new RevisionAction <JiraLink>()
            {
                ChangeType = RevisionChangeType.Added, Value = l
            }).ToList();
            var fieldActions = fields;

            var reporter  = (string)fields["reporter"];
            var createdOn = (DateTime)fields["created"];

            var firstRevision = new JiraRevision(jiraItem)
            {
                Time = createdOn, Author = reporter, AttachmentActions = attActions, Fields = fieldActions, LinkActions = linkActions
            };

            revisions.Push(firstRevision);
            var listOfRevisions = revisions.ToList();

            List <JiraRevision> commentRevisions = BuildCommentRevisions(jiraItem, jiraProvider);

            listOfRevisions.AddRange(commentRevisions);
            listOfRevisions.Sort();

            foreach (var revAndI in listOfRevisions.Select((r, i) => (r, i)))
            {
                revAndI.Item1.Index = revAndI.Item2;
            }

            return(listOfRevisions);
        }
 public async Task <List <RevisionAction <JiraAttachment> > > DownloadAttachments(JiraRevision rev)
 {
     //TODO:attachments
     return(new List <RevisionAction <JiraAttachment> >());
 }