private async void _objects_TextUpdate(object sender, EventArgs e)
        {
            if (sender is ComboBox comboBox && _mitigation.Model != null)
            {
                var connector = DevOpsManager.GetConnector(_mitigation.Model);

                if (connector != null)
                {
                    string filter = comboBox.Text;
                    comboBox.Items.Clear();

                    var itemsAsync = await connector.GetItemsAsync(filter);

                    var items = itemsAsync?.ToArray();
                    if (items?.Any() ?? false)
                    {
                        comboBox.Items.AddRange(items);
                    }

                    comboBox.DroppedDown     = true;
                    comboBox.IntegralHeight  = true;
                    comboBox.SelectedIndex   = -1;
                    comboBox.Text            = filter;
                    comboBox.SelectionStart  = filter.Length;
                    comboBox.SelectionLength = 0;
                }
            }
        }
Esempio n. 2
0
        private async void _add_Click(object sender, EventArgs e)
        {
            var connector = DevOpsManager.GetConnector(_model);

            if (connector != null)
            {
                var fieldsAsync = await connector.GetWorkItemDevOpsFieldsAsync();

                var fields = fieldsAsync?.ToArray();
                if (fields?.Any() ?? false)
                {
                    var dialog = new FieldAssociationDialog();
                    dialog.Initialize(_model, fields);
                    if (dialog.ShowDialog(this) == DialogResult.OK)
                    {
                        var field         = dialog.Field;
                        var identityField = dialog.IdentityField;
                        if (field != null && identityField != null)
                        {
                            connector.SetWorkItemFieldMapping(field, identityField);
                            var row = new GridRow(field.Label, identityField)
                            {
                                Tag = field
                            };
                            _gridFields.PrimaryGrid.Rows.Add(row);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private async Task <KeyValuePair <IDevOpsField, IdentityField> > ManageField(IDevOpsField field, IdentityField identityField)
        {
            KeyValuePair <IDevOpsField, IdentityField> result = new KeyValuePair <IDevOpsField, IdentityField>(null, null);

            var connector = DevOpsManager.GetConnector(_model);

            if (connector != null)
            {
                var fieldsAsync = await connector.GetWorkItemDevOpsFieldsAsync();

                var fields = fieldsAsync?.ToArray();
                if (fields?.Any() ?? false)
                {
                    var dialog = new FieldAssociationDialog();
                    dialog.Initialize(_model, fields);
                    if (field != null && identityField != null)
                    {
                        dialog.SetField(field, identityField);
                    }
                    if (dialog.ShowDialog(this) == DialogResult.OK)
                    {
                        field         = dialog.Field;
                        identityField = dialog.IdentityField;
                        if (field != null && identityField != null)
                        {
                            connector.SetWorkItemFieldMapping(field, identityField);
                            DevOpsManager.UpdateConfig(_model);
                            result = new KeyValuePair <IDevOpsField, IdentityField>(field, identityField);
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
 private void KnownStateChanged(object sender, PropertyChangedEventArgs e)
 {
     if (!_loading && sender is GridCell cell)
     {
         try
         {
             _loading = true;
             if (cell.GridRow.Tag is string devOpsState)
             {
                 switch (cell.GridColumn.Name)
                 {
                 case "KnownState":
                     var connector = DevOpsManager.GetConnector(_model);
                     if (connector != null && cell.Value is string knownStateString)
                     {
                         var knownState = knownStateString.GetEnumValue <WorkItemStatus>();
                         connector.SetWorkItemStateMapping(devOpsState, knownState);
                         if (knownState == WorkItemStatus.Unknown)
                         {
                             cell.Value = connector.WorkItemStateMappings?
                                          .Where(x => string.CompareOrdinal(x.Key, devOpsState) == 0)?
                                          .Select(x => x.Value)
                                          .FirstOrDefault() ?? WorkItemStatus.Unknown;
                         }
                     }
                     break;
                 }
             }
         }
         finally
         {
             _loading = false;
         }
     }
 }
Esempio n. 5
0
        public async Task Initialize([NotNull] IThreatModel model)
        {
            _model    = model;
            _tag.Text = null;
            _parents.Items.Clear();
            _itemTypes.Items.Clear();

            var connector = DevOpsManager.GetConnector(model);

            if (connector != null)
            {
                _tag.Text = connector.Tag;

                if (connector.MasterParent != null)
                {
                    _parents.Items.Add(connector.MasterParent);
                    _parents.SelectedIndex = 0;
                    _parentItemType.Text   = connector.MasterParent.WorkItemType;
                }

                var itemTypesAsync = await connector.GetWorkItemTypesAsync();

                var itemTypes = itemTypesAsync?.ToArray();
                if (itemTypes?.Any() ?? false)
                {
                    _itemTypes.Items.AddRange(itemTypes);
                    if (!string.IsNullOrEmpty(connector.WorkItemType) &&
                        _itemTypes.Items.Contains(connector.WorkItemType))
                    {
                        _itemTypes.SelectedItem = connector.WorkItemType;
                    }
                }

                var fieldsAsync = await connector.GetWorkItemDevOpsFieldsAsync();

                var fields = fieldsAsync?.ToArray();
                if (fields?.Any() ?? false)
                {
                    var mappings = connector.WorkItemFieldMappings?.ToArray();

                    foreach (var mapping in mappings)
                    {
                        var field = fields.FirstOrDefault(x => string.CompareOrdinal(mapping.Key, x.Id) == 0);
                        if (field != null)
                        {
                            var row = new GridRow(field.Label, mapping.Value)
                            {
                                Tag = field
                            };
                            _gridFields.PrimaryGrid.Rows.Add(row);
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        private void _parents_SelectedIndexChanged(object sender, EventArgs e)
        {
            var connector = DevOpsManager.GetConnector(_model);

            if (connector != null && _parents.SelectedItem is IDevOpsItemInfo item)
            {
                connector.MasterParent = item;
                DevOpsManager.UpdateConfig(_model);
                _parentItemType.Text = item.WorkItemType;
            }
        }
Esempio n. 7
0
        public void Dispose()
        {
            DevOpsManager.ConnectorAdded   -= OnConnectorAdded;
            DevOpsManager.ConnectorRemoved -= OnConnectorRemoved;
            var connector = DevOpsManager.GetConnector(_model);

            if (connector != null)
            {
                connector.ProjectOpened -= OnProjectOpened;
            }
            _model = null;
        }
Esempio n. 8
0
        public void ExecuteRibbonAction(IThreatModel threatModel, IActionDefinition action)
        {
            try
            {
                var connector = DevOpsManager.GetConnector(threatModel);

                switch (action.Name)
                {
                case "Connect":
                    var dialog = new DevOpsConnectionDialog();
                    if (connector == null)
                    {
                        var schemaManager = new DevOpsConfigPropertySchemaManager(threatModel);
                        dialog.Connection = schemaManager.ConnectionInfo;
                    }
                    else
                    {
                        dialog.Connector = connector;
                    }
                    if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                    {
                        connector = dialog.Connector;
                        var project = dialog.ProjectName;
                        if (connector != null && !string.IsNullOrWhiteSpace(project))
                        {
                            connector.OpenProject(project);
                            DevOpsManager.Register(connector, threatModel);
                            ChangeDisconnectButtonStatus(connector, true);
                        }
                    }

                    break;

                case "Disconnect":
                    if (connector != null && MessageBox.Show(Form.ActiveForm,
                                                             "You are about to disconnect the current DevOps Connector.\nIf you proceed, all the links between the Threat Model and the DevOps system may be lost.\nAre you sure?",
                                                             "DevOps Disconnect", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) ==
                        DialogResult.Yes)
                    {
                        DevOpsManager.Unregister(threatModel);
                        ChangeDisconnectButtonStatus(null, false);
                    }
                    break;
                }
            }
            catch
            {
                ShowWarning?.Invoke("Connection failed.");
                throw;
            }
        }
        private void LoadModel()
        {
            try
            {
                _loading = true;
                ClearPalettes();

                var schemaManager       = new RoadmapPropertySchemaManager(_model);
                var devOpsSchemaManager = new DevOpsPropertySchemaManager(_model);
                var connector           = DevOpsManager.GetConnector(_model);
                IEnumerable <IMitigation> mitigations;
                if (_filter == RoadmapStatus.NoActionRequired)
                {
                    mitigations = _model?.GetUniqueMitigations()?
                                  .Where(x => (schemaManager.GetStatus(x) != RoadmapStatus.NoActionRequired) ||
                                         (connector != null && devOpsSchemaManager.GetDevOpsInfo(x, connector) != null))
                                  .OrderBy(x => x.Name).ToArray();
                }
                else
                {
                    mitigations = _model?.GetUniqueMitigations()?
                                  .Where(x => schemaManager.GetStatus(x) == _filter)
                                  .OrderBy(x => x.Name).ToArray();
                }

                if (mitigations?.Any() ?? false)
                {
                    var summaries = DevOpsManager.GetMitigationsSummary(_model);

                    foreach (var mitigation in mitigations)
                    {
                        if (summaries?.ContainsKey(mitigation) ?? false)
                        {
                            var summary = summaries[mitigation];
                            AddItem(mitigation, summary.Status, summary.AssignedTo);
                        }
                        else
                        {
                            AddItem(mitigation, WorkItemStatus.Unknown, null);
                        }
                    }

                    RefreshNodes();
                }
            }
            finally
            {
                _loading = false;
            }
        }
 private async void _auto_Click(object sender, EventArgs e)
 {
     if (_mitigation?.Model != null)
     {
         var connector = DevOpsManager.GetConnector(_mitigation.Model);
         if (connector != null)
         {
             var devOpsItemInfos = (await connector.GetItemsAsync(_mitigation.Name))?.ToArray();
             if (devOpsItemInfos?.Any() ?? false)
             {
                 _objects.Items.AddRange(devOpsItemInfos);
                 _objects.SelectedIndex = 0;
             }
         }
     }
 }
Esempio n. 11
0
        public void Initialize([NotNull] IThreatModel model)
        {
            if (_model != null)
            {
                Dispose();
            }

            _model = model;
            var connector = DevOpsManager.GetConnector(model);

            if (connector != null)
            {
                connector.ProjectOpened += OnProjectOpened;
            }
            DevOpsManager.ConnectorAdded   += OnConnectorAdded;
            DevOpsManager.ConnectorRemoved += OnConnectorRemoved;
        }
Esempio n. 12
0
        public string GetValue([NotNull] IMitigation mitigation)
        {
            string result = null;

            // ReSharper disable once PatternAlwaysOfType
            if (mitigation.Model is IThreatModel model)
            {
                var connector = DevOpsManager.GetConnector(model);
                if (connector != null)
                {
                    var schemaManager = new DevOpsPropertySchemaManager(model);
                    var status        = schemaManager.GetDevOpsInfo(mitigation, connector);
                    result = (status?.Status ?? WorkItemStatus.Unknown).GetEnumLabel();
                }
            }

            return(result);
        }
Esempio n. 13
0
        private async void _itemTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            var connector = DevOpsManager.GetConnector(_model);

            if (connector != null && _itemTypes.SelectedItem is string itemType)
            {
                try
                {
                    _loading = true;
                    _gridStates.PrimaryGrid.Rows.Clear();

                    connector.WorkItemType = itemType;
                    DevOpsManager.UpdateConfig(_model);
                    var itemStatesAsync = await connector.GetWorkItemStatesAsync();

                    var itemStates = itemStatesAsync?.ToArray();
                    if (itemStates?.Any() ?? false)
                    {
                        var mappings = connector.WorkItemStateMappings?.ToArray();

                        foreach (var state in itemStates)
                        {
                            var value = mappings?
                                        .Where(x => string.CompareOrdinal(x.Key, state) == 0)?
                                        .Select(x => x.Value)
                                        .FirstOrDefault() ?? WorkItemStatus.Unknown;

                            var row = new GridRow(state, value.GetEnumLabel())
                            {
                                Tag = state
                            };
                            _gridStates.PrimaryGrid.Rows.Add(row);
                            row.Cells["KnownState"].PropertyChanged += KnownStateChanged;
                        }
                    }
                }
                finally
                {
                    _loading = false;
                }
            }
        }
Esempio n. 14
0
        public async void ExecuteRibbonAction([NotNull] IThreatModel threatModel, IActionDefinition action)
        {
            try
            {
                switch (action.Name)
                {
                case "Configure":
                    var dialog = new DevOpsConfigurationDialog();
                    await dialog.Initialize(threatModel);

                    dialog.ShowDialog(Form.ActiveForm);
                    var connector = DevOpsManager.GetConnector(threatModel);
                    Connect.ChangeDisconnectButtonStatus(connector, true);
                    break;
                }
            }
            catch
            {
                throw;
            }
        }
        private async void _ok_Click(object sender, EventArgs e)
        {
            if (_objects.SelectedItem is IDevOpsItemInfo devOpsItemInfo)
            {
                var connector = DevOpsManager.GetConnector(_mitigation.Model);
                if (connector != null)
                {
                    var workItemInfo = await connector.GetWorkItemInfoAsync(devOpsItemInfo.Id);

                    if (workItemInfo != null)
                    {
                        var schemaManager = new DevOpsPropertySchemaManager(_mitigation.Model);
                        schemaManager.SetDevOpsStatus(_mitigation, connector, devOpsItemInfo.Id,
                                                      devOpsItemInfo.Url, devOpsItemInfo.AssignedTo, workItemInfo.Status);

                        SelectedItemInfo = devOpsItemInfo;
                        DialogResult     = DialogResult.OK;
                    }
                }
            }
        }
Esempio n. 16
0
        private void _remove_Click(object sender, EventArgs e)
        {
            var selectedRow = _gridFields.GetSelectedCells()?
                              .OfType <GridCell>()
                              .Select(x => x.GridRow)
                              .FirstOrDefault();

            if (selectedRow.Tag is IDevOpsField field)
            {
                if (MessageBox.Show($"Are you sure you want to remove field '{field.Label}'?",
                                    "Remove field", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    var connector = DevOpsManager.GetConnector(_model);
                    if (connector != null)
                    {
                        connector.SetWorkItemFieldMapping(field, null);
                        _gridFields.PrimaryGrid.Rows.Remove(selectedRow);
                    }
                }
            }
        }
        private void InitializePalettes()
        {
            var connector  = DevOpsManager.GetConnector(_model);
            var itemStates = connector.GetWorkItemStatesAsync().Result?.ToArray();

            if (itemStates?.Any() ?? false)
            {
                var mappings = connector.WorkItemStateMappings?.ToArray();
                var captions = new List <string>();
                captions.Add(WorkItemStatus.Unknown.GetEnumLabel());
                var labels = itemStates
                             .Where(x => mappings.Any(y => string.CompareOrdinal(x, y.Key) == 0))
                             .Select(x => mappings.Where(y => string.CompareOrdinal(x, y.Key) == 0).First().Value.GetEnumLabel())
                             .Where(x => x != null)
                             .Distinct();
                if (labels.Any())
                {
                    captions.AddRange(labels);
                }
                Initialize(captions);
            }
        }
Esempio n. 18
0
        private void ItemOnMoveRequired(KanbanItem item)
        {
            if (item?.Item is IMitigation mitigation)
            {
                var connector = DevOpsManager.GetConnector(_model);
                if (connector != null)
                {
                    var indexInitial  = GetPosition(item);
                    var schemaManager = new DevOpsPropertySchemaManager(_model);
                    var devOpsInfo    = schemaManager.GetDevOpsInfo(mitigation, connector);
                    if (devOpsInfo != null)
                    {
                        var indexFinal = _captions.IndexOf(devOpsInfo.Status.GetEnumLabel());
                        Remove(item, indexInitial);
                        Add(item, indexFinal);

                        RefreshPalette(indexInitial);
                        RefreshPalette(indexFinal);
                    }
                }
            }
        }
Esempio n. 19
0
        private async void ObjectSingleClicked(object sender, GoObjectEventArgs e)
        {
            if (e.GoObject.ParentNode is KanbanItem item)
            {
                _itemDetails.Item = item.Item;

                _comments.Clear();

                if (item.Item is IThreatModelChild child && child.Model != null && item.Item is IMitigation mitigation)
                {
                    var connector = DevOpsManager.GetConnector(child.Model);
                    IEnumerable <Comment> comments = null;
                    var itemId = item.DevOpsId;
                    if (itemId > 0)
                    {
                        comments = (await connector.GetWorkItemCommentsAsync(itemId))?.ToArray();
                    }
                    else
                    {
                        comments = (await connector.GetWorkItemCommentsAsync(mitigation))?.ToArray();
                    }

                    if (comments?.Any() ?? false)
                    {
                        var myself = UserName.GetDisplayName();

                        foreach (var comment in comments)
                        {
                            _comments.AddComment(comment.Author, comment.Text, comment.Timestamp,
                                                 string.CompareOrdinal(myself, comment.Author) == 0);
                        }
                    }

                    LoadGrid(mitigation);
                }

                _comments.RefreshNodes();
            }
        }
Esempio n. 20
0
        public KanbanItem([NotNull] IMitigation mitigation, int originalContainer) :
            this(mitigation.Name, ThreatModelManager.StandardColor, originalContainer)
        {
            Item = mitigation;

            var model = mitigation.Model;

            if (model != null)
            {
                var schemaManager = new RoadmapPropertySchemaManager(model);
                SetInfo("Roadmap", schemaManager.GetStatus(mitigation).GetEnumLabel());

                _connector = DevOpsManager.GetConnector(model);
                if (_connector != null)
                {
                    var devOpsSchemaManager = new DevOpsPropertySchemaManager(model);
                    var info = devOpsSchemaManager.GetDevOpsInfo(mitigation, _connector);
                    if ((info?.Id ?? -1) >= 0)
                    {
                        DevOpsId = info.Id;
                    }
                }
            }
        }
Esempio n. 21
0
        private async void FieldButtonClick(object sender, EventArgs e)
        {
            if (sender is GridButtonXEditControl bc &&
                bc.EditorCell is GridCell cell &&
                cell.GridRow is GridRow row &&
                row.Tag is IDevOpsField field &&
                cell.Value is IdentityField identityField)
            {
                var connector = DevOpsManager.GetConnector(_model);
                if (connector != null)
                {
                    var fieldsAsync = await connector.GetWorkItemDevOpsFieldsAsync();

                    var fields = fieldsAsync?.ToArray();
                    if (fields?.Any() ?? false)
                    {
                        var dialog = new FieldAssociationDialog();
                        dialog.Initialize(_model, fields);
                        dialog.SetField(field, identityField);
                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            connector.SetWorkItemFieldMapping(field, null);
                            field         = dialog.Field;
                            identityField = dialog.IdentityField;
                            if (field != null && identityField != null)
                            {
                                connector.SetWorkItemFieldMapping(field, identityField);
                                row.Cells["Field"].Value = field.Label;
                                cell.Value = identityField;
                                row.Tag    = field;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 22
0
        public async void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            string text    = null;
            bool   warning = false;

            try
            {
                switch (action.Name)
                {
                case "Sync":
                    if ((await DevOpsManager.UpdateAsync(_model)) > 0)
                    {
                        LoadModel();
                    }
                    break;

                case "Auto":
                    var summaries     = DevOpsManager.GetMitigationsSummary(_model);
                    var schemaManager = new RoadmapPropertySchemaManager(_model);
                    var mitigations   = _model?.GetUniqueMitigations()?
                                        .Where(x => (schemaManager.GetStatus(x) != RoadmapStatus.NoActionRequired) &&
                                               !(summaries?.ContainsKey(x) ?? false))
                                        .OrderBy(x => x.Name).ToArray();
                    if (mitigations?.Any() ?? false)
                    {
                        var connector           = DevOpsManager.GetConnector(_model);
                        var devOpsSchemaManager = new DevOpsPropertySchemaManager(_model);
                        _countdown = new CountdownEvent(mitigations.Length);
                        foreach (var mitigation in mitigations)
                        {
                            AutoLoad(mitigation, connector, devOpsSchemaManager);
                        }

                        ShowMessage?.Invoke("Automatic Load in progress...");
                        AutomaticLoadCompletion();
                    }
                    else
                    {
                        ShowWarning?.Invoke("Automatic Load has not identified any action to do.");
                    }
                    break;

                case "ShortTerm":
                    _filter = RoadmapStatus.ShortTerm;
                    LoadModel();
                    break;

                case "MidTerm":
                    _filter = RoadmapStatus.MidTerm;
                    LoadModel();
                    break;

                case "LongTerm":
                    _filter = RoadmapStatus.LongTerm;
                    LoadModel();
                    break;

                case "All":
                    _filter = RoadmapStatus.NoActionRequired;
                    LoadModel();
                    break;

                case "Refresh":
                    LoadModel();
                    break;
                }

                //if (warning)
                //    ShowWarning?.Invoke(text);
                //else if (text != null)
                //    ShowMessage?.Invoke($"{text} has been executed successfully.");
            }
            catch
            {
                //ShowWarning?.Invoke($"An error occurred during the execution of the action.");
                throw;
            }
        }