-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (117 loc) · 3.33 KB
/
server.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
import { MongoClient } from 'mongodb';
import nodemailer from 'nodemailer';
import { marked } from 'marked';
async function retrieve() {
const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
Authorization: `bearer ${process.env.GITHUB_ACCESS_TOKEN}`
},
body: JSON.stringify(
{
query: `
{
search(type: REPOSITORY, query: "org:${process.env.ORGANISATION}", first: 100) {
repositoryCount
edges {
node {
... on Repository {
name
description
url
forkCount
stargazers {
totalCount
}
defaultBranchRef {
target {
... on Commit {
history {
totalCount
}
}
}
}
}
}
}
}
}
`
}
)
});
const json = await response.json();
return json.data.search.edges.map(({ node }) => {
const {
name,
description,
url,
forkCount: forks,
stargazers: { totalCount: stars }
} = node;
const commits = (node.defaultBranchRef && node.defaultBranchRef.target.history.totalCount) || 0;
return { name, description, url, forks, stars, commits };
});
}
async function store(rs) {
const client = await new MongoClient(process.env.MONGO_URL).connect();
for (const r of rs) {
const { value } = await client
.db('trending')
.collection('repositories')
.replaceOne({ name: r.name }, r, { upsert: true });
Object.assign(r, {
deltaForks: r.forks - ((value && value.forks) || 0),
deltaStars: r.stars - ((value && value.stars) || 0),
deltaCommits: r.commits - ((value && value.commits) || 0)
});
}
await client.close();
return {
forks: Math.max(...rs.map(r => r.deltaForks)),
stars: Math.max(...rs.map(r => r.deltaStars)),
commits: Math.max(...rs.map(r => r.deltaCommits))
};
}
(async () => {
const rs = await retrieve();
const maxDeltas = await store(rs);
let text = '';
if (maxDeltas.forks) {
text +=
`Most forked (+${maxDeltas.forks}):\n\n` +
rs
.filter(r => r.deltaForks === maxDeltas.forks)
.map(r => `- [${r.name}](${r.url}) (${r.forks})`)
.join('\n') +
'\n\n';
}
if (maxDeltas.stars) {
text +=
`Most starred (+${maxDeltas.stars}):\n\n` +
rs
.filter(r => r.deltaStars === maxDeltas.stars)
.map(r => `- [${r.name}](${r.url}) (${r.stars})`)
.join('\n') +
'\n\n';
}
if (maxDeltas.commits) {
text +=
`Most commits (+${maxDeltas.commits}):\n\n` +
rs
.filter(r => r.deltaCommits === maxDeltas.commits)
.map(r => `- [${r.name}](${r.url}) (${r.commits})`)
.join('\n') +
'\n\n';
}
if (text) {
const transporter = nodemailer.createTransport(process.env.SMTP_URL);
transporter.sendMail({
from: process.env.EMAIL_FROM,
to: process.env.EMAIL_TO,
subject: `Trending repositories for ${process.env.ORGANISATION}`,
html: marked(text)
});
}
})();