|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +# |
| 13 | + |
| 14 | +import json |
| 15 | +import urllib.request |
| 16 | + |
| 17 | +ADOPTIUM_API_URL = "https://api.adoptium.net/v3/info/available_releases" |
| 18 | + |
| 19 | + |
| 20 | +def _fetch_release_data(): |
| 21 | + """Fetch release info from the Adoptium API.""" |
| 22 | + req = urllib.request.Request( |
| 23 | + ADOPTIUM_API_URL, |
| 24 | + headers={"User-Agent": "Adoptium Dockerfile Updater"}, |
| 25 | + ) |
| 26 | + with urllib.request.urlopen(req) as response: |
| 27 | + return json.loads(response.read().decode("utf-8")) |
| 28 | + |
| 29 | + |
| 30 | +def get_supported_versions(): |
| 31 | + """Fetch supported versions from the Adoptium API. |
| 32 | +
|
| 33 | + Returns all LTS versions plus any non-LTS versions between the most |
| 34 | + recent LTS and the most recent feature release (inclusive). |
| 35 | +
|
| 36 | + For example, if LTS versions are [8, 11, 17, 21, 25] and the most |
| 37 | + recent feature release is 26, this returns [8, 11, 17, 21, 25, 26]. |
| 38 | + """ |
| 39 | + data = _fetch_release_data() |
| 40 | + |
| 41 | + lts_versions = set(data["available_lts_releases"]) |
| 42 | + most_recent_lts = data["most_recent_lts"] |
| 43 | + most_recent_feature = data["most_recent_feature_release"] |
| 44 | + |
| 45 | + # All LTS versions + anything between latest LTS and most recent feature release |
| 46 | + versions = set(lts_versions) |
| 47 | + for v in range(most_recent_lts + 1, most_recent_feature + 1): |
| 48 | + if v in data["available_releases"]: |
| 49 | + versions.add(v) |
| 50 | + |
| 51 | + return sorted(versions) |
| 52 | + |
| 53 | + |
| 54 | +def get_latest_lts(): |
| 55 | + """Return the most recent LTS version number.""" |
| 56 | + data = _fetch_release_data() |
| 57 | + return data["most_recent_lts"] |
0 commit comments