/// <summary> /// Handle title changes, check for JIRA /// </summary> /// <param name="windowTitle">string with title</param> private void MonitorTitleChangeEvent(string windowTitle) { var jiraKeyMatch = _jiraKeyPattern.Match(windowTitle); if (!jiraKeyMatch.Success) { return; } // Found a possible JIRA title var jiraKey = jiraKeyMatch.Value; var jiraKeyParts = jiraKey.Split('-'); var projectKey = jiraKeyParts[0]; var jiraId = jiraKeyParts[1]; // Check if we have a JIRA instance with a project for this key if (_projectJiraClientMap.TryGetValue(projectKey, out var _)) { // We have found a project for this _jira key, so it must be a valid & known JIRA if (_recentJiras.TryGetValue(jiraKey, out var currentJiraDetails)) { // update currentJiraDetails.SeenAt = DateTimeOffset.Now; return; } // We detected an unknown JIRA, so add it to our list currentJiraDetails = new JiraDetails { Id = jiraId, ProjectKey = projectKey }; _recentJiras.Add(currentJiraDetails.JiraKey, currentJiraDetails); // Make sure we don't collect _jira's until the memory is full if (_recentJiras.Count > _jiraConfiguration.MaxEntries) { // Add it to the list of recent Jiras _recentJiras = (from jiraDetails in _recentJiras.Values.ToList() orderby jiraDetails.SeenAt descending select jiraDetails).Take(_jiraConfiguration.MaxEntries).ToDictionary(jd => jd.JiraKey, jd => jd); } // Now we can get the title from JIRA itself // ReSharper disable once UnusedVariable var updateTitleTask = DetectedNewJiraIssueAsync(currentJiraDetails); } else { Log.Info().WriteLine("Couldn't match possible JIRA key {0} to projects in a configured JIRA instance, ignoring", projectKey); } }
/// <summary> /// This method will update details, like the title, and send an event to registed listeners of the JiraEvent /// </summary> /// <param name="jiraDetails">Contains the jira key to retrieve the title (XYZ-1234)</param> /// <returns>Task</returns> private async Task DetectedNewJiraIssueAsync(JiraDetails jiraDetails) { try { if (_projectJiraClientMap.TryGetValue(jiraDetails.ProjectKey, out var jiraClient)) { var issue = await jiraClient.Issue.GetAsync(jiraDetails.JiraKey).ConfigureAwait(false); jiraDetails.JiraIssue = issue; } } catch (Exception ex) { Log.Warn().WriteLine("Couldn't retrieve JIRA title: {0}", ex.Message); } }
/// <summary> /// Retrieve the API belonging to a JiraDetails /// </summary> /// <param name="jiraDetails"></param> /// <returns>IJiraClient</returns> public IJiraClient GetJiraClientForKey(JiraDetails jiraDetails) { return(_projectJiraClientMap[jiraDetails.ProjectKey]); }