-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-watchify.js
More file actions
82 lines (72 loc) · 2.26 KB
/
github-watchify.js
File metadata and controls
82 lines (72 loc) · 2.26 KB
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
'use strict';
function Watchify(options) {
let request = require('request');
let self = this;
self.token = options.token;
self.userAgent = options.userAgent;
return {
watch: watch
}
function watch(params) {
let currentCommit = '';
const repo = params.targetUser + '/' + params.targetRepo;
const interval = params.interval;
const onPing = params.onPing;
const onCommit = params.onCommit;
//ping first, then set the interval
_pingRepo(repo, currentCommit, onPing).then(function(mostRecentCommit) {
currentCommit = mostRecentCommit;
});
setInterval(function() {
_pingRepo(repo, currentCommit, onPing).then(function(mostRecentCommit) {
if(currentCommit !== mostRecentCommit) {
_compareCommits(repo, currentCommit, mostRecentCommit, onCommit);
currentCommit = mostRecentCommit;
}
});
}, interval);
}
function _pingRepo(repo, currentCommit, onPing) {
return new Promise(function(resolve, reject) {
request.get({
url:'https://api.github.com/repos/' + repo + '/commits?per_page=2',
headers: {
'User-Agent': self.userAgent,
'Authorization': 'token ' + self.token
}
}, function(err, response, body) {
if(err) {
console.log(err);
reject(err);
}
let data = JSON.parse(body);
let changedFiles;
if(onPing && onPing instanceof Function) {
onPing(data[0].sha);
}
resolve(data[0].sha);
});
})
}
function _compareCommits(repo, base, head, onCommit) {
request.get({
url:'https://api.github.com/repos/' + repo + '/compare/'+base+'...'+head,
// url: 'https://api.github.com/repos/Fyrd/caniuse/compare/734fe7198f6e294e293c27669eaf8cdec835b219...be9c49dc039c8db44efff3f12a6bf724540f20b1',
headers: {
'User-Agent': self.userAgent,
'Authorization': 'token ' + self.token
}
}, function(err, response, body) {
if(err) {
console.log(err);
return;
}
let data = JSON.parse(body);
let listOfChangedFiles = data.files;
if(onCommit && onCommit instanceof Function) {
onCommit(data, listOfChangedFiles);
}
})
}
};
module.exports = Watchify;