<commit_before>import * as React from 'react';
import { chain, xor } from 'lodash';
import { $q } from 'ngimport';

import {
  AccountTag,
  FirewallLabels,
  ISecurityGroup,
  IAggregatedAccounts,
  IVpc,
  IAccountDetails,
  ReactInjector,
  AccountService,
} from '@spinnaker/core';

import { ServerGroupSecurityGroupsRemoved, SecurityGroupSelector, VpcReader } from '@spinnaker/amazon';

export interface ITitusSecurityGroupPickerProps {
  account: string;
  region: string;
  command: any;
  groupsToEdit: string[];
  hideLabel: boolean;
  amazonAccount: string;
  onChange: (groups: string[]) => void;
}

export interface ITitusSecurityGroupPickerState {
  availableGroups: ISecurityGroup[];
  removedGroups: string[];
  loaded: boolean;
}

export class TitusSecurityGroupPicker extends React.Component<
  ITitusSecurityGroupPickerProps,
  ITitusSecurityGroupPickerState
> {
  public state: ITitusSecurityGroupPickerState = {
    availableGroups: [],
    removedGroups: [],
    loaded: false,
  };

  private credentials: IAggregatedAccounts = {};
  private vpcs: IVpc[] = [];
  private securityGroups: any = {};

  constructor(props: ITitusSecurityGroupPickerProps) {
    super(props);
  }

  private getCredentials(): IAccountDetails {
    return this.credentials[this.props.command.credentials];
  }

  private getAwsAccount(): string {
    return this.getCredentials().awsAccount;
  }

  private getRegion(): string {
    return this.props.command.region || (this.props.command.cluster ? this.props.command.cluster.region : null);
  }

  private getVpcId(): string {
    const credentials = this.getCredentials();
    const match = this.vpcs.find(
      vpc =>
        vpc.name === credentials.awsVpc &&
        vpc.account === credentials.awsAccount &&
        vpc.region === this.getRegion() &&
        vpc.cloudProvider === 'aws',
    );
    return match ? match.id : null;
  }

  private getRegionalSecurityGroups(): ISecurityGroup[] {
    const newSecurityGroups: any = this.securityGroups[this.getAwsAccount()] || { aws: {} };
    return chain<ISecurityGroup>(newSecurityGroups.aws[this.getRegion()])
      .filter({ vpcId: this.getVpcId() })
      .sortBy('name')
      .value();
  }

  public refreshSecurityGroups(skipCommandReconfiguration?: boolean) {
    return ReactInjector.cacheInitializer.refreshCache('securityGroups').then(() => {
      return ReactInjector.securityGroupReader.getAllSecurityGroups().then(securityGroups => {
        this.securityGroups = securityGroups;
        if (!skipCommandReconfiguration) {
          this.configureSecurityGroupOptions();
        }
      });
    });
  }

  private configureSecurityGroupOptions(): void {
    let availableGroups = this.getRegionalSecurityGroups();
    const oldAvailableGroups = this.state.availableGroups.length ? this.state.availableGroups : availableGroups;
    let removedGroups: string[];

    let groupsToEdit = this.props.groupsToEdit;
    if (availableGroups && this.props.groupsToEdit) {
      // not initializing - we are actually changing groups
      const oldGroupNames: string[] = groupsToEdit.map((groupId: string) => {
        const match = oldAvailableGroups.find(o => o.id === groupId);
        return match ? match.name : groupId;
      });

      const matchedGroups: ISecurityGroup[] = groupsToEdit
        .map((groupId: string) => {<commit_msg>fix(runjob): Fixing security group matching by name (#7164)<commit_after>import * as React from 'react';
import { chain, xor } from 'lodash';
import { $q } from 'ngimport';

import {
  AccountTag,
  FirewallLabels,
  ISecurityGroup,
  IAggregatedAccounts,
  IVpc,
  IAccountDetails,
  ReactInjector,
  AccountService,
} from '@spinnaker/core';

import { ServerGroupSecurityGroupsRemoved, SecurityGroupSelector, VpcReader } from '@spinnaker/amazon';

export interface ITitusSecurityGroupPickerProps {
  account: string;
  region: string;
  command: any;
  groupsToEdit: string[];
  hideLabel: boolean;
  amazonAccount: string;
  onChange: (groups: string[]) => void;
}

export interface ITitusSecurityGroupPickerState {
  availableGroups: ISecurityGroup[];
  removedGroups: string[];
  loaded: boolean;
}

export class TitusSecurityGroupPicker extends React.Component<
  ITitusSecurityGroupPickerProps,
  ITitusSecurityGroupPickerState
> {
  public state: ITitusSecurityGroupPickerState = {
    availableGroups: [],
    removedGroups: [],
    loaded: false,
  };

  private credentials: IAggregatedAccounts = {};
  private vpcs: IVpc[] = [];
  private securityGroups: any = {};

  constructor(props: ITitusSecurityGroupPickerProps) {
    super(props);
  }

  private getCredentials(): IAccountDetails {
    return this.credentials[this.props.command.credentials];
  }

  private getAwsAccount(): string {
    return this.getCredentials().awsAccount;
  }

  private getRegion(): string {
    return this.props.command.region || (this.props.command.cluster ? this.props.command.cluster.region : null);
  }

  private getVpcId(): string {
    const credentials = this.getCredentials();
    const match = this.vpcs.find(
      vpc =>
        vpc.name === credentials.awsVpc &&
        vpc.account === credentials.awsAccount &&
        vpc.region === this.getRegion() &&
        vpc.cloudProvider === 'aws',
    );
    return match ? match.id : null;
  }

  private getRegionalSecurityGroups(): ISecurityGroup[] {
    const newSecurityGroups: any = this.securityGroups[this.getAwsAccount()] || { aws: {} };
    return chain<ISecurityGroup>(newSecurityGroups.aws[this.getRegion()])
      .filter({ vpcId: this.getVpcId() })
      .sortBy('name')
      .value();
  }

  public refreshSecurityGroups(skipCommandReconfiguration?: boolean) {
    return ReactInjector.cacheInitializer.refreshCache('securityGroups').then(() => {
      return ReactInjector.securityGroupReader.getAllSecurityGroups().then(securityGroups => {
        this.securityGroups = securityGroups;
        if (!skipCommandReconfiguration) {
          this.configureSecurityGroupOptions();
        }
      });
    });
  }

  private configureSecurityGroupOptions(): void {
    let availableGroups = this.getRegionalSecurityGroups();
    const oldAvailableGroups = this.state.availableGroups.length ? this.state.availableGroups : availableGroups;
    let removedGroups: string[];

    let groupsToEdit = this.props.groupsToEdit;
    if (availableGroups && this.props.groupsToEdit) {
      // not initializing - we are actually changing groups
      const oldGroupNames: string[] = groupsToEdit.map((groupId: string) => {
        const match = oldAvailableGroups.find(o => o.id === groupId);
        return match ? match.name : groupId;
      });

      const matchedGroups: ISecurityGroup[] = oldGroupNames
        .map((groupId: string) => {
{'fraction_non_alphanumeric': 0.1181697023545091, 'fraction_numerical': 0.0005923293351103214, 'mean_word_length': 3.922740524781341, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '422392', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1922, 'n_words': 405}