PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/GoIntegration/GoIntegrator.cs

https://gitlab.com/TaviscaSolutions/lucy
C# | 161 lines | 134 code | 24 blank | 3 comment | 0 complexity | d8c2e57a3b21fb2f442efa5ed6de6098 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Lucy.Core.Model;
  7. using Lucy.Core.Contracts;
  8. using System.Net;
  9. using System.IO;
  10. using Newtonsoft.Json.Linq;
  11. using System.Xml;
  12. using System.Xml.Linq;
  13. using Lucy.Core.CustomAttributes;
  14. namespace GoIntegration
  15. {
  16. public class GoIntegrator : IGoIntegrator
  17. {
  18. private const string URL = "http://hfl-build.pune.tavisca.com:8153/go/api/pipelines/";
  19. private const string DATA = @"{""object"":{""name"":""Name""}}";
  20. [CommandArgumentAttribute("Pipeline Name", "string", "Name of the pipeline to be run.")]
  21. [CommandAttribute("TiggerPipelineInstance(string [pipeline_name])", "Runs the pipeline and returns the status of execution")]
  22. [CommandReturnsAttribute("XmlDocument", "Returns the status of pipeline")]
  23. public XmlDocument TriggerPipelineInstance(string pipelineName)
  24. {
  25. string url=URL+pipelineName+"/schedule";
  26. //string url = URL +pipelineName+ "hfl-local/schedule";
  27. string response=_GetResponseFromGo(url);
  28. string xmlResponse = "<status>" + response + "</status>";
  29. XmlDocument xmlDoc = new XmlDocument();
  30. xmlDoc.LoadXml(xmlResponse);
  31. Console.WriteLine(xmlResponse);
  32. return xmlDoc;
  33. }
  34. [CommandArgumentAttribute("PipelineName,CommitId", "string,string", "Name of the pipeline to be run,Revision no of pipeline to be run.")]
  35. [CommandAttribute("TriggerPipelineInstance(string [pipeline_name])", "Runs the pipeline and returns the status of execution")]
  36. [CommandReturnsAttribute("XmlDocument", "Returns the status of pipeline")]
  37. public XmlDocument TriggerPipelineInstanceWithCommitId(string pipelineName, string commitTag)
  38. {
  39. string url = "materials[svn_material]="+ commitTag + " " + URL + pipelineName+ "/schedule";
  40. string response = _GetResponseFromGo(url);
  41. string xmlResponse = "<status>" + response + "</status>";
  42. XmlDocument xmlDoc = new XmlDocument();
  43. xmlDoc.LoadXml(xmlResponse);
  44. Console.WriteLine(xmlResponse);
  45. return xmlDoc;
  46. }
  47. private string _GetResponseFromGo(string url)
  48. {
  49. string response = string.Empty;
  50. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  51. request.Method = "POST";
  52. request.ContentType = "application/json";
  53. request.ContentLength = DATA.Length;
  54. StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
  55. requestWriter.Write(DATA);
  56. requestWriter.Close();
  57. try
  58. {
  59. WebResponse webResponse = request.GetResponse();
  60. Stream webStream = webResponse.GetResponseStream();
  61. StreamReader responseReader = new StreamReader(webStream);
  62. response = responseReader.ReadToEnd();
  63. Console.Out.WriteLine(response);
  64. responseReader.Close();
  65. }
  66. catch (Exception e)
  67. {
  68. GoCustomExceptions goServerNotFound = new GoCustomExceptions("GoServerNotFound", "Connection to go server not established " + e.Message);
  69. throw goServerNotFound;
  70. }
  71. return response;
  72. }
  73. private System.Xml.XmlDocument _ConvertToResponse(System.Xml.XmlDocument inputDocument)
  74. {
  75. XDocument doc = XDocument.Load(@"D:\ConsoleApplication3\ConsoleApplication3\Sample.xml");
  76. var entries = doc.Descendants("entry")
  77. .Select(x => new
  78. {
  79. Title = (string)x.Element("title"),
  80. Id = (string)x.Element("id")
  81. });
  82. foreach (var entry in entries)
  83. {
  84. Console.WriteLine("Start: {0}; End: {1}", entry.Title, entry.Id);
  85. }
  86. Console.ReadLine();
  87. XmlDocument xmlDoc1 = new XmlDocument();
  88. XmlNode rootNode = xmlDoc1.CreateElement("GoResponse");
  89. XmlNode feedNode = xmlDoc1.CreateElement("feed");
  90. xmlDoc1.AppendChild(rootNode);
  91. rootNode.AppendChild(feedNode);
  92. XmlNode entryNode;
  93. XmlNode titleNode;
  94. XmlNode idNode;
  95. foreach (var project in entries)
  96. {
  97. entryNode = xmlDoc1.CreateElement("entry");
  98. titleNode = xmlDoc1.CreateElement("title");
  99. idNode = xmlDoc1.CreateElement("id");
  100. // Console.WriteLine("Start: {0}; End: {1}", project.Title, project.Id);
  101. titleNode.InnerXml = project.Title;
  102. idNode.InnerXml = project.Id;
  103. feedNode.AppendChild(entryNode);
  104. entryNode.AppendChild(titleNode);
  105. entryNode.AppendChild(idNode);
  106. }
  107. return xmlDoc1;
  108. //xmlDoc1.Save("Samples.xml");
  109. }
  110. public string RequestResponse(string url)
  111. {
  112. string response=string.Empty;
  113. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  114. request.Method = "POST";
  115. request.ContentType = "application/json";
  116. request.ContentLength = DATA.Length;
  117. StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
  118. requestWriter.Write(DATA);
  119. requestWriter.Close();
  120. try
  121. {
  122. WebResponse webResponse = request.GetResponse();
  123. Stream webStream = webResponse.GetResponseStream();
  124. StreamReader responseReader = new StreamReader(webStream);
  125. response = responseReader.ReadToEnd();
  126. Console.Out.WriteLine(response);
  127. responseReader.Close();
  128. }
  129. catch (Exception e)
  130. {
  131. Console.Out.WriteLine("-----------------");
  132. Console.Out.WriteLine(e.Message);
  133. }
  134. return response;
  135. }
  136. }
  137. }