1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const fs = require("fs-extra");
|
19 | const graphql_1 = require("./graphql");
|
20 | const http_1 = require("./http");
|
21 | const log_1 = require("./log");
|
22 | const message_1 = require("./message");
|
23 | const parameter_prompt_1 = require("./parameter_prompt");
|
24 | const payload_1 = require("./payload");
|
25 | const project_1 = require("./project");
|
26 | const clone_1 = require("./project/clone");
|
27 | const secrets_1 = require("./secrets");
|
28 | const storage_1 = require("./storage");
|
29 | const util_1 = require("./util");
|
30 | function createContext(payload, ctx) {
|
31 | var _a, _b;
|
32 | const apiKey = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.secrets) === null || _a === void 0 ? void 0 : _a.find(s => s.uri === "atomist://api-key")) === null || _b === void 0 ? void 0 : _b.value;
|
33 | const wid = payload_1.workspaceId(payload);
|
34 | const graphql = graphql_1.createGraphQLClient(apiKey, wid);
|
35 | const storage = storage_1.createStorageProvider();
|
36 | const credential = new secrets_1.DefaultCredentialProvider(graphql, payload);
|
37 | if (payload_1.isCommandIncoming(payload)) {
|
38 | if (payload.raw_message) {
|
39 | const parameters = util_1.extractParameters(payload.raw_message);
|
40 | payload.parameters.push(...parameters);
|
41 | }
|
42 | const message = new message_1.PubSubCommandMessageClient(payload, graphql);
|
43 | return Object.assign(Object.assign({ parameters: {
|
44 | prompt: parameter_prompt_1.commandRequestParameterPromptFactory(message, payload),
|
45 | }, name: payload.command, correlationId: payload.correlation_id, executionId: ctx.eventId, workspaceId: wid, credential,
|
46 | graphql, http: http_1.createHttpClient(), audit: log_1.wrapAuditLogger({
|
47 | eventId: ctx.eventId,
|
48 | correlationId: payload.correlation_id,
|
49 | workspaceId: wid,
|
50 | }, {
|
51 | name: payload.command,
|
52 | }), storage,
|
53 | message, project: project_1.createProjectLoader(), trigger: payload }, extractConfiguration(payload)), { skill: payload.skill, close });
|
54 | }
|
55 | else if (payload_1.isEventIncoming(payload)) {
|
56 | return Object.assign(Object.assign({ data: payload.data, name: payload.extensions.operationName, correlationId: payload.extensions.correlation_id, executionId: ctx.eventId, workspaceId: wid, credential,
|
57 | graphql, http: http_1.createHttpClient(), audit: log_1.wrapAuditLogger({
|
58 | eventId: ctx.eventId,
|
59 | correlationId: payload.extensions.correlation_id,
|
60 | workspaceId: wid,
|
61 | }, {
|
62 | name: payload.extensions.operationName,
|
63 | }), storage, message: new message_1.PubSubEventMessageClient(payload, graphql), project: project_1.createProjectLoader(), trigger: payload }, extractConfiguration(payload)), { skill: payload.skill, close });
|
64 | }
|
65 | return undefined;
|
66 | }
|
67 | exports.createContext = createContext;
|
68 | function extractConfiguration(payload) {
|
69 | var _a, _b, _c;
|
70 | return {
|
71 | configuration: (_c = (_b = (_a = payload.skill) === null || _a === void 0 ? void 0 : _a.configuration) === null || _b === void 0 ? void 0 : _b.instances) === null || _c === void 0 ? void 0 : _c.map(c => ({
|
72 | name: c.name,
|
73 | parameters: extractConfigurationParameters(c.parameters),
|
74 | resourceProviders: extractConfigurationResourceProviders(c.resourceProviders),
|
75 | url: `https://go.atomist.${(process.env.ATOMIST_GRAPHQL_ENDPOINT || "").includes("staging")
|
76 | ? "services" : "com"}/manage/${payload_1.workspaceId(payload)}/skills/configure/${payload.skill.id}/${encodeURIComponent(c.name)}`,
|
77 | })),
|
78 | };
|
79 | }
|
80 | function extractConfigurationParameters(params) {
|
81 | const parameters = {};
|
82 | params === null || params === void 0 ? void 0 : params.forEach(p => parameters[p.name] = p.value);
|
83 | return parameters;
|
84 | }
|
85 | function extractConfigurationResourceProviders(params) {
|
86 | const resourceProviders = {};
|
87 | params === null || params === void 0 ? void 0 : params.forEach(p => resourceProviders[p.name] = { typeName: p.typeName, selectedResourceProviders: p.selectedResourceProviders });
|
88 | return resourceProviders;
|
89 | }
|
90 | async function close() {
|
91 |
|
92 | await fs.remove(clone_1.ClonePath);
|
93 | }
|
94 |
|
\ | No newline at end of file |