Skip to content

Commit

Permalink
Automatically suggest updates based on JDT-LS Java language support.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
mavaddat authored Jan 12, 2024
1 parent 174e8a8 commit d5536ac
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
93 changes: 93 additions & 0 deletions .github/scripts/check_and_update_jdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python3

import re
import requests
import json
import ast

readme_ver_pattern = r'(?:(?<=Supports code from Java 1\.5 to Java )(\d+)|(?<=JavaSE-)~|(?<=path/to/jdk-)~)' # After resolving current_jdk, we will replace ~

# Query the Oracle website for the latest JDK version
response = requests.get('http://javadl-esd-secure.oracle.com/update/baseline.version')
latest_jdk = re.search(r'(?P<major>\d+)\.', response.text)
if latest_jdk is None:
print('Failed to retrieve latest JDK version')
exit(1)
latest_jdk = latest_jdk.group('major')
print(f'Latest JDK version: {latest_jdk}')

# Query the vscode-java repo for the current supported JDK version
with open('README.md', 'r') as f: # Open the README.md file in read mode
readme = f.read() # Read the file content as a string
current_jdk = re.search(readme_ver_pattern, readme) # Search for the JDK version in the string
if current_jdk is None:
print('Failed to retrieve current JDK version')
exit(1)
current_jdk = current_jdk.group(1)
print(f'Current supported JDK version: {current_jdk}')

# If the latest JDK version is not the same as the current supported JDK version, check the test status and update the files
if latest_jdk != current_jdk:
print(f'New JDK version detected: {latest_jdk}')
# Create a formatted string template from the URI structure
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'
# Define the test URLs to check using the template and list comprehension
tests = [
uri_base.format(package='correction', java_class='ModifierCorrectionsQuickFixTest', method=m) for m in ['testAddSealedMissingClassModifierProposal', 'testAddSealedAsDirectSuperClass', 'testAddPermitsToDirectSuperClass']
] + [
uri_base.format(package='correction', java_class='UnresolvedTypesQuickFixTest', method='testTypeInSealedTypeDeclaration')
] + [
uri_base.format(package='managers', java_class=c, method=m) for c, m in [('EclipseProjectImporterTest', 'testPreviewFeaturesDisabledByDefault'), ('InvisibleProjectImporterTest', 'testPreviewFeaturesEnabledByDefault'), ('MavenProjectImporterTest', f'testJava{latest_jdk}Project')]
]

# Check the test status for each test URL
all_tests_passed = True
for i in range(len(tests)):
response = requests.get(tests[i])
data = ast.literal_eval(response.text) # Use ast.literal_eval, because response.json() fails
try:
if data['status'] != 'PASSED':
print(f'Test #{i + 1} failed ({tests[i]})')
all_tests_passed = False
break
except KeyError:
print(f'Test #{i + 1} not found ({tests[i]})')
all_tests_passed = False
break

# If all tests passed, update the README.md and the package.json files
if all_tests_passed:
print('All tests passed')

# Replace the ~ with current_jdk
readme_ver_pattern = re.sub('~', current_jdk, readme_ver_pattern)

# Write this to a file for the create-pull-request workflow
with open('latest_jdk.txt', 'w') as f:
f.write(latest_jdk)

# Replace the current supported JDK version with the latest JDK version
readme = re.sub(readme_ver_pattern, latest_jdk, readme)

# Write the updated README.md file
with open('README.md', 'w') as f:
f.write(readme)

# Read the package.json file
with open('package.json', 'r') as f:
package = json.load(f)

# Add the latest JDK version to the java.configuration.runtimes array
jdks_config = next(filter(lambda e: e['id'] == "java-jdks", package['contributes']['configuration']))
jdks_config['properties']['java.configuration.runtimes']['items']['properties']['name']['enum'].append(f'JavaSE-{latest_jdk}')

# Write the updated package.json file
with open('package.json', 'w') as f:
json.dump(package, f, indent=2)
else:
print('Some tests failed, aborting update')
exit(1)
else:
print('No new JDK version detected, nothing to do')
exit(0)
exit(0)
58 changes: 58 additions & 0 deletions .github/workflows/bump-jdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Update JDK Version
on:
schedule:
- cron: '0 10 * * *'
workflow_dispatch:
jobs:
update-jdk-version:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests
- name: Check and update JDK version
run: |
if [ -f latest_jdk.txt ]; then
rm latest_jdk.txt # Remove previous records
fi
python .github/scripts/check_and_update_jdk.py
- name: Read latest JDK version from file
run: |
if [ -f latest_jdk.txt ]; then
version=$(cat latest_jdk.txt)
echo "Latest JDK version: $version"
echo "latest_jdk=$version" >> $GITHUB_ENV # set the latest_jdk environment variable
else
echo "No new JDK version detected, nothing to do"
exit 0
fi
- name: Check for existing PR
id: check_pr
run: |
pr_number=$(gh pr list --search "Found JavaSE version ${{ env.latest_jdk }}" --json number --jq '.[0].number')
echo "pr_number=$pr_number" >> $GITHUB_ENV
- name: Branch and push changes
if: ${{ success() && env.latest_jdk != '' && steps.check_pr.outputs.pr_number == '' }}
run: |
git config --global user.email "[email protected]"
git config --global user.name "redhattools-bot"
git checkout -b "update-jdk-${{ env.latest_jdk }}"
git commit -am "Bump JDK to ${{ env.latest_jdk }}"
git push origin "update-jdk-${{ env.latest_jdk }}"
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)"

0 comments on commit d5536ac

Please sign in to comment.