Skip to content

Instantly share code, notes, and snippets.

@themasch
Last active September 2, 2016 14:42
Show Gist options
  • Save themasch/5c0470093c3dc949f8d48c65e9a26b9c to your computer and use it in GitHub Desktop.
Save themasch/5c0470093c3dc949f8d48c65e9a26b9c to your computer and use it in GitHub Desktop.
serve gitlab artifacts via http
"use strict";
const express = require('express');
const gitlab = require('gitlab');
const Zip = require('jszip');
const got = require('got');
const mimeType = require('mime-types');
const glClient = gitlab({url: 'https://my.cool.gitlab.instance.io', token: 'YOUR_TOKEN_HERE'});
const app = express();
let zipCache = {};
function getZipServer(project_web_url, build_id) {
let key = project_web_url + ':' + build_id;
if (!zipCache[key]) {
let url = project_web_url + '/builds/' + build_id + '/artifacts/download?private_token=YOUR_TOKEN_HERE';
zipCache[key] = got(url, {encoding: null})
.then(response => new Zip(response.body))
}
return zipCache[key];
}
let buildCache = {};
function getBuildInformation(project_name, build_id) {
let key = project_name + ":" + build_id;
if (!buildCache[key]) {
buildCache[key] = new Promise(resolve => {
glClient.projects.search(project_name, projects => {
let project = projects[0];
glClient.projects.builds.showBuild(project.id, build_id,
build => resolve({project, build})
);
})
})
}
return buildCache[key]
}
app.get('/project/:project_name/:build_id/*', (req, res) => {
let build_id = req.params.build_id;
let project_name = req.params.project_name;
let localUrl = req.params[0];
getBuildInformation(project_name, build_id)
.then(info => getZipServer(info.project.web_url, info.build.id))
.then(archive => {
let file = archive.file(localUrl);
let folder = archive.folder(localUrl);
if (file) {
res.append('Content-Type', mimeType.lookup(localUrl));
res.append('Cache-Control', 'public, max-age=31557600000');
archive.file(localUrl).async("nodebuffer").then(buffer => res.send(buffer));
} else if (folder) {
let content = folder.file(/.*/)
.map(file => file.name.replace(new RegExp('^' +localUrl), ''))
.map(filename => '<li><a href="' + filename + '">' + filename + '</a></li>').join('');
res.send('<ul>' + content + '</ul>');
} else {
res.status(404).end();
}
})
.catch(err => console.error(err));
});
app.listen(8080);
{
"name": "museum",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Mark Schmale <[email protected]>",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"gitlab": "^1.7.1",
"got": "^6.3.0",
"jszip": "^3.1.2",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment