|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -'use strict' |
21 |
| - |
22 | 20 | const { join } = require('path')
|
23 |
| -const minimist = require('minimist') |
24 | 21 | const stream = require('stream')
|
25 | 22 | const { promisify } = require('util')
|
26 | 23 | const { createWriteStream, promises } = require('fs')
|
27 |
| -const rimraf = require('rimraf') |
| 24 | +const { rimraf } = require('rimraf') |
28 | 25 | const fetch = require('node-fetch')
|
29 | 26 | const crossZip = require('cross-zip')
|
30 | 27 | const ora = require('ora')
|
31 | 28 |
|
32 |
| -const { mkdir, writeFile } = promises |
| 29 | +const { mkdir, cp } = promises |
33 | 30 | const pipeline = promisify(stream.pipeline)
|
34 | 31 | const unzip = promisify(crossZip.unzip)
|
35 |
| -const rm = promisify(rimraf) |
36 |
| - |
37 |
| -const esFolder = join(__dirname, '..', 'elasticsearch') |
38 |
| -const zipFolder = join(esFolder, 'artifacts.zip') |
39 |
| -const specFolder = join(esFolder, 'rest-api-spec', 'api') |
40 |
| -const freeTestFolder = join(esFolder, 'rest-api-spec', 'test', 'free') |
41 |
| -const xPackTestFolder = join(esFolder, 'rest-api-spec', 'test', 'platinum') |
42 |
| -const artifactInfo = join(esFolder, 'info.json') |
43 |
| - |
44 |
| -async function downloadArtifacts (opts) { |
45 |
| - if (typeof opts.version !== 'string') { |
46 |
| - throw new Error('Missing version') |
47 |
| - } |
48 | 32 |
|
49 |
| - const log = ora('Checking out spec and test').start() |
| 33 | +const testYamlFolder = join(__dirname, '..', 'yaml-rest-tests') |
| 34 | +const zipFile = join(__dirname, '..', 'elasticsearch-clients-tests.zip') |
50 | 35 |
|
51 |
| - log.text = 'Resolving versions' |
52 |
| - let resolved |
53 |
| - try { |
54 |
| - resolved = await resolve(opts.version, opts.hash) |
55 |
| - } catch (err) { |
56 |
| - log.fail(err.message) |
57 |
| - process.exit(1) |
58 |
| - } |
| 36 | +const schemaFolder = join(__dirname, '..', 'schema') |
| 37 | +const schemaJson = join(schemaFolder, 'schema.json') |
59 | 38 |
|
60 |
| - opts.id = opts.id || resolved.id |
61 |
| - opts.hash = opts.hash || resolved.hash |
62 |
| - opts.version = resolved.version |
| 39 | +async function downloadArtifacts (localTests, version = 'main') { |
| 40 | + const log = ora('Checking out spec and test').start() |
63 | 41 |
|
64 |
| - const info = loadInfo() |
| 42 | + const { GITHUB_TOKEN } = process.env |
65 | 43 |
|
66 |
| - if (info && info.version === opts.version) { |
67 |
| - if (info.hash === opts.hash && info.id === opts.id) { |
68 |
| - log.succeed('The artifact copy present locally is already up to date') |
69 |
| - return |
70 |
| - } |
| 44 | + if (version !== 'main') { |
| 45 | + version = version.split('.').slice(0, 2).join('.') |
71 | 46 | }
|
72 | 47 |
|
73 |
| - log.text = 'Cleanup checkouts/elasticsearch' |
74 |
| - await rm(esFolder) |
75 |
| - await mkdir(esFolder, { recursive: true }) |
| 48 | + log.text = 'Clean tests folder' |
| 49 | + await rimraf(testYamlFolder) |
| 50 | + await mkdir(testYamlFolder, { recursive: true }) |
76 | 51 |
|
77 |
| - log.text = 'Downloading artifacts' |
78 |
| - const response = await fetch(resolved.url) |
79 |
| - if (!response.ok) { |
80 |
| - log.fail(`unexpected response ${response.statusText}`) |
81 |
| - process.exit(1) |
82 |
| - } |
83 |
| - await pipeline(response.body, createWriteStream(zipFolder)) |
| 52 | + log.text = `Fetch test YAML files for version ${version}` |
84 | 53 |
|
85 |
| - log.text = 'Unzipping' |
86 |
| - await unzip(zipFolder, esFolder) |
| 54 | + if (localTests) { |
| 55 | + log.text = `Copying local tests from ${localTests}` |
| 56 | + await cp(localTests, testYamlFolder, { recursive: true }) |
| 57 | + } else { |
| 58 | + if (!GITHUB_TOKEN) { |
| 59 | + log.fail("Missing required environment variable 'GITHUB_TOKEN'") |
| 60 | + process.exit(1) |
| 61 | + } |
87 | 62 |
|
88 |
| - log.text = 'Cleanup' |
89 |
| - await rm(zipFolder) |
| 63 | + const response = await fetch(`https://api.github.com/repos/elastic/elasticsearch-clients-tests/zipball/${version}`, { |
| 64 | + headers: { |
| 65 | + Authorization: `Bearer ${GITHUB_TOKEN}`, |
| 66 | + Accept: 'application/vnd.github+json' |
| 67 | + } |
| 68 | + }) |
90 | 69 |
|
91 |
| - log.text = 'Update info' |
92 |
| - await writeFile(artifactInfo, JSON.stringify(opts), 'utf8') |
| 70 | + if (!response.ok) { |
| 71 | + log.fail(`unexpected response ${response.statusText}`) |
| 72 | + process.exit(1) |
| 73 | + } |
93 | 74 |
|
94 |
| - log.succeed('Done') |
95 |
| -} |
| 75 | + log.text = 'Downloading tests zipball' |
| 76 | + await pipeline(response.body, createWriteStream(zipFile)) |
96 | 77 |
|
97 |
| -function loadInfo () { |
98 |
| - try { |
99 |
| - return require(artifactInfo) |
100 |
| - } catch (err) { |
101 |
| - return null |
102 |
| - } |
103 |
| -} |
| 78 | + log.text = 'Unzipping tests' |
| 79 | + await unzip(zipFile, testYamlFolder) |
104 | 80 |
|
105 |
| -async function resolve (version, hash) { |
106 |
| - const response = await fetch(`https://artifacts-api.elastic.co/v1/versions/${version}`) |
107 |
| - if (!response.ok) { |
108 |
| - throw new Error(`unexpected response ${response.statusText}`) |
| 81 | + log.text = 'Cleanup' |
| 82 | + await rimraf(zipFile) |
109 | 83 | }
|
110 | 84 |
|
111 |
| - const data = await response.json() |
112 |
| - const esBuilds = data.version.builds |
113 |
| - .filter(build => build.projects.elasticsearch != null) |
114 |
| - .map(build => { |
115 |
| - return { |
116 |
| - projects: build.projects.elasticsearch, |
117 |
| - buildId: build.build_id, |
118 |
| - date: build.start_time, |
119 |
| - version: build.version |
120 |
| - } |
121 |
| - }) |
122 |
| - .sort((a, b) => { |
123 |
| - const dA = new Date(a.date) |
124 |
| - const dB = new Date(b.date) |
125 |
| - if (dA > dB) return -1 |
126 |
| - if (dA < dB) return 1 |
127 |
| - return 0 |
128 |
| - }) |
| 85 | + log.text = 'Fetching Elasticsearch specification' |
| 86 | + await rimraf(schemaFolder) |
| 87 | + await mkdir(schemaFolder, { recursive: true }) |
129 | 88 |
|
130 |
| - if (hash != null) { |
131 |
| - const build = esBuilds.find(build => build.projects.commit_hash === hash) |
132 |
| - if (!build) { |
133 |
| - throw new Error(`Can't find any build with hash '${hash}'`) |
134 |
| - } |
135 |
| - const zipKey = Object.keys(build.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip')) |
136 |
| - return { |
137 |
| - url: build.projects.packages[zipKey].url, |
138 |
| - id: build.buildId, |
139 |
| - hash: build.projects.commit_hash, |
140 |
| - version: build.version |
141 |
| - } |
| 89 | + const response = await fetch(`https://raw.githubusercontent.com/elastic/elasticsearch-specification/${version}/output/schema/schema.json`) |
| 90 | + if (!response.ok) { |
| 91 | + log.fail(`unexpected response ${response.statusText}`) |
| 92 | + process.exit(1) |
142 | 93 | }
|
143 | 94 |
|
144 |
| - const lastBuild = esBuilds[0] |
145 |
| - const zipKey = Object.keys(lastBuild.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip')) |
146 |
| - return { |
147 |
| - url: lastBuild.projects.packages[zipKey].url, |
148 |
| - id: lastBuild.buildId, |
149 |
| - hash: lastBuild.projects.commit_hash, |
150 |
| - version: lastBuild.version |
151 |
| - } |
| 95 | + log.text = 'Downloading schema.json' |
| 96 | + await pipeline(response.body, createWriteStream(schemaJson)) |
| 97 | + |
| 98 | + log.succeed('Done') |
152 | 99 | }
|
153 | 100 |
|
154 |
| -async function main (options) { |
155 |
| - delete options._ |
156 |
| - await downloadArtifacts(options) |
| 101 | +async function main () { |
| 102 | + await downloadArtifacts() |
157 | 103 | }
|
| 104 | + |
158 | 105 | if (require.main === module) {
|
159 | 106 | process.on('unhandledRejection', function (err) {
|
160 | 107 | console.error(err)
|
161 | 108 | process.exit(1)
|
162 | 109 | })
|
163 | 110 |
|
164 |
| - const options = minimist(process.argv.slice(2), { |
165 |
| - string: ['id', 'version', 'hash'] |
166 |
| - }) |
167 |
| - main(options).catch(t => { |
| 111 | + main().catch(t => { |
168 | 112 | console.log(t)
|
169 | 113 | process.exit(2)
|
170 | 114 | })
|
171 | 115 | }
|
172 | 116 |
|
173 | 117 | module.exports = downloadArtifacts
|
174 |
| -module.exports.locations = { |
175 |
| - specFolder, |
176 |
| - freeTestFolder, |
177 |
| - xPackTestFolder |
178 |
| -} |
| 118 | +module.exports.locations = { testYamlFolder, zipFile, schemaJson } |
0 commit comments