summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/deploy_keys/components/key.vue
blob: 6c2af7fa768ab34d4ca606ffc0455db73bb5f071 (plain)
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
<script>
import _ from 'underscore';
import { s__, sprintf } from '~/locale';
import icon from '~/vue_shared/components/icon.vue';
import tooltip from '~/vue_shared/directives/tooltip';
import timeagoMixin from '~/vue_shared/mixins/timeago';

import actionBtn from './action_btn.vue';

export default {
  components: {
    actionBtn,
    icon,
  },
  directives: {
    tooltip,
  },
  mixins: [timeagoMixin],
  props: {
    deployKey: {
      type: Object,
      required: true,
    },
    store: {
      type: Object,
      required: true,
    },
    endpoint: {
      type: String,
      required: true,
    },
    projectId: {
      type: String,
      required: false,
      default: null,
    },
  },
  data() {
    return {
      projectsExpanded: false,
    };
  },
  computed: {
    editDeployKeyPath() {
      return `${this.endpoint}/${this.deployKey.id}/edit`;
    },
    projects() {
      const projects = [...this.deployKey.deploy_keys_projects];

      if (this.projectId !== null) {
        const indexOfCurrentProject = _.findIndex(
          projects,
          project =>
            project &&
            project.project &&
            project.project.id &&
            project.project.id.toString() === this.projectId,
        );

        if (indexOfCurrentProject > -1) {
          const currentProject = projects.splice(indexOfCurrentProject, 1);
          currentProject[0].project.full_name = s__('DeployKeys|Current project');
          return currentProject.concat(projects);
        }
      }
      return projects;
    },
    firstProject() {
      return _.head(this.projects);
    },
    restProjects() {
      return _.tail(this.projects);
    },
    restProjectsTooltip() {
      return sprintf(s__('DeployKeys|Expand %{count} other projects'), {
        count: this.restProjects.length,
      });
    },
    restProjectsLabel() {
      return sprintf(s__('DeployKeys|+%{count} others'), { count: this.restProjects.length });
    },
    isEnabled() {
      return this.store.isEnabled(this.deployKey.id);
    },
    isRemovable() {
      return (
        this.store.isEnabled(this.deployKey.id) &&
        this.deployKey.destroyed_when_orphaned &&
        this.deployKey.almost_orphaned
      );
    },
    isExpandable() {
      return !this.projectsExpanded && this.restProjects.length > 1;
    },
    isExpanded() {
      return this.projectsExpanded || this.restProjects.length === 1;
    },
  },
  methods: {
    projectTooltipTitle(project) {
      return project.can_push
        ? s__('DeployKeys|Write access allowed')
        : s__('DeployKeys|Read access only');
    },
    toggleExpanded() {
      this.projectsExpanded = !this.projectsExpanded;
    },
  },
};
</script>

<template>
  <div class="gl-responsive-table-row deploy-key">
    <div class="table-section section-40">
      <div
        role="rowheader"
        class="table-mobile-header">
        {{ s__('DeployKeys|Deploy key') }}
      </div>
      <div class="table-mobile-content">
        <strong class="title qa-key-title">
          {{ deployKey.title }}
        </strong>
        <div class="fingerprint qa-key-fingerprint">
          {{ deployKey.fingerprint }}
        </div>
      </div>
    </div>
    <div class="table-section section-30 section-wrap">
      <div
        role="rowheader"
        class="table-mobile-header">
        {{ s__('DeployKeys|Project usage') }}
      </div>
      <div class="table-mobile-content deploy-project-list">
        <template v-if="projects.length > 0">
          <a
            class="label deploy-project-label"
            :title="projectTooltipTitle(firstProject)"
            v-tooltip
          >
            <span>
              {{ firstProject.project.full_name }}
            </span>
            <icon :name="firstProject.can_push ? 'lock-open' : 'lock'"/>
          </a>
          <a
            v-if="isExpandable"
            class="label deploy-project-label"
            @click="toggleExpanded"
            :title="restProjectsTooltip"
            v-tooltip
          >
            <span>{{ restProjectsLabel }}</span>
          </a>
          <a
            v-else-if="isExpanded"
            v-for="deployKeysProject in restProjects"
            :key="deployKeysProject.project.full_path"
            class="label deploy-project-label"
            :href="deployKeysProject.project.full_path"
            :title="projectTooltipTitle(deployKeysProject)"
            v-tooltip
          >
            <span>
              {{ deployKeysProject.project.full_name }}
            </span>
            <icon :name="deployKeysProject.can_push ? 'lock-open' : 'lock'"/>
          </a>
        </template>
        <span
          v-else
          class="text-secondary">{{ __('None') }}</span>
      </div>
    </div>
    <div class="table-section section-15 text-right">
      <div
        role="rowheader"
        class="table-mobile-header">
        {{ __('Created') }}
      </div>
      <div class="table-mobile-content text-secondary key-created-at">
        <span
          :title="tooltipTitle(deployKey.created_at)"
          v-tooltip>
          <icon name="calendar"/>
          <span>{{ timeFormated(deployKey.created_at) }}</span>
        </span>
      </div>
    </div>
    <div class="table-section section-15 table-button-footer deploy-key-actions">
      <div class="btn-group table-action-buttons">
        <action-btn
          v-if="!isEnabled"
          :deploy-key="deployKey"
          type="enable"
        >
          {{ __('Enable') }}
        </action-btn>
        <a
          v-if="deployKey.can_edit"
          class="btn btn-default text-secondary"
          :href="editDeployKeyPath"
          :title="__('Edit')"
          data-container="body"
          v-tooltip
        >
          <icon name="pencil"/>
        </a>
        <action-btn
          v-if="isRemovable"
          :deploy-key="deployKey"
          btn-css-class="btn-danger"
          type="remove"
          :title="__('Remove')"
          data-container="body"
          v-tooltip
        >
          <icon name="remove"/>
        </action-btn>
        <action-btn
          v-else-if="isEnabled"
          :deploy-key="deployKey"
          btn-css-class="btn-warning"
          type="disable"
          :title="__('Disable')"
          data-container="body"
          v-tooltip
        >
          <icon name="cancel"/>
        </action-btn>
      </div>
    </div>
  </div>
</template>