All files / app/assets/javascripts/clusters_list/components agents.vue

96.55% Statements 28/29
88.88% Branches 24/27
95.45% Functions 21/22
96.55% Lines 28/29

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 1913x                                                         24x           22x 22x     23x     1x                                                                 24x               23x 23x       26x           23x   64x     51x 51x 51x 51x       23x     22x     19x     47x     19x     4x     4x         22x   22x 22x 19x         23x                                                                                                        
<!-- eslint-disable vue/multi-word-component-names -->
<script>
import { GlAlert, GlLoadingIcon, GlBanner } from '@gitlab/ui';
import feedbackBannerIllustration from '@gitlab/svgs/dist/illustrations/chat-sm.svg?url';
import { s__ } from '~/locale';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import { AGENT_FEEDBACK_ISSUE, AGENT_FEEDBACK_KEY } from '../constants';
import getAgentsQuery from '../graphql/queries/get_agents.query.graphql';
import { getAgentLastContact, getAgentStatus } from '../clusters_util';
import AgentEmptyState from './agent_empty_state.vue';
import AgentTable from './agent_table.vue';
import GitopsDeprecationAlert from './gitops_deprecation_alert.vue';
 
export default {
  i18n: {
    feedbackBannerTitle: s__('ClusterAgents|Tell us what you think'),
    feedbackBannerText: s__(
      'ClusterAgents|We would love to learn more about your experience with the GitLab Agent.',
    ),
    feedbackBannerButton: s__('ClusterAgents|Give feedback'),
    error: s__('ClusterAgents|An error occurred while loading your agents'),
  },
  AGENT_FEEDBACK_ISSUE,
  AGENT_FEEDBACK_KEY,
  apollo: {
    agents: {
      query: getAgentsQuery,
      variables() {
        return {
          defaultBranchName: this.defaultBranchName,
          projectPath: this.projectPath,
        };
      },
      update(data) {
        this.updateTreeList(data);
        return data;
      },
      result() {
        this.emitAgentsLoaded();
      },
      error() {
        this.queryErrored = true;
      },
    },
  },
  components: {
    AgentEmptyState,
    AgentTable,
    GlAlert,
    GlLoadingIcon,
    GlBanner,
    LocalStorageSync,
    GitopsDeprecationAlert,
  },
  mixins: [glFeatureFlagMixin()],
  inject: ['projectPath'],
  props: {
    defaultBranchName: {
      default: '.noBranch',
      required: false,
      type: String,
    },
    isChildComponent: {
      default: false,
      required: false,
      type: Boolean,
    },
    limit: {
      default: null,
      required: false,
      type: Number,
    },
  },
  data() {
    return {
      folderList: {},
      feedbackBannerDismissed: false,
      queryErrored: false,
    };
  },
  computed: {
    agentList() {
      const localAgents = this.agents?.project?.clusterAgents?.nodes || [];
      const sharedAgents = [
        ...(this.agents?.project?.ciAccessAuthorizedAgents?.nodes || []),
        ...(this.agents?.project?.userAccessAuthorizedAgents?.nodes || []),
      ].map((node) => {
        return {
          ...node.agent,
          isShared: true,
        };
      });
 
      const filteredList = [...localAgents, ...sharedAgents]
        .filter((node, index, list) => {
          return node && index === list.findIndex((agent) => agent.id === node.id);
        })
        .map((agent) => {
          const configFolder = this.folderList[agent.name];
          const lastContact = getAgentLastContact(agent?.tokens?.nodes);
          const status = getAgentStatus(lastContact);
          return { ...agent, configFolder, lastContact, status };
        })
        .sort((a, b) => b.lastUsedAt - a.lastUsedAt);
 
      return filteredList;
    },
    agentConfigs() {
      return this.agentList.map((agent) => agent.configFolder?.path).filter(Boolean) || [];
    },
    projectGid() {
      return this.agents?.project?.id || '';
    },
    isLoading() {
      return this.$apollo.queries.agents.loading;
    },
    feedbackBannerEnabled() {
      return this.glFeatures.showGitlabAgentFeedback;
    },
    feedbackBannerClasses() {
      return this.isChildComponent ? 'gl-my-2' : 'gl-mb-4';
    },
    feedbackBannerIllustration() {
      return feedbackBannerIllustration;
    },
  },
  methods: {
    updateTreeList(data) {
      const configFolders = data?.project?.repository?.tree?.trees?.nodes;
 
      if (configFolders) {
        configFolders.forEach((folder) => {
          this.folderList[folder.name] = folder;
        });
      }
    },
    emitAgentsLoaded() {
      this.$emit('onAgentsLoad', this.agentList?.length);
    },
    handleBannerClose() {
      this.feedbackBannerDismissed = true;
    },
  },
};
</script>
 
<template>
  <gl-loading-icon v-if="isLoading" size="lg" />
 
  <section v-else-if="!queryErrored">
    <gitops-deprecation-alert
      v-if="agentConfigs.length"
      :agent-configs="agentConfigs"
      :project-gid="projectGid"
    />
 
    <div v-if="agentList.length">
      <local-storage-sync
        v-if="feedbackBannerEnabled"
        v-model="feedbackBannerDismissed"
        :storage-key="$options.AGENT_FEEDBACK_KEY"
      >
        <gl-banner
          v-if="!feedbackBannerDismissed"
          :class="feedbackBannerClasses"
          :title="$options.i18n.feedbackBannerTitle"
          :button-text="$options.i18n.feedbackBannerButton"
          :button-link="$options.AGENT_FEEDBACK_ISSUE"
          :svg-path="feedbackBannerIllustration"
          @close="handleBannerClose"
        >
          <p>{{ $options.i18n.feedbackBannerText }}</p>
        </gl-banner>
      </local-storage-sync>
 
      <agent-table
        :agents="agentList"
        :default-branch-name="defaultBranchName"
        :max-agents="limit"
      />
    </div>
 
    <agent-empty-state v-else />
  </section>
 
  <gl-alert v-else variant="danger" :dismissible="false">
    {{ $options.i18n.error }}
  </gl-alert>
</template>