2017-12-15 21:39:18 +00:00
|
|
|
|
import Component from '@ember/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
|
import { action, computed } from '@ember/object';
|
|
|
|
|
import { classNames } from '@ember-decorators/component';
|
2021-04-20 13:33:16 +00:00
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
|
import messageForError from 'nomad-ui/utils/message-from-adapter-error';
|
2020-06-10 13:49:16 +00:00
|
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
|
|
const changeTypes = ['Added', 'Deleted', 'Edited'];
|
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
|
@classic
|
|
|
|
|
@classNames('job-version', 'boxed-section')
|
|
|
|
|
export default class JobVersion extends Component {
|
|
|
|
|
version = null;
|
|
|
|
|
isOpen = false;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
|
|
// Passes through to the job-diff component
|
2020-06-10 13:49:16 +00:00
|
|
|
|
verbose = true;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
2021-04-20 13:33:16 +00:00
|
|
|
|
@service router;
|
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
|
@computed('version.diff')
|
|
|
|
|
get changeCount() {
|
2017-09-19 14:47:10 +00:00
|
|
|
|
const diff = this.get('version.diff');
|
|
|
|
|
const taskGroups = diff.TaskGroups || [];
|
|
|
|
|
|
|
|
|
|
if (!diff) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
fieldChanges(diff) +
|
|
|
|
|
taskGroups.reduce(arrayOfFieldChanges, 0) +
|
2021-12-28 16:08:12 +00:00
|
|
|
|
(taskGroups.mapBy('Tasks') || [])
|
|
|
|
|
.reduce(flatten, [])
|
|
|
|
|
.reduce(arrayOfFieldChanges, 0)
|
2017-09-19 14:47:10 +00:00
|
|
|
|
);
|
2020-06-10 13:49:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 13:33:16 +00:00
|
|
|
|
@computed('version.{number,job.version}')
|
|
|
|
|
get isCurrent() {
|
|
|
|
|
return this.get('version.number') === this.get('version.job.version');
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
|
@action
|
|
|
|
|
toggleDiff() {
|
|
|
|
|
this.toggleProperty('isOpen');
|
|
|
|
|
}
|
2021-04-20 13:33:16 +00:00
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
|
@task(function* () {
|
2021-04-20 13:33:16 +00:00
|
|
|
|
try {
|
|
|
|
|
const versionBeforeReversion = this.get('version.job.version');
|
|
|
|
|
|
|
|
|
|
yield this.version.revertTo();
|
|
|
|
|
yield this.version.job.reload();
|
|
|
|
|
|
|
|
|
|
const versionAfterReversion = this.get('version.job.version');
|
|
|
|
|
|
|
|
|
|
if (versionBeforeReversion === versionAfterReversion) {
|
|
|
|
|
this.handleError({
|
|
|
|
|
level: 'warn',
|
|
|
|
|
title: 'Reversion Had No Effect',
|
2021-12-28 16:08:12 +00:00
|
|
|
|
description:
|
|
|
|
|
'Reverting to an identical older version doesn’t produce a new version',
|
2021-04-20 13:33:16 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const job = this.get('version.job');
|
|
|
|
|
|
|
|
|
|
this.router.transitionTo('jobs.job', job.get('plainId'), {
|
|
|
|
|
queryParams: { namespace: job.get('namespace.name') },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.handleError({
|
|
|
|
|
level: 'danger',
|
|
|
|
|
title: 'Could Not Revert',
|
|
|
|
|
description: messageForError(e, 'revert'),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
revertTo;
|
2020-06-10 13:49:16 +00:00
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
|
|
const flatten = (accumulator, array) => accumulator.concat(array);
|
2021-12-28 16:08:12 +00:00
|
|
|
|
const countChanges = (total, field) =>
|
|
|
|
|
changeTypes.includes(field.Type) ? total + 1 : total;
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
|
|
function fieldChanges(diff) {
|
|
|
|
|
return (
|
|
|
|
|
(diff.Fields || []).reduce(countChanges, 0) +
|
|
|
|
|
(diff.Objects || []).reduce(arrayOfFieldChanges, 0)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function arrayOfFieldChanges(count, diff) {
|
|
|
|
|
if (!diff) {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count + fieldChanges(diff);
|
|
|
|
|
}
|