Skip to content
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

Deploy tests #336

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 tmp_deploy.py

1 change: 1 addition & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ jobs:
- name: Run tests
run: |
source .venv/bin/activate
uv pip install -e .
pytest --headless

7 changes: 7 additions & 0 deletions PRs/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dash import *

app = Dash(__name__)

app.layout = html.Div('Hi PyCafe')

app.run()
1 change: 1 addition & 0 deletions PRs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dash
Empty file removed deploy_test.py
Empty file.
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
88 changes: 88 additions & 0 deletions tmp_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import json
import gzip
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 not in ['app.py','requirements.txt']:
with open(file_path, 'rb') as file:
files.append(
{
"name": f,
"content": base64.b64encode(file.read()).decode("utf8"),
"encoding": "base64"
}
)
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 = {
"code": code,
"files": files,
}
json_text = json.dumps(json_object)
# gzip -> base64
compressed_json_text = gzip.compress(json_text.encode("utf8"))
base64_text = base64.b64encode(compressed_json_text).decode("utf8")
query = urlencode({"c": base64_text}, quote_via=quote)
base_url = "https://py.cafe"
type = "dash" # replace by dash, or streamlit
return f"{base_url}/snippet/{type}/v1?{query}"

# 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: [{REPO_NAME}-{PR_NUMBER}]({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)
Loading