commit.vue 4.6 KB
Newer Older
1
<script>
N
Nathan Friend 已提交
2 3
import _ from 'underscore';
import { GlTooltipDirective, GlLink } from '@gitlab/ui';
4 5
import UserAvatarLink from './user_avatar/user_avatar_link.vue';
import Icon from '../../vue_shared/components/icon.vue';
6

7 8
export default {
  directives: {
9
    GlTooltip: GlTooltipDirective,
10 11 12 13
  },
  components: {
    UserAvatarLink,
    Icon,
N
Nathan Friend 已提交
14
    GlLink,
15 16 17
  },
  props: {
    /**
G
George Tsiolis 已提交
18
     * Indicates the existence of a tag.
19 20 21 22 23 24 25
     * Used to render the correct icon, if true will render `fa-tag` icon,
     * if false will render a svg sprite fork icon
     */
    tag: {
      type: Boolean,
      required: false,
      default: false,
F
Filipa Lacerda 已提交
26
    },
27 28 29 30 31 32 33 34 35 36 37
    /**
     * If provided is used to render the branch name and url.
     * Should contain the following properties:
     * name
     * ref_url
     */
    commitRef: {
      type: Object,
      required: false,
      default: () => ({}),
    },
N
Nathan Friend 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    /**
     * If provided, is used the render the MR IID and link
     * in place of the branch name.  Must contains the
     * following properties:
     *   - iid (number)
     *   - path (non-empty string)
     *
     * May optionally contain the following properties:
     *   - title (string): used in a tooltip if provided
     *
     * Any additional properties are ignored.
     */
    mergeRequestRef: {
      type: Object,
      required: false,
      default: undefined,
      validator: ref =>
        _.isUndefined(ref) || (_.isFinite(ref.iid) && _.isString(ref.path) && !_.isEmpty(ref.path)),
    },

59 60 61 62 63 64 65
    /**
     * Used to link to the commit sha.
     */
    commitUrl: {
      type: String,
      required: false,
      default: '',
F
Filipa Lacerda 已提交
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
    /**
     * Used to show the commit short sha that links to the commit url.
     */
    shortSha: {
      type: String,
      required: false,
      default: '',
    },
    /**
     * If provided shows the commit tile.
     */
    title: {
      type: String,
      required: false,
      default: '',
    },
    /**
     * If provided renders information about the author of the commit.
     * When provided should include:
     * `avatar_url` to render the avatar icon
     * `web_url` to link to user profile
     * `username` to render alt and title tags
     */
    author: {
      type: Object,
      required: false,
      default: () => ({}),
    },
N
Nathan Friend 已提交
96 97 98 99 100

    /**
     * Indicates whether or not to show the branch/MR ref info
     */
    showRefInfo: {
101 102 103
      type: Boolean,
      required: false,
      default: true,
104
    },
105 106 107
  },
  computed: {
    /**
N
Nathan Friend 已提交
108
     * Determines if we shoud render the ref info section based
109
     */
N
Nathan Friend 已提交
110 111
    shouldShowRefInfo() {
      return this.showRefInfo && (this.commitRef || this.mergeRequestRef);
112
    },
N
Nathan Friend 已提交
113

114 115 116 117 118 119 120 121
    /**
     * Used to verify if all the properties needed to render the commit
     * author section were provided.
     *
     * @returns {Boolean}
     */
    hasAuthor() {
      return this.author && this.author.avatar_url && this.author.path && this.author.username;
122
    },
123 124 125 126 127 128 129 130 131 132 133
    /**
     * If information about the author is provided will return a string
     * to be rendered as the alt attribute of the img tag.
     *
     * @returns {String}
     */
    userImageAltDescription() {
      return this.author && this.author.username ? `${this.author.username}'s avatar` : null;
    },
  },
};
134 135 136
</script>
<template>
  <div class="branch-commit">
N
Nathan Friend 已提交
137
    <template v-if="shouldShowRefInfo">
138
      <div class="icon-container">
N
Nathan Friend 已提交
139 140 141
        <icon v-if="tag" name="tag" />
        <icon v-else-if="mergeRequestRef" name="git-merge" />
        <icon v-else name="branch" />
142
      </div>
143

N
Nathan Friend 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
      <gl-link
        v-if="mergeRequestRef"
        v-gl-tooltip
        :href="mergeRequestRef.path"
        :title="mergeRequestRef.title"
        class="ref-name"
      >
        {{ mergeRequestRef.iid }}
      </gl-link>
      <gl-link
        v-else
        v-gl-tooltip
        :href="commitRef.ref_url"
        :title="commitRef.name"
        class="ref-name"
      >
F
Filipa Lacerda 已提交
160
        {{ commitRef.name }}
N
Nathan Friend 已提交
161
      </gl-link>
162
    </template>
M
Mike Greiling 已提交
163
    <icon name="commit" class="commit-icon js-commit-icon" />
164

165
    <gl-link :href="commitUrl" class="commit-sha mr-0"> {{ shortSha }} </gl-link>
166 167

    <div class="commit-title flex-truncate-parent">
M
Mike Greiling 已提交
168
      <span v-if="title" class="flex-truncate-child">
169 170 171 172 173 174
        <user-avatar-link
          v-if="hasAuthor"
          :link-href="author.path"
          :img-src="author.avatar_url"
          :img-alt="userImageAltDescription"
          :tooltip-text="author.username"
175
          class="avatar-image-container"
176
        />
N
Nathan Friend 已提交
177
        <gl-link :href="commitUrl" class="commit-row-message"> {{ title }} </gl-link>
178
      </span>
M
Mike Greiling 已提交
179
      <span v-else> Can't find HEAD commit for this branch </span>
180 181 182
    </div>
  </div>
</template>