/
ProjectListenerInvokingProjectLoader.ts
132 lines (115 loc) · 5.21 KB
/
ProjectListenerInvokingProjectLoader.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* 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 { Success } from "@atomist/automation-client/lib/HandlerResult";
import { GitProject } from "@atomist/automation-client/lib/project/git/GitProject";
import { createSkillContext } from "../../api/context/skillContext";
import { ExecuteGoalResult } from "../../api/goal/ExecuteGoalResult";
import {
GoalInvocation,
GoalProjectListenerEvent,
GoalProjectListenerRegistration,
} from "../../api/goal/GoalInvocation";
import { SdmGoalEvent } from "../../api/goal/SdmGoalEvent";
import { PushListenerInvocation } from "../../api/listener/PushListener";
import { AnyPush } from "../../api/mapping/support/commonPushTests";
import {
ProjectLoader,
ProjectLoadingParameters,
WithLoadedProject,
} from "../../spi/project/ProjectLoader";
import { updateGoal } from "../goal/storeGoals";
import { serializeResult } from "../misc/result";
/**
* ProjectLoader implementation that invokes GoalProjectListener instances on the loaded project.
* Can be used to restore state into a project/workspace area.
*/
export class ProjectListenerInvokingProjectLoader implements ProjectLoader {
constructor(private readonly gi: GoalInvocation,
private readonly listeners: GoalProjectListenerRegistration[]) {
}
public async doWithProject(params: ProjectLoadingParameters, action: WithLoadedProject): Promise<any> {
return this.gi.configuration.sdm.projectLoader.doWithProject(params, async p => {
let result;
try {
// invoke the before listeners
const beforeResult = await this.invokeListeners(p, GoalProjectListenerEvent.before);
updateGoalProperties(this.gi.goalEvent, beforeResult);
if (!!beforeResult && beforeResult.code !== 0) {
return beforeResult;
}
// invoke the wrapped action
result = await action(p);
} catch (err) {
throw err;
} finally {
// invoke the after listeners
const afterResult = await this.invokeListeners(p, GoalProjectListenerEvent.after);
updateGoalProperties(this.gi.goalEvent, afterResult);
if (!!afterResult && afterResult.code !== 0) {
result = afterResult;
}
}
return result;
});
}
private async invokeListeners(p: GitProject,
event: GoalProjectListenerEvent): Promise<ExecuteGoalResult> {
const pli: PushListenerInvocation = {
addressChannels: this.gi.addressChannels,
preferences: this.gi.preferences,
configuration: this.gi.configuration,
context: this.gi.context,
credentials: this.gi.credentials,
id: this.gi.id,
project: p,
push: this.gi.goalEvent.push,
skill: createSkillContext(this.gi.context),
};
for (const lr of this.listeners) {
const pushTest = lr.pushTest || AnyPush;
const events = lr.events || [GoalProjectListenerEvent.before, GoalProjectListenerEvent.after];
if (events.includes(event) && await pushTest.mapping(pli)) {
this.gi.progressLog.write("/--");
this.gi.progressLog.write(`Invoking ${event} project listener: ${lr.name}`);
await updateGoal(
this.gi.context,
this.gi.goalEvent,
{
state: this.gi.goalEvent.state,
phase: lr.name,
description: this.gi.goalEvent.description,
url: this.gi.goalEvent.url,
});
const postResult = await lr.listener(p, this.gi, event);
updateGoalProperties(this.gi.goalEvent, postResult);
this.gi.progressLog.write(`Result: ${postResult ? serializeResult(postResult) : "Success"}`);
this.gi.progressLog.write("\\--");
if (!!postResult && postResult.code !== 0) {
return postResult;
}
}
}
return Success;
}
}
function updateGoalProperties(goalEvent: SdmGoalEvent, result: void | ExecuteGoalResult): void {
if (!!result) {
goalEvent.description = !!result.description ? result.description : goalEvent.description;
goalEvent.data = !!result.data ? result.data : goalEvent.data;
goalEvent.externalUrls = !!result.externalUrls ? result.externalUrls : goalEvent.externalUrls;
goalEvent.phase = !!result.phase ? result.phase : goalEvent.phase;
}
}