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 | 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 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | const automation_client_1 = require("@atomist/automation-client");
|
28 | const sdm_1 = require("@atomist/sdm");
|
29 | const AWS = require("aws-sdk");
|
30 | const proxy = require("proxy-agent");
|
31 | const deleteS3_1 = require("./deleteS3");
|
32 | const putS3_1 = require("./putS3");
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | class 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 |
|
45 |
|
46 |
|
47 |
|
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 | }
|
62 | exports.PublishToS3 = PublishToS3;
|
63 | function 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 |
|
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 | }
|
113 | exports.executePublishToS3 = executePublishToS3;
|
114 | function 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 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function 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 | }
|
151 | exports.pushToS3 = pushToS3;
|
152 |
|
\ | No newline at end of file |