summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/tree/components/commit_pipeline_status_component.vue
blob: c772fca14bb64e86d00bbaf43756f9ee1b6fafa7 (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
<script>
  import Visibility from 'visibilityjs';
  import ciIcon from '~/vue_shared/components/ci_icon.vue';
  import loadingIcon from '~/vue_shared/components/loading_icon.vue';
  import Poll from '~/lib/utils/poll';
  import Flash from '~/flash';
  import { s__, sprintf } from '~/locale';
  import tooltip from '~/vue_shared/directives/tooltip';
  import CommitPipelineService from '../services/commit_pipeline_service';

  export default {
    directives: {
      tooltip,
    },
    components: {
      ciIcon,
      loadingIcon,
    },
    props: {
      endpoint: {
        type: String,
        required: true,
      },
      /* This prop can be used to replace some of the `render_commit_status`
      used across GitLab, this way we could use this vue component and add a
      realtime status where it makes sense
      realtime: {
        type: Boolean,
        required: false,
        default: true,
      }, */
    },
    data() {
      return {
        ciStatus: {},
        isLoading: true,
      };
    },
    computed: {
      statusTitle() {
        return sprintf(s__('Commits|Commit: %{commitText}'), { commitText: this.ciStatus.text });
      },
    },
    mounted() {
      this.service = new CommitPipelineService(this.endpoint);
      this.initPolling();
    },
    methods: {
      successCallback(res) {
        const pipelines = res.data.pipelines;
        if (pipelines.length > 0) {
          // The pipeline entity always keeps the latest pipeline info on the `details.status`
          this.ciStatus = pipelines[0].details.status;
        }
        this.isLoading = false;
      },
      errorCallback() {
        this.ciStatus = {
          text: 'not found',
          icon: 'status_notfound',
          group: 'notfound',
        };
        this.isLoading = false;
        Flash(s__('Something went wrong on our end'));
      },
      initPolling() {
        this.poll = new Poll({
          resource: this.service,
          method: 'fetchData',
          successCallback: response => this.successCallback(response),
          errorCallback: this.errorCallback,
        });

        if (!Visibility.hidden()) {
          this.isLoading = true;
          this.poll.makeRequest();
        } else {
          this.fetchPipelineCommitData();
        }

        Visibility.change(() => {
          if (!Visibility.hidden()) {
            this.poll.restart();
          } else {
            this.poll.stop();
          }
        });
      },
      fetchPipelineCommitData() {
        this.service.fetchData()
          .then(this.successCallback)
          .catch(this.errorCallback);
      },
    },
    destroy() {
      this.poll.stop();
    },
  };
</script>
<template>
  <div>
    <loading-icon
      v-if="isLoading"
      label="Loading pipeline status"
      size="3"
    />
    <a
      v-else
      :href="ciStatus.details_path"
    >
      <ci-icon
        v-tooltip
        :title="statusTitle"
        :aria-label="statusTitle"
        :status="ciStatus"
        data-container="body"
      />
    </a>
  </div>
</template>