All files / app/assets/javascripts/pipelines/components/pipelines_list pipelines.vue

83.82% Statements 57/68
90% Branches 36/40
77.78% Functions 14/18
83.82% Lines 57/68

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420  1x   1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                               61x                                                                   147x   147x 74x     73x 6x     67x 39x     28x 18x     10x             147x 147x                 136x           18x 1x     17x       68x 68x   68x                                                 61x       61x 61x       5x       5x         5x   5x         82x 82x 82x 82x                                             6x   6x 6x   6x     12x         9x     12x       12x 3x             6x       6x               728x                                                                                                                                                                                                          
<script>
import { GlDropdown, GlDropdownItem, GlEmptyState, GlIcon, GlLoadingIcon } from '@gitlab/ui';
import { isEqual } from 'lodash';
import createFlash from '~/flash';
import { getParameterByName } from '~/lib/utils/url_utility';
import { __, s__ } from '~/locale';
import NavigationTabs from '~/vue_shared/components/navigation_tabs.vue';
import TablePagination from '~/vue_shared/components/pagination/table_pagination.vue';
import {
  ANY_TRIGGER_AUTHOR,
  RAW_TEXT_WARNING,
  FILTER_TAG_IDENTIFIER,
  PipelineKeyOptions,
} from '../../constants';
import PipelinesMixin from '../../mixins/pipelines_mixin';
import PipelinesService from '../../services/pipelines_service';
import { validateParams } from '../../utils';
import EmptyState from './empty_state.vue';
import NavigationControls from './nav_controls.vue';
import PipelinesFilteredSearch from './pipelines_filtered_search.vue';
import PipelinesTableComponent from './pipelines_table.vue';
 
export default {
  PipelineKeyOptions,
  components: {
    EmptyState,
    GlDropdown,
    GlDropdownItem,
    GlEmptyState,
    GlIcon,
    GlLoadingIcon,
    NavigationTabs,
    NavigationControls,
    PipelinesFilteredSearch,
    PipelinesTableComponent,
    TablePagination,
  },
  mixins: [PipelinesMixin],
  props: {
    store: {
      type: Object,
      required: true,
    },
    // Can be rendered in 3 different places, with some visual differences
    // Accepts root | child
    // `root` -> main view
    // `child` -> rendered inside MR or Commit View
    viewType: {
      type: String,
      required: false,
      default: 'root',
    },
    endpoint: {
      type: String,
      required: true,
    },
    pipelineScheduleUrl: {
      type: String,
      required: false,
      default: '',
    },
    emptyStateSvgPath: {
      type: String,
      required: true,
    },
    errorStateSvgPath: {
      type: String,
      required: true,
    },
    noPipelinesSvgPath: {
      type: String,
      required: true,
    },
    hasGitlabCi: {
      type: Boolean,
      required: true,
    },
    canCreatePipeline: {
      type: Boolean,
      required: true,
    },
    ciLintPath: {
      type: String,
      required: false,
      default: null,
    },
    resetCachePath: {
      type: String,
      required: false,
      default: null,
    },
    newPipelinePath: {
      type: String,
      required: false,
      default: null,
    },
    projectId: {
      type: String,
      required: true,
    },
    params: {
      type: Object,
      required: true,
    },
    codeQualityPagePath: {
      type: String,
      required: false,
      default: null,
    },
    ciRunnerSettingsPath: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      // Start with loading state to avoid a glitch when the empty state will be rendered
      isLoading: true,
      state: this.store.state,
      scope: getParameterByName('scope') || 'all',
      page: getParameterByName('page') || '1',
      requestData: {},
      isResetCacheButtonLoading: false,
      selectedPipelineKeyOption: this.$options.PipelineKeyOptions[0],
    };
  },
  stateMap: {
    // with tabs
    loading: 'loading',
    tableList: 'tableList',
    error: 'error',
    emptyTab: 'emptyTab',
 
    // without tabs
    emptyState: 'emptyState',
  },
  scopes: {
    all: 'all',
    finished: 'finished',
    branches: 'branches',
    tags: 'tags',
  },
  computed: {
    /**
     * `hasGitlabCi` handles both internal and external CI.
     * The order on which  the checks are made in this method is
     * important to guarantee we handle all the corner cases.
     */
    stateToRender() {
      const { stateMap } = this.$options;
 
      if (this.isLoading) {
        return stateMap.loading;
      }
 
      if (this.hasError) {
        return stateMap.error;
      }
 
      if (this.state.pipelines.length) {
        return stateMap.tableList;
      }
 
      if ((this.scope !== 'all' && this.scope !== null) || this.hasGitlabCi) {
        return stateMap.emptyTab;
      }
 
      return stateMap.emptyState;
    },
    /**
     * Tabs are rendered in all states except empty state.
     * They are not rendered before the first request to avoid a flicker on first load.
     */
    shouldRenderTabs() {
      const { stateMap } = this.$options;
      return (
        this.hasMadeRequest &&
        [stateMap.loading, stateMap.tableList, stateMap.error, stateMap.emptyTab].includes(
          this.stateToRender,
        )
      );
    },
 
    shouldRenderButtons() {
      return (
        (this.newPipelinePath || this.resetCachePath || this.ciLintPath) && this.shouldRenderTabs
      );
    },
 
    emptyTabMessage() {
      if (this.scope === this.$options.scopes.finished) {
        return s__('Pipelines|There are currently no finished pipelines.');
      }
 
      return s__('Pipelines|There are currently no pipelines.');
    },
 
    tabs() {
      const { count } = this.state;
      const { scopes } = this.$options;
 
      return [
        {
          name: __('All'),
          scope: scopes.all,
          count: count.all,
          isActive: this.scope === 'all',
        },
        {
          name: __('Finished'),
          scope: scopes.finished,
          isActive: this.scope === 'finished',
        },
        {
          name: __('Branches'),
          scope: scopes.branches,
          isActive: this.scope === 'branches',
        },
        {
          name: __('Tags'),
          scope: scopes.tags,
          isActive: this.scope === 'tags',
        },
      ];
    },
    validatedParams() {
      return validateParams(this.params);
    },
  },
  created() {
    this.service = new PipelinesService(this.endpoint);
    this.requestData = { page: this.page, scope: this.scope, ...this.validatedParams };
  },
  methods: {
    onChangeTab(scope) {
I      if (this.scope === scope) {
        return;
      }
 
      let params = {
        scope,
        page: '1',
      };
 
      params = this.onChangeWithFilter(params);
 
      this.updateContent(params);
    },
    successCallback(resp) {
      // Because we are polling & the user is interacting verify if the response received
      // matches the last request made
E      if (isEqual(resp.config.params, this.requestData)) {
        this.store.storeCount(resp.data.count);
        this.store.storePagination(resp.headers);
        this.setCommonData(resp.data.pipelines);
      }
    },
    handleResetRunnersCache(endpoint) {
      this.isResetCacheButtonLoading = true;
 
      this.service
        .postAction(endpoint)
        .then(() => {
          this.isResetCacheButtonLoading = false;
          createFlash({
            message: s__('Pipelines|Project cache successfully reset.'),
            type: 'notice',
          });
        })
        .catch(() => {
          this.isResetCacheButtonLoading = false;
          createFlash({
            message: s__('Pipelines|Something went wrong while cleaning runners cache.'),
          });
        });
    },
    resetRequestData() {
      this.requestData = { page: this.page, scope: this.scope };
    },
    filterPipelines(filters) {
      this.resetRequestData();
 
      filters.forEach((filter) => {
        // do not add Any for username query param, so we
        // can fetch all trigger authors
        if (
          filter.type &&
          filter.value.data !== ANY_TRIGGER_AUTHOR &&
          filter.type !== FILTER_TAG_IDENTIFIER
        ) {
          this.requestData[filter.type] = filter.value.data;
        }
 
I        if (filter.type === FILTER_TAG_IDENTIFIER) {
          this.requestData.ref = filter.value.data;
        }
 
        if (!filter.type) {
          createFlash({
            message: RAW_TEXT_WARNING,
            type: 'warning',
          });
        }
      });
 
I      if (filters.length === 0) {
        this.resetRequestData();
      }
 
      this.updateContent(this.requestData);
    },
    changeVisibilityPipelineID(val) {
      this.selectedPipelineKeyOption = val;
    },
  },
};
</script>
<template>
  <div class="pipelines-container">
    <div
      v-if="shouldRenderTabs || shouldRenderButtons"
      class="top-area scrolling-tabs-container inner-page-scroll-tabs gl-border-none"
    >
      <div class="fade-left"><gl-icon name="chevron-lg-left" :size="12" /></div>
      <div class="fade-right"><gl-icon name="chevron-lg-right" :size="12" /></div>
 
      <navigation-tabs
        v-if="shouldRenderTabs"
        :tabs="tabs"
        scope="pipelines"
        @onChangeTab="onChangeTab"
      />
 
      <navigation-controls
        v-if="shouldRenderButtons"
        :new-pipeline-path="newPipelinePath"
        :reset-cache-path="resetCachePath"
        :ci-lint-path="ciLintPath"
        :is-reset-cache-button-loading="isResetCacheButtonLoading"
        @resetRunnersCache="handleResetRunnersCache"
      />
    </div>
 
    <div v-if="stateToRender !== $options.stateMap.emptyState" class="gl-display-flex">
      <div class="row-content-block gl-display-flex gl-flex-grow-1">
        <pipelines-filtered-search
          class="gl-display-flex gl-flex-grow-1 gl-mr-4"
          :project-id="projectId"
          :params="validatedParams"
          @filterPipelines="filterPipelines"
        />
        <gl-dropdown
          class="gl-display-flex"
          :text="selectedPipelineKeyOption.text"
          data-testid="pipeline-key-dropdown"
        >
          <gl-dropdown-item
            v-for="(val, index) in $options.PipelineKeyOptions"
            :key="index"
            :is-checked="selectedPipelineKeyOption.key === val.key"
            is-check-item
            @click="changeVisibilityPipelineID(val)"
          >
            {{ val.text }}
          </gl-dropdown-item>
        </gl-dropdown>
      </div>
    </div>
 
    <div class="content-list pipelines">
      <gl-loading-icon
        v-if="stateToRender === $options.stateMap.loading"
        :label="s__('Pipelines|Loading Pipelines')"
        size="lg"
        class="prepend-top-20"
      />
 
      <empty-state
        v-else-if="stateToRender === $options.stateMap.emptyState"
        :empty-state-svg-path="emptyStateSvgPath"
        :can-set-ci="canCreatePipeline"
        :code-quality-page-path="codeQualityPagePath"
        :ci-runner-settings-path="ciRunnerSettingsPath"
      />
 
      <gl-empty-state
        v-else-if="stateToRender === $options.stateMap.error"
        :svg-path="errorStateSvgPath"
        :title="
          s__(`Pipelines|There was an error fetching the pipelines.
        Try again in a few moments or contact your support team.`)
        "
      />
 
      <gl-empty-state
        v-else-if="stateToRender === $options.stateMap.emptyTab"
        :svg-path="noPipelinesSvgPath"
        :title="emptyTabMessage"
      />
 
      <div v-else-if="stateToRender === $options.stateMap.tableList">
        <pipelines-table-component
          :pipelines="state.pipelines"
          :pipeline-schedule-url="pipelineScheduleUrl"
          :update-graph-dropdown="updateGraphDropdown"
          :view-type="viewType"
          :pipeline-key-option="selectedPipelineKeyOption"
        />
      </div>
 
      <table-pagination
        v-if="shouldRenderPagination"
        :change="onChangePage"
        :page-info="state.pageInfo"
      />
    </div>
  </div>
</template>