All files / app/assets/javascripts/vue_merge_request_widget/components/terraform mr_widget_terraform_container.vue

100% Statements 31/31
87.5% Branches 7/8
100% Functions 14/14
100% Lines 31/31

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  1x 1x 1x 1x 1x 1x                                 14x               14x 2x     12x             51x     14x     14x     14x 5x     9x               14x     10x     14x 14x   14x   14x         11x   11x 11x 11x       3x 3x 3x       14x           176x                                                                            
<script>
import { GlDeprecatedSkeletonLoading as GlSkeletonLoading, GlSprintf } from '@gitlab/ui';
import axios from '~/lib/utils/axios_utils';
import Poll from '~/lib/utils/poll';
import { n__ } from '~/locale';
import MrWidgetExpanableSection from '../mr_widget_expandable_section.vue';
import TerraformPlan from './terraform_plan.vue';
 
export default {
  name: 'MRWidgetTerraformContainer',
  components: {
    GlSkeletonLoading,
    GlSprintf,
    MrWidgetExpanableSection,
    TerraformPlan,
  },
  props: {
    endpoint: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      loading: true,
      plansObject: {},
      poll: null,
    };
  },
  computed: {
    inValidPlanCountText() {
      if (this.numberOfInvalidPlans === 0) {
        return null;
      }
 
      return n__(
        'Terraform|%{number} Terraform report failed to generate',
        'Terraform|%{number} Terraform reports failed to generate',
        this.numberOfInvalidPlans,
      );
    },
    numberOfInvalidPlans() {
      return Object.values(this.plansObject).filter((plan) => plan.tf_report_error).length;
    },
    numberOfPlans() {
      return Object.keys(this.plansObject).length;
    },
    numberOfValidPlans() {
      return this.numberOfPlans - this.numberOfInvalidPlans;
    },
    validPlanCountText() {
      if (this.numberOfValidPlans === 0) {
        return null;
      }
 
      return n__(
        'Terraform|%{number} Terraform report was generated in your pipelines',
        'Terraform|%{number} Terraform reports were generated in your pipelines',
        this.numberOfValidPlans,
      );
    },
  },
  created() {
    this.fetchPlans();
  },
  beforeDestroy() {
    this.poll.stop();
  },
  methods: {
    fetchPlans() {
      this.loading = true;
 
      this.poll = new Poll({
        resource: {
          fetchPlans: () => axios.get(this.endpoint),
        },
        data: this.endpoint,
        method: 'fetchPlans',
        successCallback: ({ data }) => {
          this.plansObject = data;
 
E          if (this.numberOfPlans > 0) {
            this.loading = false;
            this.poll.stop();
          }
        },
        errorCallback: () => {
          this.plansObject = { bad_plan: { tf_report_error: 'api_error' } };
          this.loading = false;
          this.poll.stop();
        },
      });
 
      this.poll.makeRequest();
    },
  },
};
</script>
 
<template>
  <section class="mr-widget-section">
    <div v-if="loading" class="mr-widget-body">
      <gl-skeleton-loading />
    </div>
 
    <mr-widget-expanable-section v-else>
      <template #header>
        <div
          data-testid="terraform-header-text"
          class="gl-flex-grow-1 gl-display-flex gl-flex-direction-column"
        >
          <p v-if="validPlanCountText" class="gl-m-0">
            <gl-sprintf :message="validPlanCountText">
              <template #number>
                <strong>{{ numberOfValidPlans }}</strong>
              </template>
            </gl-sprintf>
          </p>
 
          <p v-if="inValidPlanCountText" class="gl-m-0">
            <gl-sprintf :message="inValidPlanCountText">
              <template #number>
                <strong>{{ numberOfInvalidPlans }}</strong>
              </template>
            </gl-sprintf>
          </p>
        </div>
      </template>
 
      <template #content>
        <div class="mr-widget-body gl-pb-1">
          <terraform-plan v-for="(plan, key) in plansObject" :key="key" :plan="plan" />
        </div>
      </template>
    </mr-widget-expanable-section>
  </section>
</template>