UNPKG

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