-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
runs-list.jsx
401 lines (346 loc) · 10.5 KB
/
runs-list.jsx
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import _ from 'lodash'
import React, { Component } from 'react'
import { observer } from 'mobx-react'
import Loader from 'react-loader'
import ipc from '../lib/ipc'
import { configFileFormatted } from '../lib/config-file-formatted'
import authStore from '../auth/auth-store'
import RunsStore from './runs-store'
import errors from '../lib/errors'
import runsApi from './runs-api'
import projectsApi from '../projects/projects-api'
import Project from '../project/project-model'
import orgsStore from '../organizations/organizations-store'
import ErrorMessage from './error-message'
import LoginForm from '../auth/login-form'
import Run from './runs-list-item'
import PermissionMessage from './permission-message'
import ProjectNotSetup from './project-not-setup'
@observer
class RunsList extends Component {
state = {
recordKey: null,
isLoading: true,
hasApiServer: false,
apiUrl: '',
apiError: null,
}
constructor (...args) {
super(...args)
this.runsStore = new RunsStore()
}
componentDidMount () {
this._pingApiServer()
this._handlePolling()
this._getKey()
}
componentDidUpdate () {
this._getKey()
this._handlePolling()
}
componentWillUnmount () {
this._stopPolling()
}
_pingApiServer = () => {
this.setState({ isLoading: true })
ipc.pingApiServer()
.then(() => {
this.setState({
apiError: null,
hasApiServer: true,
isLoading: false,
})
})
.catch(({ apiUrl, message }) => {
this.setState({
apiError: message,
apiUrl,
isLoading: false,
})
})
}
_getRuns = () => {
if (authStore.isAuthenticated && !!this.props.project.id) {
runsApi.loadRuns(this.runsStore)
}
}
_handlePolling () {
if (this._shouldPollRuns()) {
this._poll()
} else {
this._stopPolling()
}
}
_shouldPollRuns () {
return (
authStore.isAuthenticated &&
!this.runsStore.error &&
!!this.props.project.id
)
}
_poll () {
if (runsApi.isPolling()) return
runsApi.loadRuns(this.runsStore)
runsApi.pollRuns(this.runsStore)
}
_stopPolling () {
runsApi.stopPollingRuns()
}
_getKey () {
if (this._needsKey()) {
projectsApi.getRecordKeys().then((keys = []) => {
if (keys.length) {
this.setState({ recordKey: keys[0].id })
}
})
}
}
_needsKey () {
return (
!this.state.recordKey &&
authStore.isAuthenticated &&
!this.runsStore.isLoading &&
!this.runsStore.error &&
!this.runsStore.runs.length &&
this.props.project.id
)
}
render () {
const { project } = this.props
// pinging api server to see if we can show anything
if (this.state.isLoading) {
return <Loader color='#888' scale={0.5}/>
}
// no connection to api server, can't load any runs
if (!this.state.hasApiServer) {
return this._noApiServer()
}
// no project id means they have not set up to record
if (!project.id) {
return this._projectNotSetup()
}
// not logged in
if (!authStore.isAuthenticated) {
return this._loginMessage()
}
// If the project is invalid
if (project.isInvalid) {
return this._projectNotSetup(false)
}
// OR if user does not have acces to the project
if (project.isUnauthorized) {
return this._permissionMessage()
}
// OR if there is an error getting the runs
if (this.runsStore.error) {
// project id missing, probably removed manually from cypress.json
if (errors.isMissingProjectId(this.runsStore.error)) {
return this._projectNotSetup()
// the project is invalid
}
if (errors.isNotFound(this.runsStore.error)) {
return this._projectNotSetup(false)
// they have been logged out
}
if (errors.isUnauthenticated(this.runsStore.error)) {
return this._loginMessage()
// they are not authorized to see runs
}
if (errors.isUnauthorized(this.runsStore.error)) {
return this._permissionMessage()
// other error, but only show if we don't already have runs
}
if (!this.runsStore.isLoaded) {
return <ErrorMessage error={this.runsStore.error} />
}
}
// OR the runs are loading for the first time
if (this.runsStore.isLoading && !this.runsStore.isLoaded) return <Loader color='#888' scale={0.5}/>
// OR there are no runs to show
if (!this.runsStore.runs.length) {
// AND they've never setup CI
if (!project.id) {
return this._projectNotSetup()
// OR they have setup CI
}
return this._empty()
}
//--------End Run States----------//
// everything's good, there are runs to show!
return (
<div className='runs'>
<header>
<h5>Runs
{this._lastUpdated()}
<button
aria-label='Refresh'
className='btn btn-link btn-sm'
disabled={this.runsStore.isLoading}
onClick={this._getRuns}
>
<i aria-hidden="true" className={`fas fa-sync-alt ${this.runsStore.isLoading ? 'fa-spin' : ''}`}></i>
</button>
</h5>
<div>
<a href="#" className='btn btn-sm see-all-runs' onClick={this._openRuns}>
See all runs <i className='fas fa-external-link-alt'></i>
</a>
</div>
</header>
<ul className='runs-container list-as-table'>
{_.map(this.runsStore.runs, (run) => (
<Run
key={run.id}
goToRun={this._openRun}
run={run}
/>
))}
</ul>
</div>
)
}
_lastUpdated () {
if (!this.runsStore.lastUpdated) return null
return (
<span className='last-updated'>
Last updated: {this.runsStore.lastUpdated}
</span>
)
}
_noApiServer () {
return (
<div className='empty empty-no-api-server'>
<h4><i className='fas fa-wifi'></i> Cannot connect to API server</h4>
<p>Viewing runs requires connecting to an external API server.</p>
<p>We tried but failed to connect to the API server at <em>{this.state.apiUrl}</em></p>
<p>
<button
className='btn btn-default btn-sm'
onClick={this._pingApiServer}
>
<i className='fas fa-sync-alt'></i>{' '}
Try again
</button>
</p>
<p>The following error was encountered:</p>
<pre className='alert alert-danger'><code>{this.state.apiError}</code></pre>
<a onClick={this._openAPIHelp}>Learn more</a>
</div>
)
}
_loginMessage () {
return (
<div className='empty empty-log-in'>
<h4>Log in to view runs</h4>
<p>
After logging in, you will see recorded runs here and on the <a href='#' onClick={this._visitDashboard}>Cypress Dashboard Service</a>.
</p>
<div className='runs-screenshots'>
<img width='150' height='150' src='https://on.cypress.io/images/desktop-onboarding-thumb-1' />
<img width='150' height='150' src='https://on.cypress.io/images/desktop-onboarding-thumb-2' />
<img width='150' height='150' src='https://on.cypress.io/images/desktop-onboarding-thumb-3' />
</div>
<LoginForm utm='Runs Tab Login Button' />
</div>
)
}
_projectNotSetup (isValid = true) {
return (
<ProjectNotSetup
isAuthenticated={authStore.isAuthenticated}
isShowingLogin={authStore.isShowingLogin}
project={this.props.project}
isValid={isValid}
onSetup={this._setProjectDetails}
/>
)
}
_permissionMessage () {
return (
<PermissionMessage
project={this.props.project}
onRetry={this._getRuns}
/>
)
}
_setProjectDetails = (projectDetails) => {
this.runsStore.setError(null)
projectsApi.updateProject(this.props.project, {
id: projectDetails.id,
name: projectDetails.projectName,
public: projectDetails.public,
orgId: projectDetails.orgId,
orgName: (orgsStore.getOrgById(projectDetails.orgId) || {}).name,
state: Project.VALID,
})
}
_empty () {
return (
<div>
<div className='first-run-instructions'>
<h4>
To record your first run...
</h4>
<h5>
<span className='pull-left'>
1. Check {configFileFormatted(this.props.project.configFile)} into source control.
</span>
<a onClick={this._openProjectIdGuide} className='pull-right'>
<i className='fas fa-question-circle'></i>{' '}
{' '}
Why?
</a>
</h5>
<pre className='line-nums'>
<span>{'{'}</span>
<span>{` "projectId": "${this.props.project.id || '<projectId>'}"`}</span>
<span>{'}'}</span>
</pre>
<h5>
<span className='pull-left'>
2. Run this command now, or in CI.
</span>
<a onClick={this._openCiGuide} className='pull-right'>
<i className='fas fa-question-circle'></i>{' '}
Need help?
</a>
</h5>
<pre>
<code>cypress run --record --key {this.state.recordKey || '<record-key>'}</code>
</pre>
<hr />
<p className='alert alert-default'>
<i className='fas fa-info-circle'></i>{' '}
Recorded runs will show up{' '}
<a href='#' onClick={this._openRunGuide}>here</a>{' '}
and on your{' '}
<a href='#' onClick={this._openRuns}>Cypress Dashboard Service</a>.
</p>
</div>
</div>
)
}
_openRunGuide = (e) => {
e.preventDefault()
ipc.externalOpen('https://on.cypress.io/recording-project-runs')
}
_openRuns = (e) => {
e.preventDefault()
ipc.externalOpen(`https://on.cypress.io/dashboard/projects/${this.props.project.id}/runs`)
}
_openCiGuide = (e) => {
e.preventDefault()
ipc.externalOpen('https://on.cypress.io/guides/continuous-integration')
}
_openProjectIdGuide = (e) => {
e.preventDefault()
ipc.externalOpen('https://on.cypress.io/what-is-a-project-id')
}
_openRun = (buildNumber) => {
ipc.externalOpen(`https://on.cypress.io/dashboard/projects/${this.props.project.id}/runs/${buildNumber}`)
}
_openAPIHelp () {
ipc.externalOpen('https://on.cypress.io/help-connect-to-api')
}
}
export default RunsList