UNPKG

10.6 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var __read = (this && this.__read) || function (o, n) {
38 var m = typeof Symbol === "function" && o[Symbol.iterator];
39 if (!m) return o;
40 var i = m.call(o), r, ar = [], e;
41 try {
42 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
43 }
44 catch (error) { e = { error: error }; }
45 finally {
46 try {
47 if (r && !r.done && (m = i["return"])) m.call(i);
48 }
49 finally { if (e) throw e.error; }
50 }
51 return ar;
52};
53var __spread = (this && this.__spread) || function () {
54 for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
55 return ar;
56};
57var __importDefault = (this && this.__importDefault) || function (mod) {
58 return (mod && mod.__esModule) ? mod : { "default": mod };
59};
60Object.defineProperty(exports, "__esModule", { value: true });
61var deepmerge_1 = __importDefault(require("deepmerge"));
62var TYPE = '%TYPE';
63var VERSION = '%VERSION';
64var defaultOptions = {
65 label: 'released',
66 lockIssues: false,
67 message: ":rocket: " + TYPE + " was released in " + VERSION + " :rocket:"
68};
69var closeIssue = /(?:Close|Closes|Closed|Fix|Fixes|Fixed|Resolve|Resolves|Resolved)\s((?:#\d+(?:,\s)?)+)/gi;
70var isCanary = function (version) { return version.match('canary'); };
71var ReleasedLabelPlugin = /** @class */ (function () {
72 function ReleasedLabelPlugin(options) {
73 if (options === void 0) { options = {}; }
74 this.name = 'Released Label';
75 this.options = deepmerge_1.default(defaultOptions, options);
76 }
77 ReleasedLabelPlugin.prototype.apply = function (auto) {
78 var _this = this;
79 auto.hooks.modifyConfig.tap(this.name, function (config) {
80 config.labels.released = config.labels.released || {
81 name: 'released',
82 description: 'This issue/pull request has been released.'
83 };
84 return config;
85 });
86 auto.hooks.afterRelease.tapPromise(this.name, function (newVersion, commits) { return __awaiter(_this, void 0, void 0, function () {
87 var head, isSkipped;
88 var _this = this;
89 return __generator(this, function (_a) {
90 switch (_a.label) {
91 case 0:
92 if (!newVersion) {
93 return [2 /*return*/];
94 }
95 if ('dryRun' in auto.args && auto.args.dryRun) {
96 return [2 /*return*/];
97 }
98 head = commits[0];
99 if (!head) {
100 return [2 /*return*/];
101 }
102 isSkipped = head.labels.find(function (label) {
103 return auto.release.options.skipReleaseLabels.includes(label);
104 });
105 if (isSkipped) {
106 return [2 /*return*/];
107 }
108 return [4 /*yield*/, Promise.all(commits.map(function (commit) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
109 return [2 /*return*/, this.addReleased(auto, commit, newVersion)];
110 }); }); }))];
111 case 1:
112 _a.sent();
113 return [2 /*return*/];
114 }
115 });
116 }); });
117 };
118 ReleasedLabelPlugin.prototype.addReleased = function (auto, commit, newVersion) {
119 return __awaiter(this, void 0, void 0, function () {
120 var messages, pr, commitsInPr, issues;
121 var _this = this;
122 return __generator(this, function (_a) {
123 switch (_a.label) {
124 case 0:
125 messages = [commit.subject];
126 if (!commit.pullRequest) return [3 /*break*/, 4];
127 return [4 /*yield*/, this.addCommentAndLabel(auto, newVersion, commit.pullRequest.number)];
128 case 1:
129 _a.sent();
130 return [4 /*yield*/, auto.git.getPullRequest(commit.pullRequest.number)];
131 case 2:
132 pr = _a.sent();
133 pr.data.body.split('\n').map(function (line) { return messages.push(line); });
134 return [4 /*yield*/, auto.git.getCommitsForPR(commit.pullRequest.number)];
135 case 3:
136 commitsInPr = _a.sent();
137 commitsInPr.map(function (c) { return messages.push(c.commit.message); });
138 _a.label = 4;
139 case 4:
140 issues = messages
141 .map(function (message) { return message.match(closeIssue); })
142 .filter(function (r) { return !!r; })
143 .reduce(function (all, arr) { return __spread(all, arr); }, [])
144 .map(function (issue) { return issue.match(/#(\d+)/i); })
145 .filter(function (r) { return !!r; })
146 .map(function (match) { return Number(match[1]); });
147 return [4 /*yield*/, Promise.all(issues.map(function (issue) { return __awaiter(_this, void 0, void 0, function () {
148 return __generator(this, function (_a) {
149 switch (_a.label) {
150 case 0: return [4 /*yield*/, this.addCommentAndLabel(auto, newVersion, issue, true)];
151 case 1:
152 _a.sent();
153 if (!(this.options.lockIssues && !isCanary(newVersion))) return [3 /*break*/, 3];
154 return [4 /*yield*/, auto.git.lockIssue(issue)];
155 case 2:
156 _a.sent();
157 _a.label = 3;
158 case 3: return [2 /*return*/];
159 }
160 });
161 }); }))];
162 case 5:
163 _a.sent();
164 return [2 /*return*/];
165 }
166 });
167 });
168 };
169 ReleasedLabelPlugin.prototype.addCommentAndLabel = function (auto, newVersion, prOrIssue, isIssue) {
170 if (isIssue === void 0) { isIssue = false; }
171 return __awaiter(this, void 0, void 0, function () {
172 var message, labels;
173 return __generator(this, function (_a) {
174 switch (_a.label) {
175 case 0:
176 message = this.createReleasedComment(isIssue, newVersion);
177 return [4 /*yield*/, auto.comment({ message: message, pr: prOrIssue, context: 'released' })];
178 case 1:
179 _a.sent();
180 // Do not add released to issue/label for canary versions
181 if (isCanary(newVersion)) {
182 return [2 /*return*/];
183 }
184 return [4 /*yield*/, auto.git.getLabels(prOrIssue)];
185 case 2:
186 labels = _a.sent();
187 if (!!labels.includes(this.options.label)) return [3 /*break*/, 4];
188 return [4 /*yield*/, auto.git.addLabelToPr(prOrIssue, this.options.label)];
189 case 3:
190 _a.sent();
191 _a.label = 4;
192 case 4: return [2 /*return*/];
193 }
194 });
195 });
196 };
197 ReleasedLabelPlugin.prototype.createReleasedComment = function (isIssue, version) {
198 return this.options.message
199 .replace(TYPE, isIssue ? 'Issue' : 'PR')
200 .replace(VERSION, version);
201 };
202 return ReleasedLabelPlugin;
203}());
204exports.default = ReleasedLabelPlugin;
205//# sourceMappingURL=index.js.map
\No newline at end of file