environment_item.js.es6 15 KB
Newer Older
1
const Vue = require('vue');
Filipa Lacerda's avatar
Filipa Lacerda committed
2
const Timeago = require('timeago.js');
3

4
require('../../lib/utils/text_utility');
Filipa Lacerda's avatar
Filipa Lacerda committed
5
require('../../vue_shared/components/commit');
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
const ActionsComponent = require('./environment_actions');
const ExternalUrlComponent = require('./environment_external_url');
const StopComponent = require('./environment_stop');
const RollbackComponent = require('./environment_rollback');
const TerminalButtonComponent = require('./environment_terminal_button');

/**
 * Envrionment Item Component
 *
 * Renders a table row for each environment.
 */

const timeagoInstance = new Timeago();

module.exports = Vue.component('environment-item', {

  components: {
    'commit-component': gl.CommitComponent,
    'actions-component': ActionsComponent,
    'external-url-component': ExternalUrlComponent,
    'stop-component': StopComponent,
    'rollback-component': RollbackComponent,
    'terminal-button-component': TerminalButtonComponent,
  },

  props: {
    model: {
      type: Object,
      required: true,
      default: () => ({}),
    },
37

38 39 40 41 42
    canCreateDeployment: {
      type: Boolean,
      required: false,
      default: false,
    },
43

44 45 46 47 48
    canReadEnvironment: {
      type: Boolean,
      required: false,
      default: false,
    },
49

50 51 52 53
    commitIconSvg: {
      type: String,
      required: false,
    },
54

55 56 57
    playIconSvg: {
      type: String,
      required: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
58
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
59

60 61 62
    terminalIconSvg: {
      type: String,
      required: false,
Filipa Lacerda's avatar
Filipa Lacerda committed
63
    },
64
  },
65

66 67 68 69 70 71 72 73 74
  computed: {
    /**
     * Verifies if `last_deployment` key exists in the current Envrionment.
     * This key is required to render most of the html - this method works has
     * an helper.
     *
     * @returns {Boolean}
     */
    hasLastDeploymentKey() {
75 76
      if (this.model.latest &&
        this.model.latest.last_deployment &&
77 78 79 80 81 82 83 84 85 86 87 88 89
        !this.$options.isObjectEmpty(this.model.latest.last_deployment)) {
        return true;
      }
      return false;
    },

    /**
     * Verifies is the given environment has manual actions.
     * Used to verify if we should render them or nor.
     *
     * @returns {Boolean|Undefined}
     */
    hasManualActions() {
90 91
      return this.model.latest &&
        this.model.latest.last_deployment &&
92 93 94 95 96 97 98 99 100 101
        this.model.latest.last_deployment.manual_actions &&
        this.model.latest.last_deployment.manual_actions.length > 0;
    },

    /**
     * Returns the value of the `stop_action?` key provided in the response.
     *
     * @returns {Boolean}
     */
    hasStopAction() {
102
      return this.model.latest['stop_action?'];
103 104 105 106 107 108 109 110 111
    },

    /**
     * Verifies if the `deployable` key is present in `last_deployment` key.
     * Used to verify whether we should or not render the rollback partial.
     *
     * @returns {Boolean|Undefined}
     */
    canRetry() {
Filipa Lacerda's avatar
Filipa Lacerda committed
112
      return this.model.latest &&
Filipa Lacerda's avatar
Filipa Lacerda committed
113
        this.hasLastDeploymentKey &&
114 115 116 117 118 119 120 121 122 123
        this.model.latest.last_deployment &&
        this.model.latest.last_deployment.deployable;
    },

    /**
     * Verifies if the date to be shown is present.
     *
     * @returns {Boolean|Undefined}
     */
    canShowDate() {
Filipa Lacerda's avatar
Filipa Lacerda committed
124
      return this.model.latest &&
Filipa Lacerda's avatar
Filipa Lacerda committed
125
        this.model.latest.last_deployment &&
126 127 128 129 130 131 132 133 134 135
        this.model.latest.last_deployment.deployable &&
        this.model.latest.last_deployment.deployable !== undefined;
    },

    /**
     * Human readable date.
     *
     * @returns {String}
     */
    createdDate() {
Filipa Lacerda's avatar
Filipa Lacerda committed
136
      if (this.model.latest &&
Filipa Lacerda's avatar
Filipa Lacerda committed
137
        this.model.latest.last_deployment &&
Filipa Lacerda's avatar
Filipa Lacerda committed
138
        this.model.latest.last_deployment.deployable &&
Filipa Lacerda's avatar
Filipa Lacerda committed
139 140 141 142
        this.model.latest.last_deployment.deployable.created_at) {
        return timeagoInstance.format(this.model.latest.last_deployment.deployable.created_at);
      }
      return '';
Filipa Lacerda's avatar
Filipa Lacerda committed
143 144
    },

145
    /**
146 147 148
     * Returns the manual actions with the name parsed.
     *
     * @returns {Array.<Object>|Undefined}
149
     */
150 151 152 153 154 155 156 157 158
    manualActions() {
      if (this.hasManualActions) {
        return this.model.latest.last_deployment.manual_actions.map((action) => {
          const parsedAction = {
            name: gl.text.humanize(action.name),
            play_path: action.play_path,
          };
          return parsedAction;
        });
159
      }
160
      return [];
161 162
    },

163 164 165 166 167 168
    /**
     * Builds the string used in the user image alt attribute.
     *
     * @returns {String}
     */
    userImageAltDescription() {
Filipa Lacerda's avatar
Filipa Lacerda committed
169 170
      if (this.model.latest &&
        this.model.latest.last_deployment &&
171 172 173 174 175 176 177 178 179 180 181 182 183
        this.model.latest.last_deployment.user &&
        this.model.latest.last_deployment.user.username) {
        return `${this.model.latest.last_deployment.user.username}'s avatar'`;
      }
      return '';
    },

    /**
     * If provided, returns the commit tag.
     *
     * @returns {String|Undefined}
     */
    commitTag() {
Filipa Lacerda's avatar
Filipa Lacerda committed
184 185
      if (this.model.latest &&
        this.model.latest.last_deployment &&
186 187 188 189 190 191 192 193 194 195 196 197
        this.model.latest.last_deployment.tag) {
        return this.model.latest.last_deployment.tag;
      }
      return undefined;
    },

    /**
     * If provided, returns the commit ref.
     *
     * @returns {Object|Undefined}
     */
    commitRef() {
Filipa Lacerda's avatar
Filipa Lacerda committed
198 199
      if (this.model.latest &&
        this.model.latest.last_deployment &&
200 201 202 203 204 205 206 207 208 209 210 211
        this.model.latest.last_deployment.ref) {
        return this.model.latest.last_deployment.ref;
      }
      return undefined;
    },

    /**
     * If provided, returns the commit url.
     *
     * @returns {String|Undefined}
     */
    commitUrl() {
Filipa Lacerda's avatar
Filipa Lacerda committed
212 213
      if (this.model.latest &&
        this.model.latest.last_deployment &&
214 215 216 217 218 219 220 221 222 223 224 225 226
        this.model.latest.last_deployment.commit &&
        this.model.latest.last_deployment.commit.commit_path) {
        return this.model.latest.last_deployment.commit.commit_path;
      }
      return undefined;
    },

    /**
     * If provided, returns the commit short sha.
     *
     * @returns {String|Undefined}
     */
    commitShortSha() {
Filipa Lacerda's avatar
Filipa Lacerda committed
227 228
      if (this.model.latest &&
        this.model.latest.last_deployment &&
229 230 231 232 233 234 235 236 237 238 239 240 241
        this.model.latest.last_deployment.commit &&
        this.model.latest.last_deployment.commit.short_id) {
        return this.model.latest.last_deployment.commit.short_id;
      }
      return undefined;
    },

    /**
     * If provided, returns the commit title.
     *
     * @returns {String|Undefined}
     */
    commitTitle() {
Filipa Lacerda's avatar
Filipa Lacerda committed
242 243
      if (this.model.latest &&
        this.model.latest.last_deployment &&
244 245 246 247 248 249 250 251 252 253 254 255 256
        this.model.latest.last_deployment.commit &&
        this.model.latest.last_deployment.commit.title) {
        return this.model.latest.last_deployment.commit.title;
      }
      return undefined;
    },

    /**
     * If provided, returns the commit tag.
     *
     * @returns {Object|Undefined}
     */
    commitAuthor() {
Filipa Lacerda's avatar
Filipa Lacerda committed
257 258
      if (this.model.latest &&
        this.model.latest.last_deployment &&
259 260 261 262 263 264 265 266 267 268 269 270 271 272
        this.model.latest.last_deployment.commit &&
        this.model.latest.last_deployment.commit.author) {
        return this.model.latest.last_deployment.commit.author;
      }

      return undefined;
    },

    /**
     * Verifies if the `retry_path` key is present and returns its value.
     *
     * @returns {String|Undefined}
     */
    retryUrl() {
Filipa Lacerda's avatar
Filipa Lacerda committed
273 274
      if (this.model.latest &&
        this.model.latest.last_deployment &&
275 276 277 278 279 280 281 282 283 284 285 286 287
        this.model.latest.last_deployment.deployable &&
        this.model.latest.last_deployment.deployable.retry_path) {
        return this.model.latest.last_deployment.deployable.retry_path;
      }
      return undefined;
    },

    /**
     * Verifies if the `last?` key is present and returns its value.
     *
     * @returns {Boolean|Undefined}
     */
    isLastDeployment() {
Filipa Lacerda's avatar
Filipa Lacerda committed
288
      return this.model.latest && this.model.latest.last_deployment &&
289 290 291 292 293 294 295 296 297
        this.model.latest.last_deployment['last?'];
    },

    /**
     * Builds the name of the builds needed to display both the name and the id.
     *
     * @returns {String}
     */
    buildName() {
Filipa Lacerda's avatar
Filipa Lacerda committed
298 299
      if (this.model.latest &&
        this.model.latest.last_deployment &&
300 301 302 303 304
        this.model.latest.last_deployment.deployable) {
        return `${this.model.latest.last_deployment.deployable.name} #${this.model.latest.last_deployment.deployable.id}`;
      }
      return '';
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
305

306 307 308 309 310 311
    /**
     * Builds the needed string to show the internal id.
     *
     * @returns {String}
     */
    deploymentInternalId() {
Filipa Lacerda's avatar
Filipa Lacerda committed
312 313
      if (this.model.latest &&
        this.model.latest.last_deployment &&
314 315 316 317 318 319 320 321 322 323 324 325
        this.model.latest.last_deployment.iid) {
        return `#${this.model.latest.last_deployment.iid}`;
      }
      return '';
    },

    /**
     * Verifies if the user object is present under last_deployment object.
     *
     * @returns {Boolean}
     */
    deploymentHasUser() {
Filipa Lacerda's avatar
Filipa Lacerda committed
326 327
      return this.model.latest &&
        !this.$options.isObjectEmpty(this.model.latest.last_deployment) &&
328 329 330 331 332 333 334 335 336 337
        !this.$options.isObjectEmpty(this.model.latest.last_deployment.user);
    },

    /**
     * Returns the user object nested with the last_deployment object.
     * Used to render the template.
     *
     * @returns {Object}
     */
    deploymentUser() {
Filipa Lacerda's avatar
Filipa Lacerda committed
338 339
      if (this.model.latest &&
        !this.$options.isObjectEmpty(this.model.latest.last_deployment) &&
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        !this.$options.isObjectEmpty(this.model.latest.last_deployment.user)) {
        return this.model.latest.last_deployment.user;
      }
      return {};
    },

    /**
     * Verifies if the build name column should be rendered by verifing
     * if all the information needed is present
     * and if the environment is not a folder.
     *
     * @returns {Boolean}
     */
    shouldRenderBuildName() {
      return !this.model.isFolder &&
Filipa Lacerda's avatar
Filipa Lacerda committed
355
        this.model.latest &&
356 357 358 359
        !this.$options.isObjectEmpty(this.model.latest.last_deployment) &&
        !this.$options.isObjectEmpty(this.model.latest.last_deployment.deployable);
    },

Filipa Lacerda's avatar
Filipa Lacerda committed
360 361 362 363 364 365 366
    /**
     * Verifies the presence of all the keys needed to render the buil_path.
     *
     * @return {String}
     */
    buildPath() {
      if (this.model.latest &&
Filipa Lacerda's avatar
Filipa Lacerda committed
367
        this.model.latest.last_deployment &&
Filipa Lacerda's avatar
Filipa Lacerda committed
368
        this.model.latest.last_deployment.deployable &&
Filipa Lacerda's avatar
Filipa Lacerda committed
369 370
        this.model.latest.last_deployment.deployable.build_path) {
        return this.model.latest.last_deployment.deployable.build_path;
Filipa Lacerda's avatar
Filipa Lacerda committed
371 372 373 374
      }

      return '';
    },
375

Filipa Lacerda's avatar
Filipa Lacerda committed
376 377 378 379 380 381 382 383 384 385 386 387 388
    /**
     * Verifies the presence of all the keys needed to render the external_url.
     *
     * @return {String}
     */
    externalURL() {
      if (this.model.latest && this.model.latest.external_url) {
        return this.model.latest.external_url;
      }

      return '';
    },

389 390 391 392 393 394 395 396 397
    /**
     * Verifies if deplyment internal ID should be rendered by verifing
     * if all the information needed is present
     * and if the environment is not a folder.
     *
     * @returns {Boolean}
     */
    shouldRenderDeploymentID() {
      return !this.model.isFolder &&
Filipa Lacerda's avatar
Filipa Lacerda committed
398
        this.model.latest &&
399 400 401
        !this.$options.isObjectEmpty(this.model.latest.last_deployment) &&
        this.model.latest.last_deployment.iid !== undefined;
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
402 403

    environmentPath() {
Filipa Lacerda's avatar
Filipa Lacerda committed
404
      if (this.model && this.model.latest && this.model.latest.environment_path) {
Filipa Lacerda's avatar
Filipa Lacerda committed
405 406 407 408 409
        return this.model.latest.environment_path;
      }

      return '';
    },
Filipa Lacerda's avatar
Filipa Lacerda committed
410 411 412 413 414 415 416 417 418 419

    /**
     * Constructs folder URL based on the current location and the folder id.
     *
     * @return {String}
     */
    folderUrl() {
      return `${window.location.pathname}/folders/${this.model.latest.id}`;
    },

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  },

  /**
   * Helper to verify if certain given object are empty.
   * Should be replaced by lodash _.isEmpty - https://lodash.com/docs/4.17.2#isEmpty
   * @param  {Object} object
   * @returns {Bollean}
   */
  isObjectEmpty(object) {
    for (const key in object) { // eslint-disable-line
      if (hasOwnProperty.call(object, key)) {
        return false;
      }
    }
    return true;
  },

  template: `
    <tr>
      <td>
        <a v-if="!model.isFolder"
          class="environment-name"
Filipa Lacerda's avatar
Filipa Lacerda committed
442
          :href="environmentPath">
443 444
          {{model.name}}
        </a>
Filipa Lacerda's avatar
Filipa Lacerda committed
445
        <a v-else class="folder-name" :href="folderUrl">
446 447 448
          <span class="folder-icon">
            <i class="fa fa-caret-right" aria-hidden="true"></i>
            <i class="fa fa-folder" aria-hidden="true"></i>
Filipa Lacerda's avatar
Filipa Lacerda committed
449 450
          </span>

451 452
          <span>
            {{model.name}}
Filipa Lacerda's avatar
Filipa Lacerda committed
453 454
          </span>

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
          <span class="badge">
            {{model.size}}
          </span>
        </a>
      </td>

      <td class="deployment-column">
        <span v-if="shouldRenderDeploymentID">
          {{deploymentInternalId}}
        </span>

        <span v-if="!model.isFolder && deploymentHasUser">
          by
          <a :href="deploymentUser.web_url" class="js-deploy-user-container">
            <img class="avatar has-tooltip s20"
              :src="deploymentUser.avatar_url"
              :alt="userImageAltDescription"
              :title="deploymentUser.username" />
Filipa Lacerda's avatar
Filipa Lacerda committed
473
          </a>
474 475 476 477 478 479
        </span>
      </td>

      <td class="environments-build-cell">
        <a v-if="shouldRenderBuildName"
          class="build-link"
Filipa Lacerda's avatar
Filipa Lacerda committed
480
          :href="buildPath">
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
          {{buildName}}
        </a>
      </td>

      <td>
        <div v-if="!model.isFolder && hasLastDeploymentKey" class="js-commit-component">
          <commit-component
            :tag="commitTag"
            :commit-ref="commitRef"
            :commit-url="commitUrl"
            :short-sha="commitShortSha"
            :title="commitTitle"
            :author="commitAuthor"
            :commit-icon-svg="commitIconSvg">
          </commit-component>
        </div>
        <p v-if="!model.isFolder && !hasLastDeploymentKey" class="commit-title">
          No deployments yet
        </p>
      </td>

      <td>
        <span v-if="!model.isFolder && canShowDate"
          class="environment-created-date-timeago">
          {{createdDate}}
        </span>
      </td>

      <td class="hidden-xs">
        <div v-if="!model.isFolder">
          <div v-if="hasManualActions && canCreateDeployment"
            class="inline js-manual-actions-container">
            <actions-component
              :play-icon-svg="playIconSvg"
              :actions="manualActions">
            </actions-component>
Filipa Lacerda's avatar
Filipa Lacerda committed
517
          </div>
518

Filipa Lacerda's avatar
Filipa Lacerda committed
519
          <div v-if="externalURL && canReadEnvironment"
520 521
            class="inline js-external-url-container">
            <external-url-component
Filipa Lacerda's avatar
Filipa Lacerda committed
522
              :external-url="externalURL">
523 524 525
            </external-url-component>
          </div>

Filipa Lacerda's avatar
Filipa Lacerda committed
526
          <div v-if="hasStopAction && canCreateDeployment && model.latest"
527 528 529 530 531 532
            class="inline js-stop-component-container">
            <stop-component
              :stop-url="model.latest.stop_path">
            </stop-component>
          </div>

Filipa Lacerda's avatar
Filipa Lacerda committed
533
          <div v-if="model.latest && model.latest.terminal_path"
534 535 536 537 538 539 540 541 542 543 544 545 546
            class="inline js-terminal-button-container">
            <terminal-button-component
              :terminal-icon-svg="terminalIconSvg"
              :terminal-path="model.latest.terminal_path">
            </terminal-button-component>
          </div>

          <div v-if="canRetry && canCreateDeployment"
            class="inline js-rollback-component-container">
            <rollback-component
              :is-last-deployment="isLastDeployment"
              :retry-url="retryUrl">
              </rollback-component>
Filipa Lacerda's avatar
Filipa Lacerda committed
547
          </div>
548 549 550 551 552
        </div>
      </td>
    </tr>
  `,
});