Exemplo n.º 1
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            expanderGrid.Visibility = Visibility.Collapsed;
            errorMessage.Content    = "";
            var credential = CredentialManager.Instance.Credentials.FirstOrDefault(x => x?.BaseURL == Config.BaseURL);

            if (credential == null)
            {
                errorMessage.Content = $"No credentials exists for that URL";
                return;
            }
            var jira         = new Jira(Config.BaseURL, credential.Username, credential.Password);
            var issueService = jira.GetIssueService();
            var fieldService = jira.GetFieldService();

            try
            {
                errorMessage.Content = $"Attempting connection...";
                issues = issueService.Search(Config.JQLFilter);
                var fields = fieldService.GetFields();
                Dictionary <string, FieldMapViewModel> fieldMaps = new Dictionary <string, FieldMapViewModel>();
                foreach (var issue in issues)
                {
                    var properties      = ((JObject)issue.InternalObject.SelectTokens("$").First()).Properties().Where(x => x.Value is JValue);
                    var fieldsContainer = issue.InternalObject.SelectToken("$.fields") as JObject;
                    var issueFields     = fieldsContainer.Properties();
                    foreach (var prop in properties)
                    {
                        if (!fieldMaps.ContainsKey(prop.Path))
                        {
                            fieldMaps.Add(prop.Path, new FieldMapViewModel {
                                DisplayedName = prop.Name, JiraJSONPath = prop.Path, Map = false, ToscaFields = toscaFields, ExampleValues = new HashSet <string>()
                            });
                        }
                        fieldMaps[prop.Path].ExampleValues.Add(prop.Value.ToString());
                    }
                    foreach (var issueField in issueFields)
                    {
                        var    field     = fields.FirstOrDefault(x => x.id == issueField.Name);
                        string fieldName = (field != null ? field.name : issueField.Name);
                        if (issueField.Value is JValue)
                        {
                            if (!fieldMaps.ContainsKey(issueField.Path))
                            {
                                fieldMaps.Add(issueField.Path, new FieldMapViewModel
                                {
                                    DisplayedName = fieldName,
                                    ExampleValues = new HashSet <string>(),
                                    JiraJSONPath  = issueField.Path,
                                    Map           = false,
                                    ToscaFields   = toscaFields
                                });
                            }
                            fieldMaps[issueField.Path].ExampleValues.Add(issueField.Value.ToString());
                        }
                        else
                        {
                            JObject fieldValue = issueField.Value as JObject;
                            if (fieldValue == null || fieldValue.Properties() == null)
                            {
                                continue;
                            }
                            var fieldValueProps = fieldValue.Properties().Where(x => x.Value is JValue);
                            foreach (var fieldValueProp in fieldValueProps)
                            {
                                if (!fieldMaps.ContainsKey(fieldValueProp.Path))
                                {
                                    fieldMaps.Add(fieldValueProp.Path, new FieldMapViewModel
                                    {
                                        DisplayedName = $"{fieldName} -> {fieldValueProp.Name}",
                                        ExampleValues = new HashSet <string>(),
                                        JiraJSONPath  = fieldValueProp.Path,
                                        Map           = false,
                                        ToscaFields   = toscaFields
                                    });
                                }
                                fieldMaps[fieldValueProp.Path].ExampleValues.Add(fieldValueProp.Value.ToString());
                            }
                        }
                    }
                }
                foreach (var map in fieldMaps.OrderBy(x => x.Value.DisplayedName))
                {
                    if (Config.Config.parentLocatorPropertyOverride == map.Value.JiraJSONPath)
                    {
                        map.Value.ParentLocator = true;
                    }
                    FieldMaps.Add(map.Value);
                }
                foreach (var configMap in Config.Config.fieldMaps)
                {
                    if (FieldMaps.Any(x => x.JiraJSONPath == configMap.jiraJsonPath))
                    {
                        var matchingFM = FieldMaps.First(x => x.JiraJSONPath == configMap.jiraJsonPath);
                        matchingFM.Map        = true;
                        matchingFM.ToscaField = configMap.toscaField;
                    }
                }
                errorMessage.Content    = $"Success - {issues.Length} issues match";
                expanderGrid.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                errorMessage.Content = $"Error: {ex.Message}";
            }
        }