/
PushRule.ts
91 lines (78 loc) · 2.74 KB
/
PushRule.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
85
86
87
88
89
90
91
/*
* Copyright © 2019 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 { logger } from "@atomist/automation-client/lib/util/logger";
import { PushListenerInvocation } from "../../listener/PushListener";
import { Predicated } from "../PredicateMapping";
import { PushMapping } from "../PushMapping";
import { PushTest } from "../PushTest";
import { allSatisfied } from "./pushTestUtils";
import { StaticPushMapping } from "./StaticPushMapping";
/**
* Generic DSL support for returning an object on a push
*/
export class PushRule<V = any> implements StaticPushMapping<V>, Predicated<PushListenerInvocation> {
private staticValue: V;
get value(): V {
return this.staticValue;
}
public get name(): string {
return this.reason;
}
/**
* The test for this rule
*/
public readonly pushTest: PushTest;
get test(): PushTest {
return this.pushTest;
}
private reason: string;
constructor(protected guard1: PushTest, protected guards: PushTest[], reason?: string) {
this.pushTest = allSatisfied(guard1, ...guards);
this.reason = reason || this.pushTest.name;
}
/**
* Set an additional reason if we want to add information to that which is
* available from the push tests themselves
* @param {string} reason
* @return {this}
*/
public itMeans(reason: string): this {
this.reason = reason;
return this;
}
/**
* Set the value that will be resolved from this rule
* @param {V} value
* @return {this}
*/
public set(value: V): this {
this.staticValue = value;
return this;
}
public async mapping(p: PushListenerInvocation): Promise<V | undefined> {
if (await this.pushTest.mapping(p)) {
logger.debug(`Push test passed: ${this.pushTest.name}`);
return this.staticValue;
} else {
logger.debug(`Push test failed: ${this.pushTest.name}`);
return undefined;
}
}
}
export function isPredicatedStaticValue<T>(pushMapping: PushMapping<T>): pushMapping is StaticPushMapping<T> & Predicated<PushListenerInvocation> {
const maybe = pushMapping as PushRule;
return maybe.test && maybe.value;
}