-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (89 loc) · 2.99 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
#!/usr/bin/env node
const axios = require('axios');
const fs = require('fs');
const config = require('./config.json');
const LATEST_COUNT = 10;
const TOP_ISSUES_LABELS = '置顶';
const ANCHOR_NUMBER = 5;
const TOKEN = process.argv.slice(2)[0];
async function getIssues(params) {
const { data } = await axios.get(`${config.api}/issues?access_token=${TOKEN}`, {
params,
});
return data;
}
async function getLabels() {
const { data } = await axios.get(`${config.api}/labels?access_token=${TOKEN}`);
return data;
}
// 返回置顶 Issues
function getTopIssues() {
return getIssues({ labels: TOP_ISSUES_LABELS });
}
// 返回最近修改的 10 条
function getLatestIssues() {
return getIssues({ per_page: LATEST_COUNT, sort: 'updated' });
}
// 添加 readme item
function addIssueItemInfo(issue) {
const time = String(issue['updated_at']).substring(0, 10);
return `- [${issue.title}](${issue['html_url']}) -- ${time}\n`;
}
// 添加一个板块
function addPartIssueInfo(title, issues) {
let partMD = `
## ${title}\n`;
issues.forEach(issue => {
partMD += addIssueItemInfo(issue);
});
return partMD;
}
function isEmpty(arr) {
return arr.length === 0;
}
async function updateReadme() {
const labels = await getLabels();
const latestIssues = await getLatestIssues();
const topIssues = await getTopIssues();
let readme = `
# 招聘信息
汇总互联网行业相关求职信息,由华科已毕业的在职学长/学姐发布,信息真实可靠。
## 如何发布
打开一个 [Issue](https://github.com/HUSTLab/job/issues/new?assignees=&labels=&template=------.md&title=%E6%A0%87%E9%A2%98%E6%A8%A1%E7%89%88%EF%BC%9A%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4-%E5%89%8D%E5%90%8E%E7%AB%AF-%E6%9D%AD%E5%B7%9E-%E5%85%B6%E4%BB%96%E4%BF%A1%E6%81%AF),按照 Issues 模版填写相关的招聘信息,并选择一个标签,提交后会自动更新 README。
## 如何订阅
点击右上方 Watch 徽标,选择 Custom 并设置监听 Issues,信息更新时您将收到邮件提醒。
`;
if (!isEmpty(topIssues)) {
// 添加置顶信息
readme += addPartIssueInfo('置顶信息', topIssues);
}
if (!isEmpty(latestIssues)) {
// 添加最近更新
readme += addPartIssueInfo('近期更新', latestIssues);
}
for (let i = 0; i < labels.length; i++) {
const label = labels[i];
if (label['name'] === TOP_ISSUES_LABELS) {
continue;
}
let partMD = `## ${label.name}\n`;
const issuesWithLabel = await getIssues({ labels: label['name'] });
if (isEmpty(issuesWithLabel)) {
continue;
}
issuesWithLabel.forEach((issue, index) => {
if (index === ANCHOR_NUMBER) {
partMD += '<details><summary>显示更多</summary>\n';
partMD += '\n';
}
partMD += addIssueItemInfo(issue);
if (index === issuesWithLabel.length-1 && index >= ANCHOR_NUMBER) {
partMD += '</details>\n';
partMD += '\n';
}
});
readme += partMD;
}
fs.writeFileSync('./README.md', readme, 'utf8');
}
updateReadme();