Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): render pipeline stages without UIs
- Loading branch information
1 parent
90f01bc
commit a2e784c
Showing
8 changed files
with
214 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...pipeline/config/stages/unmatchedStageTypeStage/unmatchedStageTypeStage.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {mock} from 'angular'; | ||
|
||
import {UNMATCHED_STAGE_TYPE_STAGE_CTRL, UnmatchedStageTypeStageCtrl} from './unmatchedStageTypeStage.controller'; | ||
import {JsonUtilityService} from 'core/utils/json/json.utility.service'; | ||
|
||
describe('Controller: UnmatchedStageTypeStageCtrl', () => { | ||
let ctrl: UnmatchedStageTypeStageCtrl; | ||
|
||
beforeEach( | ||
mock.module( | ||
UNMATCHED_STAGE_TYPE_STAGE_CTRL, | ||
) | ||
); | ||
|
||
beforeEach(() => { | ||
mock.inject(($controller: ng.IControllerService, $rootScope: ng.IRootScopeService, _jsonUtilityService_: JsonUtilityService) => { | ||
ctrl = $controller('UnmatchedStageTypeStageCtrl', { | ||
$scope: $rootScope.$new(), | ||
jsonUtilityService: _jsonUtilityService_ | ||
}) as UnmatchedStageTypeStageCtrl; | ||
ctrl.$onInit(); | ||
}); | ||
}); | ||
|
||
describe('Stage validation', () => { | ||
it('throws an error if the given JSON encoded string is not valid JSON', () => { | ||
ctrl.stageJson = `{ | ||
"type": "upsertLoadBalancer", | ||
"comma-dangle": true, | ||
}`; | ||
ctrl.updateStage(); | ||
expect(ctrl.errorMessage).toBeDefined(); | ||
}); | ||
|
||
it('throws an error if the given JSON encoded string does not include a `type` property.', () => { | ||
ctrl.stageJson = '{}'; | ||
ctrl.updateStage(); | ||
expect(ctrl.errorMessage).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('Stage key removal', () => { | ||
it('omits stage properties from JSON encoded string', () => { | ||
ctrl.$scope.stage = { | ||
refId: '1', | ||
requisiteStageRefIds: [], | ||
failPipeline: true, | ||
}; | ||
ctrl.setStageJson(); | ||
expect(ctrl.stageJson).toEqual('{}'); | ||
}); | ||
|
||
it('maintains the omitted stage properties when updating with new values from JSON encoded string', () => { | ||
ctrl.$scope.stage = { | ||
refId: '1', | ||
requisiteStageRefIds: [], | ||
failPipeline: true, | ||
}; | ||
ctrl.stageJson = `{"type": "upsertLoadBalancer"}`; | ||
ctrl.updateStage(); | ||
expect(ctrl.$scope.stage).toEqual({ | ||
refId: '1', | ||
requisiteStageRefIds: [], | ||
failPipeline: true, | ||
type: 'upsertLoadBalancer', | ||
}); | ||
}); | ||
|
||
it('overrides the omitted stage properties with properties from the JSON encoded string', () => { | ||
ctrl.$scope.stage = { | ||
failPipeline: true, | ||
}; | ||
ctrl.stageJson = `{"failPipeline": false, "type": "upsertLoadBalancer"}`; | ||
ctrl.updateStage(); | ||
expect(ctrl.$scope.stage).toEqual({ | ||
failPipeline: false, | ||
type: 'upsertLoadBalancer', | ||
}); | ||
}); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
...core/pipeline/config/stages/unmatchedStageTypeStage/unmatchedStageTypeStage.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {module, IComponentController, IScope, isDefined} from 'angular'; | ||
import {cloneDeep, isEqual} from 'lodash'; | ||
import {JSON_UTILITY_SERVICE, JsonUtilityService} from 'core/utils/json/json.utility.service'; | ||
import {IStage} from 'core/domain/IStage'; | ||
|
||
export class UnmatchedStageTypeStageCtrl implements IComponentController { | ||
public stageJson: string; | ||
public errorMessage: string; | ||
public textareaRows: number; | ||
// These values are editable with standard UI controls. | ||
private keysToHide = new Set<string>(['refId', 'requisiteStageRefIds', 'failPipeline', 'continuePipeline', | ||
'completeOtherBranchesThenFail', 'restrictExecutionDuringTimeWindow', | ||
'restrictedExecutionWindow', 'stageEnabled', 'sendNotifications', | ||
'notifications', 'comments', 'name']); | ||
|
||
static get $inject() { return ['$scope', 'jsonUtilityService']; } | ||
|
||
constructor(public $scope: IScope, private jsonUtilityService: JsonUtilityService) { } | ||
|
||
public $onInit(): void { | ||
this.stageJson = this.jsonUtilityService.makeSortedStringFromObject(this.makeCleanStageCopy(this.$scope.stage || {})); | ||
this.textareaRows = this.stageJson.split('\n').length; | ||
} | ||
|
||
public updateStage(): void { | ||
let parsedStage: IStage; | ||
this.errorMessage = null; | ||
|
||
try { | ||
parsedStage = JSON.parse(this.stageJson); | ||
} catch (e) { | ||
this.errorMessage = e.message; | ||
} | ||
if (parsedStage && !parsedStage.type) { | ||
this.errorMessage = 'Cannot delete property <em>type</em>.'; | ||
} | ||
|
||
if (!this.errorMessage) { | ||
Object.keys(this.$scope.stage).forEach(key => { | ||
if (!this.keysToHide.has(key)) { | ||
delete this.$scope.stage[key]; | ||
} | ||
}); | ||
Object.assign(this.$scope.stage, parsedStage); | ||
this.setStageJson(); | ||
} | ||
} | ||
|
||
public setStageJson(): void { | ||
const stageCopy = this.makeCleanStageCopy(this.$scope.stage); | ||
// If there are no property differences between the JSON string and the stage object, don't bother updating - | ||
// we might end up cutting out whitespace unexpectedly. | ||
if (!isEqual(stageCopy, JSON.parse(this.stageJson || '{}'))) { | ||
this.stageJson = this.jsonUtilityService.makeStringFromObject(stageCopy); | ||
} | ||
} | ||
|
||
private makeCleanStageCopy(stage: IStage): IStage { | ||
const stageCopy = cloneDeep(stage); | ||
this.keysToHide.forEach(key => { | ||
if (isDefined(stageCopy[key])) { | ||
delete stageCopy[key]; | ||
} | ||
}); | ||
return stageCopy; | ||
} | ||
} | ||
|
||
export const UNMATCHED_STAGE_TYPE_STAGE_CTRL = 'spinnaker.core.pipeline.stage.unmatchedStageTypeStage.controller'; | ||
module(UNMATCHED_STAGE_TYPE_STAGE_CTRL, [JSON_UTILITY_SERVICE]) | ||
.controller('UnmatchedStageTypeStageCtrl', UnmatchedStageTypeStageCtrl); |
20 changes: 20 additions & 0 deletions
20
.../modules/core/pipeline/config/stages/unmatchedStageTypeStage/unmatchedStageTypeStage.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<div ng-controller="UnmatchedStageTypeStageCtrl as $ctrl"> | ||
<form name="form" class="form-horizontal flex-fill"> | ||
<div class="flex-fill"> | ||
<textarea class="code form-control flex-fill" | ||
ng-model="$ctrl.stageJson" | ||
spellcheck="false" | ||
ng-blur="$ctrl.updateStage()" | ||
ng-change="$ctrl.updateStage()" | ||
ng-model-options="{'debounce': 500}" | ||
rows="{{::$ctrl.textareaRows}}"></textarea> | ||
</div> | ||
</form> | ||
|
||
<div class="form-group row" style="margin-top: 10px;"> | ||
<div class="col-md-9 col-md-offset-3 error-message slide-in" ng-if="$ctrl.errorMessage"> | ||
Error: <span ng-bind-html="$ctrl.errorMessage"></span> | ||
</div> | ||
</div> | ||
</div> | ||
|
15 changes: 15 additions & 0 deletions
15
...ts/modules/core/pipeline/config/stages/unmatchedStageTypeStage/unmatchedStageTypeStage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {module} from 'angular'; | ||
import {UNMATCHED_STAGE_TYPE_STAGE_CTRL} from './unmatchedStageTypeStage.controller'; | ||
|
||
export const UNMATCHED_STAGE_TYPE_STAGE = 'spinnaker.core.pipeline.stage.unmatchedStageType'; | ||
module(UNMATCHED_STAGE_TYPE_STAGE, [ | ||
require('core/pipeline/config/pipelineConfigProvider.js'), | ||
UNMATCHED_STAGE_TYPE_STAGE_CTRL, | ||
]).config((pipelineConfigProvider: any) => { | ||
pipelineConfigProvider.registerStage({ | ||
key: 'unmatched', | ||
synthetic: true, | ||
templateUrl: require('./unmatchedStageTypeStage.html'), | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters