Skip to content
Permalink
Browse files

Add mergeOptions method

[changelog:added]
  • Loading branch information...
cdupuis committed Jan 11, 2019
1 parent 8b27e81 commit 939c04cf28114c42808712d3246645d0ab4bb96f
Showing with 47 additions and 5 deletions.
  1. +29 −4 lib/api/goal/GoalWithFulfillment.ts
  2. +18 −1 test/api/goal/GoalWithFulfillment.test.ts
@@ -14,6 +14,8 @@
* limitations under the License.
*/

import { configurationValue } from "@atomist/automation-client";
import * as _ from "lodash";
import { LogSuppressor } from "../../api-helper/log/logInterpreters";
import { AbstractSoftwareDeliveryMachine } from "../../api-helper/machine/AbstractSoftwareDeliveryMachine";
import { InterpretLog } from "../../spi/log/InterpretedLog";
@@ -169,10 +171,10 @@ export abstract class FulfillableGoal extends GoalWithPrecondition implements Re

public withExecutionListener(listener: GoalExecutionListener): this {
const wrappedListener = async gi => {
if (gi.goalEvent.uniqueName === this.uniqueName) {
return listener(gi);
}
};
if (gi.goalEvent.uniqueName === this.uniqueName) {
return listener(gi);
}
};
if (this.sdm) {
this.sdm.addGoalExecutionListener(wrappedListener);
}
@@ -312,6 +314,12 @@ export function goal(details: FulfillableGoalDetails = {},
return g;
}

/**
* Construct a PredicatedGoalDefinition from the provided goalDetails
* @param goalDetails
* @param uniqueName
* @param definition
*/
// tslint:disable:cyclomatic-complexity
export function getGoalDefinitionFrom(goalDetails: FulfillableGoalDetails | string,
uniqueName: string,
@@ -352,6 +360,23 @@ export function getGoalDefinitionFrom(goalDetails: FulfillableGoalDetails | stri
}
}

/**
* Merge Goal configuration options into a final options object.
* Starts off by merging the explicitly provided options over the provided defaults; finally merges the configuration
* values at the given configuration path (prefixed with sdm.) over the previous merge.
* @param defaults
* @param explicit
* @param configurationPath
*/
export function mergeOptions<OPTIONS>(defaults: OPTIONS, explicit: OPTIONS, configurationPath?: string): OPTIONS {
const options: OPTIONS = _.merge(defaults, explicit || {});
if (!!configurationPath) {
const configurationOptions = configurationValue<OPTIONS>(`sdm.${configurationPath}`, {} as any);
return _.merge(options, configurationOptions);
}
return options;
}

function getEnvironment(details?: { environment?: string | GoalEnvironment }): GoalEnvironment {
if (details && details.environment && typeof details.environment === "string") {
switch (details.environment) {
@@ -15,7 +15,10 @@
*/

import * as assert from "power-assert";
import { goal } from "../../../lib/api/goal/GoalWithFulfillment";
import {
goal,
mergeOptions,
} from "../../../lib/api/goal/GoalWithFulfillment";
import { IndependentOfEnvironment } from "../../../lib/api/goal/support/environment";

describe("GoalWithFulfillment", () => {
@@ -31,4 +34,18 @@ describe("GoalWithFulfillment", () => {

});

describe("mergeOptions", () => {

it("should merge defaults with explicit values", () => {
const opts = mergeOptions({ foo: { bar: 1 } }, { foo: { bar: 2, bla: 3 } });
assert.deepStrictEqual(opts, { foo: { bar: 2, bla: 3 } });
});

it("should merge defaults with explicit values with partial overwrite", () => {
const opts = mergeOptions({ foo: { bar: 1 } }, { foo: { bla: 3 } });
assert.deepStrictEqual(opts, { foo: { bar: 1, bla: 3 } });
});

});

});

0 comments on commit 939c04c

Please sign in to comment.
You can’t perform that action at this time.