forked from gege-circle/github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perform.js
85 lines (75 loc) · 1.95 KB
/
perform.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
let { Octokit } = require('@octokit/rest')
let fetchArticle = require('./src/fetchArticle')
let renderToMarkdown = require('./src/renderToMarkdown')
let fetch = require('node-fetch')
require('dotenv').config()
let TOKEN = process.env.TOKEN
let REPOSITORY = process.env.REPOSITORY
let EVENT = process.env.EVENT
let [OWNER, REPO] = REPOSITORY.split('/')
let octokit = new Octokit({
auth: TOKEN
})
function checkSubmission(body) {
//if (body.split("\n").length > 1) return false
return true
}
async function getTasks() {
if (EVENT) {
console.log('getting single task')
return [JSON.parse(EVENT).issue]
} else {
console.log('getting list of tasks')
let { data } = await octokit.issues.listForRepo({
owner: OWNER,
repo: REPO,
state: 'open'
})
return data
}
}
async function performTasks(list) {
let promises = list.map(async (issue) => {
try {
if (!checkSubmission(issue.body || issue.title)) {
throw "Invalid submission"
}
let url = issue.body || issue.title
let resp = await fetch(url)
let articleData = await fetchArticle(resp.url)
await octokit.issues.createComment({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
body: renderToMarkdown(articleData)
})
await octokit.issues.update({
owner: OWNER,
repo: REPO,
issue_number: issue.number,
state: 'opened',
title: articleData.title
})
} 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',
})
throw error
}
})
await Promise.all(promises)
}
async function perform() {
let tasks = await getTasks()
await performTasks(tasks)
}
perform()