namespace GitHubImporter { using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Atlassian.Jira; using Stwalkerster.SharphConduit; using Stwalkerster.SharphConduit.Applications; using Stwalkerster.SharphConduit.Applications.Maniphest; internal class Jira { private static void Main() { GetValue().Wait(); } private static async Task GetValue() { try { var phabricatorProject = "#hmb"; var githubToPhabricatorMap = new Dictionary(); githubToPhabricatorMap.Add("stwalkerster", new PhabUser("stwalkerster", "api-****************************")); githubToPhabricatorMap.Add("import-bot", new PhabUser("import-bot", "api-****************************")); githubToPhabricatorMap.Add("jenkins", new PhabUser("jenkins", "api-****************************")); var phabUrl = "https://phabricator.stwalkerster.co.uk/"; /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// var jira = Atlassian.Jira.Jira.CreateRestClient( "https://jira.stwalkerster.co.uk/", "stwalkerster", @"****************************", new JiraRestClientSettings()); var issuesFromJql = jira.GetIssuesFromJql("project = HMB and resolution = unresolved").ToList(); var conduit = new ConduitClient(phabUrl, githubToPhabricatorMap["import-bot"].API); var phid = new PHIDLookup(conduit); var users = issuesFromJql.Select(x => x.Assignee) .Union(issuesFromJql.Select(x => x.Reporter)) .Distinct() .Where(x => x != null) .Select(x => "@" + x); var lookupPhids = new List(users) { phabricatorProject, "#ircv3" }; var phids = phid.GetPHIDForObject(lookupPhids.ToArray()); foreach (var id in phids) { if (id.Key.StartsWith("@")) { var uname = id.Key.Substring(1); githubToPhabricatorMap[uname].PHID = id.Value; } } foreach (var issue in issuesFromJql) { var title = issue.Summary; var description = issue.Description + "\n"; var author = githubToPhabricatorMap["import-bot"]; if (githubToPhabricatorMap.ContainsKey(issue.Reporter)) { author = githubToPhabricatorMap[issue.Reporter]; } else { description += "\n**Original Author:** " + issue.Reporter; } string owner = null; if (issue.Assignee != null && githubToPhabricatorMap.ContainsKey(issue.Assignee)) { owner = githubToPhabricatorMap[issue.Assignee].PHID; } description += "\n**Created:** " + issue.Created.GetValueOrDefault().ToUniversalTime().ToString("r"); if (issue.Updated.HasValue) { description += "\n**Last Updated:** " + issue.Updated.Value.ToUniversalTime().ToString("r"); } var task = new ManiphestTask(); task.Title = title; task.Description = description; task.Owner = owner; task.Priority = ConvertPriority(issue.Priority); if (author.Maniphest == null) { author.Client = new ConduitClient(phabUrl, author.API); author.Maniphest = new Maniphest(author.Client); } task.AddProjects(new[] { phids[phabricatorProject] }); task.AddTransaction( "custom.stwalkerster:external-ref", new Transaction { Type = "custom.stwalkerster:external-ref", Value = issue.Key.ToString() }); // save it author.Maniphest.Edit(task); var comments = issue.GetComments().OrderBy(x => x.CreatedDate.GetValueOrDefault().ToUniversalTime()).ToList(); if (comments.Count != 0) { foreach (var issueComment in comments) { var comment = issueComment.Body; var authorUsername = issueComment.Author; PhabUser authorAccount; if (!githubToPhabricatorMap.ContainsKey(authorUsername)) { authorAccount = githubToPhabricatorMap["import-bot"]; } else { authorAccount = githubToPhabricatorMap[authorUsername]; } comment = string.Format( "**{0}** wrote at {1}:\n\n> {2}", authorUsername, issueComment.CreatedDate.GetValueOrDefault().ToUniversalTime().ToString("r"), comment.Replace("\n", "\n> ")); task.AddComment(comment); if (authorAccount.Maniphest == null) { authorAccount.Client = new ConduitClient(phabUrl, authorAccount.API); authorAccount.Maniphest = new Maniphest(authorAccount.Client); } authorAccount.Maniphest.Edit(task); } } } } catch (Exception ex) { Debugger.Break(); } } private static string ConvertPriority(IssuePriority priority) { switch (priority.Id) { case "1": return "100"; case "2": return "80"; case "3": return "50"; case "4": return "25"; case "5": return "0"; default: return "90"; } // blocker => 1 => unbreak now => 100 // critical => 2 => high => 80 // major => 3 => normal => 50 // minor => 4 => low => 25 // trivial => 5 => wishlist => 0 // unprioritised => 10000 => needs triage => 90 } } }