Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using RestSharp;
- using jira_RemoteIssueLinkCreator.Data;
- using System.Text.RegularExpressions;
- namespace jira_RemoteIssueLinkCreator
- {
- class Program
- {
- private const string URL_BASE = "http://******************.com/";
- private const string URL_API_BASE = URL_BASE + "rest/api/2/";
- private const string URL_BrowseIssue = URL_BASE + "browse/{0}";
- private const string URL_Search = URL_API_BASE + "search?jql={0}&maxResults=1000&fields=*all,-comment";
- private const string URL_Issue = URL_API_BASE + "issue/{0}";
- private const string LOG_CREATED_LINKS = "log_createdLinks.txt";
- private const string LOG_NO_LINKS = "log_noLinks.txt";
- private const string LOG_EQUAL_LINKS = "log_equalLinks.txt";
- private const string JQLInternal = "project in (JIRA, PROJKEY2) AND \"Team Internal Thread\" ~ \"http*\"";
- private const string JQLOrigin = "project in (JIRA, PROJKEY2) AND \"Issue Origin Thread\" ~ \"http*\"";
- private static List<string> issues = new List<string>() { "JIRA-0815", "JIRA-4711" };
- static RestClient client = new RestClient();
- private static bool isTest;
- private static string username;
- private static string password;
- private static void Main(string[] args)
- {
- client = new RestClient();
- client.BaseUrl = URL_API_BASE;
- File.WriteAllText(LOG_CREATED_LINKS, string.Empty);
- File.WriteAllText(LOG_EQUAL_LINKS, string.Empty);
- File.WriteAllText(LOG_NO_LINKS, string.Empty);
- Console.WriteLine("jira-RemoteIssueLinkCreate");
- Console.WriteLine("==========================");
- AskForCredentials();
- bool doExit = false;
- while (!doExit)
- {
- Console.WriteLine("What do you want to do? (exit / test / run / login)");
- string input = Console.ReadLine();
- doExit = input == "exit";
- if (input == "run")
- {
- MigrateIssuesOfQuery(JQLInternal);
- MigrateIssuesOfQuery(JQLOrigin);
- }
- else if (input == "test")
- {
- foreach (string issue in issues)
- MigrateIssue(issue);
- }
- else if (input == "login")
- AskForCredentials();
- }
- }
- private static void AskForCredentials()
- {
- Console.WriteLine();
- Console.WriteLine("Enter your username:");
- username = Console.ReadLine();
- Console.WriteLine("Enter your password (ATTENTION !!! PASSWORD WILL BE VISIBLE):");
- password = Console.ReadLine();
- client.Authenticator = new HttpBasicAuthenticator(username, password);
- }
- private static void MigrateIssuesOfQuery(string jql)
- {
- string url = string.Format(URL_Search, jql);
- //url = WebUtility.UrlEncode(url);
- string json = DownloadJSON(url);
- JObject o = JObject.Parse(json);
- JArray issues = (JArray)o["issues"];
- foreach (JToken issue in issues)
- MigrateIssue((JObject)issue);
- }
- private static void MigrateIssue(string issueKey)
- {
- string url = string.Format(URL_Issue, issueKey);
- string json = DownloadJSON(url);
- JObject issue = JObject.Parse(json);
- MigrateIssue(issue);
- }
- private static void MigrateIssue(JObject issue)
- {
- string issueKey = (string)issue["key"];
- Console.Write(issueKey);
- string internalThread;
- try
- {
- internalThread = (string)issue["fields"]["customfield_10411"];
- }
- catch (Exception)
- {
- internalThread = null;
- }
- string originThread;
- try
- {
- originThread = (string)issue["fields"]["customfield_10409"];
- }
- catch (Exception)
- {
- originThread = null;
- }
- // log if issue has not links
- if (string.IsNullOrEmpty(internalThread) && string.IsNullOrEmpty(originThread))
- {
- File.AppendAllText(LOG_NO_LINKS, string.Format(URL_BrowseIssue, issueKey) + Environment.NewLine);
- return;
- }
- // log if links are equal and set origin to null
- // assuming the link is an internal thread
- if (Equals(originThread, internalThread))
- {
- File.AppendAllText(LOG_EQUAL_LINKS, string.Format(URL_BrowseIssue, issueKey) + Environment.NewLine);
- originThread = null;
- }
- if (!string.IsNullOrEmpty(internalThread))
- {
- CreateRemoteIssueLink(issueKey, true, internalThread);
- }
- if (!string.IsNullOrEmpty(originThread))
- {
- CreateRemoteIssueLink(issueKey, false, originThread);
- }
- }
- private static void CreateRemoteIssueLink(string issueKey, bool isInternal, string linkURL)
- {
- string url = string.Format(URL_BrowseIssue, issueKey);
- File.AppendAllText(LOG_CREATED_LINKS,
- string.Format("Link created: issue {0}", url) + Environment.NewLine);
- File.AppendAllText(LOG_CREATED_LINKS, isInternal
- ? string.Format(" internal {0}", linkURL) + Environment.NewLine
- : string.Format(" public {0}", linkURL) + Environment.NewLine);
- string linkTitle = isInternal ? "Internal Forum Thread" : "Forum Thread";
- RemoteIssueLink linkObject = new RemoteIssueLink(linkTitle, linkURL);
- linkObject.Object.Icon.Icon16x16URL = "http://somelinkurl.com/favicon.ico";
- string jsonString = JsonConvert.SerializeObject(linkObject);
- var request = new RestRequest();
- request.AddHeader("Content-Type", "application/json");
- request.Method = Method.POST;
- request.Resource = "issue/{IssueKey}/remotelink";
- request.AddUrlSegment("IssueKey", issueKey);
- request.RequestFormat = DataFormat.Json;
- request.AddParameter("application/json", jsonString, ParameterType.RequestBody);
- var response = client.Execute(request);
- if (!string.IsNullOrEmpty(response.ErrorMessage))
- File.AppendAllText(LOG_CREATED_LINKS, response.ErrorMessage + Environment.NewLine);
- else
- File.AppendAllText(LOG_CREATED_LINKS, response.StatusCode + Environment.NewLine);
- Console.WriteLine(" " + response.StatusCode);
- }
- private static string CleanLinkURL(string linkURL)
- {
- Regex regexPost = new Regex(@".*\.(\d+).*-(\d+)");
- Regex regexThread = new Regex(@".*\.(\d+)\/");
- var match = regexPost.Match(linkURL);
- if (match.Groups.Count == 1)
- {
- match = regexThread.Match(linkURL);
- linkURL = string.Format(
- "http://somelinkurl.com/threads/{0}",
- match.Groups[1].Value);
- }
- else
- {
- linkURL = string.Format(
- "http://somelinkurl.com/posts/{0}",
- match.Groups[2].Value);
- }
- return linkURL;
- }
- private static string DownloadJSON(string url)
- {
- CompressionWebClient webClient = new CompressionWebClient(true) { Encoding = Encoding.UTF8 };
- //foreach (KeyValuePair<string, string> headerEntry in Headers)
- // webClient.Headers[headerEntry.Key] = headerEntry.Value;
- return webClient.DownloadString(url);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement