Advertisement
Guest User

Untitled

a guest
Sep 5th, 2013
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using RestSharp;
  8. using jira_RemoteIssueLinkCreator.Data;
  9. using System.Text.RegularExpressions;
  10.  
  11. namespace jira_RemoteIssueLinkCreator
  12. {
  13.   class Program
  14.   {
  15.     private const string URL_BASE = "http://******************.com/";
  16.     private const string URL_API_BASE = URL_BASE + "rest/api/2/";
  17.     private const string URL_BrowseIssue = URL_BASE + "browse/{0}";
  18.  
  19.     private const string URL_Search = URL_API_BASE + "search?jql={0}&maxResults=1000&fields=*all,-comment";
  20.     private const string URL_Issue = URL_API_BASE + "issue/{0}";
  21.  
  22.     private const string LOG_CREATED_LINKS = "log_createdLinks.txt";
  23.     private const string LOG_NO_LINKS = "log_noLinks.txt";
  24.     private const string LOG_EQUAL_LINKS = "log_equalLinks.txt";
  25.  
  26.     private const string JQLInternal = "project in (JIRA, PROJKEY2) AND \"Team Internal Thread\" ~ \"http*\"";
  27.     private const string JQLOrigin = "project in (JIRA, PROJKEY2) AND \"Issue Origin Thread\" ~ \"http*\"";
  28.  
  29.     private static List<string> issues = new List<string>() { "JIRA-0815", "JIRA-4711" };
  30.  
  31.     static RestClient client = new RestClient();
  32.     private static bool isTest;
  33.     private static string username;
  34.     private static string password;
  35.  
  36.     private static void Main(string[] args)
  37.     {
  38.       client = new RestClient();
  39.       client.BaseUrl = URL_API_BASE;
  40.  
  41.       File.WriteAllText(LOG_CREATED_LINKS, string.Empty);
  42.       File.WriteAllText(LOG_EQUAL_LINKS, string.Empty);
  43.       File.WriteAllText(LOG_NO_LINKS, string.Empty);
  44.  
  45.       Console.WriteLine("jira-RemoteIssueLinkCreate");
  46.       Console.WriteLine("==========================");
  47.       AskForCredentials();
  48.  
  49.       bool doExit = false;
  50.       while (!doExit)
  51.       {
  52.         Console.WriteLine("What do you want to do? (exit / test / run / login)");
  53.         string input = Console.ReadLine();
  54.  
  55.         doExit = input == "exit";
  56.         if (input == "run")
  57.         {
  58.           MigrateIssuesOfQuery(JQLInternal);
  59.           MigrateIssuesOfQuery(JQLOrigin);
  60.         }
  61.         else if (input == "test")
  62.         {
  63.           foreach (string issue in issues)
  64.             MigrateIssue(issue);
  65.         }
  66.         else if (input == "login")
  67.           AskForCredentials();
  68.       }
  69.     }
  70.    
  71.     private static void AskForCredentials()
  72.     {
  73.       Console.WriteLine();
  74.       Console.WriteLine("Enter your username:");
  75.       username = Console.ReadLine();
  76.       Console.WriteLine("Enter your password (ATTENTION !!! PASSWORD WILL BE VISIBLE):");
  77.       password = Console.ReadLine();
  78.  
  79.       client.Authenticator = new HttpBasicAuthenticator(username, password);
  80.     }
  81.  
  82.     private static void MigrateIssuesOfQuery(string jql)
  83.     {
  84.       string url = string.Format(URL_Search, jql);
  85.       //url = WebUtility.UrlEncode(url);
  86.       string json = DownloadJSON(url);
  87.       JObject o = JObject.Parse(json);
  88.       JArray issues = (JArray)o["issues"];
  89.  
  90.       foreach (JToken issue in issues)
  91.         MigrateIssue((JObject)issue);
  92.     }
  93.  
  94.     private static void MigrateIssue(string issueKey)
  95.     {
  96.       string url = string.Format(URL_Issue, issueKey);
  97.       string json = DownloadJSON(url);
  98.       JObject issue = JObject.Parse(json);
  99.  
  100.       MigrateIssue(issue);
  101.     }
  102.  
  103.     private static void MigrateIssue(JObject issue)
  104.     {
  105.       string issueKey = (string)issue["key"];
  106.       Console.Write(issueKey);
  107.  
  108.       string internalThread;
  109.       try
  110.       {
  111.         internalThread = (string)issue["fields"]["customfield_10411"];
  112.       }
  113.       catch (Exception)
  114.       {
  115.         internalThread = null;
  116.       }
  117.  
  118.       string originThread;
  119.       try
  120.       {
  121.         originThread = (string)issue["fields"]["customfield_10409"];
  122.       }
  123.       catch (Exception)
  124.       {
  125.         originThread = null;
  126.       }
  127.  
  128.       // log if issue has not links
  129.       if (string.IsNullOrEmpty(internalThread) && string.IsNullOrEmpty(originThread))
  130.       {
  131.         File.AppendAllText(LOG_NO_LINKS, string.Format(URL_BrowseIssue, issueKey) + Environment.NewLine);
  132.         return;
  133.       }
  134.  
  135.       // log if links are equal and set origin to null
  136.       // assuming the link is an internal thread
  137.       if (Equals(originThread, internalThread))
  138.       {
  139.         File.AppendAllText(LOG_EQUAL_LINKS, string.Format(URL_BrowseIssue, issueKey) + Environment.NewLine);
  140.         originThread = null;
  141.       }
  142.  
  143.       if (!string.IsNullOrEmpty(internalThread))
  144.       {
  145.         CreateRemoteIssueLink(issueKey, true, internalThread);
  146.       }
  147.  
  148.       if (!string.IsNullOrEmpty(originThread))
  149.       {
  150.         CreateRemoteIssueLink(issueKey, false, originThread);
  151.       }
  152.     }
  153.  
  154.     private static void CreateRemoteIssueLink(string issueKey, bool isInternal, string linkURL)
  155.     {
  156.       string url = string.Format(URL_BrowseIssue, issueKey);
  157.       File.AppendAllText(LOG_CREATED_LINKS,
  158.           string.Format("Link created: issue     {0}", url) + Environment.NewLine);
  159.       File.AppendAllText(LOG_CREATED_LINKS, isInternal
  160.         ? string.Format("              internal  {0}", linkURL) + Environment.NewLine
  161.         : string.Format("              public    {0}", linkURL) + Environment.NewLine);
  162.  
  163.       string linkTitle = isInternal ? "Internal Forum Thread" : "Forum Thread";
  164.  
  165.       RemoteIssueLink linkObject = new RemoteIssueLink(linkTitle, linkURL);
  166.       linkObject.Object.Icon.Icon16x16URL = "http://somelinkurl.com/favicon.ico";
  167.       string jsonString = JsonConvert.SerializeObject(linkObject);
  168.  
  169.       var request = new RestRequest();
  170.       request.AddHeader("Content-Type", "application/json");
  171.       request.Method = Method.POST;
  172.       request.Resource = "issue/{IssueKey}/remotelink";
  173.       request.AddUrlSegment("IssueKey", issueKey);
  174.       request.RequestFormat = DataFormat.Json;
  175.       request.AddParameter("application/json", jsonString, ParameterType.RequestBody);
  176.  
  177.       var response = client.Execute(request);
  178.  
  179.       if (!string.IsNullOrEmpty(response.ErrorMessage))
  180.         File.AppendAllText(LOG_CREATED_LINKS, response.ErrorMessage + Environment.NewLine);
  181.       else
  182.         File.AppendAllText(LOG_CREATED_LINKS, response.StatusCode + Environment.NewLine);
  183.       Console.WriteLine("   " + response.StatusCode);
  184.     }
  185.  
  186.     private static string CleanLinkURL(string linkURL)
  187.     {
  188.       Regex regexPost = new Regex(@".*\.(\d+).*-(\d+)");
  189.       Regex regexThread = new Regex(@".*\.(\d+)\/");
  190.  
  191.       var match = regexPost.Match(linkURL);
  192.       if (match.Groups.Count == 1)
  193.       {
  194.         match = regexThread.Match(linkURL);
  195.         linkURL = string.Format(
  196.           "http://somelinkurl.com/threads/{0}",
  197.           match.Groups[1].Value);
  198.       }
  199.       else
  200.       {
  201.         linkURL = string.Format(
  202.           "http://somelinkurl.com/posts/{0}",
  203.           match.Groups[2].Value);
  204.       }
  205.  
  206.       return linkURL;
  207.     }
  208.  
  209.  
  210.  
  211.  
  212.     private static string DownloadJSON(string url)
  213.     {
  214.       CompressionWebClient webClient = new CompressionWebClient(true) { Encoding = Encoding.UTF8 };
  215.       //foreach (KeyValuePair<string, string> headerEntry in Headers)
  216.       //  webClient.Headers[headerEntry.Key] = headerEntry.Value;
  217.  
  218.       return webClient.DownloadString(url);
  219.     }
  220.   }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement