-
Notifications
You must be signed in to change notification settings - Fork 528
/
EditorPage.jsx
709 lines (639 loc) · 27.5 KB
/
EditorPage.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import React from 'react';
import Extensions from '@jenkins-cd/js-extensions';
import {
Fetch,
UrlBuilder,
locationService,
ContentPageHeader,
pipelineService,
Paths,
RunApi,
activityService,
i18nTranslator,
} from '@jenkins-cd/blueocean-core-js';
import { Dialog, TextArea, RadioButtonGroup, TextInput, FormElement, Alerts } from '@jenkins-cd/design-language';
import { isSshRepositoryUrl } from './GitUtils';
import ScmContentApi, { LoadError } from './api/ScmContentApi';
import { convertInternalModelToJson, convertJsonToPipeline, convertPipelineToJson, convertJsonToInternalModel } from './services/PipelineSyntaxConverter';
import pipelineValidator from './services/PipelineValidator';
import pipelineStore from './services/PipelineStore';
import { observer } from 'mobx-react';
import saveApi from './SaveApi';
import { EditorMain } from './components/editor/EditorMain';
import { CopyPastePipelineDialog } from './components/editor/CopyPastePipelineDialog';
const Base64 = { encode: data => btoa(data), decode: str => atob(str) };
const t = i18nTranslator('blueocean-pipeline-editor');
class SaveDialog extends React.Component {
constructor(props) {
super(props);
const { branch } = this.props;
this.state = { branch: branch };
this.branchOptions = [{ branch: branch, toString: () => ['Commit to ', <i>{branch}</i>] }, { branch: '', toString: () => `Commit to new branch` }];
}
save() {
const { functions } = this.props;
this.setState({ errorMessage: null, branchError: false });
this.setState({ saving: true });
functions.save(this.state.branch, this.state.commitMessage, (...args) => this.showError(...args));
}
cancel() {
if (!this.state.saving) {
this.props.cancel();
}
}
showError(err, saveRequest) {
const { functions } = this.props;
let errorMessage = err.message ? err.message : err.errors ? err.errors.map(e => <div>{e.error}</div>) : err;
if (err.responseBody && err.responseBody.message) {
// GH JSON is dumped as a string in err.responseBody.message
// error: 409.
if (err.responseBody.message.indexOf('error: 409.') >= 0) {
if (this.props.branch !== saveRequest.content.branch) {
errorMessage = ['The branch ', <i>{saveRequest.content.branch}</i>, ' already exists'];
this.setState({ branchError: errorMessage });
errorMessage = null;
} else {
errorMessage = [
<div>Your Pipeline was changed by another user.</div>,
<div>
<a
href="#"
onClick={() => functions.overwriteChanges(this.state.branch, this.state.commitMessage, (...args) => this.showError(...args))}
>
Keep my changes
</a>{' '}
or{' '}
<a href="#" onClick={() => functions.discardChanges()}>
discard my changes
</a>
</div>,
];
}
//errorMessage = ['An error occurred saving to Github: ', ...errorMessage];
} else if (errorMessage === 'Server Error') {
errorMessage = err.responseBody.message;
} else if (err.response && err.response.status === 500) {
errorMessage = err.responseBody.message;
} else if (/^fetch failed:.*/.test(errorMessage)) {
errorMessage = err.responseBody.message;
}
}
if (err.responseBody && err.responseBody.errors && err.responseBody.errors.length) {
errorMessage = (
<div>
<div>
<strong>{errorMessage}</strong>
</div>
{err.responseBody.errors.map(e => (
<div>
<em>{e.code}</em>: {e.message}
</div>
))}
</div>
);
}
this.setState({ saving: false, errorMessage });
}
processBranchName(event) {
const cleanBranchName = event.target.value.replace(/\s/g, '');
this.branchOptions[1].branch = cleanBranchName;
this.setState({ branch: cleanBranchName });
}
render() {
const { branch } = this.props;
const { errorMessage } = this.state;
const buttons = [
<button className="btn-primary" onClick={() => this.save()} disabled={this.state.saving}>
Save & run
</button>,
<button className="btn-link btn-secondary" disabled={this.state.saving} onClick={() => this.cancel()}>
{t('editor.page.common.cancel', { default: 'Cancel' })}
</button>,
];
return (
<Dialog onDismiss={() => this.cancel()} title="Save Pipeline" buttons={buttons} className="save-pipeline-dialog">
{errorMessage && (
<div style={{ marginBottom: '10px' }}>
<Alerts type="Error" title="Error" message={errorMessage} />
</div>
)}
<div style={{ width: '400px', marginBottom: '16px' }}>Saving the pipeline will commit a Jenkinsfile to the repository.</div>
<FormElement title="Description">
<TextArea
placeholder="What changed?"
defaultValue=""
width="100%"
cols={2}
disabled={this.state.saving}
onChange={value => this.setState({ commitMessage: value })}
/>
</FormElement>
<RadioButtonGroup
options={this.branchOptions}
defaultOption={this.branchOptions[0]}
onChange={o => this.setState({ branch: o.branch })}
disabled={this.state.saving}
/>
<div className="indent-form" style={{ marginBottom: '-6px' }}>
<FormElement className="customBranch" errorMessage={this.state.branchError}>
<div className="TextInput">
<input
className="TextInput-control"
type="text"
placeholder="my-new-branch"
value={this.state.branch}
onChange={event => this.processBranchName(event)}
disabled={this.state.branch !== this.branchOptions[1].branch || this.state.saving}
/>
</div>
</FormElement>
</div>
</Dialog>
);
}
}
SaveDialog.propTypes = {
branch: React.PropTypes.string,
functions: React.PropTypes.object,
cancel: React.PropTypes.function,
};
@observer
class PipelineLoader extends React.Component {
state = {};
constructor(props) {
super(props);
this.contentApi = new ScmContentApi();
this.state = {
scriptPath: null,
scmSource: null,
credential: null,
sha: null,
};
}
componentWillMount() {
pipelineStore.setPipeline(null); // reset any previous loaded pipeline
this.loadPipeline();
}
componentDidMount() {
this.context.router.setRouteLeaveHook(this.props.route, e => this.routerWillLeave(e));
this.priorUnload = window.onbeforeunload;
window.onbeforeunload = e => this.routerWillLeave(e);
pipelineStore.addListener((this.pipelineUpdated = p => this.checkForModification()));
document.addEventListener(
'keydown',
(this.openPipelineScriptDialog = e => {
if (e.keyCode == 83 && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
this.showPipelineScript();
}
}),
false
);
}
componentWillUnmount() {
window.onbeforeunload = this.priorUnload;
pipelineStore.removeListener(this.pipelineUpdated);
document.removeEventListener('keypress', this.openPipelineScriptDialog);
}
routerWillLeave(e) {
if (this.pipelineIsModified) {
const t = 'There are unsaved changes, discard them?';
if (e) {
e.returnValue = t;
}
return t;
}
}
checkForModification() {
if (!this.lastPipeline) {
this.lastPipeline = JSON.stringify(convertInternalModelToJson(pipelineStore.pipeline));
return;
}
if (!this.pipelineIsModified) {
this.pipelineIsModified = JSON.stringify(convertInternalModelToJson(pipelineStore.pipeline)) !== this.lastPipeline;
}
}
makeEmptyPipeline() {
// maybe show a dialog the user can choose
// empty or template
pipelineStore.setPipeline({
agent: { type: 'any' },
children: [],
});
this.forceUpdate();
}
showLoadingError(
err,
generalMessage = (
<div>
There was an error loading the pipeline from the Jenkinsfile in this repository. Correct the error by editing the Jenkinsfile using the
declarative syntax then commit it back to the repository.
</div>
),
title = 'Error loading Pipeline'
) {
this.showErrorDialog(
<div className="errors">
{generalMessage}
<div> </div>
<div>
<i>{this.extractErrorMessage(err)}</i>
</div>
</div>,
{
buttonRow: (
<button className="btn-primary" onClick={() => this.cancel()}>
Go Back
</button>
),
onClose: () => this.cancel(),
title,
}
);
}
loadPipeline() {
const { pipeline } = this.props.params;
this.opener = locationService.previous;
if (!pipeline) {
this.makeEmptyPipeline();
return; // no pipeline to load
}
this.loadPipelineMetadata().then(() => {
this.loadBranchMetadata();
this.loadContent();
});
}
refreshPipeline(onComplete) {
this.loadContent(onComplete);
}
loadPipelineMetadata() {
const { organization, pipeline } = this.props.params;
const team = pipeline;
this.href = Paths.rest.pipeline(organization, team);
return pipelineService
.fetchPipeline(this.href, { useCache: true })
.then(pipeline => this._savePipelineMetadata(pipeline))
.catch(err => {
this.showErrorDialog(err);
});
}
loadBranchMetadata() {
const { organization, pipeline, branch } = this.props.params;
if (!branch) {
const team = pipeline;
const repo = pipeline;
const { id: scmId, apiUrl } = this.state.scmSource;
const orgRestUrl = UrlBuilder.buildRestUrl(organization);
let repositoryUrl = `${orgRestUrl}scm/${scmId}/organizations/${team}/repositories/${repo}/`;
// the team is wrong and do match the github org used so get the branch by searching
// github repo from the job
if (scmId == 'github') {
repositoryUrl = `${orgRestUrl}scm/github/repository?jobName=${repo}`;
} else if (apiUrl) {
repositoryUrl += `?apiUrl=${apiUrl}`;
}
// in case of error it's ignored and we use hardcoded master
// olamy this could be changed to main which is the new default on github
return Fetch.fetchJSON(repositoryUrl)
.then(({ defaultBranch }) => {
this.defaultBranch = defaultBranch || 'master';
})
.catch(err => (this.defaultBranch = 'master'));
}
this.defaultBranch = branch;
return new Promise(resolve => resolve(null));
}
_savePipelineMetadata(pipeline) {
this.setState({
scriptPath: pipeline.scriptPath,
scmSource: pipeline.scmSource,
});
}
checkForNestedSequentialStages(internalModelPipeline, handler) {
for (let i = 0; i < internalModelPipeline.children.length; i++) {
for (let j = 0; j < internalModelPipeline.children[i].children.length; j++) {
if (typeof internalModelPipeline.children[i].children[j].stages !== 'undefined') {
handler(internalModelPipeline.children[i].children[j].name); //name of stage that contains sequential parallel stages
}
}
}
handler(false);
}
loadContent(onComplete) {
const { organization, pipeline, branch, path = this.state.scriptPath } = this.props.params;
this.contentApi
.loadContent({ organization, pipeline, branch, path })
.then(({ content }) => {
if (!content.base64Data) {
throw { type: LoadError.JENKINSFILE_NOT_FOUND };
}
const pipelineScript = Base64.decode(content.base64Data);
this.setState({ sha: content.sha });
convertPipelineToJson(pipelineScript, (p, err) => {
if (!err) {
const internal = convertJsonToInternalModel(p);
//check if pipeline contains sequential parallel stages and show error if it does
this.checkForNestedSequentialStages(internal, errParentStageName => {
if (errParentStageName) {
const nestedSequentialStagesError = [
{
error: t('editor.page.common.pipeline.nestedSequentialStagesError', { 0: errParentStageName }),
},
];
this.showLoadingError(nestedSequentialStagesError);
}
});
if (onComplete) {
onComplete(internal);
} else {
pipelineStore.setPipeline(internal);
this.forceUpdate();
}
} else {
this.showLoadingError(err);
if (err[0].location) {
// revalidate in case something missed it (e.g. create an empty stage then load/save)
pipelineValidator.validate();
}
}
});
})
.catch(err => {
if (err.type === LoadError.JENKINSFILE_NOT_FOUND) {
if (onComplete) onComplete();
this.makeEmptyPipeline();
} else if (err.type === LoadError.TOKEN_NOT_FOUND || err.type === LoadError.TOKEN_REVOKED) {
// if there already is a dialog, make sure to show it: an invalid credential was selected
this.showCredentialDialog({ loading: !this.state.dialog });
} else {
if (onComplete) onComplete();
this.showLoadingError(err);
}
});
}
overwriteChanges(saveToBranch, commitMessage, errorHandler) {
const currentPipeline = pipelineStore.pipeline;
this.refreshPipeline(() => {
pipelineStore.setPipeline(currentPipeline);
this.save(saveToBranch, commitMessage, errorHandler);
});
}
discardChanges() {
this.refreshPipeline(pipeline => {
pipelineStore.setPipeline(pipeline);
this.setState({ showSaveDialog: false });
});
}
showPipelineScript() {
this.setState({ dialog: <CopyPastePipelineDialog onClose={() => this.closeDialog()} /> });
}
cancel() {
const { organization, pipeline, branch } = this.props.params;
const { router } = this.context;
const location = {};
location.pathname = branch == null ? '/' : UrlBuilder.buildPipelineUrl(organization, pipeline);
location.query = null;
if (this.opener) {
router.goBack();
} else {
router.push(location);
}
}
goToActivity() {
const { organization, pipeline, branch } = this.props.params;
const { router } = this.context;
const location = UrlBuilder.buildPipelineUrl(organization, pipeline);
activityService.removeItem(activityService.pagerKey(organization, pipeline, branch));
router.push(location);
}
closeDialog() {
this.setState({ dialog: null });
}
extractErrorMessage(err) {
let errorMessage = err;
if (err instanceof String || typeof err === 'string') {
errorMessage = err;
} else if (err instanceof Array || typeof err === 'array') {
errorMessage = err.map(e => <div>{this.extractErrorMessage(e.error)}</div>);
} else if (err.responseBody && err.responseBody.message) {
// Github error
errorMessage = err.responseBody.message;
// error: 409.
if (errorMessage.indexOf('error: 409.') >= 0) {
if (this.props.params.branch !== saveRequest.content.branch) {
errorMessage = ['the branch ', <i>{saveRequest.content.branch}</i>, ' already exists'];
} else {
errorMessage = ['the pipeline was modified outside of the editor'];
}
errorMessage = ['An error occurred saving to Github: ', ...errorMessage];
} else if (errorMessage === 'Server Error') {
errorMessage = err.responseBody.message;
} else if (err.response && err.response.status === 500) {
errorMessage = err.responseBody.message;
}
} else if (err.message) {
errorMessage = err.message;
}
return errorMessage;
}
showErrorDialog(err, { saveRequest, buttonRow, onClose, title } = {}) {
const buttons = buttonRow || [
<button className="btn-primary" onClick={() => this.closeDialog()}>
Ok
</button>,
];
this.setState({
showSaveDialog: false,
dialog: (
<Dialog onDismiss={() => (onClose ? onClose() : this.closeDialog())} title={title || 'Error'} className="Dialog--error" buttons={buttons}>
<div style={{ width: '28em' }}>{this.extractErrorMessage(err)}</div>
</Dialog>
),
});
}
showCredentialDialog({ loading = false } = {}) {
const { branch = this.defaultBranch } = this.props.params;
const pipeline = pipelineService.getPipeline(this.href);
const { scmSource } = pipeline;
// if (!scmSource || !scmSource.id || (scmSource.id === 'git' && !isSshRepositoryUrl(scmSource.apiUrl))) {
// this.showLoadingError('', 'Saving Pipelines is unsupported using http/https repositories. Please use SSH instead.', 'No save access');
// return;
// }
// if showing this dialog with a credential, the write test failed
// except for git, where we need to prompt with the user's public key so they can continue
// in the case the user has a credential, like a github token there is only one
// so we don't want to prompt them for another, instead they should contact someone
// to fix the permissions
if (this.state.credential && this.state.credential.id && scmSource.id !== 'git') {
this.showLoadingError('', 'You do not appear to be able to save to this repository', 'No save access');
return;
}
const title = this.getScmTitle(scmSource.id);
const githubConfig = {
scmId: scmSource.id,
apiUrl: scmSource.apiUrl,
};
// hide the dialog until it reports as ready (i.e. credential fetch is done)
const dialogClassName = `dialog-token ${loading ? 'loading' : ''}`;
//FIXME: should show a message about existing credentials failing
this.setState({
dialog: (
<Dialog title={title} className={dialogClassName} buttons={[]} onDismiss={() => this.cancel()}>
<Extensions.Renderer
extensionPoint="jenkins.credentials.selection"
onStatus={status => this.onCredentialStatus(status)}
onComplete={cred => this.onCredentialSelected(cred)}
type={scmSource.id}
githubConfig={githubConfig}
pipeline={{ fullName: pipeline.fullName }}
requirePush
branch={branch}
dialog
existingFailed
/>
</Dialog>
),
});
}
getScmTitle(scmId) {
let scmLabel = '';
if (scmId === 'github') {
scmLabel = 'GitHub';
} else if (scmId === 'github-enterprise') {
scmLabel = 'GitHub Enterprise';
} else if (scmId === 'bitbucket-cloud') {
scmLabel = 'Bitbucket Cloud';
} else if (scmId === 'bitbucket-server') {
scmLabel = 'Bitbucket Server';
} else if (scmId === 'git') {
scmLabel = 'Git';
}
if (scmLabel) {
return `Connect to ${scmLabel}`;
}
return 'Unknown SCM Provider';
}
onCredentialStatus(status) {
if (status === 'promptReady') {
this.showCredentialDialog();
}
}
onCredentialSelected(credential) {
this.setState({
credential,
});
this.loadContent(internal => {
if (internal) {
// may be no pipeline here
pipelineStore.setPipeline(internal);
}
this.setState({ dialog: null });
});
}
showSaveDialog() {
pipelineValidator.validate(err => {
if (!pipelineValidator.hasValidationErrors(pipelineStore.pipeline)) {
this.setState({ showSaveDialog: true });
} else {
this.showErrorDialog('There are validation errors, please check the pipeline.');
}
});
}
save(saveToBranch, commitMessage, errorHandler) {
const { organization, pipeline, branch = this.defaultBranch } = this.props.params;
const pipelineJson = convertInternalModelToJson(pipelineStore.pipeline);
const team = pipeline;
const repo = pipeline;
const saveMessage = commitMessage || (this.state.isSaved ? 'Updated Jenkinsfile' : 'Added Jenkinsfile');
convertJsonToPipeline(JSON.stringify(pipelineJson), (pipelineScript, err) => {
if (!err) {
const saveParams = {
organization,
pipeline: team,
repo,
sourceBranch: branch,
targetBranch: saveToBranch || this.defaultBranch,
sha: this.state.sha,
message: saveMessage,
path: this.state.scriptPath,
content: pipelineScript,
};
// TODO: building the body so we can pass it down is a little awkward
const body = this.contentApi.buildSaveContentRequest(saveParams);
this.contentApi
.saveContent(saveParams)
.then(data => {
this.pipelineIsModified = false;
this.lastPipeline = JSON.stringify(convertInternalModelToJson(pipelineStore.pipeline));
// If this is a save on the same branch that already has a Jenkinsfile, just re-run it
if (this.state.isSaved && branch === data.content.branch) {
RunApi.startRun({ _links: { self: { href: this.href + 'branches/' + encodeURIComponent(branch) + '/' } } })
.then(() => this.goToActivity())
.catch(err => errorHandler(err, body));
} else {
// if a different branch, call indexing so this one gets picked up
// only time we have 'github' is when we are using an org folder
// in which case use the existing saveApi
const { id: scmId, apiUrl } = this.state.scmSource;
saveApi.index(this.href, () => this.goToActivity(), err => errorHandler(err));
}
this.setState({ sha: data.sha, isSaved: true });
})
.catch(err => {
errorHandler(err, body);
});
} else {
errorHandler(err);
}
});
}
render() {
const { pipeline: pipelineName, branch } = this.props.params;
const { pipelineScript } = this.state;
const pipeline = pipelineService.getPipeline(this.href);
const repo = pipelineName;
let title = pipeline ? decodeURIComponent(pipeline.fullDisplayName.replace('/', ' / ')) : pipelineName;
if (branch || repo) {
title += ' / ' + (branch || repo);
}
return (
<div className="pipeline-page">
<Extensions.Renderer extensionPoint="pipeline.editor.css" />
<ContentPageHeader>
<div className="u-flex-grow">
<h1>{pipeline && title}</h1>
</div>
{pipelineStore.pipeline && (
<div className="editor-page-header-controls">
<button className="btn-link inverse" onClick={() => this.cancel()}>
{t('editor.page.common.cancel', { default: 'Cancel' })}
</button>
{pipelineName && (
<button className="btn-primary inverse" onClick={() => this.showSaveDialog()}>
{t('editor.page.common.save', { default: 'Save' })}
</button>
)}
</div>
)}
</ContentPageHeader>
{pipelineStore.pipeline && (
<div className="pipeline-editor">
<EditorMain />
</div>
)}
{this.state.dialog}
{this.state.showSaveDialog && (
<SaveDialog branch={branch || this.defaultBranch} cancel={() => this.setState({ showSaveDialog: false })} functions={this} />
)}
</div>
);
}
}
PipelineLoader.contextTypes = {
router: React.PropTypes.object,
location: React.PropTypes.object,
};
PipelineLoader.propTypes = {
params: React.PropTypes.object,
};
export const EditorPage = PipelineLoader;