Skip to content

Commit d5536ac

Browse files
authored
Automatically suggest updates based on JDT-LS Java language support.
- Checks for JDK updates, that JDT-LS adopted the language feature, and creates a PR - Create bump-jdk.yml workflow - Create check_and_update_jdk.py script
1 parent 174e8a8 commit d5536ac

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
import requests
5+
import json
6+
import ast
7+
8+
readme_ver_pattern = r'(?:(?<=Supports code from Java 1\.5 to Java )(\d+)|(?<=JavaSE-)~|(?<=path/to/jdk-)~)' # After resolving current_jdk, we will replace ~
9+
10+
# Query the Oracle website for the latest JDK version
11+
response = requests.get('http://javadl-esd-secure.oracle.com/update/baseline.version')
12+
latest_jdk = re.search(r'(?P<major>\d+)\.', response.text)
13+
if latest_jdk is None:
14+
print('Failed to retrieve latest JDK version')
15+
exit(1)
16+
latest_jdk = latest_jdk.group('major')
17+
print(f'Latest JDK version: {latest_jdk}')
18+
19+
# Query the vscode-java repo for the current supported JDK version
20+
with open('README.md', 'r') as f: # Open the README.md file in read mode
21+
readme = f.read() # Read the file content as a string
22+
current_jdk = re.search(readme_ver_pattern, readme) # Search for the JDK version in the string
23+
if current_jdk is None:
24+
print('Failed to retrieve current JDK version')
25+
exit(1)
26+
current_jdk = current_jdk.group(1)
27+
print(f'Current supported JDK version: {current_jdk}')
28+
29+
# If the latest JDK version is not the same as the current supported JDK version, check the test status and update the files
30+
if latest_jdk != current_jdk:
31+
print(f'New JDK version detected: {latest_jdk}')
32+
# Create a formatted string template from the URI structure
33+
uri_base = 'https://ci.eclipse.org/ls/job/jdt-ls-master/lastCompletedBuild/testReport/org.eclipse.jdt.ls.core.internal.{package}/{java_class}/{method}/api/python'
34+
# Define the test URLs to check using the template and list comprehension
35+
tests = [
36+
uri_base.format(package='correction', java_class='ModifierCorrectionsQuickFixTest', method=m) for m in ['testAddSealedMissingClassModifierProposal', 'testAddSealedAsDirectSuperClass', 'testAddPermitsToDirectSuperClass']
37+
] + [
38+
uri_base.format(package='correction', java_class='UnresolvedTypesQuickFixTest', method='testTypeInSealedTypeDeclaration')
39+
] + [
40+
uri_base.format(package='managers', java_class=c, method=m) for c, m in [('EclipseProjectImporterTest', 'testPreviewFeaturesDisabledByDefault'), ('InvisibleProjectImporterTest', 'testPreviewFeaturesEnabledByDefault'), ('MavenProjectImporterTest', f'testJava{latest_jdk}Project')]
41+
]
42+
43+
# Check the test status for each test URL
44+
all_tests_passed = True
45+
for i in range(len(tests)):
46+
response = requests.get(tests[i])
47+
data = ast.literal_eval(response.text) # Use ast.literal_eval, because response.json() fails
48+
try:
49+
if data['status'] != 'PASSED':
50+
print(f'Test #{i + 1} failed ({tests[i]})')
51+
all_tests_passed = False
52+
break
53+
except KeyError:
54+
print(f'Test #{i + 1} not found ({tests[i]})')
55+
all_tests_passed = False
56+
break
57+
58+
# If all tests passed, update the README.md and the package.json files
59+
if all_tests_passed:
60+
print('All tests passed')
61+
62+
# Replace the ~ with current_jdk
63+
readme_ver_pattern = re.sub('~', current_jdk, readme_ver_pattern)
64+
65+
# Write this to a file for the create-pull-request workflow
66+
with open('latest_jdk.txt', 'w') as f:
67+
f.write(latest_jdk)
68+
69+
# Replace the current supported JDK version with the latest JDK version
70+
readme = re.sub(readme_ver_pattern, latest_jdk, readme)
71+
72+
# Write the updated README.md file
73+
with open('README.md', 'w') as f:
74+
f.write(readme)
75+
76+
# Read the package.json file
77+
with open('package.json', 'r') as f:
78+
package = json.load(f)
79+
80+
# Add the latest JDK version to the java.configuration.runtimes array
81+
jdks_config = next(filter(lambda e: e['id'] == "java-jdks", package['contributes']['configuration']))
82+
jdks_config['properties']['java.configuration.runtimes']['items']['properties']['name']['enum'].append(f'JavaSE-{latest_jdk}')
83+
84+
# Write the updated package.json file
85+
with open('package.json', 'w') as f:
86+
json.dump(package, f, indent=2)
87+
else:
88+
print('Some tests failed, aborting update')
89+
exit(1)
90+
else:
91+
print('No new JDK version detected, nothing to do')
92+
exit(0)
93+
exit(0)

.github/workflows/bump-jdk.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Update JDK Version
2+
on:
3+
schedule:
4+
- cron: '0 10 * * *'
5+
workflow_dispatch:
6+
jobs:
7+
update-jdk-version:
8+
runs-on: ubuntu-latest
9+
env:
10+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.x'
19+
20+
- name: Install dependencies
21+
run: |
22+
python -m pip install --upgrade pip
23+
pip install requests
24+
25+
- name: Check and update JDK version
26+
run: |
27+
if [ -f latest_jdk.txt ]; then
28+
rm latest_jdk.txt # Remove previous records
29+
fi
30+
python .github/scripts/check_and_update_jdk.py
31+
32+
- name: Read latest JDK version from file
33+
run: |
34+
if [ -f latest_jdk.txt ]; then
35+
version=$(cat latest_jdk.txt)
36+
echo "Latest JDK version: $version"
37+
echo "latest_jdk=$version" >> $GITHUB_ENV # set the latest_jdk environment variable
38+
else
39+
echo "No new JDK version detected, nothing to do"
40+
exit 0
41+
fi
42+
43+
- name: Check for existing PR
44+
id: check_pr
45+
run: |
46+
pr_number=$(gh pr list --search "Found JavaSE version ${{ env.latest_jdk }}" --json number --jq '.[0].number')
47+
echo "pr_number=$pr_number" >> $GITHUB_ENV
48+
49+
- name: Branch and push changes
50+
if: ${{ success() && env.latest_jdk != '' && steps.check_pr.outputs.pr_number == '' }}
51+
run: |
52+
git config --global user.email "[email protected]"
53+
git config --global user.name "redhattools-bot"
54+
git checkout -b "update-jdk-${{ env.latest_jdk }}"
55+
git commit -am "Bump JDK to ${{ env.latest_jdk }}"
56+
git push origin "update-jdk-${{ env.latest_jdk }}"
57+
gh pr create --title "Found JavaSE version ${{ env.latest_jdk }}" --body "See [Raw logs](https://github.com/${{ github.repository }}/commit/${{ github.sha }}/checks/${{ github.check_run_id }}/logs)"
58+

0 commit comments

Comments
 (0)