public void Initialize(Project project, BuildDetails buildDetails) { this.project = project; this.buildDetails = buildDetails; buildNumberLabel.Text = buildDetails.Number.ToString(); reasonMemoEdit.Select(); }
public static void FillInBuildDetails(BuildDetails res, XmlDocument xml) { XmlNode claimedNode = xml.SelectSingleNode("/*/action[claimed/text() = 'true']"); if (claimedNode == null) return; var reasonNode = claimedNode.SelectSingleNode("reason"); var claimedByNode = claimedNode.SelectSingleNode("claimedBy"); ClaimDetails claimDetails = new ClaimDetails(); claimDetails.User = claimedByNode.InnerText; claimDetails.Reason = reasonNode != null ? reasonNode.InnerText : ""; res.ClaimDetails = claimDetails; }
public void ClaimBuild(Project project, BuildDetails buildDetails, string reason, bool sticky) { string url = NetUtils.ConcatUrls(project.Url, buildDetails.Number.ToString(), "/claim/claim"); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // we don't want to follow redirections request.AllowAutoRedirect = false; var credentials = project.Server.Credentials; if (credentials != null) request.Credentials = new NetworkCredential(credentials.Username, credentials.Password); using (Stream postStream = request.GetRequestStream()) { var claim = new ClaimDetailsDto { Reason = reason, Sticky = sticky }; var stream = new MemoryStream(); var serializer = new DataContractJsonSerializer(typeof(ClaimDetailsDto)); serializer.WriteObject(stream, claim); string json = Encoding.UTF8.GetString(stream.ToArray()); string postData = "json=" + HttpUtility.UrlEncode(json, Encoding.UTF8); using (var writer = new StreamWriter(postStream)) { writer.Write(postData); } } using (var response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.Found && response.StatusCode != HttpStatusCode.OK) throw new Exception("Received response code " + response.StatusCode); } string buildUrl = NetUtils.ConcatUrls(project.Url, buildDetails.Number.ToString(), "/api/xml"); JenkinsService.RemoveFromCache(buildUrl); }
public static void FillInBuildCauses(BuildDetails res, XmlDocument xml) { XmlNodeList causes = xml.SelectNodes("/*/action/cause"); res.Causes = new BuildCauses(); res.Causes.HasUniqueCauses = null; BuildCauseEnum causeEnum = BuildCauseEnum.Unknown; foreach (XmlNode causeNode in causes) { string causeShortDesc = causeNode["shortDescription"].InnerText; BuildCause cause = new BuildCause(); cause.ShortDescription = causeShortDesc; if (causeShortDesc.StartsWith("Started by user")) { cause.Cause = BuildCauseEnum.User; var userName = causeNode["userName"]; if (userName != null && userName.InnerText.Length > 0) { cause.Starter = userName.InnerText.ToString(); } var userID = causeNode["userId"]; if (userID != null && userID.InnerText.Length > 0) { cause.UserID = userID.InnerText.ToString(); } } else if (causeShortDesc.StartsWith("Started by Timer", StringComparison.CurrentCultureIgnoreCase)) { cause.Cause = BuildCauseEnum.Timer; } else if (causeShortDesc.StartsWith("Started by Upstream Project", StringComparison.CurrentCultureIgnoreCase)) { cause.Cause = BuildCauseEnum.UpstreamProject; var upstreamProject = causeNode["upstreamProject"]; if (upstreamProject != null && upstreamProject.InnerText.Length > 0) { cause.Starter = upstreamProject.InnerText.ToString(); } } else if (causeShortDesc.StartsWith("Started by an SCM change", StringComparison.CurrentCultureIgnoreCase)) { cause.Cause = BuildCauseEnum.SCM; } else if (causeShortDesc.StartsWith("Started by remote host", StringComparison.CurrentCultureIgnoreCase)) { cause.Cause = BuildCauseEnum.RemoteHost; string startedBy = @"Started by remote host "; string remoteHost = causeShortDesc.Remove(0, startedBy.Length); int index = remoteHost.IndexOf(" with note: "); cause.Starter = index > 0 ? remoteHost.Remove(index) : remoteHost; } else { cause.Cause = BuildCauseEnum.Unknown; } if (res.Causes.HasUniqueCauses == null) { causeEnum = cause.Cause; res.Causes.HasUniqueCauses = true; } else { if (cause.Cause != causeEnum) res.Causes.HasUniqueCauses = false; } res.Causes.Causes.Add(cause); } }
public void ClaimBuild(Project project, BuildDetails buildDetails, string reason, bool sticky) { string url = NetUtils.ConcatUrls(project.Url, buildDetails.Number.ToString(), "/claim/claim"); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; // we don't want to follow redirections request.AllowAutoRedirect = false; // currently supporting assigning to self string assignee = string.Empty; var credentials = project.Server.Credentials; if (credentials != null) { // claim plugin requests uses the username from the session cookie but not from request credentials request.Credentials = new NetworkCredential(credentials.Username, credentials.Password); assignee = credentials.Username; } try { using (Stream postStream = request.GetRequestStream()) { var claim = new ClaimDetailsDto { Assignee = assignee, Reason = reason, Sticky = sticky }; var stream = new MemoryStream(); var serializer = new DataContractJsonSerializer(typeof(ClaimDetailsDto)); serializer.WriteObject(stream, claim); string json = Encoding.UTF8.GetString(stream.ToArray()); string postData = "json=" + HttpUtility.UrlEncode(json, Encoding.UTF8); using (var writer = new StreamWriter(postStream)) { writer.Write(postData); } } using (var response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.Found && response.StatusCode != HttpStatusCode.OK) throw new Exception("Received response code " + response.StatusCode); } } catch (WebException webEx) { MessageBox.Show(webEx.Message, "Claim failed", MessageBoxButtons.OK, MessageBoxIcon.Error); LoggingHelper.LogError(logger, webEx); } catch (Exception ex) { LoggingHelper.LogError(logger, ex); } string buildUrl = NetUtils.ConcatUrls(project.Url, buildDetails.Number.ToString(), "/api/xml"); JenkinsService.RemoveFromCache(buildUrl); }
private BuildDetails GetBuildDetails(Credentials credentials, string buildUrl, bool ignoreUntrustedCertificate) { if (buildUrl == null) return null; String url = NetUtils.ConcatUrls(buildUrl, "/api/xml"); if (logger.IsDebugEnabled) logger.Debug("Getting build details from " + url); String xmlStr = DownloadString(credentials, url, true, ignoreUntrustedCertificate); if (logger.IsTraceEnabled) logger.Trace("XML: " + xmlStr); XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlStr); string number = xml.SelectSingleNode("/*/number").InnerText; string fullDisplayName = xml.SelectSingleNode("/*/fullDisplayName").InnerText; string timestamp = xml.SelectSingleNode("/*/timestamp").InnerText; string estimatedDuration = xml.SelectSingleNode("/*/estimatedDuration").InnerText; string duration = xml.SelectSingleNode("/*/duration").InnerText; XmlNode xmlResult = xml.SelectSingleNode("/*/result"); string result = xmlResult == null ? string.Empty : xmlResult.InnerText; XmlNodeList userNodes = xml.SelectNodes("/*/culprit/fullName"); TimeSpan ts = TimeSpan.FromSeconds(long.Parse(timestamp) / 1000); DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); date = date.Add(ts); TimeSpan estimatedts = TimeSpan.FromSeconds(long.Parse(estimatedDuration) / 1000); TimeSpan durationts = TimeSpan.FromSeconds(long.Parse(estimatedDuration) / 1000); ISet<string> users = new HashedSet<string>(); foreach (XmlNode userNode in userNodes) { string userName = StringUtils.ExtractUserName(userNode.InnerText); users.Add(userName); } BuildDetails res = new BuildDetails(); BuildCauses.FillInBuildCauses(res, xml); res.Number = int.Parse(number); res.DisplayName = fullDisplayName; res.Time = date; res.EstimatedDuration = estimatedts; res.Duration = durationts; res.Result = BuildStatus.StringToBuildStatus(result); res.Users = users; ClaimService.FillInBuildDetails(res, xml); if (logger.IsDebugEnabled) logger.Debug("Done getting build details"); return res; }
private string FormatUsers(BuildDetails details) { if (details == null) return "-"; string res = StringUtils.Join(details.Users, JenkinsTrayResources.BuildDetails_UserSeparator); return res; }
private string FormatEstimatedDuration(BuildDetails details) { if (details == null) return string.Empty; string res = string.Empty; DateTime endtime = details.Time.Add(details.EstimatedDuration); TimeSpan timeleft = TimeSpan.FromTicks(endtime.Subtract(DateTime.UtcNow).Ticks); if (timeleft.TotalHours >= 1) { res = string.Format(JenkinsTrayResources.BuildDetails_EstimatedDuration_HHMM_Remaining, timeleft.Days * 24 + timeleft.Hours, timeleft.Minutes); } else if (timeleft.TotalHours < -1) { res = string.Format(JenkinsTrayResources.BuildDetails_EstimatedDuration_HHMM_LongerThanUsual, Math.Abs(timeleft.Days * 24 + timeleft.Hours), Math.Abs(timeleft.Minutes)); } else if (timeleft.TotalHours < 0) { res = string.Format(JenkinsTrayResources.BuildDetails_EstimatedDuration_MM_LongerThanUsual, Math.Abs(timeleft.Minutes)); } else { res = string.Format(JenkinsTrayResources.BuildDetails_EstimatedDuration_MM_Remaining, timeleft.Minutes); } return res; }
private string FormatDuration(BuildDetails details) { if (details == null) return string.Empty; string res = string.Empty; if (details.Duration.TotalHours > 1) { res = string.Format(JenkinsTrayResources.BuildDetails_Duration_HHMM, details.Duration.Days * 24 + details.Duration.Hours, details.Duration.Minutes); } else if (details.Duration.TotalMinutes < 1) { res = JenkinsTrayResources.BuildDetails_Duration_0M; } else { res = string.Format(JenkinsTrayResources.BuildDetails_Duration_MM, Math.Max(details.Duration.Minutes + (details.Duration.Seconds >= 30 ? 1 : 0), 1)); } return res; }
private string FormatBuildDetailsWithDisplayName(BuildDetails details) { if (details == null) return "-"; string shortDisplayName = details.DisplayName.Replace(Project.Name, string.Empty).Trim(); string res = string.Empty; if (shortDisplayName.Equals(string.Concat("#", details.Number.ToString()))) { res = string.Format(JenkinsTrayResources.BuildDetails_Format_NumberDate, details.Number.ToString(), details.Time.ToLocalTime()); } else { res = string.Format(JenkinsTrayResources.BuildDetails_Format_DisplayName_NumberDate, shortDisplayName, details.Number.ToString(), details.Time.ToLocalTime()); } return res; }
private string FormatBuildDetails(BuildDetails details) { if (details == null) return "-"; string res = string.Format(JenkinsTrayResources.BuildDetails_Format_NumberDate, details.Number, details.Time.ToLocalTime()); return res; }