-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Workflow to update JupyterLab dependencies automatically #7281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jtpio
merged 14 commits into
jupyter:main
from
itsmevichu:workflow-update-jupyterlab-dependencies
Dec 20, 2024
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dbc615e
workflow to upgrade JupyterLab dependencies
itsmevichu 2fb9d89
Update upgrade-lab-dependencies.py
itsmevichu 0538550
Prettier code for Test Lint check
itsmevichu 4de7436
Prettier code for Test Lint checks
itsmevichu 9d7ea27
Prettier code for Test Lint check
itsmevichu 81e218a
added ts scripts to upgrade lab dependencies
itsmevichu fb857e9
Merge branch 'main' into workflow-update-jupyterlab-dependencies
itsmevichu 2154f0f
Prettier code for test lint check
itsmevichu e409390
Merge branch 'main' into workflow-update-jupyterlab-dependencies
itsmevichu 026ed8b
Update buildutils/src/upgrade-lab-dependencies.ts
itsmevichu 15c87c9
Merge branch 'main' into workflow-update-jupyterlab-dependencies
itsmevichu 91d0d0c
Merge branch 'main' into workflow-update-jupyterlab-dependencies
jtpio f4ef243
Lint
jtpio eca1a7a
Merge branch 'main' into workflow-update-jupyterlab-dependencies
jtpio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
name: Check for latest JupyterLab releases | ||
|
||
on: | ||
schedule: | ||
- cron: 30 17 * * * | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
description: 'JupyterLab version' | ||
default: latest | ||
required: true | ||
type: string | ||
|
||
env: | ||
version_tag: 'latest' | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
jobs: | ||
check_for_lab_updates: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.11' | ||
|
||
- name: Install required dependencies | ||
run: | | ||
python -m pip install jupyterlab | ||
sudo apt-get install hub | ||
|
||
- name: Install Node | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '20.x' | ||
|
||
- name: Install npm dependencies and build buildutils | ||
run: | | ||
jlpm install | ||
jlpm run build:utils | ||
|
||
- name: Check for new releases and update | ||
shell: bash | ||
run: | | ||
set -eux | ||
for version in ${{ inputs.version || env.version_tag }} | ||
do | ||
export LATEST=$(node buildutils/lib/get-latest-lab-version.js --set-version $version) | ||
done | ||
|
||
echo "latest=${LATEST}" >> $GITHUB_ENV | ||
node buildutils/lib/upgrade-lab-dependencies.js --set-version ${LATEST} | ||
if [[ ! -z "$(git status --porcelain package.json)" ]]; then | ||
jlpm install | ||
fi | ||
|
||
- name: Create a PR | ||
shell: bash | ||
env: | ||
GITHUB_USER: ${{ secrets.G_USER }} | ||
GITHUB_TOKEN: ${{ secrets.G_TOKEN }} | ||
run: | | ||
set -eux | ||
|
||
export LATEST=${{ env.latest }} | ||
export BRANCH_NAME=update-to-v${LATEST} | ||
|
||
# if resulted in any change: | ||
if [[ ! -z "$(git status --porcelain package.json)" ]]; then | ||
# if branch already exists. | ||
if git ls-remote --heads origin | grep "refs/heads/${BRANCH_NAME}$" > /dev/null; then | ||
echo "Branch '${BRANCH_NAME}' exists." | ||
else | ||
# new branch is created | ||
git checkout -b "${BRANCH_NAME}" | ||
git config user.name "Jupyter Bot" | ||
git config user.email '[email protected]' | ||
|
||
git commit . -m "Update to JupyterLab v${LATEST}" | ||
|
||
git push --set-upstream origin "${BRANCH_NAME}" | ||
hub pull-request -m "Update to JupyterLab v${LATEST}" \ | ||
-m "New JupyterLab release [v${LATEST}](https://github.com/jupyterlab/jupyterlab/releases/tag/v${LATEST}) is available. Please review the lock file carefully.". | ||
fi | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function extractVersionFromReleases( | ||
releases: any, | ||
versionTag: string | ||
): string | null { | ||
for (const release of releases) { | ||
const tagName: string = release['tag_name']; | ||
if (versionTag === 'latest') { | ||
if (!release['prerelease'] && !release['draft']) { | ||
return tagName; | ||
} | ||
} else if (versionTag === tagName) { | ||
return tagName; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
async function findVersion(versionTag: string): Promise<string> { | ||
const url = 'https://api.github.com/repos/jupyterlab/jupyterlab/releases'; | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
const error_message = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`; | ||
throw new Error(error_message); | ||
} | ||
const releases: any = await response.json(); | ||
const version: string | null = extractVersionFromReleases( | ||
releases, | ||
versionTag | ||
); | ||
if (version === null) { | ||
const error_message = 'Invalid release tag'; | ||
throw new Error(error_message); | ||
} | ||
return version.substring(1); | ||
} | ||
|
||
async function getLatestLabVersion(): Promise<void> { | ||
const args: string[] = process.argv.slice(2); | ||
if (args.length !== 2 || args[0] !== '--set-version') { | ||
console.error('Usage: node script.js --set-version <version>'); | ||
process.exit(1); | ||
} | ||
const version_tag: string = args[1]; | ||
|
||
try { | ||
const result: string = await findVersion(version_tag); | ||
console.log(result); | ||
} catch (error: any) { | ||
console.error('Error:', error.message); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
getLatestLabVersion(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
const PACKAGE_JSON_PATHS: string[] = [ | ||
'app/package.json', | ||
'buildutils/package.json', | ||
'package.json', | ||
'packages/application-extension/package.json', | ||
'packages/application/package.json', | ||
'packages/console-extension/package.json', | ||
'packages/docmanager-extension/package.json', | ||
'packages/documentsearch-extension/package.json', | ||
'packages/help-extension/package.json', | ||
'packages/lab-extension/package.json', | ||
'packages/notebook-extension/package.json', | ||
'packages/terminal-extension/package.json', | ||
'packages/tree-extension/package.json', | ||
'packages/tree/package.json', | ||
'packages/ui-components/package.json', | ||
]; | ||
|
||
const DEPENDENCY_GROUP = '@jupyterlab'; | ||
|
||
async function updatePackageJson(newVersion: string): Promise<void> { | ||
const url = `https://raw.githubusercontent.com/jupyterlab/jupyterlab/v${newVersion}/jupyterlab/staging/package.json`; | ||
const response = await fetch(url); | ||
|
||
if (!response.ok) { | ||
const errorMessage = `Failed to fetch package.json from ${url}. HTTP status code: ${response.status}`; | ||
throw new Error(errorMessage); | ||
} | ||
|
||
const newPackageJson = await response.json(); | ||
|
||
for (const packageJsonPath of PACKAGE_JSON_PATHS) { | ||
const filePath: string = path.resolve(packageJsonPath); | ||
const existingPackageJson = JSON.parse(fs.readFileSync(filePath, 'utf-8')); | ||
|
||
const newDependencies = { | ||
...newPackageJson.devDependencies, | ||
...newPackageJson.resolutions, | ||
}; | ||
|
||
updateDependencyVersion(existingPackageJson, newDependencies); | ||
|
||
fs.writeFileSync(filePath, JSON.stringify(existingPackageJson, null, 2)); | ||
} | ||
} | ||
|
||
function updateDependencyVersion(existingJson: any, newJson: any): void { | ||
if (!existingJson) { | ||
return; | ||
} | ||
|
||
const sectionPaths: string[] = [ | ||
'resolutions', | ||
'dependencies', | ||
'devDependencies', | ||
]; | ||
|
||
for (const section of sectionPaths) { | ||
if (!existingJson[section]) { | ||
continue; | ||
} | ||
|
||
const updated = existingJson[section]; | ||
|
||
for (const [pkg, version] of Object.entries<string>( | ||
existingJson[section] | ||
)) { | ||
if (pkg.startsWith(DEPENDENCY_GROUP) && pkg in newJson) { | ||
if (version[0] === '^' || version[0] === '~') { | ||
updated[pkg] = version[0] + absoluteVersion(newJson[pkg]); | ||
} else { | ||
updated[pkg] = absoluteVersion(newJson[pkg]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function absoluteVersion(version: string): string { | ||
if (version.length > 0 && (version[0] === '^' || version[0] === '~')) { | ||
return version.substring(1); | ||
} | ||
return version; | ||
} | ||
|
||
async function upgradeLabDependencies(): Promise<void> { | ||
const args: string[] = process.argv.slice(2); | ||
|
||
if (args.length !== 2 || args[0] !== '--set-version') { | ||
console.error('Usage: node script.js --set-version <version>'); | ||
process.exit(1); | ||
} | ||
|
||
const newVersion: string = args[1]; | ||
await updatePackageJson(newVersion); | ||
} | ||
|
||
upgradeLabDependencies(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.