All files / ee/app/assets/javascripts/geo_replicable/store actions.js

100% Statements 64/64
100% Branches 10/10
100% Functions 20/20
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                      4x 4x 1x 4x 1x         1x     4x 30x   30x 30x     30x 30x   30x 5x 5x 5x 25x 5x     30x   30x   30x             25x 5x 5x     20x 20x 20x         20x     5x         4x 1x 4x       1x           1x 1x   4x 1x                 1x     4x 2x   2x   2x               1x   1x         4x 1x 4x 1x 1x 1x   4x 1x     1x     4x 2x   2x   2x                 1x   1x         4x 1x     4x 1x    
import { createAlert } from '~/alert';
import { s__, __, sprintf } from '~/locale';
import toast from '~/vue_shared/plugins/global_toast';
import { PREV, NEXT, DEFAULT_PAGE_SIZE } from '../constants';
import buildReplicableTypeQuery from '../graphql/replicable_type_query_builder';
import replicableTypeUpdateMutation from '../graphql/replicable_type_update_mutation.graphql';
import replicableTypeBulkUpdateMutation from '../graphql/replicable_type_bulk_update_mutation.graphql';
import { getGraphqlClient } from '../utils';
import * as types from './mutation_types';
 
// Fetch Replicable Items
export const requestReplicableItems = ({ commit }) => commit(types.REQUEST_REPLICABLE_ITEMS);
export const receiveReplicableItemsSuccess = ({ commit }, data) =>
  commit(types.RECEIVE_REPLICABLE_ITEMS_SUCCESS, data);
export const receiveReplicableItemsError = ({ state, commit }) => {
  createAlert({
    message: sprintf(__('There was an error fetching the %{replicableType}'), {
      replicableType: state.replicableType,
    }),
  });
  commit(types.RECEIVE_REPLICABLE_ITEMS_ERROR);
};
 
export const fetchReplicableItems = ({ state, dispatch }, direction) => {
  dispatch('requestReplicableItems');
 
  let before = '';
  let after = '';
 
  // If we are going backwards we want the last 20, otherwise get the first 20.
  let first = DEFAULT_PAGE_SIZE;
  let last = null;
 
  if (direction === PREV) {
    before = state.paginationData.startCursor;
    first = null;
    last = DEFAULT_PAGE_SIZE;
  } else if (direction === NEXT) {
    after = state.paginationData.endCursor;
  }
 
  const replicationState = state.statusFilter ? state.statusFilter.toUpperCase() : null;
 
  const client = getGraphqlClient(state.geoCurrentSiteId, state.geoTargetSiteId);
 
  client
    .query({
      query: buildReplicableTypeQuery(state.graphqlFieldName, state.verificationEnabled),
      variables: { first, last, before, after, replicationState },
    })
    .then((res) => {
      // Query.geoNode to be renamed to Query.geoSite => https://gitlab.com/gitlab-org/gitlab/-/issues/396739
      if (!res.data.geoNode || !(state.graphqlFieldName in res.data.geoNode)) {
        dispatch('receiveReplicableItemsSuccess', { data: [], pagination: null });
        return;
      }
 
      const registries = res.data.geoNode[state.graphqlFieldName];
      const data = registries.nodes;
      const pagination = {
        ...registries.pageInfo,
        page: state.paginationData.page,
      };
 
      dispatch('receiveReplicableItemsSuccess', { data, pagination });
    })
    .catch(() => {
      dispatch('receiveReplicableItemsError');
    });
};
 
// Initiate All Replicable Action
export const requestInitiateAllReplicableAction = ({ commit }) =>
  commit(types.REQUEST_INITIATE_ALL_REPLICABLE_ACTION);
export const receiveInitiateAllReplicableActionSuccess = (
  { getters, commit, dispatch },
  { action },
) => {
  toast(
    sprintf(s__('Geo|All %{replicableType} are being scheduled for %{action}'), {
      replicableType: getters.replicableTypeName,
      action: action.replace('_', ' '),
    }),
  );
  commit(types.RECEIVE_INITIATE_ALL_REPLICABLE_ACTION_SUCCESS);
  dispatch('fetchReplicableItems');
};
export const receiveInitiateAllReplicableActionError = ({ getters, commit }, { action }) => {
  createAlert({
    message: sprintf(
      s__('Geo|There was an error scheduling action %{action} for %{replicableType}'),
      {
        replicableType: getters.replicableTypeName,
        action: action.replace('_', ' '),
      },
    ),
  });
  commit(types.RECEIVE_INITIATE_ALL_REPLICABLE_ACTION_ERROR);
};
 
export const initiateAllReplicableAction = ({ state, dispatch }, { action }) => {
  dispatch('requestInitiateAllReplicableAction');
 
  const client = getGraphqlClient(state.geoCurrentSiteId, state.geoTargetSiteId);
 
  client
    .mutate({
      mutation: replicableTypeBulkUpdateMutation,
      variables: {
        action: action.toUpperCase(),
        registryClass: state.graphqlMutationRegistryClass,
      },
    })
    .then(() => dispatch('receiveInitiateAllReplicableActionSuccess', { action }))
    .catch(() => {
      dispatch('receiveInitiateAllReplicableActionError', { action });
    });
};
 
// Initiate Replicable Action
export const requestInitiateReplicableAction = ({ commit }) =>
  commit(types.REQUEST_INITIATE_REPLICABLE_ACTION);
export const receiveInitiateReplicableActionSuccess = ({ commit, dispatch }, { name, action }) => {
  toast(sprintf(__('%{name} is scheduled for %{action}'), { name, action }));
  commit(types.RECEIVE_INITIATE_REPLICABLE_ACTION_SUCCESS);
  dispatch('fetchReplicableItems');
};
export const receiveInitiateReplicableActionError = ({ commit }, { name }) => {
  createAlert({
    message: sprintf(__('There was an error syncing project %{name}'), { name }),
  });
  commit(types.RECEIVE_INITIATE_REPLICABLE_ACTION_ERROR);
};
 
export const initiateReplicableAction = ({ state, dispatch }, { registryId, name, action }) => {
  dispatch('requestInitiateReplicableAction');
 
  const client = getGraphqlClient(state.geoCurrentSiteId, state.geoTargetSiteId);
 
  client
    .mutate({
      mutation: replicableTypeUpdateMutation,
      variables: {
        action: action.toUpperCase(),
        registryId,
        registryClass: state.graphqlMutationRegistryClass,
      },
    })
    .then(() => dispatch('receiveInitiateReplicableActionSuccess', { name, action }))
    .catch(() => {
      dispatch('receiveInitiateReplicableActionError', { name });
    });
};
 
// Filtering/Pagination
export const setStatusFilter = ({ commit }, filter) => {
  commit(types.SET_STATUS_FILTER, filter);
};
 
export const setSearch = ({ commit }, search) => {
  commit(types.SET_SEARCH, search);
};