-
Notifications
You must be signed in to change notification settings - Fork 49
David/GitHub action update cdn #389
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
be3b168
557d820
367fa75
3d2e071
094f1ba
466e210
4527f79
a4a95d1
9dd17c0
c889c3a
236dacf
02f6139
34d475f
144d03e
36df18e
03159c2
93319a5
1164c82
2066721
639b91b
b6d807c
498489d
c65fb81
a920171
957adc7
e781418
f9f3d06
ffe0e37
0571252
95bbdf5
7282765
d234a2e
9aa3470
2ecae33
f082060
8b0c001
58df698
33daf5b
5eb45f3
03096ca
0952c3f
c5e2505
8f4f51d
699f684
4571b45
2283ab7
b9d5ae7
2e9f7c6
f75f646
6d24751
5cfd409
d7e91fd
a324eeb
dc0732e
a68096c
9932131
41ed234
bd27d46
8235cbf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Update CDN | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
issues: write | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "0 13 * * 1" | ||
|
||
jobs: | ||
inspection: | ||
runs-on: ubuntu-latest | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
REPO: ${{ github.repository }} | ||
defaults: | ||
run: | ||
working-directory: ./src/py/ | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: astral-sh/setup-uv@v5 | ||
- run: uv sync | ||
- name: Update CDN & PR | ||
run: uv run --python 3.12 --with semver --with jq --with aiohttp scripts/update_cdn.py |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#!/usr/bin/env python3 | ||
import asyncio | ||
import os | ||
import pathlib | ||
import subprocess | ||
import sys | ||
|
||
import aiohttp | ||
import jq | ||
import json | ||
import semver | ||
from kaleido._page_generator import DEFAULT_PLOTLY | ||
from kaleido._page_generator import __file__ as FILE_PATH | ||
|
||
REPO = os.environ["REPO"] | ||
|
||
|
||
async def run(commands: list[str]) -> tuple[bytes, bytes, int | None]: | ||
p = await asyncio.create_subprocess_exec( | ||
*commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE | ||
) | ||
|
||
return (*(await p.communicate()), p.returncode) | ||
|
||
|
||
async def get_latest_version() -> str: | ||
out, _, _ = await run(["gh", "api", "repos/plotly/plotly.js/tags", "--paginate"]) | ||
tags = jq.compile('map(.name | ltrimstr("v"))').input_value(json.loads(out)).first() | ||
versions = [semver.VersionInfo.parse(v) for v in tags] | ||
return str(max(versions)) | ||
|
||
|
||
async def verify_url(url: str) -> bool: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.head(url) as response: | ||
return response.status == 200 | ||
|
||
|
||
async def create_pr(latest_version: str) -> None: | ||
branch = f"bot/update-cdn-{latest_version}" | ||
_, _, reteval = await run( | ||
["gh", "api", f"repos/{REPO}/branches/{branch}", "--silent"] | ||
) | ||
pr, _, _ = await run( | ||
["gh", "pr", "list", "-R", REPO, "-H", branch, "--state", "all"] | ||
) | ||
if not reteval: | ||
print(f"The branch {branch} already exists") | ||
sys.exit(0) | ||
|
||
if pr.decode(): | ||
print(f"Pull request '{branch}' already exists") | ||
|
||
sys.exit(0) | ||
|
||
await run(["git", "checkout", "-b", branch]) | ||
await run(["git", "add", FILE_PATH]) | ||
await run( | ||
[ | ||
"git", | ||
"-c", | ||
"user.name='github-actions'", | ||
"-c", | ||
"user.email='[email protected]'", | ||
"commit", | ||
"-m", | ||
f"chore: update Plotly CDN to v{latest_version}", | ||
] | ||
) | ||
await run(["git", "push", "-u", "origin", branch]) | ||
|
||
title = f"Update Plotly CDN to v{latest_version}" | ||
body = f"This PR updates the CDN URL to v{latest_version}." | ||
new_pr, _, reteval = await run( | ||
["gh", "pr", "create", "-B", "master", "-H", branch, "-t", title, "-b", body] | ||
) | ||
print("Pull request:", new_pr.decode().strip()) | ||
sys.exit(reteval) | ||
davidangarita1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
async def main(): | ||
latest_version = await get_latest_version() | ||
new_cdn = f"https://cdn.plot.ly/plotly-{latest_version}.js" | ||
|
||
if new_cdn == DEFAULT_PLOTLY: | ||
print("Already up to date") | ||
sys.exit(0) | ||
davidangarita1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
cdn_exists = await verify_url(new_cdn) | ||
if cdn_exists: | ||
p = pathlib.Path(FILE_PATH) | ||
s = p.read_text(encoding="utf-8").replace(DEFAULT_PLOTLY, new_cdn, 1) | ||
p.write_text(s, encoding="utf-8") | ||
|
||
await create_pr(latest_version) | ||
else: | ||
title = f"CDN not reachable for Plotly v{latest_version}" | ||
body = f"URL: {new_cdn} - invalid url" | ||
brc, _, _ = await run(["gh", "issue", "list", "--search", title]) | ||
if brc.decode(): | ||
print(f"Issue '{title}' already exists") | ||
sys.exit(0) | ||
davidangarita1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
new, err, _ = await run( | ||
["gh", "issue", "create", "-R", REPO, "-t", title, "-b", body] | ||
) | ||
print( | ||
f"The issue '{title}' was created in {new.decode().strip()}" | ||
if not err | ||
else err | ||
davidangarita1 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
asyncio.run(main()) |
Uh oh!
There was an error while loading. Please reload this page.