-
Notifications
You must be signed in to change notification settings - Fork 18
/
action.js
160 lines (143 loc) · 5.04 KB
/
action.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
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
let { Octokit } = require('@octokit/rest')
let { loadWikipedia, generateArticle, googlePhoto } = require('./lib')
const crypto = require('crypto');
let fs = require('fs')
require('dotenv').config()
let TOKEN = process.env.TOKEN
let REPOSITORY = process.env.REPOSITORY
let [OWNER, REPO] = REPOSITORY.split('/')
let octokit = new Octokit({
auth: TOKEN
})
const FILE = '100644'; // commit mode
const encoding = 'utf-8';
const ref = 'heads/master';
let committer = {
name: 'NodeBE4',
email: 'you@example.com'
}
async function performTasks() {
let { data } = await octokit.issues.listForRepo({
owner: OWNER,
repo: REPO,
state: 'open'
})
let promises = data.map(async (issue) => {
try {
if (issue.title.substring(0,11)=='add_request'){
let repo = issue.body.split('\n')[0]
let desc = issue.body.split('---描述---\n')[1].trim()
let match = /^https:\/\/(github|gitlab).com\//
if (match.test(repo)){
// let rawfile = await octokit.repos.getContent({
// owner: OWNER,
// repo: REPO,
// path: `index.json`,
// ref: ref
// })
// let buff = new Buffer.from(rawfile.data.content, 'base64')
// let text = buff.toString('utf-8')
let text = fs.readFileSync('./index.json', {encoding:'utf8', flag:'r'})
let json = JSON.parse(text)
let text2 = fs.readFileSync('./blacklist.json', {encoding:'utf8', flag:'r'})
let bjson = JSON.parse(text2)
var topleader = bjson.filter(function (item) {
return item.repo == repo ;
});
if (topleader.length > 0){
throw Error('這個項目在黑名單上。');
}
let hash = crypto.createHash('md5').update(repo).digest("hex")
var thisperson = json.filter(function (item) {
return item.repo == repo ;
});
if (thisperson.length > 0){
let votefile = `_data/votes/vote_${thisperson[0].hash}`
let count = 2
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
count = parseInt(text)+1
}
thisperson[0].vote = count
fs.writeFileSync(votefile, count.toString())
json.map(item =>{
let votefile = `_data/votes/vote_${item.hash}`;
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
item.vote = parseInt(text)
}
})
let content = JSON.stringify(json, undefined, 4);
fs.writeFileSync(`./index.json`, content)
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: `${thisperson[0].repo} 已经榜上有名了,你对TA赞了一次,试试在网页上的搜索TA的名字`
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
title: issue.title,
labels: ['duplicate']
})
}else{
let newhero = {
repo: repo,
desc: desc,
vote: 1,
hash: hash
}
json.push({
...newhero
});
console.log(json);
json.map(item =>{
let votefile = `_data/votes/vote_${item.hash}`;
if (fs.existsSync(votefile)) {
text = fs.readFileSync(votefile)
item.vote = parseInt(text)
}
})
let content = JSON.stringify(json, undefined, 4);
let prTitle = `添加新項目-${repo}`
fs.writeFileSync(`./index.json`, content)
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: `已成功添加 ${repo}`
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
title: prTitle,
labels: ['success']
})
}
}
}
} catch(error) {
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: `错误 ${error.toString()}`
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'closed',
labels: ['error']
})
throw error
}
})
await Promise.all(promises)
}
performTasks()