git: fix git checkout
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
1ddd70ac55
commit
f93a4b6064
65
index.js
65
index.js
@ -2,11 +2,62 @@ const Git = require('nodegit');
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
|
const mkdirp = require('mkdirp');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 9119;
|
const port = 9119;
|
||||||
const root = 'reveal-root/';
|
const root = 'reveal-root/';
|
||||||
|
|
||||||
|
function update_repo(repo) {
|
||||||
|
console.log('updating repo ' + repo);
|
||||||
|
repo.fetch('origin').then(function() {
|
||||||
|
console.log('setting head on repo');
|
||||||
|
repo.setHead('FETCH_HEAD').then(function() {
|
||||||
|
console.log('checking out origin/master');
|
||||||
|
repo.checkoutBranch('origin/master');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function redeploy_repo(url, path, name) {
|
||||||
|
mkdirp.sync(config.repo_root);
|
||||||
|
var new_path = path + '/' + name;
|
||||||
|
console.log(`cloning ${url} into ${new_path} (${path} / ${name})`);
|
||||||
|
|
||||||
|
var repo = Git.Repository.open(new_path)
|
||||||
|
.then(update_repo)
|
||||||
|
.catch(function(e) {
|
||||||
|
console.log('Unable to update, trying clone: ');
|
||||||
|
console.log(e);
|
||||||
|
Git.Clone(url, new_path).then(update_repo).catch(function(e) {
|
||||||
|
console.log('Unable to clone:');
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
/*
|
||||||
|
let mut new_path = path.to_path_buf();
|
||||||
|
new_path.push(name);
|
||||||
|
eprintln!("Cloning {} into {:?} ({:?} / {})", url, new_path, path, name);
|
||||||
|
|
||||||
|
let repo = match Repository::open(&new_path) {
|
||||||
|
Ok(repo) => repo,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("can't open repo, going to try cloning it: {}", e.description());
|
||||||
|
match Repository::clone(url, &new_path) {
|
||||||
|
Ok(repo) => repo,
|
||||||
|
Err(e) => panic!("failed to clone: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
repo.find_remote("origin")?.fetch(&["master"], None, None)?;
|
||||||
|
repo.set_head("FETCH_HEAD")?;
|
||||||
|
repo.checkout_head(Some(git2::build::CheckoutBuilder::new().force().use_theirs(true)))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
function webhook(config, req, res) {
|
function webhook(config, req, res) {
|
||||||
if (req.get('X-GitHub-Event') !== 'push')
|
if (req.get('X-GitHub-Event') !== 'push')
|
||||||
return res.status(200).send('X-GitHub-Event was not "push"');
|
return res.status(200).send('X-GitHub-Event was not "push"');
|
||||||
@ -14,7 +65,8 @@ function webhook(config, req, res) {
|
|||||||
const wh = req.body;
|
const wh = req.body;
|
||||||
|
|
||||||
// Ensure the secret is present, and matches
|
// Ensure the secret is present, and matches
|
||||||
if (!config['secret'] || wh['secret'] || config['secret'] !== wh['secret'])
|
console.log('secret: ' + config['secret'] + ', wh: ' + wh['secret']);
|
||||||
|
if (!config['secret'] || !wh['secret'] || config['secret'] !== wh['secret'])
|
||||||
return res.status(403).send('invalid secret token');
|
return res.status(403).send('invalid secret token');
|
||||||
|
|
||||||
// Reference the repository node, which must exist
|
// Reference the repository node, which must exist
|
||||||
@ -30,7 +82,7 @@ function webhook(config, req, res) {
|
|||||||
// Ensure the prefix is one that we recognize
|
// Ensure the prefix is one that we recognize
|
||||||
found_prefix = false;
|
found_prefix = false;
|
||||||
config.repo_prefixes.forEach(function(prefix) {
|
config.repo_prefixes.forEach(function(prefix) {
|
||||||
if (html_url.startsWtih(prefix)) {
|
if (html_url.startsWith(prefix)) {
|
||||||
found_prefix = true;
|
found_prefix = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -38,7 +90,8 @@ function webhook(config, req, res) {
|
|||||||
return res.status(403).send('prefix does not match');
|
return res.status(403).send('prefix does not match');
|
||||||
|
|
||||||
// Figure out where to place the repo
|
// Figure out where to place the repo
|
||||||
const website_url = new URL(repository['website']);
|
const website_url = url.parse(repository['website']);
|
||||||
|
console.log('website_url: ' + website_url + ', repo: ' + repository['website'] + ', pathname: ' + website_url.pathname);
|
||||||
if (!website_url || !website_url.pathname)
|
if (!website_url || !website_url.pathname)
|
||||||
return res.status(403).send('missing "website" parameter');
|
return res.status(403).send('missing "website" parameter');
|
||||||
|
|
||||||
@ -47,12 +100,14 @@ function webhook(config, req, res) {
|
|||||||
return res.status(403).send('"website" parameter is not valid');
|
return res.status(403).send('"website" parameter is not valid');
|
||||||
|
|
||||||
console.log(`deploying to ${path} at ${config.repo_root} from ${html_url}`);
|
console.log(`deploying to ${path} at ${config.repo_root} from ${html_url}`);
|
||||||
|
|
||||||
|
redeploy_repo(html_url, config.repo_root, path);
|
||||||
res.send('Ok');
|
res.send('Ok');
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
secret: "1234",
|
secret: "1234",
|
||||||
repo_root: "D:\\Code\\talkserved",
|
repo_root: "repo-root",
|
||||||
repo_prefixes: [
|
repo_prefixes: [
|
||||||
"https://git.xobs.io/xobs"
|
"https://git.xobs.io/xobs"
|
||||||
]
|
]
|
||||||
@ -63,4 +118,4 @@ app.use(bodyParser.urlencoded({ extended: true })); // Parse application/x-www-f
|
|||||||
|
|
||||||
app.post('/webhook', function (req, res) { return webhook(config, req, res); });
|
app.post('/webhook', function (req, res) { return webhook(config, req, res); });
|
||||||
app.use(express.static(config.repo_root));
|
app.use(express.static(config.repo_root));
|
||||||
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.18.3",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
|
"mkdirp": "^0.5.1",
|
||||||
"nodegit": "^0.23.0",
|
"nodegit": "^0.23.0",
|
||||||
"socket-io": "^1.0.0"
|
"socket-io": "^1.0.0"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user