-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from BSd3v/deploy_tests
Deploy tests
- Loading branch information
Showing
7 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,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 | ||
|
This file contains 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 |
---|---|---|
|
@@ -44,5 +44,6 @@ jobs: | |
- name: Run tests | ||
run: | | ||
source .venv/bin/activate | ||
uv pip install -e . | ||
pytest --headless | ||
This file contains 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,7 @@ | ||
from dash import * | ||
|
||
app = Dash(__name__) | ||
|
||
app.layout = html.Div('Hi PyCafe') | ||
|
||
app.run() |
This file contains 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 @@ | ||
dash |
Empty file.
This file contains 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
This file contains 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,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) |