|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 12 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 13 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 14 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 15 | + function step(op) { |
| 16 | + if (f) throw new TypeError("Generator is already executing."); |
| 17 | + while (_) try { |
| 18 | + 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; |
| 19 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 20 | + switch (op[0]) { |
| 21 | + case 0: case 1: t = op; break; |
| 22 | + case 4: _.label++; return { value: op[1], done: false }; |
| 23 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 24 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 25 | + default: |
| 26 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 27 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 28 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 29 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 30 | + if (t[2]) _.ops.pop(); |
| 31 | + _.trys.pop(); continue; |
| 32 | + } |
| 33 | + op = body.call(thisArg, _); |
| 34 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 35 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 36 | + } |
| 37 | +}; |
| 38 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 39 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 40 | +}; |
| 41 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 42 | +exports.Bot = void 0; |
| 43 | +var string_template_1 = __importDefault(require("string-template")); |
| 44 | +var translate = require("@vitalets/google-translate-api"); |
| 45 | +function isDev() { |
| 46 | + return process && process.env && process.env.DEV === "true"; |
| 47 | +} |
| 48 | +function containsChinese(title) { |
| 49 | + return /[\u4e00-\u9fa5]/.test(title); |
| 50 | +} |
| 51 | +var Bot = /** @class */ (function () { |
| 52 | + function Bot(context, config, log) { |
| 53 | + this.context = context; |
| 54 | + this.config = config; |
| 55 | + this.log = log; |
| 56 | + } |
| 57 | + /** |
| 58 | + * 回复指定 Labeled |
| 59 | + */ |
| 60 | + Bot.prototype.replyLabeled = function () { |
| 61 | + return __awaiter(this, void 0, void 0, function () { |
| 62 | + var configs, label, issue, opener, config, issueComment, e_1; |
| 63 | + return __generator(this, function (_a) { |
| 64 | + switch (_a.label) { |
| 65 | + case 0: |
| 66 | + configs = this.config.issue.labeledReplay; |
| 67 | + if (!configs) |
| 68 | + return [2 /*return*/]; |
| 69 | + label = this.context.payload.label.name; |
| 70 | + issue = this.context.payload.issue; |
| 71 | + opener = issue.user.login; |
| 72 | + config = configs.find(function (e) { return e.labels.includes(label); }); |
| 73 | + if (!config) return [3 /*break*/, 4]; |
| 74 | + this.log.trace({ |
| 75 | + issue: issue, |
| 76 | + label: label, |
| 77 | + }, "replying labeled..."); |
| 78 | + issueComment = this.context.issue({ |
| 79 | + body: string_template_1.default(config.replay, { user: opener }), |
| 80 | + }); |
| 81 | + _a.label = 1; |
| 82 | + case 1: |
| 83 | + _a.trys.push([1, 3, , 4]); |
| 84 | + return [4 /*yield*/, this.context.github.issues.createComment(issueComment)]; |
| 85 | + case 2: |
| 86 | + _a.sent(); |
| 87 | + this.log.info({ |
| 88 | + issue: issue, |
| 89 | + label: label, |
| 90 | + issueComment: issueComment, |
| 91 | + }, "replyed by label."); |
| 92 | + return [3 /*break*/, 4]; |
| 93 | + case 3: |
| 94 | + e_1 = _a.sent(); |
| 95 | + this.log.error({ |
| 96 | + issue: issue, |
| 97 | + label: label, |
| 98 | + issueComment: issueComment, |
| 99 | + error: new Error(e_1), |
| 100 | + }, "reply error!"); |
| 101 | + return [3 /*break*/, 4]; |
| 102 | + case 4: return [2 /*return*/]; |
| 103 | + } |
| 104 | + }); |
| 105 | + }); |
| 106 | + }; |
| 107 | + /** |
| 108 | + * 回复翻译 |
| 109 | + */ |
| 110 | + Bot.prototype.replyTranslate = function () { |
| 111 | + return __awaiter(this, void 0, void 0, function () { |
| 112 | + var config, issue, body, title, content, issueComment, e_2; |
| 113 | + return __generator(this, function (_a) { |
| 114 | + switch (_a.label) { |
| 115 | + case 0: |
| 116 | + config = this.config.issue.translate; |
| 117 | + issue = this.context.payload.issue; |
| 118 | + if (!containsChinese(issue.title)) return [3 /*break*/, 6]; |
| 119 | + return [4 /*yield*/, translate(issue.body.replace(/<!--(.*?)-->/g, ""), { |
| 120 | + from: "zh-CN", |
| 121 | + to: "en", |
| 122 | + })]; |
| 123 | + case 1: |
| 124 | + body = _a.sent(); |
| 125 | + return [4 /*yield*/, translate(issue.title, { from: "zh-CN", to: "en" })]; |
| 126 | + case 2: |
| 127 | + title = _a.sent(); |
| 128 | + if (!(body.text && title.text)) return [3 /*break*/, 6]; |
| 129 | + content = string_template_1.default(config.replay, { |
| 130 | + title: title.text, |
| 131 | + body: body.text, |
| 132 | + }); |
| 133 | + issueComment = this.context.issue({ body: content }); |
| 134 | + this.log.trace({ |
| 135 | + issue: issue, |
| 136 | + issueComment: issueComment, |
| 137 | + }, "translating issue..."); |
| 138 | + _a.label = 3; |
| 139 | + case 3: |
| 140 | + _a.trys.push([3, 5, , 6]); |
| 141 | + return [4 /*yield*/, this.context.github.issues.createComment(issueComment)]; |
| 142 | + case 4: |
| 143 | + _a.sent(); |
| 144 | + this.log.info({ |
| 145 | + issue: issue, |
| 146 | + issueComment: issueComment, |
| 147 | + }, "translated issue."); |
| 148 | + return [3 /*break*/, 6]; |
| 149 | + case 5: |
| 150 | + e_2 = _a.sent(); |
| 151 | + this.log.error({ |
| 152 | + error: new Error(e_2), |
| 153 | + issue: issue, |
| 154 | + issueComment: issueComment, |
| 155 | + }, "translate issue error!"); |
| 156 | + return [3 /*break*/, 6]; |
| 157 | + case 6: return [2 /*return*/]; |
| 158 | + } |
| 159 | + }); |
| 160 | + }); |
| 161 | + }; |
| 162 | + /** |
| 163 | + * 回复需要重现链接 |
| 164 | + */ |
| 165 | + Bot.prototype.replyNeedReproduce = function () { |
| 166 | + return __awaiter(this, void 0, void 0, function () { |
| 167 | + var config, label, issue, opener, issueComment, e_3; |
| 168 | + return __generator(this, function (_a) { |
| 169 | + switch (_a.label) { |
| 170 | + case 0: |
| 171 | + config = this.config.issue.needReproduce; |
| 172 | + label = this.context.payload.label.name; |
| 173 | + issue = this.context.payload.issue; |
| 174 | + opener = issue.user.login; |
| 175 | + if (!new RegExp(config.label).test(label)) return [3 /*break*/, 6]; |
| 176 | + issueComment = this.context.issue({ |
| 177 | + body: string_template_1.default(config.replay, { user: opener }), |
| 178 | + }); |
| 179 | + this.log.trace({ |
| 180 | + issue: issue, |
| 181 | + label: label, |
| 182 | + }, "replying `NeedReproduce` issue..."); |
| 183 | + _a.label = 1; |
| 184 | + case 1: |
| 185 | + _a.trys.push([1, 5, , 6]); |
| 186 | + return [4 /*yield*/, this.context.github.issues.createComment(issueComment)]; |
| 187 | + case 2: |
| 188 | + _a.sent(); |
| 189 | + if (!config.afterLabel) return [3 /*break*/, 4]; |
| 190 | + return [4 /*yield*/, this.context.github.issues.addLabels(this.context.issue({ labels: [config.afterLabel] }))]; |
| 191 | + case 3: |
| 192 | + _a.sent(); |
| 193 | + _a.label = 4; |
| 194 | + case 4: |
| 195 | + this.log.info({ |
| 196 | + issue: issue, |
| 197 | + label: label, |
| 198 | + issueComment: issueComment, |
| 199 | + }, "replyed `NeedReproduce` issue."); |
| 200 | + return [3 /*break*/, 6]; |
| 201 | + case 5: |
| 202 | + e_3 = _a.sent(); |
| 203 | + this.log.error({ |
| 204 | + error: new Error(e_3), |
| 205 | + issue: issue, |
| 206 | + label: label, |
| 207 | + issueComment: issueComment, |
| 208 | + }, "reply `NeedReproduce` issue error!"); |
| 209 | + return [3 /*break*/, 6]; |
| 210 | + case 6: return [2 /*return*/]; |
| 211 | + } |
| 212 | + }); |
| 213 | + }); |
| 214 | + }; |
| 215 | + /** |
| 216 | + * 回复所有非 ng-alain-issue-help 创建的 Issues |
| 217 | + */ |
| 218 | + Bot.prototype.replyInvalid = function () { |
| 219 | + return __awaiter(this, void 0, void 0, function () { |
| 220 | + var config, isMember, issue, opener, mark, labels, issueComment, e_4; |
| 221 | + return __generator(this, function (_a) { |
| 222 | + switch (_a.label) { |
| 223 | + case 0: |
| 224 | + config = this.config.issue.invalid; |
| 225 | + return [4 /*yield*/, this.isMember()]; |
| 226 | + case 1: |
| 227 | + isMember = _a.sent(); |
| 228 | + issue = this.context.payload.issue; |
| 229 | + opener = issue.user.login; |
| 230 | + mark = config.mark; |
| 231 | + labels = []; |
| 232 | + if (Array.isArray(config.labels)) { |
| 233 | + labels = config.labels; |
| 234 | + } |
| 235 | + else { |
| 236 | + labels = [config.labels]; |
| 237 | + } |
| 238 | + if (!(!issue.body.includes(mark) && !isMember)) return [3 /*break*/, 7]; |
| 239 | + issueComment = this.context.issue({ |
| 240 | + body: string_template_1.default(config.replay, { user: opener }), |
| 241 | + }); |
| 242 | + this.log.trace({ |
| 243 | + issue: issue, |
| 244 | + }, "replying Invalid issue..."); |
| 245 | + _a.label = 2; |
| 246 | + case 2: |
| 247 | + _a.trys.push([2, 6, , 7]); |
| 248 | + return [4 /*yield*/, this.context.github.issues.createComment(issueComment)]; |
| 249 | + case 3: |
| 250 | + _a.sent(); |
| 251 | + return [4 /*yield*/, this.context.github.issues.update(this.context.issue({ state: "closed" }))]; |
| 252 | + case 4: |
| 253 | + _a.sent(); |
| 254 | + return [4 /*yield*/, this.context.github.issues.addLabels(this.context.issue({ labels: labels }))]; |
| 255 | + case 5: |
| 256 | + _a.sent(); |
| 257 | + this.log.info({ |
| 258 | + issue: issue, |
| 259 | + }, "replyed invalid issue..."); |
| 260 | + return [3 /*break*/, 7]; |
| 261 | + case 6: |
| 262 | + e_4 = _a.sent(); |
| 263 | + this.log.error({ |
| 264 | + error: new Error(e_4), |
| 265 | + issue: issue, |
| 266 | + }, "reply invalid issue error!"); |
| 267 | + return [3 /*break*/, 7]; |
| 268 | + case 7: return [2 /*return*/]; |
| 269 | + } |
| 270 | + }); |
| 271 | + }); |
| 272 | + }; |
| 273 | + Bot.prototype.isMember = function () { |
| 274 | + return __awaiter(this, void 0, void 0, function () { |
| 275 | + var members, issue, opener; |
| 276 | + return __generator(this, function (_a) { |
| 277 | + switch (_a.label) { |
| 278 | + case 0: |
| 279 | + if (isDev()) { |
| 280 | + return [2 /*return*/, false]; |
| 281 | + } |
| 282 | + return [4 /*yield*/, this.getMembers()]; |
| 283 | + case 1: |
| 284 | + members = _a.sent(); |
| 285 | + issue = this.context.payload.issue; |
| 286 | + opener = issue.user.login; |
| 287 | + return [2 /*return*/, members.includes(opener)]; |
| 288 | + } |
| 289 | + }); |
| 290 | + }); |
| 291 | + }; |
| 292 | + Bot.prototype.getMembers = function () { |
| 293 | + return __awaiter(this, void 0, void 0, function () { |
| 294 | + var members, repo, response; |
| 295 | + return __generator(this, function (_a) { |
| 296 | + switch (_a.label) { |
| 297 | + case 0: |
| 298 | + members = []; |
| 299 | + repo = this.context.repo(); |
| 300 | + return [4 /*yield*/, this.context.github.orgs.listMembers({ |
| 301 | + org: repo.owner, |
| 302 | + })]; |
| 303 | + case 1: |
| 304 | + response = _a.sent(); |
| 305 | + if (response && response.data) { |
| 306 | + members = response.data; |
| 307 | + } |
| 308 | + return [2 /*return*/, members.map(function (m) { return m.login; })]; |
| 309 | + } |
| 310 | + }); |
| 311 | + }); |
| 312 | + }; |
| 313 | + return Bot; |
| 314 | +}()); |
| 315 | +exports.Bot = Bot; |
0 commit comments