-
Notifications
You must be signed in to change notification settings - Fork 1
/
daily-new.js
105 lines (97 loc) · 2.71 KB
/
daily-new.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
// 收集每个仓库今天新增的 issue。
// 渲染
// 机器人推送 id
const { Octokit } = require("@octokit/rest");
const { exec } = require("child_process");
const { ReposEnum } = require("./const");
const dayjs = require("dayjs");
const truncate = require('lodash/truncate');
const MAX_CONTENT_LENGTH = 2048;
class DailyNew {
constructor({ wxhook, token, octokit }) {
this.wxhook = wxhook;
this.octokit = octokit || new Octokit({ auth: token });
this.title = "今天新创建的 ISSUE";
this.chatid = "";
this.dateString = dayjs().subtract(1, "day").format("YYYY-MM-DD");
}
async getData() {
const allList = await Promise.all(
ReposEnum.map((repo) =>
this.octokit.rest.issues
.listForRepo({
owner: "Tencent",
repo: repo,
state: "open",
sort: "created",
})
.then((res) => {
const arr = res.data
.filter((item) => item.created_at.split("T")[0] === this.dateString)
.map((item) => ({
...item,
repo: item.repository_url.split("Tencent/")[1],
}));
arr.repoName = repo;
return arr;
})
)
);
return allList;
}
async render(data) {
if (data.every((li) => !li.length)) return "";
return [
`## 今日新增的 ISSUE(${this.dateString})
${data
.filter((repo) => repo.filter((item) => !item.pull_request).length)
.map((repo) => {
return `#### ${repo.repoName}
${repo
.filter((item) => !item.pull_request)
.map((item) => {
return `- [${item.title}](${item.html_url})`;
})
.sort()
.join("\n")}`;
})
.join("\n \n")}`,
];
}
async run() {
let res;
try {
res = await this.getData();
} catch (error) {
console.log(error, "error");
}
if (!res) return false;
let template = await this.render(res);
template = truncate(template, {
length: MAX_CONTENT_LENGTH,
separator: /(\r|\n|\r\n)+/,
omission: '\n\nToo large to show...',
}).replaceAll('"', "'");
exec(
`curl ${this.wxhook} \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"chatid": "wrkSFfCgAAnG3Nyak3hg0-tc_9SRhJHA",
"markdown": {
"content": "${template}"
}
}'`,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
}
);
}
}
module.exports = DailyNew;