<commit_before>import $ from 'jquery';
import _ from 'underscore';
import { __, sprintf } from './locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';
import { convertPermissionToBoolean } from './lib/utils/common_utils';

class ImporterStatus {
  constructor({ jobsUrl, importUrl, ciCdOnly }) {
    this.jobsUrl = jobsUrl;
    this.importUrl = importUrl;
    this.ciCdOnly = ciCdOnly;
    this.initStatusPage();
    this.setAutoUpdate();
  }

  initStatusPage() {
    $('.js-add-to-import')
      .off('click')
      .on('click', this.addToImport.bind(this));

    $('.js-import-all')
      .off('click')
      .on('click', function onClickImportAll() {
        const $btn = $(this);
        $btn.disable().addClass('is-loading');
        return $('.js-add-to-import').each(function triggerAddImport() {
          return $(this).trigger('click');
        });
      });
  }

  addToImport(event) {
    const $btn = $(event.currentTarget);
    const $tr = $btn.closest('tr');
    const $targetField = $tr.find('.import-target');
    const $namespaceInput = $targetField.find('.js-select-namespace option:selected');
    const id = $tr.attr('id').replace('repo_', '');
    let targetNamespace;
    let newName;
    if ($namespaceInput.length > 0) {
      targetNamespace = $namespaceInput[0].innerHTML;
      newName = $targetField.find('#path').prop('value');
      $targetField.empty().append(`${targetNamespace}/${newName}`);
    }
    $btn.disable().addClass('is-loading');

    return axios.post(this.importUrl, {
      repo_id: id,
      target_namespace: targetNamespace,
      new_name: newName,
      ci_cd_only: this.ciCdOnly,
    })
    .then(({ data }) => {
      const job = $(`tr#repo_${id}`);
      job.attr('id', `project_${data.id}`);

      job.find('.import-target').html(`<a href="${data.full_path}">${data.full_path}</a>`);
      $('table.import-jobs tbody').prepend(job);

      job.addClass('table-active');
      const connectingVerb = this.ciCdOnly ? __('connecting') : __('importing');
      job.find('.import-actions').html(sprintf(
        _.escape(__('%{loadingIcon} Started')), {
          loadingIcon: `<i class="fa fa-spinner fa-spin" aria-label="${_.escape(connectingVerb)}"></i>`,
        },
        false,
      ));
    })
    .catch((error) => {
      let details = error;

      if (error.response && error.response.data && error.response.data.errors) {
        details = error.response.data.errors;
      }

      flash(__(`An error occurred while importing project: ${details}`));
    });
  }

  autoUpdate() {
    return axios.get(this.jobsUrl)
      .then(({ data = [] }) => {
        data.forEach((job) => {
          const jobItem = $(`#project_${job.id}`);
          const statusField = jobItem.find('.job-status');

          const spinner = '<i class="fa fa-spinner fa-spin"></i>';

          switch (job.import_status) {
            case 'finished':
              jobItem.removeClass('table-active').addClass('table-success');<commit_msg>Stop import spinner when import failed<commit_after>import $ from 'jquery';
import _ from 'underscore';
import { __, sprintf } from './locale';
import axios from './lib/utils/axios_utils';
import flash from './flash';
import { convertPermissionToBoolean } from './lib/utils/common_utils';

class ImporterStatus {
  constructor({ jobsUrl, importUrl, ciCdOnly }) {
    this.jobsUrl = jobsUrl;
    this.importUrl = importUrl;
    this.ciCdOnly = ciCdOnly;
    this.initStatusPage();
    this.setAutoUpdate();
  }

  initStatusPage() {
    $('.js-add-to-import')
      .off('click')
      .on('click', this.addToImport.bind(this));

    $('.js-import-all')
      .off('click')
      .on('click', function onClickImportAll() {
        const $btn = $(this);
        $btn.disable().addClass('is-loading');
        return $('.js-add-to-import').each(function triggerAddImport() {
          return $(this).trigger('click');
        });
      });
  }

  addToImport(event) {
    const $btn = $(event.currentTarget);
    const $tr = $btn.closest('tr');
    const $targetField = $tr.find('.import-target');
    const $namespaceInput = $targetField.find('.js-select-namespace option:selected');
    const id = $tr.attr('id').replace('repo_', '');
    let targetNamespace;
    let newName;
    if ($namespaceInput.length > 0) {
      targetNamespace = $namespaceInput[0].innerHTML;
      newName = $targetField.find('#path').prop('value');
      $targetField.empty().append(`${targetNamespace}/${newName}`);
    }
    $btn.disable().addClass('is-loading');

    this.id = id;

    return axios.post(this.importUrl, {
      repo_id: id,
      target_namespace: targetNamespace,
      new_name: newName,
      ci_cd_only: this.ciCdOnly,
    })
    .then(({ data }) => {
      const job = $(`tr#repo_${id}`);
      job.attr('id', `project_${data.id}`);

      job.find('.import-target').html(`<a href="${data.full_path}">${data.full_path}</a>`);
      $('table.import-jobs tbody').prepend(job);

      job.addClass('table-active');
      const connectingVerb = this.ciCdOnly ? __('connecting') : __('importing');
      job.find('.import-actions').html(sprintf(
        _.escape(__('%{loadingIcon} Started')), {
          loadingIcon: `<i class="fa fa-spinner fa-spin" aria-label="${_.escape(connectingVerb)}"></i>`,
        },
        false,
      ));
    })
    .catch((error) => {
      let details = error;

      const jobItem = $(`#repo_${this.id}`);
      const statusField = jobItem.find('.job-status');
      statusField.html(__('Failed'));

      if (error.response && error.response.data && error.response.data.errors) {
        details = error.response.data.errors;
      }

      flash(__(`An error occurred while importing project: ${details}`));
    });
  }

  autoUpdate() {
    return axios.get(this.jobsUrl)
      .then(({ data = [] }) => {
        data.forEach((job) => {
          const jobItem = $(`#project_${job.id}`);
          const statusField = jobItem.find('.job-status');

          const spinner = '<i class="fa fa-spinner fa-spin"></i>';

          switch (job.import_status) {
            case 'finished':
              jobItem.removeClass('table-active').addClass('table-success');
{'fraction_non_alphanumeric': 0.18682557577709777, 'fraction_numerical': 0.0006442261233693026, 'mean_word_length': 2.8909774436090228, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, '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': '4727095', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1925, 'n_words': 337}