-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
230 lines (203 loc) · 5.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
const AWS = require("aws-sdk"),
fs = require("fs"),
path = require('path'),
url = require('url');
class BtrzPactS3 {
constructor(options, logger) {
if (!options) {
throw new Error("BtrzPactS3 options are mandatory.")
}
if (!options.accessKeyId) {
throw new Error("BtrzPactS3 options.accessKeyId is mandatory.")
}
if (!options.secretAccessKey) {
throw new Error("BtrzPactS3 options.secretAccessKey is mandatory.")
}
if (!options.bucket) {
throw new Error("BtrzPactS3 options.bucket is mandatory.")
}
if (logger && !logger.error) {
throw new Error("BtrzPactS3 logger is not valid.")
}
this.accessKeyId = options.accessKeyId;
this.secretAccessKey = options.secretAccessKey;
this.bucket = options.bucket;
this.logger = logger;
}
publishSinglePact(pact, _s3Client_) {
return this.getFileKey(pact)
.then((key) => {
if (this.logger) {
this.logger.info(`BtrzPactS3::publishPacts() - Uploading ${key}`);
}
let s3Client = _s3Client_ || new AWS.S3({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey
});
let self = this;
function resolver(resolve, reject) {
s3Client.putObject(
{
Bucket: self.bucket,
Key: key,
ACL: "public-read",
Body: fs.createReadStream(pact, {encoding: "utf8", autoClose: true})
},
function (err, result) {
if (err) {
return reject(err);
}
return resolve();
}
);
}
return new Promise(resolver);
})
.catch((err) => {
if (this.logger) {
this.logger.error("Error on BtrzPactS3::publishSinglePact()", err);
}
throw err;
});
}
getFilesFromPath(dir) {
let self = this;
var walk = function(dir, cb) {
var results = [];
fs.stat(dir, (err, stat) => {
if (stat && stat.isFile()) {
results.push(dir);
return cb(null, results);
} else {
fs.readdir(dir, (err, list) => {
if (err) {
return cb(err);
}
var pending = list.length;
if (!pending) {
return cb(null, results);
}
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, (err, stat) => {
if (stat && stat.isDirectory()) {
walk(file, (err, res) => {
results = results.concat(res);
if (!--pending) {
return cb(null, results);
}
});
} else {
results.push(file);
if (!--pending) {
return cb(null, results);
}
}
});
});
});
}
});
};
function resolver(resolve, reject) {
walk(dir, function promisify(err, results) {
if (err && !results) {
if (self.logger) {
self.logger.error("Error on BtrzPactS3::getFilesFromPath()", err);
}
return reject(err);
}
return resolve(results);
});
}
return new Promise(resolver);
}
publishPacts(pacts, _s3Client_) {
let self = this;
if (!pacts) {
throw new Error("No pacts array passed.")
}
//pacts.forEach(function(pact) {
return this.getFilesFromPath(pacts[0])
.then((files) => {
return Promise.all(files.map(function (file) {
return self.publishSinglePact(file, _s3Client_);
}));
})
.catch((err) => {
if (this.logger) {
this.logger.error(`Error on BtrzPactS3::publishPacts()`, err);
}
throw err;
});
}
verifyPacts(providerBaseUrl, providerName, _s3Client_, _pact_) {
let self = this;
const pact = _pact_ || require('@pact-foundation/pact-node');
const s3Client = _s3Client_ || new AWS.S3({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey
});
return s3Client.listObjects({Bucket: self.bucket}).promise()
.then((data) => {
let keysFromProvider = data.Contents.filter((content) => {
return (content.Key.indexOf(`${providerName.toLowerCase()}/`) === 0);
}).map((content) => {return content.Key});
if (keysFromProvider.length === 0) {
throw new Error(`There are no pacts for the provider ${providerName}`);
}
return Promise.all(keysFromProvider.map((key) => {
return s3Client.getObject({Bucket: self.bucket, Key: key}).promise()
.then((data) => {
if (!data.Body) {
throw new Error(`The file with key ${key} cannot be readed`);
}
let filePath = `${__dirname}/pacts-to-verify/${path.basename(key)}`;
return new Promise((resolve, reject) => {
fs.writeFile(filePath, data.Body, "utf8", (err) => {
if (err) {
return reject(err);
}
return resolve(filePath);
});
});
});
})
);
})
.then((filePaths) => {
let opts = {
providerBaseUrl: providerBaseUrl,
pactUrls: filePaths
};
return pact.verifyPacts(opts);
})
.catch((err) => {
if (this.logger) {
this.logger.error(`Error on BtrzPactS3::verifyPacts()`, err);
}
throw err;
});
}
getFileKey(filePath) {
function resolver(resolve, reject) {
return fs.readFile(filePath, "utf8", function (err, data) {
if (err) {
return reject(err);
}
let fileName = path.basename(filePath);
let pact = JSON.parse(data);
if (!pact.consumer.name) {
return reject(new Error(`The consumer name was not especified in the ${fileName} pact file`));
}
if (!pact.provider.name) {
return reject(new Error(`The provider name was not especified in the ${fileName} pact file`));
}
return resolve(`${pact.provider.name.toLowerCase()}/${pact.consumer.name.toLowerCase()}/${fileName}`);
});
}
return new Promise(resolver);
}
}
exports.BtrzPactS3 = BtrzPactS3;