All files / ee/app/assets/javascripts/integrations/edit/components jira_issue_creation_vulnerabilities.vue

4% Statements 1/25
0% Branches 0/12
0% Functions 0/14
4.16% Lines 1/24

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                                  1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
<script>
import {
  GlAlert,
  GlBadge,
  GlButton,
  GlButtonGroup,
  GlCollapsibleListbox,
  GlFormCheckbox,
  GlIcon,
  GlTooltipDirective,
} from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapGetters, mapState } from 'vuex';
import { s__ } from '~/locale';
import { billingPlans, billingPlanNames } from '~/integrations/constants';
import { defaultJiraIssueTypeId } from '../constants';
 
export const i18n = {
  checkbox: {
    label: s__('JiraService|Enable Jira issue creation from vulnerabilities'),
    description: s__(
      'JiraService|Issues created from vulnerabilities in this project will be Jira issues, even if GitLab issues are enabled.',
    ),
  },
  issueTypeSelect: {
    description: s__('JiraService|Create Jira issues of this type from vulnerabilities.'),
    defaultText: s__('JiraService|Select issue type'),
  },
  issueTypeLabel: s__('JiraService|Jira issue type'),
  fetchIssueTypesButtonLabel: s__('JiraService|Fetch issue types for this Jira project'),
  fetchIssueTypesErrorMessage: s__('JiraService|An error occurred while fetching issue list'),
  projectKeyWarnings: {
    missing: s__('JiraService|Project key is required to generate issue types'),
    changed: s__('JiraService|Project key changed, refresh list'),
  },
};
 
export default {
  i18n,
  components: {
    GlAlert,
    GlBadge,
    GlButton,
    GlButtonGroup,
    GlCollapsibleListbox,
    GlFormCheckbox,
    GlIcon,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    showFullFeature: {
      type: Boolean,
      required: false,
      default: true,
    },
    projectKey: {
      type: String,
      required: false,
      default: '',
    },
    initialIssueTypeId: {
      type: String,
      required: false,
      default: defaultJiraIssueTypeId,
    },
    initialIsEnabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isLoadingErrorAlertDismissed: false,
      projectKeyForCurrentIssues: '',
      isJiraVulnerabilitiesEnabled: this.initialIsEnabled,
      selectedJiraIssueTypeId: null,
    };
  },
  computed: {
    ...mapGetters(['isInheriting', 'propsSource']),
    ...mapState(['jiraIssueTypes', 'isLoadingJiraIssueTypes', 'loadingJiraIssueTypesErrorMessage']),
    checkboxDisabled() {
      return !this.showFullFeature || this.isInheriting;
    },
    hasProjectKeyChanged() {
      return this.projectKeyForCurrentIssues && this.projectKey !== this.projectKeyForCurrentIssues;
    },
    shouldShowLoadingErrorAlert() {
      return !this.isLoadingErrorAlertDismissed && this.loadingJiraIssueTypesErrorMessage;
    },
    projectKeyWarning() {
      const {
        $options: {
          i18n: { projectKeyWarnings },
        },
      } = this;
 
      Iif (!this.projectKey) {
        return projectKeyWarnings.missing;
      }
      Iif (this.hasProjectKeyChanged) {
        return projectKeyWarnings.changed;
      }
      return '';
    },
    ultimateBadgeText() {
      return billingPlanNames[billingPlans.ULTIMATE];
    },
    initialJiraIssueType() {
      return this.jiraIssueTypes?.find(({ id }) => {
        return id === this.initialIssueTypeId;
      });
    },
    jiraIssueTypesList() {
      return this.jiraIssueTypes.map((item) => {
        return {
          value: item.id,
          text: item.name,
        };
      });
    },
    jiraIssueTypesToggleText() {
      return (
        this.jiraIssueTypes.find(({ id }) => id === this.selectedJiraIssueTypeId)?.name ||
        this.$options.i18n.issueTypeSelect.defaultText
      );
    },
  },
  watch: {
    jiraIssueTypes() {
      Iif (!this.selectedJiraIssueTypeId) {
        this.selectedJiraIssueTypeId = this.initialJiraIssueType ? this.initialIssueTypeId : null;
      }
    },
  },
  mounted() {
    Iif (this.initialIsEnabled) {
      this.requestJiraIssueTypes();
    }
  },
  methods: {
    requestJiraIssueTypes() {
      this.$emit('request-jira-issue-types');
    },
    handleLoadJiraIssueTypesClick() {
      this.requestJiraIssueTypes();
      this.projectKeyForCurrentIssues = this.projectKey;
      this.isLoadingErrorAlertDismissed = false;
    },
  },
};
</script>
 
<template>
  <div>
    <gl-form-checkbox
      v-model="isJiraVulnerabilitiesEnabled"
      :disabled="checkboxDisabled"
      data-testid="jira-enable-vulnerabilities-checkbox"
    >
      <span>{{ $options.i18n.checkbox.label }}</span
      ><gl-badge
        :href="propsSource.aboutPricingUrl"
        target="_blank"
        rel="noopener noreferrer"
        variant="tier"
        icon="license"
        class="gl-vertical-align-middle gl-mt-n2 gl-ml-2"
      >
        {{ ultimateBadgeText }}
      </gl-badge>
      <template #help>
        {{ $options.i18n.checkbox.description }}
      </template>
    </gl-form-checkbox>
 
    <template v-if="showFullFeature">
      <input
        name="service[vulnerabilities_enabled]"
        type="hidden"
        :value="isJiraVulnerabilitiesEnabled"
      />
      <div
        v-if="isJiraVulnerabilitiesEnabled"
        class="gl-mt-3 gl-ml-6"
        data-testid="issue-type-section"
      >
        <label id="issue-type-label" class="gl-mb-0">{{ $options.i18n.issueTypeLabel }}</label>
        <p class="gl-mb-3">{{ $options.i18n.issueTypeSelect.description }}</p>
        <gl-alert
          v-if="shouldShowLoadingErrorAlert"
          class="gl-mb-5"
          variant="danger"
          :title="$options.i18n.fetchIssueTypesErrorMessage"
          @dismiss="isLoadingErrorAlertDismissed = true"
        >
          {{ loadingJiraIssueTypesErrorMessage }}
        </gl-alert>
        <div class="gl-display-flex gl-align-items-center gl-flex-wrap gl-gap-3 gl-mb-5">
          <input
            name="service[vulnerabilities_issuetype]"
            type="hidden"
            :value="selectedJiraIssueTypeId || initialIssueTypeId"
          />
          <gl-button-group>
            <gl-collapsible-listbox
              v-model="selectedJiraIssueTypeId"
              :items="jiraIssueTypesList"
              :disabled="!jiraIssueTypes.length"
              :loading="isLoadingJiraIssueTypes"
              :toggle-text="jiraIssueTypesToggleText"
              class="btn-group"
              data-testid="jira-select-issue-type-dropdown"
              toggle-aria-labelled-by="issue-type-label"
            >
              <template #list-item="{ item }">
                <span data-testid="jira-type" :data-qa-service-type="item.text">{{
                  item.text
                }}</span>
              </template>
            </gl-collapsible-listbox>
            <gl-button
              v-gl-tooltip.hover
              :title="$options.i18n.fetchIssueTypesButtonLabel"
              :aria-label="$options.i18n.fetchIssueTypesButtonLabel"
              :disabled="!projectKey"
              icon="retry"
              data-testid="jira-issue-types-fetch-retry-button"
              @click="handleLoadJiraIssueTypesClick"
            />
          </gl-button-group>
          <p v-if="projectKeyWarning" class="gl-my-0">
            <gl-icon name="warning" class="gl-text-orange-500" />
            {{ projectKeyWarning }}
          </p>
        </div>
      </div>
    </template>
  </div>
</template>