forked from guilhem/rss-issues-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
154 lines (128 loc) · 5.11 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
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
const core = require('@actions/core')
const { getOctokit, context } = require('@actions/github')
const RSSParser = require('rss-parser')
const html2md = require('html-to-md')
const parseDurationInMilliseconds = (text) => {
let ms = 0
const milliSeconds = text.match(/(\d+)\s*m/)
if (milliSeconds) ms += parseInt(milliSeconds[1])
const seconds = text.match(/(\d+)\s*m/)
if (seconds) ms += parseInt(seconds[1]) * 1000
const minutes = text.match(/(\d+)\s*m/)
if (minutes) ms += parseInt(minutes[1]) * 60000
const hours = text.match(/(\d+)\s*h/)
if (hours) ms += parseInt(hours[1]) * 3600000
const days = text.match(/(\d+)\s*d/)
if (days) ms += parseInt(days[1]) * 86400000
return ms
}
const run = async () => {
try {
let issueTitlePrefix = core.getInput('prefix')
issueTitlePrefix = issueTitlePrefix ? issueTitlePrefix + ' ' : ''
let dryRun = core.getInput('dry-run')
if (dryRun) dryRun = dryRun === 'true'
let aggregate = core.getInput('aggregate')
if (aggregate) aggregate = aggregate === 'true'
let characterLimit = core.getInput('character-limit')
if (characterLimit) characterLimit = parseInt(characterLimit)
const titlePattern = core.getInput('title-pattern')
const contentPattern = core.getInput('content-pattern')
const limitTime = Date.now() - parseDurationInMilliseconds(core.getInput('max-age'))
core.debug(`limitTime ${limitTime}`)
const labels = core.getInput('labels')
core.debug(`labels ${labels}`)
// Instantiate GitHub client
const octokit = getOctokit(core.getInput('github-token'))
// Instantiate feed parser
const feed = await (new RSSParser({ xml2js: { trim: true } })).parseURL(core.getInput('feed'))
core.info(feed && feed.title)
if (!feed.items || feed.items.length === 0) return
// Remove old items in feed
feed.items = feed.items.filter(x => x.pubDate === undefined || limitTime < new Date(x.pubDate).getTime())
const { data: issues } = await octokit.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'all',
labels: labels
})
core.debug(`${issues.length} issues`)
const createdIssues = []
// Iterate
let counter = 0
for (const item of feed.items) {
const title = `${issueTitlePrefix}${item.title || (item.pubDate && new Date(item.pubDate).toUTCString())}`
if (titlePattern && !title.match(titlePattern)) {
core.debug(`Feed item skipped because it does not match the title pattern (${title})`)
continue
}
core.debug(`Issue '${title}'`)
if (issues.find(x => x.title === title)) {
core.warning(`Issue ${title} already exists`)
continue
}
if (aggregate && issues.find(x => x.title.startsWith(issueTitlePrefix) && Date.parse(x.created_at) > Date.parse(item.isoDate))) {
core.warning('Newer issue with same prefix already exists')
continue
}
// Issue Content
const content = item.content || item.description || ''
if (contentPattern && !content.match(contentPattern)) {
core.debug$(`Feed item skipped because it does not match the content pattern (${title})`)
continue
}
let markdown = html2md(content)
// truncate if characterLimit > 0
if (characterLimit && markdown.length > characterLimit) {
markdown = `${markdown.substr(0, characterLimit)}…\n\n---\n## Would you like to know more?\nRead the full article on the following website:`
}
// Render issue content
const body = `${markdown || ''}\n${item.link ? `\n${item.link}` : ''}`
// Default to creating an issue per item
// Create first issue if aggregate
if (!aggregate || createdIssues.length === 0) {
// Create Issue
createdIssues.push({ title, body, labels })
} else {
if (counter === 1) {
// The title of aggregated items will be "<n>new items";
// Add the title to the body so that it does not go poof
createdIssues[0].body = `# ${createdIssues[0].title}\n\n${createdIssues[0].body}`
}
createdIssues[0].body += `\n\n# ${title}\n\n${body}`
}
counter++
}
if (aggregate && counter > 1) {
createdIssues[0].title = `${issueTitlePrefix}${counter} new items`
}
for (const issue of createdIssues) {
if (dryRun) {
core.info(`Would create issue '${issue.title}' with content '${issue.body}'`)
} else {
try {
const { data } = await octokit.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issue.title,
body: issue.body,
labels: issue.labels ? issue.labels.split(',') : undefined
})
issue.id = data.id
} catch (e) {
core.warning(`Failed to create issue ${issue.title}: ${e}`)
continue
}
}
}
core.setOutput('issues', createdIssues.map(item => item.id).join(','))
} catch (e) {
if (typeof jest !== 'undefined') throw e
core.setFailed(e.message)
}
}
if (typeof jest !== 'undefined') {
module.exports = run
} else {
run()
}