Skip to content

Commit

Permalink
attempt to allow for GHA to deploy tests to PyCafe from the PRs direc…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
BSd3v committed Oct 10, 2024
1 parent c75574d commit 1fc4e01
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 6 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/host-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Run tests

on:
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
if: github.event.pull_request != null
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: 3.12
- name: 'Setup Chrome and chromedriver'
uses: nanasess/setup-chromedriver@v2

- name: 'Setup chromedriver environment'
run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
- name: Start XVFB
run: Xvfb :99 &

- name: Setup uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv
- name: Install package and Build
run: |
source .venv/bin/activate
uv pip install --upgrade pip
uv pip install wheel
uv pip install ".[dev]"
uv pip install PyGithub
npm ci
npm i
npm run build
npm run dist
timeout-minutes: 20

- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: dist-artifact
path: dist/*.tar.gz

- name: Get download URL
id: artifact
run: echo "artifact_url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts" >> $GITHUB_ENV

- name: Post Link
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
ARTIFACT_URL: ${{env.artifact_url}}
run: python deploy_test.py

3 changes: 1 addition & 2 deletions PRs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
dash
dash_mantine_components
dash
56 changes: 53 additions & 3 deletions deploy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import base64
from urllib.parse import quote, urlencode
import os
from github import Github

files = []
code = ''
reqs = {}
for f in os.listdir('PRs'):
file_path = os.path.join('PRs', f)
if f != 'app.py':
if f not in ['app.py','requirements.txt']:
with open(file_path, 'rb') as file:
files.append(
{
Expand All @@ -17,9 +19,31 @@
"encoding": "base64"
}
)
else:
elif f == 'app.py':
with open(file_path, 'r') as file:
code = file.read()
else:
with open(file_path, 'r') as file:
reqs = {
'name': f,
'content': file.read()
}

# Find the last modified tar.gz file in the 'dist' directory
dist_path = 'dist'
latest_tar_gz = None
latest_time = 0

for f in os.listdir(dist_path):
if f.endswith('.tar.gz'):
file_path = os.path.join(dist_path, f)
modified_time = os.path.getmtime(file_path)
if modified_time > latest_time:
latest_time = modified_time
latest_tar_gz = file_path

reqs['content'] += f'\n{os.environ.get("ARTIFACT_URL")}'
files.append(reqs)

def generate_link(files, code):
json_object = {
Expand All @@ -35,4 +59,30 @@ def generate_link(files, code):
type = "dash" # replace by dash, or streamlit
return f"{base_url}/snippet/{type}/v1?{query}"

print(generate_link(files, code))
# Generate the link
link = generate_link(files, code)

# Post the link as a comment on the pull request
def post_comment(link):
# Get environment variables
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
REPO_NAME = os.getenv('GITHUB_REPOSITORY')
PR_NUMBER = int(os.getenv('PR_NUMBER'))

# Initialize Github object
g = Github(GITHUB_TOKEN)

# Get the repository
repo = g.get_repo(REPO_NAME)

# Get the pull request
pull_request = repo.get_pull(PR_NUMBER)

# Add a comment to the pull request
comment_body = f"Generated link: {link}"
pull_request.create_issue_comment(comment_body)

print("Comment added to the pull request.")

# Call the function to post the comment
post_comment(link)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"build:js": "webpack",
"build:backends": "dash-generate-components ./src/ts/components dash_mantine_components -p package-info.json --r-prefix '' --jl-prefix '' --ignore \\.test\\.",
"build": "npm run build:js && npm run build:backends",
"watch": "npm run build:js::dev -- --watch"
"watch": "npm run build:js::dev -- --watch",
"dist": "python setup.py sdist bdist_wheel"
},
"devDependencies": {
"@braintree/sanitize-url": "^7.0.0",
Expand Down

0 comments on commit 1fc4e01

Please sign in to comment.