Skip to content

Commit 0464df5

Browse files
authored
Fetch JSON spec from Artifacts Snapshot API (#2949)
1 parent 593ad71 commit 0464df5

File tree

2 files changed

+21
-71
lines changed

2 files changed

+21
-71
lines changed

.github/download-artifacts/index.js

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,21 @@ const downloadedSpec = join(esFolder, 'rest-api-spec', 'api')
4040
const specFolder = join(__dirname, '..', '..', 'specification', '_json_spec')
4141

4242
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')
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)
@@ -96,74 +94,26 @@ async function downloadArtifacts (opts) {
9694
core.info('Done')
9795
}
9896

99-
async function resolve (version, hash) {
100-
if (version === 'latest') {
101-
const response = await fetch('https://artifacts-api.elastic.co/v1/versions')
102-
if (!response.ok) {
103-
throw new Error(`unexpected response ${response.statusText}`)
104-
}
105-
const { versions } = await response.json()
106-
version = versions.pop()
107-
}
108-
109-
core.info(`Resolving version ${version}`)
110-
const response = await fetch(`https://artifacts-api.elastic.co/v1/versions/${version}`)
97+
async function resolve (branch) {
98+
const url = `https://artifacts-snapshot.elastic.co/elasticsearch/latest/${branch}.json`
99+
const response = await fetch(url)
111100
if (!response.ok) {
112-
throw new Error(`unexpected response ${response.statusText}`)
101+
throw new Error(`Unexpected response. Invalid version? ${url}: ${response.statusText}`)
113102
}
114-
115103
const data = await response.json()
116-
const esBuilds = data.version.builds
117-
.filter(build => build.projects.elasticsearch != null)
118-
.map(build => {
119-
return {
120-
projects: build.projects.elasticsearch,
121-
buildId: build.build_id,
122-
date: build.start_time,
123-
version: build.version
124-
}
125-
})
126-
.sort((a, b) => {
127-
const dA = new Date(a.date)
128-
const dB = new Date(b.date)
129-
if (dA > dB) return -1
130-
if (dA < dB) return 1
131-
return 0
132-
})
133-
134-
if (hash != null) {
135-
const build = esBuilds.find(build => build.projects.commit_hash === hash)
136-
if (!build) {
137-
throw new Error(`Can't find any build with hash '${hash}'`)
138-
}
139-
const zipKey = Object.keys(build.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip'))
140-
return {
141-
url: build.projects.packages[zipKey].url,
142-
id: build.buildId,
143-
hash: build.projects.commit_hash,
144-
version: build.version
145-
}
146-
}
147104

148-
const lastBuild = esBuilds[0]
149-
const zipKey = Object.keys(lastBuild.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip'))
150-
return {
151-
url: lastBuild.projects.packages[zipKey].url,
152-
id: lastBuild.buildId,
153-
hash: lastBuild.projects.commit_hash,
154-
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}`)
155109
}
156-
}
110+
const manifestData = await manifestResponse.json()
111+
const elasticsearch = manifestData.projects.elasticsearch
112+
const restResourceName = `rest-resources-zip-${manifestData.version}.zip`
157113

158-
function fromBranch (branch) {
159-
if (branch === 'main') {
160-
return 'latest'
161-
} else if (branch === '7.x') {
162-
return '7.x-SNAPSHOT'
163-
} else if ((branch.startsWith('7.') || branch.startsWith('8.')) && !isNaN(Number(branch.split('.')[1]))) {
164-
return `${branch}-SNAPSHOT`
165-
} else {
166-
throw new Error(`Cannot derive version from branch '${branch}'`)
114+
return {
115+
url: elasticsearch.packages[restResourceName].url,
116+
commit_url: elasticsearch.commit_url,
167117
}
168118
}
169119

@@ -172,7 +122,7 @@ async function main (options) {
172122
}
173123

174124
const options = minimist(process.argv.slice(2), {
175-
string: ['id', 'version', 'hash', 'branch']
125+
string: ['branch']
176126
})
177127
main(options).catch(t => {
178128
core.error(t)

.github/workflows/update-rest-api-json.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
3838
- name: Generate output
3939
run: |
40-
SKIP_VERSION_UPDATE=true make contrib
40+
make contrib
4141
4242
- name: Debug git status
4343
run: |

0 commit comments

Comments
 (0)