-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathissuesRss.js
93 lines (83 loc) · 2.63 KB
/
issuesRss.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
const fs = require('fs');
const path = require('path');
const dayjs = require('dayjs');
const argv = require('minimist')(process.argv.slice(2));
const { graphqlClient } = require('./utils');
module.exports = async function genIssuesRss(totalCount, repoLink) {
let limit = 20;
let list = [];
let last = null;
if (!argv.limit) {
for (let i = 0; i < Math.ceil(totalCount / limit); i++) {
const temp = await fetchRssData(limit, last);
last = Array.from(temp).pop().cursor;
list = [...list, ...temp];
}
} else {
limit = +argv.limit;
const temp = await fetchRssData(limit, null);
list = temp;
}
let content = '';
list.forEach(({ node }) => {
const issuesLink = `${repoLink}/discussions/${node.number}`;
content += postItem({ title: node.title, link: issuesLink, date: dayjs(node.updatedAt).format('YYYY-MM-DD'), html: node.bodyHTML });
});
const _filename = argv.filename || 'feed.xml';
const feedxml = feedXML({
siteTitle: argv['site-title'] || 'RSS',
siteLink: argv['site-link'] || '/',
siteDesc: argv['site-desc'] || 'GitHub Discussions',
filename: _filename,
postItems: content,
});
if (argv.outdir) fs.mkdirSync(path.resolve(argv.outdir), { recursive: true });
fs.writeFileSync(path.resolve(argv.outdir || '.', _filename), feedxml);
console.log('✨ RSS Done!');
}
function postItem({ title, link, date, html }) {
return `<item>
<title><![CDATA[${title}]]></title>
<link>${link}</link>
<guid isPermaLink=\"false\">${link}</guid>
<pubDate>${date}</pubDate>
<description><![CDATA[${html}]]></description>
</item>
`;
}
function feedXML({ siteTitle, siteLink, siteDesc, filename, postItems }) {
return `<?xml version=\"1.0\" encoding=\"utf-8\"?><rss xmlns:atom=\"http://www.w3.org/2005/Atom\" version=\"2.0\">
<channel>
<title>${siteTitle}</title>
<atom:link href=\"${siteLink.replace(/\/$/, '')}/${filename}\" rel=\"self\" type=\"application/rss+xml\" />
<link>${siteLink}</link>
<description>${siteDesc}</description>
${postItems}
</channel>
</rss>`;
}
async function fetchRssData(size, lastCursor) {
const { repository } = await graphqlClient(`
query ($owner: String!, $repo: String!, $limit: Int!, $cursor: String) {
repository(owner: $owner, name: $repo) {
issues(first: $limit, after: $cursor) {
edges {
cursor
node {
title
number
updatedAt
bodyHTML
}
}
}
}
}
`, {
owner: argv['issues-owner'],
repo: argv['issues-repo'],
limit: size,
cursor: lastCursor,
});
return repository.issues.edges;
}