1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { logger } from "@atomist/automation-client";
|
18 | import {
|
19 | ExtensionPack,
|
20 | metadata,
|
21 | SdmGoalState,
|
22 | } from "@atomist/sdm";
|
23 | import * as cluster from "cluster";
|
24 | import * as _ from "lodash";
|
25 | import { isInLocalMode } from "../../internal/machine/modes";
|
26 | import {
|
27 | cancelGoalSetsCommand,
|
28 | listPendingGoalSetsCommand,
|
29 | } from "./cancelGoals";
|
30 | import { manageGoalSetsTrigger } from "./manageGoalSets";
|
31 | import { resetGoalsCommand } from "./resetGoals";
|
32 | import { setGoalStateCommand } from "./setGoalState";
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | export interface GoalStateOptions {
|
38 |
|
39 | cancellation?: {
|
40 |
|
41 | enabled?: boolean;
|
42 | |
43 |
|
44 |
|
45 |
|
46 | timeout?: number;
|
47 | |
48 |
|
49 |
|
50 |
|
51 | state?: SdmGoalState.canceled | SdmGoalState.failure;
|
52 | };
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export function goalStateSupport(options?: GoalStateOptions): ExtensionPack {
|
59 | return {
|
60 | ...metadata("goal-state"),
|
61 | configure: sdm => {
|
62 | if (isInLocalMode()) {
|
63 | logger.warn("Setting goal state is not available in local mode.");
|
64 | logger.warn("Resetting goals does not work in local mode. Use `atomist trigger post-commit` instead.");
|
65 | } else {
|
66 | sdm.addCommand(setGoalStateCommand(sdm));
|
67 | sdm.addCommand(resetGoalsCommand(sdm));
|
68 | sdm.addCommand(cancelGoalSetsCommand(sdm));
|
69 | sdm.addCommand(listPendingGoalSetsCommand(sdm));
|
70 |
|
71 | if ((cluster.isMaster || !_.get(sdm.configuration, "cluster.enabled")) &&
|
72 | !process.env.ATOMIST_ISOLATED_GOAL &&
|
73 | !!options && !!options.cancellation && !!options.cancellation.enabled) {
|
74 | logger.debug(`Timeout based goal cancellation enabled for this SDM`);
|
75 | sdm.addTriggeredListener({
|
76 | trigger: { interval: 1000 * 30 },
|
77 | listener: manageGoalSetsTrigger(options.cancellation),
|
78 | });
|
79 | }
|
80 | }
|
81 | },
|
82 | };
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | export function goalState(): ExtensionPack {
|
89 | return goalStateSupport();
|
90 | }
|