-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (111 loc) · 3.18 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
import {run} from '@cycle/xstream-run';
import {makeHTTPDriver} from '@cycle/http';
import xs from 'xstream';
import delay from 'xstream/extra/delay';
import flattenSequentially from 'xstream/extra/flattenSequentially';
import process from 'process';
import fs from 'fs';
const CLIENT_ID = process.env.GITHUB_CLIENT_ID;
const SECRET_KEY = process.env.GITHUB_SECRET_KEY;
const README_PREFACE = fs.readFileSync('README_SOURCE.md', 'utf-8');
const GENERATED_WARNING = `<!-- This file was automatically generated by index.js. If you wish to change the README text, please change README_SOURCE.md -->`;
if (!CLIENT_ID || !SECRET_KEY) {
console.log('Please ensure $GITHUB_CLIENT_ID and $GITHUB_SECRET_KEY are set');
}
function apiCall (path) {
return `https://api.github.com/${path}?client_id=${CLIENT_ID}&client_secret=${SECRET_KEY}`
}
function prettyIssue (issue) {
return `* [#${issue.number} - ${issue.title}](${issue.html_url})`
}
function prettyIssues (issues) {
return (
Object.keys(issues).map(repoName => (
[
`**${repoName}**`,
issues[repoName].map(prettyIssue).join('\n')
].join('\n')
)).join('\n\n')
);
}
function readmeDriver (sink$) {
sink$.addListener({
next (issues) {
fs.writeFileSync(
'README.md',
[
GENERATED_WARNING,
README_PREFACE,
prettyIssues(issues),
'\n',
GENERATED_WARNING
].join('\n')
);
},
error (err) {
console.error(err);
},
complete () {
console.log('done!')
}
});
}
function labeledWithHelp (issue) {
const labels = issue.labels.map(label => label.name);
return labels
.map(label => label.toLowerCase())
.some(label => label.includes('help') || label.includes('pr-welcome'));
}
function addIssues (issuesByRepo, issues) {
const issuesLabeledWithHelp = issues.filter(labeledWithHelp);
if (issuesLabeledWithHelp.length === 0) {
return issuesByRepo;
}
const repoName = issuesLabeledWithHelp[0].repository_url
.replace('https://api.github.com/repos/cyclejs-community/', '');
issuesByRepo[repoName] = issuesLabeledWithHelp;
return issuesByRepo;
}
function main ({HTTP}) {
const repoIssueURLs$ = HTTP
.select('repos')
.flatten()
.map(response => response.body)
.map(repos => repos.map(
repo => repo.issues_url.replace(
'{/number}',
`?client_id=${CLIENT_ID}&client_secret=${SECRET_KEY}&assignee=none&state=open`
)
));
const issues$ = HTTP
.select('issues')
.compose(flattenSequentially)
.map(response => response.body)
.fold(addIssues, {});
const fetchRepos$ = xs.of({
url: apiCall('orgs/cyclejs-community/repos'),
category: 'repos'
});
const fetchIssues$ = repoIssueURLs$
.map(urls =>
urls.map(url => ({
url,
category: 'issues'
}))
)
.map(requests => xs.of(
...requests.map(request => xs.of(request).compose(delay(1000)))
))
.flatten()
.compose(flattenSequentially);
const request$ = xs.merge(fetchRepos$, fetchIssues$);
return {
HTTP: request$,
README: issues$
};
}
const drivers = {
HTTP: makeHTTPDriver(),
README: readmeDriver
};
run(main, drivers);