public async Task <ActionResult> Create(TeamCityViewModel viewModel, string returnUrl) { var projects = db.ProjectUser.Where(pu => pu.UserName.Trim() == User.Identity.Name).Select(pu => pu.Project); viewModel.ProjectList = new SelectList(projects.ToList(), "Id", "Name"); viewModel.BuildIdsList = new SelectList(new List <string>()); if (ModelState.IsValid) { try { if (CheckTeamCitySetup(viewModel)) { // Save Data var teamCityModel = new TeamCitySetup() { Uri = viewModel.URI.Trim(), Crendential = new Crypt32().Encrypt(Crypt32.KeyType.UserKey, viewModel.UserName + ";" + viewModel.Password), IdProject = viewModel.Project.Id, BuildId = viewModel.BuildId }; db.TeamCitySetup.Add(teamCityModel); db.SaveChanges(); TempData["Success"] = "Yes"; return(RedirectToAction("Index")); } } catch (WebException e) { if (e.Response != null) { using (WebResponse response = e.Response) { var httpResponse = (HttpWebResponse)response; if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) { ModelState.AddModelError(string.Empty, "Invalid username or password."); } else { ModelState.AddModelError(string.Empty, "Cannot establish connection with this server."); } } } else { ModelState.AddModelError(string.Empty, "Cannot establish connection with this server."); } } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); } } return(View(viewModel)); }
public static string GetTeamCityLastBuildId(TeamCitySetup teamCityModel) { // http://10.101.48.18:8111/httpAuth/app/rest/builds/?locator=project:TaggedForms try { var userName = new Crypt32().Decrypt(teamCityModel.Crendential).Split(';')[0]; var password = new Crypt32().Decrypt(teamCityModel.Crendential).Split(';')[1]; var tcProjectId = teamCityModel.BuildId.Split('_')[0]; var request = (HttpWebRequest)WebRequest.Create(teamCityModel.Uri.Trim() + "/httpAuth/app/rest/builds/?locator=project:" + tcProjectId); request.Method = "GET"; request.ContentType = "application/xml"; request.Accept = "application/xml"; String encoded = System.Convert.ToBase64String( System.Text.Encoding.GetEncoding("ISO-8859-1") .GetBytes(userName + ":" + password)); request.Headers.Add("Authorization", "Basic " + encoded); string buildId = ""; using (WebResponse response = request.GetResponse()) { if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); string responseStr = new StreamReader(responseStream).ReadToEnd(); var set = new XmlReaderSettings(); set.ConformanceLevel = ConformanceLevel.Fragment; var doc = new XPathDocument(XmlReader.Create(new StringReader(responseStr), set)); XPathNavigator nav = doc.CreateNavigator(); var node = nav.Select("builds/build"); if (node.MoveNext()) { return(node.Current.GetAttribute("id", "")); } } } return(""); } catch (Exception ex) { return(ex.Message); } }
public static TeamCityBuildTO GetBuildStatus(string buildId, TeamCitySetup teamCityModel) { /* GET /httpAuth/app/rest/buildQueue/id:135763 */ var userName = new Crypt32().Decrypt(teamCityModel.Crendential).Split(';')[0]; var password = new Crypt32().Decrypt(teamCityModel.Crendential).Split(';')[1]; var request = (HttpWebRequest)WebRequest.Create(teamCityModel.Uri.Trim() + "/httpAuth/app/rest/buildQueue/id:" + buildId); String encoded = System.Convert.ToBase64String( System.Text.Encoding.GetEncoding("ISO-8859-1") .GetBytes(userName + ":" + password)); request.Headers.Add("Authorization", "Basic " + encoded); using (WebResponse response = request.GetResponse()) { if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK) { Stream responseStream = response.GetResponseStream(); string responseStr = new StreamReader(responseStream).ReadToEnd(); var set = new XmlReaderSettings(); set.ConformanceLevel = ConformanceLevel.Fragment; var doc = new XPathDocument(XmlReader.Create(new StringReader(responseStr), set)); XPathNavigator nav = doc.CreateNavigator(); var tcBuildTO = new TeamCityBuildTO(); tcBuildTO.State = nav.SelectSingleNode("build").GetAttribute("state", ""); tcBuildTO.Status = nav.SelectSingleNode("build").GetAttribute("status", ""); if ((tcBuildTO.State == "finished")) { tcBuildTO.Number = int.Parse(nav.SelectSingleNode("build").GetAttribute("number", "")); tcBuildTO.StatusText = nav.SelectSingleNode("build/statusText").ToString(); tcBuildTO.StartDate = DateTime.ParseExact(nav.SelectSingleNode("build/startDate").ToString(), "yyyyMMdd'T'HHmmss-ffff", CultureInfo.InvariantCulture); } // 2016 08 24 T 15 09 10 -0300 return(tcBuildTO); } } return(null); }