@@ -29,7 +29,7 @@ const rimraf = require('rimraf')
2929const fetch = require ( 'node-fetch' )
3030const crossZip = require ( 'cross-zip' )
3131
32- const { mkdir, rename, readdir } = promises
32+ const { mkdir, rename, readdir, unlink } = promises
3333const pipeline = promisify ( stream . pipeline )
3434const unzip = promisify ( crossZip . unzip )
3535const rm = promisify ( rimraf )
@@ -40,23 +40,21 @@ const downloadedSpec = join(esFolder, 'rest-api-spec', 'api')
4040const specFolder = join ( __dirname , '..' , '..' , 'specification' , '_json_spec' )
4141
4242async function downloadArtifacts ( opts ) {
43- if ( typeof opts . version !== 'string' && typeof opts . branch !== 'string' ) {
44- throw new Error ( 'Missing version or branch' )
43+ if ( typeof opts . branch !== 'string' ) {
44+ throw new Error ( 'Missing branch' )
4545 }
4646
47- core . info ( 'Checking out spec and test ' )
47+ core . info ( 'Resolving artifact URL ' )
4848
49- core . info ( 'Resolving version' )
5049 let resolved
5150 try {
52- resolved = await resolve ( opts . version || fromBranch ( opts . branch ) , opts . hash )
51+ resolved = await resolve ( opts . branch )
5352 } catch ( err ) {
5453 core . error ( err . message )
5554 process . exit ( 1 )
5655 }
5756
58- opts . version = resolved . version
59- core . info ( `Resolved version ${ opts . version } ` )
57+ core . info ( `Resolved artifact URL for ${ resolved . commit_url } ` )
6058
6159 core . info ( 'Cleanup' )
6260 await rm ( esFolder )
@@ -85,77 +83,37 @@ async function downloadArtifacts (opts) {
8583 await rename ( join ( downloadedSpec , file ) , join ( specFolder , file ) )
8684 }
8785
88- core . info ( 'Done' )
89- }
90-
91- async function resolve ( version , hash ) {
92- if ( version === 'latest' ) {
93- const response = await fetch ( 'https://artifacts-api.elastic.co/v1/versions' )
94- if ( ! response . ok ) {
95- throw new Error ( `unexpected response ${ response . statusText } ` )
86+ /** Delete files that weren't in the zip file */
87+ const specFiles = await readdir ( specFolder )
88+ for ( const file of specFiles ) {
89+ if ( ! files . includes ( file ) ) {
90+ await unlink ( join ( specFolder , file ) )
9691 }
97- const { versions } = await response . json ( )
98- version = versions . pop ( )
9992 }
10093
101- core . info ( `Resolving version ${ version } ` )
102- const response = await fetch ( `https://artifacts-api.elastic.co/v1/versions/${ version } ` )
94+ core . info ( 'Done' )
95+ }
96+
97+ async function resolve ( branch ) {
98+ const url = `https://artifacts-snapshot.elastic.co/elasticsearch/latest/${ branch } .json`
99+ const response = await fetch ( url )
103100 if ( ! response . ok ) {
104- throw new Error ( `unexpected response ${ response . statusText } ` )
101+ throw new Error ( `Unexpected response. Invalid version? ${ url } : ${ response . statusText } ` )
105102 }
106-
107103 const data = await response . json ( )
108- const esBuilds = data . version . builds
109- . filter ( build => build . projects . elasticsearch != null )
110- . map ( build => {
111- return {
112- projects : build . projects . elasticsearch ,
113- buildId : build . build_id ,
114- date : build . start_time ,
115- version : build . version
116- }
117- } )
118- . sort ( ( a , b ) => {
119- const dA = new Date ( a . date )
120- const dB = new Date ( b . date )
121- if ( dA > dB ) return - 1
122- if ( dA < dB ) return 1
123- return 0
124- } )
125-
126- if ( hash != null ) {
127- const build = esBuilds . find ( build => build . projects . commit_hash === hash )
128- if ( ! build ) {
129- throw new Error ( `Can't find any build with hash '${ hash } '` )
130- }
131- const zipKey = Object . keys ( build . projects . packages ) . find ( key => key . startsWith ( 'rest-resources-zip-' ) && key . endsWith ( '.zip' ) )
132- return {
133- url : build . projects . packages [ zipKey ] . url ,
134- id : build . buildId ,
135- hash : build . projects . commit_hash ,
136- version : build . version
137- }
138- }
139104
140- const lastBuild = esBuilds [ 0 ]
141- const zipKey = Object . keys ( lastBuild . projects . packages ) . find ( key => key . startsWith ( 'rest-resources-zip-' ) && key . endsWith ( '.zip' ) )
142- return {
143- url : lastBuild . projects . packages [ zipKey ] . url ,
144- id : lastBuild . buildId ,
145- hash : lastBuild . projects . commit_hash ,
146- version : lastBuild . version
105+ let manifest_url = data . manifest_url
106+ const manifestResponse = await fetch ( manifest_url )
107+ if ( ! manifestResponse . ok ) {
108+ throw new Error ( `Unexpected manifestResponse. ${ manifest_url } : ${ manifestResponse . statusText } ` )
147109 }
148- }
110+ const manifestData = await manifestResponse . json ( )
111+ const elasticsearch = manifestData . projects . elasticsearch
112+ const restResourceName = `rest-resources-zip-${ manifestData . version } .zip`
149113
150- function fromBranch ( branch ) {
151- if ( branch === 'main' ) {
152- return 'latest'
153- } else if ( branch === '7.x' ) {
154- return '7.x-SNAPSHOT'
155- } else if ( ( branch . startsWith ( '7.' ) || branch . startsWith ( '8.' ) ) && ! isNaN ( Number ( branch . split ( '.' ) [ 1 ] ) ) ) {
156- return `${ branch } -SNAPSHOT`
157- } else {
158- throw new Error ( `Cannot derive version from branch '${ branch } '` )
114+ return {
115+ url : elasticsearch . packages [ restResourceName ] . url ,
116+ commit_url : elasticsearch . commit_url ,
159117 }
160118}
161119
@@ -164,7 +122,7 @@ async function main (options) {
164122}
165123
166124const options = minimist ( process . argv . slice ( 2 ) , {
167- string : [ 'id' , 'version' , 'hash' , ' branch']
125+ string : [ 'branch' ]
168126} )
169127main ( options ) . catch ( t => {
170128 core . error ( t )
0 commit comments