@@ -29,7 +29,7 @@ const rimraf = require('rimraf')
29
29
const fetch = require ( 'node-fetch' )
30
30
const crossZip = require ( 'cross-zip' )
31
31
32
- const { mkdir, rename, readdir } = promises
32
+ const { mkdir, rename, readdir, unlink } = promises
33
33
const pipeline = promisify ( stream . pipeline )
34
34
const unzip = promisify ( crossZip . unzip )
35
35
const rm = promisify ( rimraf )
@@ -40,23 +40,21 @@ const downloadedSpec = join(esFolder, 'rest-api-spec', 'api')
40
40
const specFolder = join ( __dirname , '..' , '..' , 'specification' , '_json_spec' )
41
41
42
42
async 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' )
45
45
}
46
46
47
- core . info ( 'Checking out spec and test ' )
47
+ core . info ( 'Resolving artifact URL ' )
48
48
49
- core . info ( 'Resolving version' )
50
49
let resolved
51
50
try {
52
- resolved = await resolve ( opts . version || fromBranch ( opts . branch ) , opts . hash )
51
+ resolved = await resolve ( opts . branch )
53
52
} catch ( err ) {
54
53
core . error ( err . message )
55
54
process . exit ( 1 )
56
55
}
57
56
58
- opts . version = resolved . version
59
- core . info ( `Resolved version ${ opts . version } ` )
57
+ core . info ( `Resolved artifact URL for ${ resolved . commit_url } ` )
60
58
61
59
core . info ( 'Cleanup' )
62
60
await rm ( esFolder )
@@ -85,77 +83,37 @@ async function downloadArtifacts (opts) {
85
83
await rename ( join ( downloadedSpec , file ) , join ( specFolder , file ) )
86
84
}
87
85
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 ) )
96
91
}
97
- const { versions } = await response . json ( )
98
- version = versions . pop ( )
99
92
}
100
93
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 )
103
100
if ( ! response . ok ) {
104
- throw new Error ( `unexpected response ${ response . statusText } ` )
101
+ throw new Error ( `Unexpected response. Invalid version? ${ url } : ${ response . statusText } ` )
105
102
}
106
-
107
103
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
- }
139
104
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 } ` )
147
109
}
148
- }
110
+ const manifestData = await manifestResponse . json ( )
111
+ const elasticsearch = manifestData . projects . elasticsearch
112
+ const restResourceName = `rest-resources-zip-${ manifestData . version } .zip`
149
113
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 ,
159
117
}
160
118
}
161
119
@@ -164,7 +122,7 @@ async function main (options) {
164
122
}
165
123
166
124
const options = minimist ( process . argv . slice ( 2 ) , {
167
- string : [ 'id' , 'version' , 'hash' , ' branch']
125
+ string : [ 'branch' ]
168
126
} )
169
127
main ( options ) . catch ( t => {
170
128
core . error ( t )
0 commit comments