summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repository/components/fork_info.vue
blob: 980fa140eb52140b2022c05b2c7f108ca824501e (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
<script>
import { GlIcon, GlLink, GlSkeletonLoader } from '@gitlab/ui';
import { s__, sprintf, n__ } from '~/locale';
import { createAlert } from '~/flash';
import forkDetailsQuery from '../queries/fork_details.query.graphql';

export const i18n = {
  forkedFrom: s__('ForkedFromProjectPath|Forked from'),
  inaccessibleProject: s__('ForkedFromProjectPath|Forked from an inaccessible project.'),
  upToDate: s__('ForksDivergence|Up to date with the upstream repository.'),
  unknown: s__('ForksDivergence|This fork has diverged from the upstream repository.'),
  behind: s__('ForksDivergence|%{behind} %{commit_word} behind'),
  ahead: s__('ForksDivergence|%{ahead} %{commit_word} ahead of'),
  behindAndAhead: s__('ForksDivergence|%{messages} the upstream repository.'),
  error: s__('ForksDivergence|Failed to fetch fork details. Try again later.'),
};

export default {
  i18n,
  components: {
    GlIcon,
    GlLink,
    GlSkeletonLoader,
  },
  apollo: {
    project: {
      query: forkDetailsQuery,
      variables() {
        return {
          projectPath: this.projectPath,
          ref: this.selectedRef,
        };
      },
      skip() {
        return !this.sourceName;
      },
      error(error) {
        createAlert({
          message: this.$options.i18n.error,
          captureError: true,
          error,
        });
      },
    },
  },
  props: {
    projectPath: {
      type: String,
      required: true,
    },
    selectedRef: {
      type: String,
      required: true,
    },
    sourceName: {
      type: String,
      required: false,
      default: '',
    },
    sourcePath: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      project: {
        forkDetails: {
          ahead: null,
          behind: null,
        },
      },
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.project.loading;
    },
    ahead() {
      return this.project?.forkDetails?.ahead;
    },
    behind() {
      return this.project?.forkDetails?.behind;
    },
    behindText() {
      return sprintf(this.$options.i18n.behind, {
        behind: this.behind,
        commit_word: n__('commit', 'commits', this.behind),
      });
    },
    aheadText() {
      return sprintf(this.$options.i18n.ahead, {
        ahead: this.ahead,
        commit_word: n__('commit', 'commits', this.ahead),
      });
    },
    isUnknownDivergence() {
      return (!this.ahead && this.ahead !== 0) || (!this.behind && this.behind !== 0);
    },
    behindAheadMessage() {
      const messages = [];
      if (this.behind > 0) {
        messages.push(this.behindText);
      }
      if (this.ahead > 0) {
        messages.push(this.aheadText);
      }
      return messages.join(', ');
    },
    hasBehindAheadMessage() {
      return this.behindAheadMessage.length > 0;
    },
    forkDivergenceMessage() {
      if (this.isUnknownDivergence) {
        return this.$options.i18n.unknown;
      }
      if (this.hasBehindAheadMessage) {
        return sprintf(this.$options.i18n.behindAndAhead, {
          messages: this.behindAheadMessage,
        });
      }
      return this.$options.i18n.upToDate;
    },
  },
};
</script>

<template>
  <div class="info-well gl-sm-display-flex gl-flex-direction-column">
    <div class="well-segment gl-p-5 gl-w-full gl-display-flex">
      <gl-icon name="fork" :size="16" class="gl-display-block gl-m-4 gl-text-center" />
      <div v-if="sourceName">
        {{ $options.i18n.forkedFrom }}
        <gl-link data-qa-selector="forked_from_link" :href="sourcePath">{{ sourceName }}</gl-link>
        <gl-skeleton-loader v-if="isLoading" :lines="1" />
        <div v-else class="gl-text-secondary">
          {{ forkDivergenceMessage }}
        </div>
      </div>
      <div v-else data-testid="inaccessible-project" class="gl-align-items-center gl-display-flex">
        {{ $options.i18n.inaccessibleProject }}
      </div>
    </div>
  </div>
</template>