diff --git a/.gitignore b/.gitignore index 6b8913e18..c4bc2f3fa 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ scripts/scratch* test/integration/yaml_suite/log ## generated files +scripts/last_rest_spec_update.sha test/browser_integration/*.xml test/browser_integration/yaml_tests.js diff --git a/scripts/generate/js_api/index.js b/scripts/generate/js_api/index.js index c33f4ddda..bf87df017 100644 --- a/scripts/generate/js_api/index.js +++ b/scripts/generate/js_api/index.js @@ -5,10 +5,12 @@ var fs = require('fs'); var mkdirp = require('mkdirp'); var templates = require('./templates'); var clean = require('../../clean'); -var urlParamRE = /\{(\w+)\}/g; +var restSpecUpdated = require('../../rest_spec_updated'); +var urlParamRE = /\{(\w+)\}/g; var outputPath = _.joinPath(__dirname, '../../../src/lib/api.js'); var docOutputPath = _.joinPath(__dirname, '../../../docs/api.md'); +var lastFetchTmp = path.join(__dirname, './last_fetch.tmp'); function download() { require('./actions').on('ready', function (actions) { @@ -34,15 +36,8 @@ function download() { }); } -if (process.env.FORCE_GEN) { - download(); -} else { - try { - var stat = fs.statSync(outputPath); - if (!stat.isFile() || stat.ctime < Date.now() - 86400000) { - download(); - } - } catch (e) { +restSpecUpdated(function (err, updated) { + if (process.env.FORCE_GEN || err || updated) { download(); } -} +}); diff --git a/scripts/generate/yaml_tests/index.js b/scripts/generate/yaml_tests/index.js index ad33ac29c..96ed52ed2 100644 --- a/scripts/generate/yaml_tests/index.js +++ b/scripts/generate/yaml_tests/index.js @@ -8,6 +8,7 @@ var jsYaml = require('js-yaml'); var spec = require('../../get_spec'); var clean = require('../../clean'); var _ = require('../../../src/lib/utils'); +var restSpecUpdated = require('../../rest_spec_updated'); var testFile = path.resolve(__dirname, '../../../test/integration/yaml_suite/yaml_tests.json'); @@ -30,15 +31,9 @@ function download() { }); } -if (process.env.FORCE_GEN) { - download(); -} else { - try { - var stat = fs.statSync(testFile); - if (!stat.isFile() || stat.ctime < Date.now() - 86400000) { - download(); - } - } catch (e) { + +restSpecUpdated(function (err, updated) { + if (process.env.FORCE_GEN || err || updated) { download(); } -} +}); diff --git a/scripts/get_spec.js b/scripts/get_spec.js index 293b91fd1..ba758edde 100644 --- a/scripts/get_spec.js +++ b/scripts/get_spec.js @@ -17,7 +17,6 @@ exports.get = function (pattern) { if (incoming.statusCode !== 200) { req.abort(); if (incoming.headers.location) { - console.log('redirecting to', incoming.headers.location); req = https.get(_.extend( url.parse(tarUrl), url.parse(incoming.headers.location) diff --git a/scripts/rest_spec_updated.js b/scripts/rest_spec_updated.js new file mode 100644 index 000000000..b17c458e8 --- /dev/null +++ b/scripts/rest_spec_updated.js @@ -0,0 +1,64 @@ +var https = require('https'); +var lastCommitUrl = 'https://api.github.com/repos/elasticsearch/elasticsearch-rest-api-spec/commits/HEAD'; +var fs = require('fs'); + +var lastRestSpecUpdateFile = __dirname + '/last_rest_spec_update.sha'; +var lastRestSpecUpdate; +var updated; + +if (fs.existsSync(lastRestSpecUpdateFile)) { + lastRestSpecUpdate = fs.readFileSync(lastRestSpecUpdateFile, 'utf8'); +} + +var req = https.get(lastCommitUrl, function (incoming) { + if (incoming.statusCode !== 200) { + req.abort(); + console.error('request for last commit failed', incoming.statusCode, incoming.headers); + return; + } + + var body = ''; + + incoming.on('data', onData); + incoming.on('end', onEnd); + + function onData(chunk) { + body += chunk; + } + + function onEnd() { + incoming.removeListener('data', onData); + incoming.removeListener('end', onEnd); + var _req = req; + req = null; + + var resp; + try { + resp = JSON.parse(body); + } catch (e) { + console.log('unable to parse response from github'); + _req.emit('ready'); + return; + } + + if (lastRestSpecUpdate === resp.sha) { + updated = false; + } else { + updated = true; + fs.writeFileSync(lastRestSpecUpdateFile, resp.sha); + } + _req.emit('ready'); + } +}); + +module.exports = function (cb) { + function done() { + cb(null, updated); + } + + if (req) { + req.on('ready', done); + } else { + process.nextTick(done); + } +}; diff --git a/test/browser_integration/server.js b/test/browser_integration/server.js index 48c174fbf..b4585c499 100644 --- a/test/browser_integration/server.js +++ b/test/browser_integration/server.js @@ -48,7 +48,8 @@ function sendBundle(req, resp, files, opts, extend) { function collectTestResults(req, resp) { var body = ''; - var logFilename = path.join(__dirname, 'test-output-' + req.query.browser + '.xml'); + var browser = req.query.browser; + var logFilename = path.join(__dirname, 'test-output-' + browser + '.xml'); req.on('data', function (chunk) { body += chunk; @@ -108,7 +109,7 @@ function collectTestResults(req, resp) { id: suiteCount++, name: suiteInfo.name, timestamp: moment(suiteInfo.start).toJSON(), - hostname: 'localhost', + hostname: browser, tests: (suiteInfo.results && suiteInfo.results.length) || 0, failures: _.where(suiteInfo.results, {pass: false}).length, errors: 0, @@ -119,7 +120,7 @@ function collectTestResults(req, resp) { var testcase = suite.ele('testcase', { name: testInfo.name, time: (testInfo.time || 0) / 1000, - classname: suiteInfo.name + classname: browser + '.' + suiteInfo.name.replace(/\.yaml$/, '').replace(/\//g, '.') }); if (testInfo.errMsg) {