-
Notifications
You must be signed in to change notification settings - Fork 48
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
Draft
davidangarita1
wants to merge
46
commits into
plotly:master
Choose a base branch
from
davidangarita1:david/github-action-update-cdn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,467
−752
Draft
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
be3b168
Create github action yml file
davidangarita1 557d820
Create pyscript for GHA
davidangarita1 367fa75
Add conditional for errors in get_latest_version
davidangarita1 3d2e071
Add conditions for errors in check branch
davidangarita1 094f1ba
Add flags in issue searching
davidangarita1 466e210
Add conditional for pull request creation
davidangarita1 4527f79
Add json flags to issue
davidangarita1 a4a95d1
Add better logic for check issues
davidangarita1 9dd17c0
Create parse_changelog base function
davidangarita1 c889c3a
Add read path file to parse_changelog
davidangarita1 236dacf
Add version title checker
davidangarita1 02f6139
Add bullet point checker
davidangarita1 34d475f
Add clean bullet point to line
davidangarita1 144d03e
Add key for unreleased versions in standalone bullet points
davidangarita1 36df18e
Add better line condition
davidangarita1 03159c2
Rename regex variables
davidangarita1 93319a5
Add walrus operator for condition
davidangarita1 1164c82
Add better logic for points
davidangarita1 2066721
Add better version parser
davidangarita1 639b91b
Remove version regex
davidangarita1 b6d807c
Add multiline checker
davidangarita1 498489d
Add better logic to parse_changelog
davidangarita1 c65fb81
Remove parse_version function
davidangarita1 a920171
Change commit title
davidangarita1 957adc7
Add changelogtxt_parser as a --with dependency
davidangarita1 e781418
Implement changelogtxt_parser and add condition
davidangarita1 f9f3d06
Remove unnecesary lines
davidangarita1 ffe0e37
Change variable names
davidangarita1 0571252
Use eval variables for validations
davidangarita1 95bbdf5
Change git command
davidangarita1 7282765
Add gha group dependencies
davidangarita1 d234a2e
Change update_cdn.yml instructions
davidangarita1 9aa3470
Add chack tag workflow
davidangarita1 2ecae33
Change text from check_tag
davidangarita1 f082060
Add check_changelog workflow
davidangarita1 8b0c001
Format workflows
davidangarita1 58df698
Add compare_changelog workflow
davidangarita1 33daf5b
Remove unnecesary environments from workflows
davidangarita1 5eb45f3
Remove gha dependencies
davidangarita1 03096ca
Add script dependencies
davidangarita1 0952c3f
Change imports
davidangarita1 c5e2505
Add stderr to prints
davidangarita1 8f4f51d
Change upda_version to update
davidangarita1 699f684
Create check_changelog action and remove unnecesary changelog actions
davidangarita1 4571b45
Move string varaibles
davidangarita1 2283ab7
Better branch conditional
davidangarita1 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,48 @@ | ||
name: Check Changelog | ||
|
||
on: | ||
push: | ||
tags: | ||
- "*" | ||
branches: | ||
- "*" | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
changelog: | ||
runs-on: ubuntu-latest | ||
env: | ||
TAG: ${{ github.ref_name }} | ||
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} | ||
IS_TAG_PUSH: ${{ github.event_name == 'push' && github.ref_type == 'tag' }} | ||
IS_PR_TO_DEFAULT: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref == github.event.repository.default_branch }} | ||
defaults: | ||
run: | ||
working-directory: ./src/py/ | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: compare files | ||
if: env.IS_PR_TO_DEFAULT == 'true' | ||
run: | | ||
set -euo pipefail | ||
cat ./CHANGELOG.txt > updated.txt | ||
git fetch origin $DEFAULT_BRANCH | ||
git show origin/"$DEFAULT_BRANCH":src/py/CHANGELOG.txt > origin.txt | ||
- name: Run composite action | ||
id: changelog | ||
uses: geopozo/changelogtxt-parser@main | ||
with: | ||
working-directory: ./src/py/ | ||
tag-check: ${{ env.IS_TAG_PUSH == 'true' && env.TAG || '' }} | ||
summarize-news: ${{ env.IS_PR_TO_DEFAULT == 'true' && 'origin.txt updated.txt' || '' }} | ||
- name: Check .editorconfig exists | ||
run: | | ||
test -e .editorconfig || exit 1 | ||
- name: Check .pre-commit-config exists | ||
run: | | ||
test -e .pre-commit-config.y*ml || exit 1 |
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,26 @@ | ||
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 | ||
- name: Update CDN & PR | ||
run: uv run --python 3.12 scripts/update_cdn.py |
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,169 @@ | ||
# /// script | ||
# requires-python = ">=3.12" | ||
# dependencies = [ | ||
# "aiohttp", | ||
# "changelogtxt_parser @ git+https://github.com/geopozo/changelogtxt-parser", | ||
# "jq", | ||
# "semver", | ||
# ] | ||
# /// | ||
|
||
import asyncio | ||
import json | ||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
import aiohttp | ||
import changelogtxt_parser as changelog | ||
import jq | ||
import semver | ||
|
||
from kaleido._page_generator import DEFAULT_PLOTLY | ||
from kaleido._page_generator import __file__ as FILE_PATH | ||
|
||
REPO = os.environ["REPO"] | ||
GITHUB_WORKSPACE = os.environ["GITHUB_WORKSPACE"] | ||
|
||
|
||
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 verify_url(url: str) -> bool: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.head(url) as response: | ||
return response.status == 200 | ||
|
||
|
||
async def get_latest_version() -> str: | ||
out, err, _ = 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] | ||
if err: | ||
print(err.decode()) | ||
sys.exit(1) | ||
return str(max(versions)) | ||
|
||
|
||
async def create_pr(latest_version: str) -> None: | ||
branch = f"bot/update-cdn-{latest_version}" | ||
title = f"Update Plotly.js CDN to v{latest_version}" | ||
body = f"This PR updates the CDN URL to v{latest_version}." | ||
|
||
_, err, brc_eval = await run( | ||
["gh", "api", f"repos/{REPO}/branches/{branch}", "--silent"] | ||
) | ||
branch_exists = (brc_eval == 0) | ||
|
||
if branch_exists: | ||
print(f"The branch {branch} already exists", file=sys.stderr) | ||
sys.exit(1) | ||
else: | ||
msg = err.decode() | ||
if "HTTP 404" not in msg: | ||
print(msg, file=sys.stderr) # unexpected errors | ||
sys.exit(1) | ||
|
||
pr, _, _ = await run( | ||
["gh", "pr", "list", "-R", REPO, "-H", branch, "--state", "all"] | ||
) | ||
|
||
if pr.decode(): | ||
print(f"Pull request for '{branch}' already exists", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
|
||
file_updated = changelog.update(latest_version, title, GITHUB_WORKSPACE) | ||
|
||
if not file_updated: | ||
print("Failed to update changelog", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
await run(["git", "checkout", "-b", branch]) | ||
await run(["git", "add", "."]) | ||
await run( | ||
[ | ||
"git", | ||
"-c", | ||
"user.name='github-actions'", | ||
"-c", | ||
"user.email='[email protected]'", | ||
"commit", | ||
"-m", | ||
f"chore: {title}", | ||
] | ||
) | ||
_, push_err, push_eval = await run(["git", "push", "-u", "origin", branch]) | ||
|
||
if push_eval: | ||
print(push_err.decode(), file=sys.stderr) | ||
sys.exit(1) | ||
|
||
|
||
new_pr, pr_err, pr_eval = await run( | ||
["gh", "pr", "create", "-B", "master", "-H", branch, "-t", title, "-b", body] | ||
) | ||
if pr_eval: | ||
print(pr_err.decode(), file=sys.stderr) | ||
sys.exit(1) | ||
|
||
print("Pull request:", new_pr.decode().strip()) | ||
|
||
|
||
async def main() -> None: | ||
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) | ||
|
||
cdn_exists = await verify_url(new_cdn) | ||
if cdn_exists: | ||
p = 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.js v{latest_version}" | ||
body = f"URL: {new_cdn} - invalid url" | ||
brc, _, _ = await run( | ||
[ | ||
"gh", | ||
"issue", | ||
"--json", | ||
"number,state", | ||
"list", | ||
"--search", | ||
title, | ||
"--state", | ||
"all", | ||
] | ||
) | ||
issues = json.loads(brc.decode()) | ||
if issues: | ||
for issue in issues: | ||
if issue.get("state") == "OPEN": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. por qué solo open? |
||
print(f"Issue '{title}' already exists in:") | ||
print(f"https://github.com/{REPO}/issues/{issue.get('number')}") | ||
sys.exit(1) | ||
print(f"Issue '{title}' is closed") | ||
sys.exit(0) | ||
davidangarita1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
new_issue, issue_err, _ = await run( | ||
["gh", "issue", "create", "-R", REPO, "-t", title, "-b", body] | ||
) | ||
if issue_err: | ||
print(issue_err.decode()) | ||
|
||
print(f"The issue '{title}' was created in {new_issue.decode().strip()}") | ||
sys.exit(1) | ||
|
||
|
||
asyncio.run(main()) |
Oops, something went wrong.
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.