-
Notifications
You must be signed in to change notification settings - Fork 117
/
fork.js
177 lines (161 loc) · 5.78 KB
/
fork.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var GitHubApi = require('github');
var config = require('../../../config');
module['exports'] = function forkHook (req, res) {
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
host: "api.github.com",
requestFormat: "json",
timeout: 5000
});
var opts;
var hook = require('./');
if (!req.isAuthenticated()) {
req.session.redirectTo = "/" + req.params.owner + "/" + req.params.hook + "/fork";
return res.redirect(302, '/login');
}
var query = { owner: req.params.owner, name: req.params.hook };
return hook.find(query, function(err, result){
if (err) {
return res.end(err.message);
}
if (result.length === 0) {
return res.end('No Hook Found!');
}
var h = result[0];
req.hook = result[0];
var sourceType = "gist";
// TODO: allow forking of non-github gists! easy.
if (typeof h.gist === "undefined" || h.gist.length === 0) {
sourceType = "hook";
// return res.end('Sorry, this hook is not backed by a Github Gist. Forking is not currently available for non-Github based Hooks.');
}
if (req.session.user === h.owner) {
return res.end('You are already the owner of this service, so you cannot fork it.\nShare this link with other developers so they may fork it.');
}
// before we fork, check if hook already exists by that name, if return error
hook.find({ owner: req.session.user, name: h.name }, function (err, result) {
if (err) {
return res.end(err.message);
}
if(result.length > 0) {
var r = {
error: true,
message: "You've already forked this Hook at " + config.app.url + "/" + req.session.user + "/" + h.name
};
if (req.jsonResponse) {
return res.json(r);
} else {
return res.end(r.message);
}
}
forkScript(h);
});
// TODO: better forking support / choose where to fork to
// add UI screen for forking
/*
if (sourceType === "gist") {
var userScript = h.gist.replace('https://gist.github.com/', '');
userScript = userScript.replace('/', ' ');
var parts = userScript.split(" ");
var username = parts[0],
script = parts[1];
if (req.session.user === username) {
return res.end('You cannot fork your own Hooks. Try creating a new Hook instead? https://hook.io/new');
}
// before we fork, check if hook already exists by that name, if return error
hook.find({ owner: req.session.user, name: h.name }, function(err, result){
if (err) {
return res.end(err.message);
}
if(result.length > 0) {
return res.end("You've already forked this Hook at " + config.app.url + "/" + req.session.user + "/" + h.name);
}
forkGithubScript();
});
} else {
if (req.session.user === username) {
return res.end('You cannot fork your own Hooks. Try creating a new Hook instead? https://hook.io/new');
}
// before we fork, check if hook already exists by that name, if return error
hook.find({ owner: req.session.user, name: h.name }, function (err, result) {
if (err) {
return res.end(err.message);
}
if(result.length > 0) {
return res.end("You've already forked this Hook at " + config.app.url + "/" + req.session.user + "/" + h.name);
}
forkScript(h);
});
}
*/
function forkScript (h) {
var _hook = {
owner: req.session.user,
name: h.name,
source: h.source,
language: h.language,
mschema: h.mschema,
mschemaStatus: h.mschemaStatus,
themeSource: h.themeSource,
presenterSource: h.presenterSource,
themeStatus: h.themeStatus,
schemaStatus: h.schemaStatus
};
return hook.create(_hook, function(err, result){
if (err) {
return callback(err.message);
}
var h = result;
//console.log('performing redirect', req.session.user);
if (req.jsonResponse) {
var r = {
status: 'created'
};
return res.json(r);
} else {
return res.redirect('/admin?owner=' + _hook.owner + "&name=" + _hook.name + "&status=forked");
}
});
};
function forkGithubScript () {
// authenticate github API
// TODO: update reference?
if (typeof req.user.accessToken === "undefined") {
// redirect to github login with fork role ability
return res.redirect(301, '/login/github/gist');
}
github.authenticate({
type: "oauth",
token: req.user.accessToken
});
var _script = script + ".js";
return github.gists.fork({
id: script,
}, function(err, rest) {
if (err) {
return res.end(err.message);
}
var _hook = {
owner: req.session.user,
name: h.name,
gist: rest.owner.html_url.replace('https://github.com/', 'https://gist.github.com/') + "/" + rest.id
};
return hook.create(_hook, function(err, result){
if (err) {
return callback(err.message);
}
var h = result;
opts = opts || {};
opts.gist = h.gist;
opts.req = req;
opts.res = res;
return res.redirect('/' + h.owner + "/" + h.name + "?forked=true");
});
});
};
});
};