<commit_before>import * as R from 'ramda';
import { logger } from '@lagoon/commons/src/local-logging';
import { keycloakAdminClient } from '../../clients/keycloakClient';
import { getSqlClient } from '../../clients/sqlClient';
import { query, prepare } from '../../util/db';
import { Group, GroupNotFoundError } from '../../models/group';
import { User } from '../../models/user';

const keycloakAuth = {
  username: 'admin',
  password: R.pathOr(
    '<password not set>',
    ['env', 'KEYCLOAK_ADMIN_PASSWORD'],
    process,
  ) as string,
  grantType: 'password',
  clientId: 'admin-cli',
};

const refreshToken = async keycloakAdminClient => {
  const tokenRaw = new Buffer(keycloakAdminClient.accessToken.split('.')[1], 'base64');
  const token = JSON.parse(tokenRaw.toString());
  const date = new Date();
  const now = Math.floor(date.getTime() / 1000);

  if (token.exp <= now) {
    logger.debug('Refreshing keycloak token');
    keycloakAdminClient.setConfig({ realmName: 'master' });
    await keycloakAdminClient.auth(keycloakAuth);
    keycloakAdminClient.setConfig({ realmName: 'lagoon' });
  }
}

(async () => {
  keycloakAdminClient.setConfig({ realmName: 'master' });
  await keycloakAdminClient.auth(keycloakAuth);
  keycloakAdminClient.setConfig({ realmName: 'lagoon' });

  const sqlClient = getSqlClient();
  const GroupModel = Group();
  const UserModel = User();

  const customerRecords = await query(sqlClient, 'SELECT * FROM `customer`');

  for (const customer of customerRecords) {
    await refreshToken(keycloakAdminClient);
    logger.debug(`Processing ${customer.name}`);

    // Add or update group<commit_msg>Remove keycloak groups before migrating<commit_after>import * as R from 'ramda';
import { logger } from '@lagoon/commons/src/local-logging';
import { keycloakAdminClient } from '../../clients/keycloakClient';
import { getSqlClient } from '../../clients/sqlClient';
import { query, prepare } from '../../util/db';
import { Group, GroupNotFoundError } from '../../models/group';
import { User } from '../../models/user';

const keycloakAuth = {
  username: 'admin',
  password: R.pathOr(
    '<password not set>',
    ['env', 'KEYCLOAK_ADMIN_PASSWORD'],
    process,
  ) as string,
  grantType: 'password',
  clientId: 'admin-cli',
};

const refreshToken = async keycloakAdminClient => {
  const tokenRaw = new Buffer(keycloakAdminClient.accessToken.split('.')[1], 'base64');
  const token = JSON.parse(tokenRaw.toString());
  const date = new Date();
  const now = Math.floor(date.getTime() / 1000);

  if (token.exp <= now) {
    logger.debug('Refreshing keycloak token');
    keycloakAdminClient.setConfig({ realmName: 'master' });
    await keycloakAdminClient.auth(keycloakAuth);
    keycloakAdminClient.setConfig({ realmName: 'lagoon' });
  }
}

(async () => {
  keycloakAdminClient.setConfig({ realmName: 'master' });
  await keycloakAdminClient.auth(keycloakAuth);
  keycloakAdminClient.setConfig({ realmName: 'lagoon' });

  const sqlClient = getSqlClient();
  const GroupModel = Group();
  const UserModel = User();

  try {
    logger.debug('Removing current keycloak groups');
    const currentGroups = await GroupModel.loadAllGroups();
    const currentGroupIds = R.pluck('id', currentGroups);

    for (const currentGroupId of currentGroupIds) {
      await GroupModel.deleteGroup(currentGroupId);
    }
  } catch (err) {
    logger.error('Could not delete current keycloak groups. Due to group/sub-group hiearchy, this could be normal. Please try again.')
    throw new Error(err);
  }

  const customerRecords = await query(sqlClient, 'SELECT * FROM `customer`');

  for (const customer of customerRecords) {
    await refreshToken(keycloakAdminClient);
    logger.debug(`Processing ${customer.name}`);

    // Add or update group
{'fraction_non_alphanumeric': 0.14972273567467653, 'fraction_numerical': 0.0036968576709796672, 'mean_word_length': 4.210453920220083, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4624403', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1163, 'n_words': 287}