UNPKG

7.01 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 */
17var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19 return new (P || (P = Promise))(function (resolve, reject) {
20 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23 step((generator = generator.apply(thisArg, _arguments || [])).next());
24 });
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27const automation_client_1 = require("@atomist/automation-client");
28const sdm_1 = require("@atomist/sdm");
29const AWS = require("aws-sdk");
30const proxy = require("proxy-agent");
31const deleteS3_1 = require("./deleteS3");
32const putS3_1 = require("./putS3");
33/**
34 * Get a goal that will publish (portions of) a project to S3.
35 * If the project needs to be built or otherwise processed first, use
36 * `.withProjectListeners` to get those prerequisite steps done.
37 */
38class PublishToS3 extends sdm_1.GoalWithFulfillment {
39 constructor(options) {
40 super(Object.assign({ workingDescription: "Publishing to S3", completedDescription: "Published to S3" }, options));
41 this.options = options;
42 }
43 /**
44 * Called by the SDM on initialization. This function calls
45 * `super.register` and adds a startup listener to the SDM. The
46 * startup listener registers a default goal fulfillment if there
47 * is none that suppresses logs posted to chat.
48 */
49 register(sdm) {
50 super.register(sdm);
51 sdm.addStartupListener(() => __awaiter(this, void 0, void 0, function* () {
52 if (this.fulfillments.length === 0 && this.callbacks.length === 0) {
53 this.with({
54 name: `publishToS3-${this.options.bucketName}`,
55 goalExecutor: executePublishToS3(this.options),
56 logInterpreter: sdm_1.LogSuppressor,
57 });
58 }
59 }));
60 }
61}
62exports.PublishToS3 = PublishToS3;
63function executePublishToS3(inputParams) {
64 const params = Object.assign({ pathTranslation: p => p, linkLabel: "S3 Website" }, inputParams);
65 return sdm_1.doWithProject((inv) => __awaiter(this, void 0, void 0, function* () {
66 if (!inv.id.sha) {
67 return { code: 99, message: "SHA is not defined. I need that" };
68 }
69 if (inputParams.proxy) {
70 AWS.config.update({
71 // The output of proxy is ultimately an http|https agent, but the typings don't line up unfortunately
72 httpOptions: { agent: new proxy(inputParams.proxy) },
73 });
74 }
75 try {
76 let s3;
77 if (inv.configuration.sdm.aws && inv.configuration.sdm.aws.accessKey && inv.configuration.sdm.aws.secretKey) {
78 const credentials = new AWS.Credentials(inv.configuration.sdm.aws.accessKey, inv.configuration.sdm.aws.secretKey);
79 s3 = new AWS.S3({ credentials });
80 }
81 else {
82 automation_client_1.logger.info(`No AWS keys in SDM configuration, falling back to default credentials`);
83 s3 = new AWS.S3();
84 }
85 const result = yield pushToS3(s3, inv, params);
86 let linkToIndex;
87 if (params.pathToIndex) {
88 linkToIndex = result.bucketUrl + params.pathTranslation(params.pathToIndex, inv);
89 inv.progressLog.write("URL: " + linkToIndex);
90 }
91 inv.progressLog.write(result.warnings.join("\n"));
92 inv.progressLog.write(`${result.fileCount} files uploaded to ${params.bucketName}`);
93 inv.progressLog.write(`${result.deleted} objects deleted from ${params.bucketName}`);
94 if (result.warnings.length > 0) {
95 yield inv.addressChannels(formatWarningMessage(linkToIndex, result.warnings, inv.id, inv.context));
96 if (result.fileCount === 0) {
97 return {
98 code: 1,
99 message: `0 files uploaded. ${result.warnings.length} warnings, including: ${result.warnings[0]}`,
100 };
101 }
102 }
103 return {
104 code: 0,
105 externalUrls: (linkToIndex) ? [{ label: params.linkLabel, url: linkToIndex }] : undefined,
106 };
107 }
108 catch (e) {
109 return { code: 98, message: e.message };
110 }
111 }), { readOnly: true });
112}
113exports.executePublishToS3 = executePublishToS3;
114function formatWarningMessage(url, warnings, id, ctx) {
115 return sdm_1.slackWarningMessage("Some files were not uploaded to S3", warnings.join("\n"), ctx, {
116 author_name: `published files from ${id.owner}/${id.repo}#${id.sha.substr(0, 7)}`,
117 author_link: url,
118 });
119}
120/**
121 * Push files in project to S3 according to options provided by
122 * `params`.
123 *
124 * @param s3 S3 client
125 * @param inv goal invocation with project to upload
126 * @param params options for upload
127 * @return information on upload success and warnings
128 */
129function pushToS3(s3, inv, params) {
130 return __awaiter(this, void 0, void 0, function* () {
131 const { bucketName, region } = params;
132 const project = inv.project;
133 const log = inv.progressLog;
134 const [fileCount, keysToKeep, warningsFromPut] = yield putS3_1.putFiles(project, inv, s3, params);
135 let deleted = 0;
136 let moreWarnings = [];
137 if (params.sync) {
138 const [keysToDelete, warningsFromGatheringFilesToDelete] = yield deleteS3_1.gatherKeysToDelete(s3, log, keysToKeep, params);
139 const [deletedCount, warningsFromDeletions] = yield deleteS3_1.deleteKeys(s3, log, params, keysToDelete);
140 deleted = deletedCount;
141 moreWarnings = [...warningsFromGatheringFilesToDelete, ...warningsFromDeletions];
142 }
143 return {
144 bucketUrl: `http://${bucketName}.s3-website.${region}.amazonaws.com/`,
145 warnings: [...warningsFromPut, ...moreWarnings],
146 fileCount,
147 deleted,
148 };
149 });
150}
151exports.pushToS3 = pushToS3;
152//# sourceMappingURL=publishToS3.js.map
\No newline at end of file