-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.js
126 lines (111 loc) · 3.16 KB
/
github.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
const passport = require('passport');
const GitHubStrategy = require('passport-github2');
const Octokit = require('@octokit/rest').plugin(require('@octokit/plugin-retry'));
const slugify = require('slugify');
const discordBot = require('./discordBot.js');
passport.use(new GitHubStrategy({
clientID: global.github.clientid,
clientSecret: global.github.secret,
callbackURL: "http://" + global.url + "/auth/github/callback"
},
function (accessToken, refreshToken, profile, cb) {
console.log("Setup Github OAuth");
//User.findOrCreate({ githubId: profile.id }, function (err, user) {
//return cb(err, user);
//});
var user = {
token: accessToken
}
return cb(null, user);
}
));
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
function Middleware(app) {
app.use(passport.initialize());
app.use(passport.session());
}
function Authenticate() {
return passport.authenticate('github', { scope: ['public_repo'] });
}
function AuthCallback(fail) {
return passport.authenticate('github', { failureRedirect: fail });
}
/*
{ name: 'One Click Test',
authorname: 'TumbleGamer',
authorid: '336869008148135948',
description: '',
approved: 0 }
*/
async function publishTP(token, tp) {
var octokit = Octokit({
auth() {
return 'token ' + token;
}
});
octokit.request('/').catch(error => {
if (error.request.request.retryCount) {
console.log(`request failed after ${error.request.request.retryCount} retries`)
}
console.error(error)
})
var owner = "boxcritters";
var repo = "boxcritters.github.io";
var slug = slugify(tp.name);
var path = "_texturepacks/" + slug + ".md";
var message = "Published Texture Pack " + tp.name + " by " + tp.authorname;
var submitter = await discordBot.getMember(tp.authorid);
var author = {
name: tp.authorname,
email: "[email protected]"
};
var contentraw = `---
title: ${tp.name || ''}
author:
- ${tp.authorname || ''}
description: ${tp.description || ''}
logo: ${tp.logo || ''}
image: ${tp.image || ''}`;
if (tp.code) {
contentraw += `
code: >-
${tp.code}`;
}
if (tp.install) {
contentraw += `
install: ${tp.install}`;
}
contentraw += `
featured: false
---`;
var content = Buffer.from(contentraw).toString('base64');
console.log("Creating file " + owner + "/" + repo + "/" + path);
try {
var fileInfo = await octokit.repos.createOrUpdateFile({
owner,
repo,
path,
message,
content,
author
});
console.log("DONE???");
console.log(fileInfo);
var baseurl = "http://boxcritters.github.io/texturepacks/"
submitter.send("Your texture pack has been submitted:" + baseurl + slug);
return fileInfo.html_url;
} catch (err) {
console.log("Error: " + err);
if (response.status == 422) {
await submitter.send("We could not submit your texture pack as one exists with the same name.");
throw new Error("Texture pack already exists. The submiter has been notified");
}
throw new Error(response);
}
}
module.exports = { Middleware, Authenticate, AuthCallback, publishTP };