Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
117 lines (92 loc) · 4.15 KB
/
Copy pathserver.js
File metadata and controls
117 lines (92 loc) · 4.15 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
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
require('org.pinf.genesis.lib').forModule(require,module,function(API,exports){
API.VERBOSE=true;
functionestablishStream(streamId,streamConfig){
API.console.verbose("Establish stream '"+streamId+"' with config:",streamConfig);
/*
// TODO: We may want to use the webhooks api instead of posting to pubsub links every time
// we need to verify that hooks are created.
return callGithub(userInfo, "GET", "/repos/" + org + "/" + repo + "/hooks", function(err, res, hooks) {
if (err) return callback(err);
console.log("hooks", hooks);
return callGithub(userInfo, "POST", "/repos/" + org + "/" + repo + "/hooks", {
}, function(err, res, result) {
if (err) return callback(err);
console.log("result", result);
});
});
*/
API.ASSERT.equal(typeofstreamConfig.user,"object");
API.ASSERT.equal(typeofstreamConfig.user.accessToken,"string");
API.ASSERT.equal(typeofstreamConfig.channels,"object");
functionestablishChannel(channelId,channelConfig){
returnAPI.Q.denodeify(function(callback){
API.console.verbose("Establish channel '"+channelId+"' in stream '"+streamId+"' with config:",channelConfig);
API.ASSERT.equal(typeofchannelConfig.org,"string");
API.ASSERT.equal(typeofchannelConfig.repo,"string");
API.ASSERT.equal(typeofchannelConfig.event,"string");
varcallbackUrl="http://test.com";
varcallbackSecret="secret!!";
console.log("callbackUrl",callbackUrl);
varsubscribeUrl="https://api.github.com/hub";
returnAPI.REQUEST({
method: "POST",
url: subscribeUrl,
headers: {
"User-Agent": "nodejs/request"
},
form: {
"hub.mode": "subscribe",
"hub.topic": "https://github.com/"+channelConfig.org+"/"+channelConfig.repo+"/events/"+channelConfig.event,
"hub.secret": callbackSecret,
"hub.callback": callbackUrl
// "hub.callback": HELPERS.makePublicklyAccessible(config.notificationUrl)
},
auth: {
user: "user",
pass: streamConfig.user.accessToken
}
},function(err,res,body){
console.log("subscribe response",body);
if(err)returncallback(err);
if(res.statusCode===422){
console.log("userInfo",userInfo);
console.error("Got status '"+res.statusCode+"' for url '"+subscribeUrl+"'! This is likely due to NOT HAVING ACCESS to this API call because your OAUTH SCOPE is too narrow! You neeed 'write:repo_hook' access. See: https://developer.github.com/v3/oauth/#scopes",res.headers);
/*
// TODO: Upgrade auth scope ...
var scope = "write:repo_hook";
if (scope) {
console.log("We are going to start a new oauth session with the new require scope added ...");
var err = new Error("Insufficient privileges. Should start new session with added scope: " + scope);
err.code = 403;
err.requestScope = scope;
return callback(err);
}
*/
returncallback(newError("Insufficient privileges. There should be a scope upgrade handler declared!"));
}
if(res.statusCode!==204){
returncallback(newError("Got status '"+res.statusCode+"' while creating hook!"));
}
// Hook successfully created!
returncallback(null);
});
})();
}
returnAPI.Q.all(Object.keys(streamConfig.channels).map(function(channelId){
returnestablishChannel(channelId,streamConfig.channels[channelId]);
}));
}
functionestablishStreams(){
console.log("API.config.streams",API.config);
if(
!API.config||
!API.config.streams
){
returnAPI.Q.resolve();
}
returnAPI.Q.all(Object.keys(API.config.streams).map(function(streamId){
returnestablishStream(streamId,API.config.streams[streamId]);
}));
}
returnestablishStreams();
});