From f93a4b606444aee7ecec6fd60c39377a9c58bbd8 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 11 Jan 2019 16:01:48 +0800 Subject: [PATCH] git: fix git checkout Signed-off-by: Sean Cross --- index.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++---- package.json | 1 + 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 40e8a6b..7a1929c 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,62 @@ const Git = require('nodegit'); const express = require('express'); const bodyParser = require('body-parser'); const url = require('url'); +const mkdirp = require('mkdirp'); const app = express(); const port = 9119; 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) { if (req.get('X-GitHub-Event') !== '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; // 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'); // Reference the repository node, which must exist @@ -30,7 +82,7 @@ function webhook(config, req, res) { // Ensure the prefix is one that we recognize found_prefix = false; config.repo_prefixes.forEach(function(prefix) { - if (html_url.startsWtih(prefix)) { + if (html_url.startsWith(prefix)) { found_prefix = true; } }); @@ -38,7 +90,8 @@ function webhook(config, req, res) { return res.status(403).send('prefix does not match'); // 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) 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'); console.log(`deploying to ${path} at ${config.repo_root} from ${html_url}`); + + redeploy_repo(html_url, config.repo_root, path); res.send('Ok'); } const config = { secret: "1234", - repo_root: "D:\\Code\\talkserved", + repo_root: "repo-root", repo_prefixes: [ "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.use(express.static(config.repo_root)); -app.listen(port, () => console.log(`Example app listening on port ${port}!`)); \ No newline at end of file +app.listen(port, () => console.log(`Example app listening on port ${port}!`)); diff --git a/package.json b/package.json index 88ddd01..a728955 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dependencies": { "body-parser": "^1.18.3", "express": "^4.16.4", + "mkdirp": "^0.5.1", "nodegit": "^0.23.0", "socket-io": "^1.0.0" }