Skip to content

Commit be57442

Browse files
authored
[7.17] Copy update-rest-api-json workflow from main (#2970)
1 parent 45d7e8c commit be57442

File tree

2 files changed

+42
-78
lines changed

2 files changed

+42
-78
lines changed

.github/download-artifacts/index.js

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const rimraf = require('rimraf')
2929
const fetch = require('node-fetch')
3030
const crossZip = require('cross-zip')
3131

32-
const { mkdir, rename, readdir } = promises
32+
const { mkdir, rename, readdir, unlink } = promises
3333
const pipeline = promisify(stream.pipeline)
3434
const unzip = promisify(crossZip.unzip)
3535
const rm = promisify(rimraf)
@@ -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)
@@ -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

166124
const options = minimist(process.argv.slice(2), {
167-
string: ['id', 'version', 'hash', 'branch']
125+
string: ['branch']
168126
})
169127
main(options).catch(t => {
170128
core.error(t)

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ name: Update rest-api-spec
33
on:
44
workflow_dispatch:
55
schedule:
6-
- cron: '0 */3 * * *' # At minute 0 past every 3rd hour.
6+
- cron: '0 4 * * *' # At 04:00.
77

88
jobs:
99
update-rest-api:
1010
name: Update rest-api-spec
1111
runs-on: ubuntu-latest
1212

1313
strategy:
14+
fail-fast: false
1415
matrix:
15-
branch: ['main', '8.1', '8.0', '7.17']
16+
branch: ['main', '8.x', '8.15', '7.17']
1617

1718
steps:
18-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@v4
1920
with:
2021
ref: ${{ matrix.branch }}
2122

@@ -29,31 +30,36 @@ jobs:
2930
npm install --prefix .github/download-artifacts
3031
npm install --prefix compiler
3132
npm install --prefix typescript-generator
33+
3234
- name: Download artifacts
3335
run: |
3436
node .github/download-artifacts/index.js --branch ${{ matrix.branch }}
37+
3538
- name: Generate output
3639
run: |
37-
SKIP_VERSION_UPDATE=true make contrib
40+
make contrib
41+
3842
- name: Debug git status
3943
run: |
4044
git status --porcelain
45+
4146
- name: Create Pull Request
42-
uses: peter-evans/create-pull-request@v3
47+
uses: peter-evans/create-pull-request@v6
4348
with:
4449
title: Update rest-api-spec ${{ matrix.branch }}
4550
body: 'As titled.'
4651
commit-message: 'Update rest-api-spec'
4752
labels: specification
4853
delete-branch: true
49-
team-reviewers: elastic/clients-team
54+
reviewers: Anaethelion,ezimuel,flobernd,JoshMock,l-trotta,miguelgrinberg,picandocodigo,pquentin,srikanthmanvi,swallez,technige
5055
branch: automated/rest-api-spec-update-${{ matrix.branch }}
5156

5257
- name: Open an issue if the action fails
5358
if: ${{ failure() }}
54-
uses: nashmaniac/create-issue-action@v1.1
59+
uses: nashmaniac/create-issue-action@v1.2
5560
with:
5661
title: rest-api-spec update failed
5762
token: ${{ secrets.GITHUB_TOKEN }}
5863
labels: bug
5964
body: The rest-api-spec action is currently failing, see [here](https://github.com/elastic/elasticsearch-specification/actions/workflows/update-rest-api-json.yml).
65+

0 commit comments

Comments
 (0)