-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildpage.js
79 lines (71 loc) · 2.59 KB
/
buildpage.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
const hbs = require('handlebars')
const fs = require('fs')
const request = require('request')
const btoa = require('btoa')
const clearUser = require('./clearuser.js')
module.exports = function (callback) {
if (process.env['CONTRIBUTORS']) {
fs.readFile(process.env['CONTRIBUTORS'], function (err, data) {
if (err) return callback(err, 'Error reading contribs file for building page.')
organizeData(data)
})
} else {
console.log('Making request for data...')
const uri = 'https://da3e-23-16-39-44.ngrok-free.app/data'
request({ url: uri, json: true }, function (err, res, body) {
if (err) return callback(err, 'Fetching latest data for building page')
organizeData(JSON.stringify(body))
})
}
function organizeData (data) {
const everyone = JSON.parse(data)
const archiveCount = 12425
const everyoneCount = everyone.length + archiveCount
const everyoneCommas = everyoneCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
const newest = everyone[everyone.length - 1]
const topHundred = everyone.reverse().slice(0, 100)
const stats = { featured: newest, everyone: topHundred, total: everyoneCommas }
return getTemplate(stats, everyone)
}
function getTemplate (stats, everyone) {
fs.readFile('template.hbs', function (err, file) {
if (err) return callback(err, 'Error reading template file.')
file = file.toString()
const template = hbs.compile(file)
const HTML = template(stats)
return writeRepo(HTML, stats, everyone)
})
}
function writeRepo (HTML, stats, everyone) {
const username = stats.featured.user
const baseURL = 'https://api.github.com/repos/'
const reqHeaders = {
'User-Agent': 'request',
'Authorization': 'token ' + process.env['REPOROBOT_TOKEN']
}
const options = {
headers: reqHeaders,
url: baseURL + 'lhl-reporobot/patchwork/contents/index.html',
json: true,
body: {
'branch': 'gh-pages',
'committer': {
'name': 'reporobot',
'email': '[email protected]'
},
'sha': '',
'content': btoa(HTML),
'message': 'Rebuilt index with ' + username
}
}
request.get(options, function (err, res, body) {
if (err) return callback(err, 'Error fetching SHA')
options.body.sha = body.sha
request.put(options, function (err, res, body) {
if (err) return callback(err, 'Error writing new index to Patchwork')
console.log(new Date(), 'Rebuilt index with ' + username)
clearUser(username, callback)
})
})
}
}