<commit_before>'use strict';

let Heroku = require('heroku-client');
let archiver = require('archiver');
let request = require('request');
var fs = require('fs');
let uuid = require('node-uuid');
var os = require('os');

function uploadCwdToSource(app, cwd) {
    let tempFilePath = os.tmpdir() + uuid.v4() + '.tar.gz';

    return app.sources().create({})
	.then(function(source){
	    let archive = archiver('tar', { gzip: true });
	    archive.on('finish', function (err) {
		if (err) {
		    throw err;
		}
		let filesize = fs.statSync(tempFilePath).size;
		let request_options = {
		    url: source.source_blob.put_url,
		    headers: {
			'Content-Type': '',
			'Content-Length': filesize
		    }
		};

		var stream = fs.createReadStream(tempFilePath);
		stream.on('close', function() {
		    fs.unlink(tempFilePath);
		});

		stream .pipe(request.put(request_options));
	    });
	    let output = fs.createWriteStream(tempFilePath);
	    archive.pipe(output);
	    // TODO: Don't add things that we don't want
	    // eg. `.git` and things in `.gitignore`
	    archive.directory(cwd, false).finalize();

	    return source.source_blob.get_url;
	});
}

function create(context) {
    let heroku = new Heroku({ token: context.auth.password });
    let app = heroku.apps(context.app);

    var sourceUrl = context.args['source-url'];

    var sourceUrlPromise = sourceUrl ?
	new Promise(function(resolve) { resolve(sourceUrl);}) :
	uploadCwdToSource(app, context.cwd);

    sourceUrlPromise
	.then(function(sourceGetUrl) {
	    // TODO we have to bail out to `request` to get edge version
	    heroku.request({
		path: '/apps/' + context.app + '/builds',
		method: 'POST',
		headers: {
		    'Accept': 'application/vnd.heroku+json; version=edge'
		},
		parseJSON: true,
		body: {
		    source_blob: {
			url: sourceGetUrl,
			version: "" // TODO
		    }
		}
	    }).then(function(build) {
		request.get(build.stream_url).pipe(process.stderr);
	    }).done();
	});
}

module.exports = {
    topic: 'builds',
    command: 'create',
    needsAuth: true,
    needsApp: true,
    help: 'Create archive from current dir and build and release it to Heroku app',
    description: 'create build',
    flags: [],
    args: [
	{ name: 'source-url', optional: true }
    ],
    run: create
};<commit_msg>let user pass in version<commit_after>'use strict';

let Heroku = require('heroku-client');
let archiver = require('archiver');
let request = require('request');
var fs = require('fs');
let uuid = require('node-uuid');
var os = require('os');

function uploadCwdToSource(app, cwd) {
    let tempFilePath = os.tmpdir() + uuid.v4() + '.tar.gz';

    return app.sources().create({})
	.then(function(source){
	    let archive = archiver('tar', { gzip: true });
	    archive.on('finish', function (err) {
		if (err) {
		    throw err;
		}
		let filesize = fs.statSync(tempFilePath).size;
		let request_options = {
		    url: source.source_blob.put_url,
		    headers: {
			'Content-Type': '',
			'Content-Length': filesize
		    }
		};

		var stream = fs.createReadStream(tempFilePath);
		stream.on('close', function() {
		    fs.unlink(tempFilePath);
		});

		stream .pipe(request.put(request_options));
	    });
	    let output = fs.createWriteStream(tempFilePath);
	    archive.pipe(output);
	    // TODO: Don't add things that we don't want
	    // eg. `.git` and things in `.gitignore`
	    archive.directory(cwd, false).finalize();

	    return source.source_blob.get_url;
	});
}

function create(context) {
    let heroku = new Heroku({ token: context.auth.password });
    let app = heroku.apps(context.app);

    var sourceUrl = context.args['source-url'];

    var sourceUrlPromise = sourceUrl ?
	new Promise(function(resolve) { resolve(sourceUrl);}) :
	uploadCwdToSource(app, context.cwd);

    sourceUrlPromise
	.then(function(sourceGetUrl) {
	    // TODO we have to bail out to `request` to get edge version
	    heroku.request({
		path: '/apps/' + context.app + '/builds',
		method: 'POST',
		headers: {
		    'Accept': 'application/vnd.heroku+json; version=edge'
		},
		parseJSON: true,
		body: {
		    source_blob: {
			url: sourceGetUrl,
			// TODO provide better default, eg. archive md5
			version: context.args['version'] || ''
		    }
		}
	    }).then(function(build) {
		request.get(build.stream_url).pipe(process.stderr);
	    }).done();
	});
}

module.exports = {
    topic: 'builds',
    command: 'create',
    needsAuth: true,
    needsApp: true,
    help: 'Create archive from current dir and build and release it to Heroku app',
    description: 'create build',
    flags: [],
    args: [
	{ name: 'source-url', optional: true },
	{ name: 'version', optional: true }
    ],
    run: create
};
{'fraction_non_alphanumeric': 0.15729144555296115, 'fraction_numerical': 0.0006368074718743367, 'mean_word_length': 3.2260089686098654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2812172', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1514, 'n_words': 367}