Пример #1
0
        private async Task CreateWorkItem(string type, Issue jiraIssue, string parentKey, string title, string description, string state, params JsonPatchOperation[] fields)
        {
            var patchDocument = new JsonPatchDocument
            {
                new JsonPatchOperation {
                    Path = "/fields/System.State", Value = state
                },
                new JsonPatchOperation {
                    Path = "/fields/System.CreatedBy", Value = MapUser(jiraIssue.ReporterUser.DisplayName)
                },
                new JsonPatchOperation {
                    Path = "/fields/System.CreatedDate", Value = jiraIssue.Created.Value.ToUniversalTime()
                },
                new JsonPatchOperation {
                    Path = "/fields/System.ChangedBy", Value = MapUser(jiraIssue.ReporterUser.DisplayName)
                },
                new JsonPatchOperation {
                    Path = "/fields/System.ChangedDate", Value = jiraIssue.Created.Value.ToUniversalTime()
                },
                new JsonPatchOperation {
                    Path = "/fields/System.Title", Value = title
                },
                new JsonPatchOperation {
                    Path = "/fields/System.Description", Value = description
                },
                new JsonPatchOperation {
                    Path = "/fields/Custom.JiraID", Value = jiraIssue.Key.Value
                },
                new JsonPatchOperation {
                    Path = "/fields/Microsoft.VSTS.Common.Priority", Value = MapPriority(jiraIssue.Priority)
                }
            };

            if (jiraIssue.AdditionalFields["customfield_10301"] != null)
            {
                patchDocument.Add(new JsonPatchOperation {
                    Path = "/fields/Custom.TestURL", Value = jiraIssue.AdditionalFields["customfield_10301"]?.ToString()
                });
            }

            if (parentKey != null)
            {
                patchDocument.Add(new JsonPatchOperation {
                    Path = "/relations/-", Value = new WorkItemRelation {
                        Rel = "System.LinkTypes.Hierarchy-Reverse", Url = $"https://ciappdev.visualstudio.com/_apis/wit/workItems/{parentKey}"
                    }
                });
            }

            if (jiraIssue.AssigneeUser != null)
            {
                patchDocument.Add(new JsonPatchOperation {
                    Path = "/fields/System.AssignedTo", Value = MapUser(jiraIssue.AssigneeUser.DisplayName)
                });
            }

            patchDocument.Add(new JsonPatchOperation {
                Path = "/fields/System.Tags", Value = _jiraProjectAbbrev
            });

            var attachments = await _jiraClient.Issues.GetAttachmentsAsync(jiraIssue.JiraIdentifier).ConfigureAwait(false);

            if (attachments != null)
            {
                foreach (var attachment in attachments)
                {
                    var bytes = attachment.DownloadData();

                    using var stream = new MemoryStream(bytes);
                    var uploaded = await _witClient.CreateAttachmentAsync(stream, _devOpsProjectName, fileName : attachment.FileName).ConfigureAwait(false);

                    patchDocument.Add(new JsonPatchOperation {
                        Path = "/relations/-", Value = new WorkItemRelation {
                            Rel = "AttachedFile", Url = uploaded.Url
                        }
                    });
                }
            }

            var all = patchDocument.Concat(fields).Where(p => p.Value != null).ToList();

            patchDocument = new JsonPatchDocument();

            patchDocument.AddRange(all);

            try
            {
                var workItem = await _witClient.CreateWorkItemAsync(patchDocument, _devOpsProjectName, type, bypassRules : true).ConfigureAwait(false);

                await CreateComments(workItem.Id.Value, jiraIssue).ConfigureAwait(false);

                Console.WriteLine($"Added {type}: {jiraIssue.Key} {title}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #2
0
        private static async Task CreateWorkItem(
            WorkItemTrackingHttpClient client,
            string type,
            Issue jira,
            string parentKey,
            string title,
            string description,
            string state,
            params JsonPatchOperation[] fields)
        {
            // Short-circuit if we've already processed this item.
            if (Migrated.ContainsKey(jira.Key.Value))
            {
                return;
            }

            var vsts = new JsonPatchDocument
            {
                new JsonPatchOperation
                {
                    Path  = "/fields/System.State",
                    Value = state
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.CreatedBy",
                    Value = ResolveUser(jira.Reporter)
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.CreatedDate",
                    Value = jira.Created.Value.ToUniversalTime()
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.ChangedBy",
                    Value = ResolveUser(jira.Reporter)
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.ChangedDate",
                    Value = jira.Created.Value.ToUniversalTime()
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.Title",
                    Value = title
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.Description",
                    Value = description
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Common.Priority",
                    Value = ResolvePriority(jira.Priority)
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Common.ClosedDate",
                    Value = jira.ResolutionDate
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Scheduling.FinishDate",
                    Value = jira.ResolutionDate
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Common.ResolvedDate",
                    Value = jira.ResolutionDate
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Common.ResolvedReason",
                    Value = jira.Resolution?.Name
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.IterationPath",
                    Value = ResolveIteration(jira.CustomFields["Sprint"]?.Values, config["AzureDevOps:Project"])
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Scheduling.StoryPoints",
                    Value = jira.CustomFields["Story Points"]?.Values[0]
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/Microsoft.VSTS.Scheduling.Effort",
                    Value = jira.CustomFields["Story Points"]?.Values[0]
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.AreaPath",
                    Value = ResolveAreaPath(jira.CustomFields["DC Team"]?.Values[0], config["AzureDevOps:Project"])
                },
                new JsonPatchOperation
                {
                    Path  = "/fields/System.AssignedTo",
                    Value = ResolveUser(jira.Assignee)
                }
            }
            ;

            if (parentKey != null)
            {
                vsts.Add(new JsonPatchOperation
                {
                    Path  = "/relations/-",
                    Value = new WorkItemRelation
                    {
                        Rel = "System.LinkTypes.Hierarchy-Reverse",
                        Url = $"https://{config["AzureDevOps:Url"]}/_apis/wit/workItems/{Migrated[parentKey]}"
                    }
                });
            }

            if (jira.Labels.Any())
            {
                vsts.Add(new JsonPatchOperation
                {
                    Path  = "/fields/System.Tags",
                    Value = jira.Labels.Aggregate("", (l, r) => $"{l};{r}").Trim(';', ' ')
                });
            }

            foreach (var attachment in await jira.GetAttachmentsAsync())
            {
                var path = Path.GetTempFileName();
                attachment.Download(path);

                await using var stream = new MemoryStream(await File.ReadAllBytesAsync(path));

                var uploaded = await client.CreateAttachmentAsync(stream, config["AzureDevOps:Project"], fileName : attachment.FileName);

                vsts.Add(new JsonPatchOperation
                {
                    Path  = "/relations/-",
                    Value = new WorkItemRelation
                    {
                        Rel = "AttachedFile",
                        Url = uploaded.Url
                    }
                });

                File.Delete(path);
            }

            var all = vsts.Concat(fields)
                      .Where(p => p.Value != null)
                      .ToList();

            vsts = new JsonPatchDocument();
            vsts.AddRange(all);
            var workItem = await client.CreateWorkItemAsync(vsts, config["AzureDevOps:Project"], type, bypassRules : true);

            AddMigrated(jira.Key.Value, workItem.Id.Value);

            await CreateComments(client, workItem.Id.Value, jira);

            Console.WriteLine($"Added {type}: {jira.Key}{title}");
        }