/
sdmGoal.ts
84 lines (77 loc) · 2.6 KB
/
sdmGoal.ts
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
/*
* Copyright © 2020 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
Configuration,
configurationValue,
} from "@atomist/automation-client/lib/configuration";
import { sprintf } from "sprintf-js";
import { SdmGoalEvent } from "../../api/goal/SdmGoalEvent";
import { SdmGoalKey } from "../../api/goal/SdmGoalMessage";
export function mapKeyToGoal<T extends SdmGoalKey>(goals: T[]): (k: SdmGoalKey) => T {
return (keyToFind: SdmGoalKey) => {
const found = goals.find(g => goalKeyEquals(g, keyToFind));
return found;
};
}
export function goalKeyEquals(a: SdmGoalKey, b: SdmGoalKey): boolean {
return a.environment === b.environment &&
a.uniqueName === b.uniqueName;
}
export function goalKeyString(gk: SdmGoalKey): string {
return sprintf("%s in %s", gk.uniqueName, gk.environment);
}
/**
* Read and parse goal event data. If the goal event has no data,
* return an empty object. Note: this purposely only works if the
* data field is stringified JSON.
*
* @param sdmGoal
* @return JSON parsed goal event data property
*/
export function goalData(sdmGoal: SdmGoalEvent): any {
if (!sdmGoal?.data) {
return {};
}
let data: any;
try {
data = JSON.parse(sdmGoal.data);
} catch (e) {
e.message = `Failed to parse goal event data for ${sdmGoal.uniqueName} as JSON '${sdmGoal.data}': ${e.message}`;
throw e;
}
return data;
}
/**
* Return a shallow merge the provided `data` and the goal event data
* property, parsed as JSON. Properties in `data` take precedence
* over those in the parsed goal event data object.
*
* @param data
* @param sdmGoal
* @return shallow merge of data and SDM goal event data property
*/
export function mergeGoalData(data: any, sdmGoal: SdmGoalEvent): any {
return {
...goalData(sdmGoal),
...data,
};
}
/**
* Return configured SDM goal timeout or default value, 10 minutes.
*/
export function sdmGoalTimeout(cfg?: Configuration): number {
return cfg?.sdm?.goal?.timeout || configurationValue<number>("sdm.goal.timeout", 1000 * 60 * 10);
}