UNPKG

5.06 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.branchAwareCodeTransform = void 0;
19const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
20const editModes_1 = require("@atomist/automation-client/lib/operations/edit/editModes");
21const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient");
22const constructionUtils_1 = require("@atomist/automation-client/lib/util/constructionUtils");
23const logger_1 = require("@atomist/automation-client/lib/util/logger");
24const slack_messages_1 = require("@atomist/slack-messages");
25const _ = require("lodash");
26const LoggingProgressLog_1 = require("../../log/LoggingProgressLog");
27const handlerRegistrations_1 = require("../../machine/handlerRegistrations");
28const toMachineOptions_1 = require("../../machine/toMachineOptions");
29const messages_1 = require("../../misc/slack/messages");
30/**
31 * Wrap a CodeTransform to determine the target branch of the transform.
32 *
33 * If the target branch, as expressed by the CodeTransformRegistration.transformPresentation,
34 * already exists, the wrapped CodeTransform will run against that branch instead of the
35 * requested branch.
36 * @param codeTransformRegistration
37 * @param sdm
38 */
39function branchAwareCodeTransform(codeTransformRegistration, sdm) {
40 return Object.assign(Object.assign({}, codeTransformRegistration), { listener: handleBranchAwareCodeTransform(codeTransformRegistration, sdm) });
41}
42exports.branchAwareCodeTransform = branchAwareCodeTransform;
43function handleBranchAwareCodeTransform(codeTransformRegistration, sdm) {
44 const codeTransform = constructionUtils_1.toFactory(handlerRegistrations_1.codeTransformRegistrationToCommand(toMachineOptions_1.toMachineOptions(sdm), codeTransformRegistration))();
45 return async (ci) => {
46 const target = ci.parameters.targets;
47 if (!target || !target.repoRef || !target.repoRef.owner || !target.repoRef.repo) {
48 await ci.context.messageClient.respond(messages_1.slackErrorMessage("Code Transform", `Can only invoke Code Transform ${slack_messages_1.italic(ci.commandName)} against a single repository. ` +
49 `Please specify ${slack_messages_1.codeLine("targets.owner")} and ${slack_messages_1.codeLine("targets.repo")} parameters.`, ci.context));
50 return HandlerResult_1.Success;
51 }
52 const id = ci.ids[0];
53 return sdm.configuration.sdm.projectLoader.doWithProject({
54 credentials: ci.credentials,
55 id,
56 readOnly: true,
57 context: ci.context,
58 cloneOptions: {
59 alwaysDeep: false,
60 },
61 }, async (p) => {
62 // Obtain the transform presentation to retrieve the target branch
63 let branch = id.branch;
64 if (codeTransformRegistration.transformPresentation) {
65 const editMode = codeTransformRegistration.transformPresentation(Object.assign(Object.assign({}, ci), { progressLog: new LoggingProgressLog_1.LoggingProgressLog(codeTransformRegistration.name, "debug") }), p);
66 if (editModes_1.isBranchCommit(editMode)) {
67 branch = editMode.branch;
68 }
69 }
70 // Check if there is already a branch named
71 const result = await ci.context.graphClient.query({
72 name: "BranchForName",
73 variables: {
74 branch,
75 owner: id.owner,
76 repo: id.repo,
77 },
78 options: GraphClient_1.QueryNoCacheOptions,
79 });
80 if (!!_.get(result, "Branch[0].id")) {
81 // Branch exists; run the code transform on that branch instead
82 logger_1.logger.debug(`Target branch '${branch}' already exists. Redirecting code transform to run against that branch`);
83 const newParams = Object.assign({}, ci.parameters);
84 newParams.targets.branch = branch;
85 return codeTransform.handle(ci.context, newParams);
86 }
87 else {
88 // Branch doesn't exist yet; run code transform as is
89 logger_1.logger.debug(`Target branch '${branch}' does not exist. Applying code transform on requested branch`);
90 return codeTransform.handle(ci.context, ci.parameters);
91 }
92 });
93 };
94}
95//# sourceMappingURL=codeTransformWrapping.js.map
\No newline at end of file