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