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 | exports.branchAwareCodeTransform = void 0;
|
19 | const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
|
20 | const editModes_1 = require("@atomist/automation-client/lib/operations/edit/editModes");
|
21 | const GraphClient_1 = require("@atomist/automation-client/lib/spi/graph/GraphClient");
|
22 | const constructionUtils_1 = require("@atomist/automation-client/lib/util/constructionUtils");
|
23 | const logger_1 = require("@atomist/automation-client/lib/util/logger");
|
24 | const slack_messages_1 = require("@atomist/slack-messages");
|
25 | const _ = require("lodash");
|
26 | const LoggingProgressLog_1 = require("../../log/LoggingProgressLog");
|
27 | const handlerRegistrations_1 = require("../../machine/handlerRegistrations");
|
28 | const toMachineOptions_1 = require("../../machine/toMachineOptions");
|
29 | const messages_1 = require("../../misc/slack/messages");
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function branchAwareCodeTransform(codeTransformRegistration, sdm) {
|
40 | return Object.assign(Object.assign({}, codeTransformRegistration), { listener: handleBranchAwareCodeTransform(codeTransformRegistration, sdm) });
|
41 | }
|
42 | exports.branchAwareCodeTransform = branchAwareCodeTransform;
|
43 | function 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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
\ | No newline at end of file |