forked from kat4/commit4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubrequest.js
107 lines (88 loc) · 2.77 KB
/
githubrequest.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
var https = require('https');
var qs = require('querystring');
var env = require('env2')('./config.env');
var renderHTML = require('./partials.js');
var githubHandler = function(req, res) {
var urlArray = req.url.split('/');
var owner = urlArray[2];
var repo = urlArray[3];
var credentials = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET
};
var options = {
host: 'api.github.com',
method: 'GET',
path: '/repos/' + owner + '/' + repo + '/commits?' + qs.stringify(credentials),
headers: {
'User-agent': 'commit4'
}
};
var commitHistory = {};
var i = 0;
var requestCommitHistory = https.request(options, getCommitsfromHistory);
requestCommitHistory.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
requestCommitHistory.end();
function getCommitsfromHistory(unparsedCommitHistory) {
parseBody(unparsedCommitHistory, function(arrayOfCommits) {
if (Array.isArray(JSON.parse(arrayOfCommits))) {
JSON.parse(arrayOfCommits).forEach(addCommitToHistoryObject);
} else {
res.end('GO SEARCH FOR A REAL REPO, DUFUS');
}
});
}
function addCommitToHistoryObject(commit) {
commitHistory[commit.commit.committer.date] = {
author: {
username: commit.committer.login,
avatar: commit.committer.avatar_url
},
files: [],
sha: commit.sha,
date: commit.commit.committer.date
};
options.path = '/repos/' + owner + '/' + repo + '/commits/' + commit.sha + '?' + qs.stringify(credentials);
var getCommitDetails = https.request(options, function(unparsedFileArray) {
addCommitFilesToHistoryObject(unparsedFileArray, commit);
});
getCommitDetails.end();
}
function addCommitFilesToHistoryObject(unparsedFileArray, commit) {
parseBody(unparsedFileArray, function(parsedFileArray) {
parsedFileArray = JSON.parse(parsedFileArray);
parsedFileArray.files.forEach(function(file, index) {
delete file.patch;
parsedFileArray.files[index] = file;
});
commitHistory[commit.commit.committer.date].files = parsedFileArray.files;
if (++i === Object.keys(commitHistory).length) {
res.writeHead(200, {
"Content-Type": "text/html"
});
res.end(renderHTML(commitHistory));
}
});
}
function parseBody(object, callback) {
var body = '';
object.setEncoding('utf8');
object.on('data', function(chunk) {
body += chunk;
});
object.on('end', function() {
callback(body);
});
}
};
var sampleRequest = {
url: '/commit4/kat4/commit4'
};
var sampleRes = {
writeHead: function(a, b) {},
end: console.log
};
// githubHandler(sampleRequest,sampleRes);
module.exports = githubHandler;