forked from ducdigital/gulp-sentry-release
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
259 lines (226 loc) · 5.85 KB
/
index.js
File metadata and controls
259 lines (226 loc) · 5.85 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
var through = require('through2');
var fs = require('fs');
var request = require('request');
var slash = require('slash');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
module.exports = function (packageFile, opt) {
if (typeof packageFile === 'object' && packageFile && !opt) {
opt = packageFile;
packageFile = null;
}
if (!opt || !opt.API_KEY || !opt.API_URL) {
throw new PluginError(
'gulp-sentry-release',
'The API_KEY and API_URL options are required'
);
}
if (!opt.versionPrefix) {
opt.versionPrefix = '';
}
var version = opt.versionPrefix + opt.version;;
if (packageFile) {
version =
opt.versionPrefix +
JSON.parse(fs.readFileSync(packageFile, 'utf8')).version;
}
if (!version) {
throw new PluginError(
'gulp-sentry-release',
'A version string is required'
);
}
var API_URL = opt.API_URL.replace(/\/?$/, '/releases/');
var API_KEY = opt.API_KEY;
var streamCount = 0;
var failedCount = 0;
/***************************************************************************
* Sentry API Request wrapper
***************************************************************************/
var sentryAPI = {
create: function (version, cb) {
return request.post({
uri: API_URL,
headers: {
'Content-Type': 'application/json'
},
auth: {
bearer: API_KEY
},
form: {
version: version
}
}, cb);
},
delete: function (version, cb) {
return request.del({
uri: API_URL + version + '/',
headers: {
'Content-Type': 'application/json'
},
auth: {
bearer: API_KEY
}
}, cb);
},
upload: function (version, file, cb){
return request.post({
uri: API_URL + version + '/files/',
headers: {
'Content-Type': 'application/json'
},
auth: {
bearer: API_KEY
},
formData: {
file: {
value: file.contents,
options: {
filename: file.relative
}
},
name: (opt.DOMAIN || '') + '/' + slash(file.relative)
}
}, cb);
}
};
/***************************************************************************
* Upload a release
***************************************************************************/
var release = function (v) {
var ver = v || version;
var init = function (cb) {
sentryAPI.create(ver, function (err, res, body){
if (err) {
gutil.log(err);
}
if (res.statusCode >= 400) {
gutil.log('Version existed: ' + ver);
} else {
gutil.log('Created version: ' + ver);
}
gutil.log('Starting upload to sentry...');
return cb();
});
};
var processFile = function (file, cb) {
sentryAPI.upload(ver, file, function (err, res, body){
if (err) {
gutil.log(err);
}
var errMsg = '';
if (res.statusCode >= 400) {
failedCount++;
errMsg = (res.statusCode === 409) ?
'File already exists' : 'File upload failed';
}
if (opt.debug) {
gutil.log(file.relative + ' | ' + body + ' | ' + errMsg);
} else {
process.stdout.write('.');
}
return cb(null, file);
});
};
var streamProcess = function (file, enc, cb) {
if (streamCount === 0) {
init(function () {
processFile(file, cb);
});
} else {
processFile(file, cb);
}
streamCount++;
};
var streamEnd = function (cb){
gutil.log('Processed ' + streamCount + ' files');
gutil.log(
'In which ' + failedCount +
' files failed to upload or already existed'
);
return cb();
};
return through.obj(streamProcess, streamEnd);
};
var getDetails = function(body) {
try {
return JSON.parse(body).detail;
} catch(e) {
// Prevent any problems in the case of response format changing.
return body;
}
};
/***************************************************************************
* Delete a version
***************************************************************************/
var deleteVersion = function (version) {
if (!version) {
throw new PluginError(
'gulp-sentry-release.deleteVersion(version)',
'Requires version to delete'
);
}
return through.obj(function (file, enc, cb) {
// Passthrough
return cb(null, file);
}, function (cb) {
sentryAPI.delete(version, function (err, res, body){
if (err) {
gutil.log(err);
}
if (res.statusCode === 400 || res.statusCode === 401) {
throw new PluginError(
'gulp-sentry-release.deleteVersion(version)',
'Server return error ' + res.statusCode +': ' + getDetails(body)
);
}
if (res.statusCode === 404) {
throw new PluginError(
'gulp-sentry-release.deleteVersion(version)',
'Server return error 404: Version not found. ' + body
);
}
gutil.log('Deleted version: ' + version);
return cb();
});
});
};
/***************************************************************************
* Create a version
***************************************************************************/
var createVersion = function (version) {
if (!version) {
throw new PluginError(
'gulp-sentry-release.createVersion(version)',
'Requires version to create'
);
}
return through.obj(function (file, enc, cb) {
// Passthrough
return cb(null, file);
}, function (cb) {
sentryAPI.create(version, function (err, res, body){
if (err) {
gutil.log(err);
}
if (res.statusCode >= 400) {
throw new PluginError(
'gulp-sentry-release.createVersion(version)',
'Version already existed. ' + body
);
}
gutil.log('Created version: ' + version);
return cb();
});
});
};
/***************************************************************************
* Finally, export the plugin
***************************************************************************/
return {
release: release,
deleteVersion: deleteVersion,
createVersion: createVersion,
sentryAPI: sentryAPI
};
};