- 
                Notifications
    You must be signed in to change notification settings 
- Fork 51
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
          
     Open
      
      
            davidangarita1
  wants to merge
  61
  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.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            61 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 b9d5ae7
              
                Change issue endpoint
              
              
                davidangarita1 2e9f7c6
              
                Create verify_issue and create_issue functions
              
              
                davidangarita1 f75f646
              
                Implement functions
              
              
                davidangarita1 6d24751
              
                Enhance code format
              
              
                davidangarita1 5cfd409
              
                Rename run() to run_cmd()
              
              
                davidangarita1 d7e91fd
              
                Enhance run_cmd() code
              
              
                davidangarita1 a324eeb
              
                Add try-except to verify_url
              
              
                davidangarita1 dc0732e
              
                Better logic for get_latest_version()
              
              
                davidangarita1 a68096c
              
                Better logic for create_pr()
              
              
                davidangarita1 9932131
              
                Enhance create_issue() logic
              
              
                davidangarita1 41ed234
              
                Better logic for main()
              
              
                davidangarita1 bd27d46
              
                Merge remote-tracking branch 'upstream/master' into david/github-acti…
              
              
                davidangarita1 8235cbf
              
                Add ruff comment
              
              
                davidangarita1 e4090f7
              
                Implement 'from-push'
              
              
                davidangarita1 a87e8a3
              
                Implement new changes from changelogtxt action
              
              
                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,32 @@ | ||
| name: Check Changelog | ||
|  | ||
| on: | ||
| push: | ||
| tags: | ||
| - "*" | ||
| branches: | ||
| - "*" | ||
| pull_request: | ||
| branches: | ||
| - main | ||
|  | ||
| jobs: | ||
| changelog: | ||
| runs-on: ubuntu-latest | ||
| defaults: | ||
| run: | ||
| working-directory: ./src/py/ | ||
| steps: | ||
| - name: Run composite action | ||
| id: changelog | ||
| uses: geopozo/changelogtxt-parser@main | ||
| with: | ||
| file-path: ./src/py/ | ||
| check-tag: "from-push" | ||
| summarize-news: "from-pr" | ||
| - 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,191 @@ | ||
| # /// 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 py.kaleido._page_generator import DEFAULT_PLOTLY | ||
| from py.kaleido._page_generator import __file__ as FILE_PATH | ||
|  | ||
| # ruff: noqa: T201 allow print in CLI | ||
|  | ||
| REPO = os.environ["REPO"] | ||
| GITHUB_WORKSPACE = os.environ["GITHUB_WORKSPACE"] | ||
|  | ||
|  | ||
| async def run_cmd(commands: list[str]) -> tuple[bytes, bytes, int | None]: | ||
| proc = await asyncio.create_subprocess_exec( | ||
| *commands, stdout=subprocess.PIPE, stderr=subprocess.PIPE | ||
| ) | ||
| stdout, stderr = await proc.communicate() | ||
| return stdout, stderr, proc.returncode | ||
|  | ||
|  | ||
| async def verify_url(url: str) -> bool: | ||
| try: | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.head(url) as response: | ||
| return response.status == 200 | ||
| except aiohttp.ClientError: | ||
| return False | ||
|  | ||
|  | ||
| async def get_latest_version() -> str: | ||
| out, err, _ = await run_cmd( | ||
| ["gh", "api", "repos/plotly/plotly.js/tags", "--paginate"] | ||
| ) | ||
| if err: | ||
| print(err.decode(), file=sys.stderr) | ||
| sys.exit(1) | ||
|  | ||
| data = json.loads(out) | ||
| tags = jq.compile('map(.name | ltrimstr("v"))').input_value(data).first() | ||
| versions = [semver.VersionInfo.parse(v) for v in tags] | ||
|  | ||
| 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}." | ||
|  | ||
| _, brc_err, brc_eval = await run_cmd( | ||
| ["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 = brc_err.decode() | ||
| if "HTTP 404" not in msg: | ||
| print(msg, file=sys.stderr) # unexpected errors | ||
| sys.exit(1) | ||
|  | ||
| pr_exist, _, _ = await run_cmd( | ||
| ["gh", "pr", "list", "-R", REPO, "-H", branch, "--state", "all"] | ||
| ) | ||
|  | ||
| if pr_exist: | ||
| print(f"Pull request for '{branch}' already exists", file=sys.stderr) | ||
| sys.exit(1) | ||
|  | ||
| try: | ||
| changelog.update(f"v{latest_version}", title, GITHUB_WORKSPACE) | ||
| except (ValueError, RuntimeError): | ||
| print("Failed to update changelog", file=sys.stderr) | ||
| sys.exit(1) | ||
|  | ||
| await run_cmd(["git", "checkout", "-b", branch]) | ||
| await run_cmd(["git", "add", "."]) | ||
| await run_cmd( | ||
| [ | ||
| "git", | ||
| "-c", | ||
| "user.name='github-actions'", | ||
| "-c", | ||
| "user.email='[email protected]'", | ||
| "commit", | ||
| "-m", | ||
| f"chore: {title}", | ||
| ] | ||
| ) | ||
| _, push_err, push_eval = await run_cmd(["git", "push", "-u", "origin", branch]) | ||
| push_failed = push_eval == 1 | ||
|  | ||
| if push_failed: | ||
| print(push_err.decode(), file=sys.stderr) | ||
| sys.exit(1) | ||
|  | ||
| new_pr_out, pr_err, pr_eval = await run_cmd( | ||
| ["gh", "pr", "create", "-B", "master", "-H", branch, "-t", title, "-b", body] | ||
| ) | ||
| pr_failed = pr_eval == 1 | ||
|  | ||
| if pr_failed: | ||
| print(pr_err.decode(), file=sys.stderr) | ||
| sys.exit(1) | ||
|  | ||
| print("Pull request:", new_pr_out.decode().strip()) | ||
|  | ||
|  | ||
| async def verify_issue(title: str) -> None: | ||
| issues_out, _, _ = await run_cmd( | ||
| [ | ||
| "gh", | ||
| "issue", | ||
| "list", | ||
| "-R", | ||
| REPO, | ||
| "--search", | ||
| title, | ||
| "--state", | ||
| "all", | ||
| "--json", | ||
| "number,state", | ||
| ] | ||
| ) | ||
| issues = json.loads(issues_out.decode()) | ||
|  | ||
| if issues: | ||
| print(f"Issue '{title}' already exists in:") | ||
| print(f"https://github.com/{REPO}/issues/{issues[0].get('number')}") | ||
| sys.exit(1) | ||
|  | ||
|  | ||
| async def create_issue(title: str, body: str) -> None: | ||
| new_issue, issue_err, _ = await run_cmd( | ||
| ["gh", "issue", "create", "-R", REPO, "-t", title, "-b", body] | ||
| ) | ||
| if issue_err: | ||
| print(issue_err.decode()) | ||
| sys.exit(1) | ||
|  | ||
| print(f"The issue '{title}' was created in {new_issue.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") | ||
| return | ||
|  | ||
| cdn_exists = await verify_url(new_cdn) | ||
|  | ||
| if cdn_exists: | ||
| file_path = Path(FILE_PATH) | ||
| content = file_path.read_text(encoding="utf-8") | ||
| updated = content.replace(DEFAULT_PLOTLY, new_cdn, 1) | ||
|  | ||
| file_path.write_text(updated, 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" | ||
|  | ||
| await verify_issue(title) | ||
| await create_issue(title, body) | ||
|  | ||
|  | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm gonna change some things