1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | const automation_client_1 = require("@atomist/automation-client");
|
27 | const sdm_1 = require("@atomist/sdm");
|
28 | const aws_sdk_1 = require("aws-sdk");
|
29 | const deleteS3_1 = require("./deleteS3");
|
30 | const putS3_1 = require("./putS3");
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | class 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 |
|
43 |
|
44 |
|
45 |
|
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 | }
|
60 | exports.PublishToS3 = PublishToS3;
|
61 | function 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 | }
|
105 | exports.executePublishToS3 = executePublishToS3;
|
106 | function 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 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function 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 | }
|
143 | exports.pushToS3 = pushToS3;
|
144 |
|
\ | No newline at end of file |