File: flow_metrics_examples.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (241 lines) | stat: -rw-r--r-- 6,060 bytes parent folder | download
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
236
237
238
239
240
241
# frozen_string_literal: true

RSpec.shared_examples 'validation on Time arguments' do
  context 'when `to` parameter is higher than `from`' do
    let(:variables) do
      {
        path: full_path,
        from: 1.day.ago.iso8601,
        to: 2.days.ago.iso8601
      }
    end

    it 'returns error' do
      expect(result).to be_nil
      expect(graphql_errors.first['message']).to include('`from` argument must be before `to` argument')
    end
  end

  context 'when from and to parameter range is higher than 180 days' do
    let(:variables) do
      {
        path: full_path,
        from: Time.now,
        to: 181.days.from_now
      }
    end

    it 'returns error' do
      expect(result).to be_nil
      expect(graphql_errors.first['message']).to include('Max of 180 days timespan is allowed')
    end
  end
end

RSpec.shared_examples 'value stream analytics flow metrics issueCount examples' do
  let_it_be(:milestone) { create(:milestone, group: group) }
  let_it_be(:label) { create(:group_label, group: group) }

  let_it_be(:author) { create(:user) }
  let_it_be(:assignee) { create(:user) }

  let_it_be(:issue1) { create(:issue, project: project1, author: author, created_at: 12.days.ago) }
  let_it_be(:issue2) { create(:issue, project: project2, author: author, created_at: 13.days.ago) }

  let_it_be(:issue3) do
    create(:labeled_issue,
      project: project1,
      labels: [label],
      author: author,
      milestone: milestone,
      assignees: [assignee],
      created_at: 14.days.ago)
  end

  let_it_be(:issue4) do
    create(:labeled_issue,
      project: project2,
      labels: [label],
      assignees: [assignee],
      created_at: 15.days.ago)
  end

  let_it_be(:issue_outside_of_the_range) { create(:issue, project: project2, author: author, created_at: 50.days.ago) }

  let(:query) do
    <<~QUERY
      query($path: ID!, $assigneeUsernames: [String!], $authorUsername: String, $milestoneTitle: String, $labelNames: [String!], $from: Time!, $to: Time!) {
        #{context}(fullPath: $path) {
          flowMetrics {
            issueCount(assigneeUsernames: $assigneeUsernames, authorUsername: $authorUsername, milestoneTitle: $milestoneTitle, labelNames: $labelNames, from: $from, to: $to) {
              value
              unit
              identifier
              title
            }
          }
        }
      }
    QUERY
  end

  let(:variables) do
    {
      path: full_path,
      from: 20.days.ago.iso8601,
      to: 10.days.ago.iso8601
    }
  end

  subject(:result) do
    post_graphql(query, current_user: current_user, variables: variables)

    graphql_data.dig(context.to_s, 'flowMetrics', 'issueCount')
  end

  it 'returns the correct count' do
    expect(result).to eq({
      'identifier' => 'issues',
      'unit' => nil,
      'value' => 4,
      'title' => n_('New issue', 'New issues', 4)
    })
  end

  context 'with partial filters' do
    let(:variables) do
      {
        path: full_path,
        assigneeUsernames: [assignee.username],
        labelNames: [label.title],
        from: 20.days.ago.iso8601,
        to: 10.days.ago.iso8601
      }
    end

    it 'returns filtered count' do
      expect(result).to eq({
        'identifier' => 'issues',
        'unit' => nil,
        'value' => 2,
        'title' => n_('New issue', 'New issues', 2)
      })
    end
  end

  context 'with all filters' do
    let(:variables) do
      {
        path: full_path,
        assigneeUsernames: [assignee.username],
        labelNames: [label.title],
        authorUsername: author.username,
        milestoneTitle: milestone.title,
        from: 20.days.ago.iso8601,
        to: 10.days.ago.iso8601
      }
    end

    it 'returns filtered count' do
      expect(result).to eq({
        'identifier' => 'issues',
        'unit' => nil,
        'value' => 1,
        'title' => n_('New issue', 'New issues', 1)
      })
    end
  end

  context 'when the user is not authorized' do
    let(:current_user) { create(:user) }

    it 'returns nil' do
      expect(result).to eq(nil)
    end
  end

  it_behaves_like 'validation on Time arguments'
end

RSpec.shared_examples 'value stream analytics flow metrics deploymentCount examples' do
  let_it_be(:deployment1) do
    create(:deployment, :success, environment: production_environment1, finished_at: 5.days.ago)
  end

  let_it_be(:deployment2) do
    create(:deployment, :success, environment: production_environment2, finished_at: 10.days.ago)
  end

  let_it_be(:deployment3) do
    create(:deployment, :success, environment: production_environment2, finished_at: 15.days.ago)
  end

  let(:variables) do
    {
      path: full_path,
      from: 12.days.ago.iso8601,
      to: 3.days.ago.iso8601
    }
  end

  let(:query) do
    <<~QUERY
      query($path: ID!, $from: Time!, $to: Time!) {
        #{context}(fullPath: $path) {
          flowMetrics {
            deploymentCount(from: $from, to: $to) {
              value
              unit
              identifier
              title
            }
          }
        }
      }
    QUERY
  end

  subject(:result) do
    post_graphql(query, current_user: current_user, variables: variables)

    graphql_data.dig(context.to_s, 'flowMetrics', 'deploymentCount')
  end

  it 'returns the correct count' do
    expect(result).to eq({
      'identifier' => 'deploys',
      'unit' => nil,
      'value' => 2,
      'title' => n_('Deploy', 'Deploys', 2)
    })
  end

  context 'when the user is not authorized' do
    let(:current_user) { create(:user) }

    it 'returns nil' do
      expect(result).to eq(nil)
    end
  end

  context 'when outside of the date range' do
    let(:variables) do
      {
        path: full_path,
        from: 20.days.ago.iso8601,
        to: 18.days.ago.iso8601
      }
    end

    it 'returns 0 count' do
      expect(result).to eq({
        'identifier' => 'deploys',
        'unit' => nil,
        'value' => 0,
        'title' => n_('Deploy', 'Deploys', 0)
      })
    end
  end

  it_behaves_like 'validation on Time arguments'
end