-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
title.js
151 lines (136 loc) · 4.1 KB
/
title.js
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
// @ts-check
import Component from '@ember/component';
import { task } from 'ember-concurrency';
import { inject as service } from '@ember/service';
import messageFromAdapterError from 'nomad-ui/utils/message-from-adapter-error';
import { tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
import jsonToHcl from 'nomad-ui/utils/json-to-hcl';
import { marked } from 'marked';
import { htmlSafe } from '@ember/template';
import DOMPurify from 'dompurify';
@classic
@tagName('')
export default class Title extends Component {
@service router;
@service notifications;
job = null;
title = null;
handleError() {}
/**
* @param {boolean} withNotifications - Whether to show a toast on success, as when triggered by keyboard shortcut
*/
@task(function* (withNotifications = false) {
try {
const job = this.job;
yield job.stop();
// Eagerly update the job status to avoid flickering
job.set('status', 'dead');
if (withNotifications) {
this.notifications.add({
title: 'Job Stopped',
message: `${job.name} has been stopped`,
color: 'success',
});
}
} catch (err) {
this.handleError({
title: 'Could Not Stop Job',
description: messageFromAdapterError(err, 'stop jobs'),
});
}
})
stopJob;
@task(function* () {
try {
const job = this.job;
yield job.purge();
this.notifications.add({
title: 'Job Purged',
message: `You have purged ${job.name}`,
color: 'success',
});
this.router.transitionTo('jobs');
} catch (err) {
this.handleError({
title: 'Error purging job',
description: messageFromAdapterError(err, 'purge jobs'),
});
}
})
purgeJob;
/**
* @param {boolean} withNotifications - Whether to show a toast on success, as when triggered by keyboard shortcut
*/
@task(function* (withNotifications = false) {
const job = this.job;
// Try to get the submission/hcl sourced specification first.
// In the event that this fails, fall back to the raw definition.
try {
const specification = yield job.fetchRawSpecification();
let _newDefinitionVariables = job.get('_newDefinitionVariables') || '';
if (specification.VariableFlags) {
_newDefinitionVariables += jsonToHcl(specification.VariableFlags);
}
if (specification.Variables) {
_newDefinitionVariables += specification.Variables;
}
job.set('_newDefinitionVariables', _newDefinitionVariables);
job.set('_newDefinition', specification.Source);
} catch {
const definition = yield job.fetchRawDefinition();
delete definition.Stop;
job.set('_newDefinition', JSON.stringify(definition));
}
try {
yield job.parse();
yield job.update();
// Eagerly update the job status to avoid flickering
job.set('status', 'running');
if (withNotifications) {
this.notifications.add({
title: 'Job Started',
message: `${job.name} has started`,
color: 'success',
});
}
} catch (err) {
this.handleError({
title: 'Could Not Start Job',
description: messageFromAdapterError(err, 'start jobs'),
});
}
})
startJob;
get description() {
if (!this.job.ui?.Description) {
return null;
}
// Put <br /> on newlines, use github-flavoured-markdown.
marked.use({
gfm: true,
breaks: true,
});
const purifyConfig = {
FORBID_TAGS: ['script', 'style'],
FORBID_ATTR: ['onerror', 'onload'],
};
const rawDescription = marked.parse(this.job.ui.Description);
if (typeof rawDescription !== 'string') {
console.error(
'Expected a string from marked.parse(), received:',
typeof rawDescription
);
return null;
}
const cleanDescription = DOMPurify.sanitize(rawDescription, purifyConfig);
return htmlSafe(cleanDescription);
}
get links() {
return this.job.ui?.Links;
}
}