Skip to content

Commit

Permalink
New approach
Browse files Browse the repository at this point in the history
  • Loading branch information
maxschulz-COL committed Oct 17, 2024
1 parent 62220ff commit 3ecd872
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 74 deletions.
70 changes: 21 additions & 49 deletions .github/workflows/pycafe-dashboards-in-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,31 @@ jobs:
path: vizro-core/dist/*.whl
retention-days: 5

- name: Fetch latest commit from target branch
run: |
# git fetch origin ${{ github.ref_name }}
latest_commit=${{ github.sha }}
echo "COMMIT_HASH=$latest_commit" >> $GITHUB_ENV
run_id=${{ github.run_id }}
echo "RUN_ID=$run_id" >> $GITHUB_ENV
# - name: Fetch latest commit from target branch
# run: |
# # git fetch origin ${{ github.ref_name }}
# latest_commit=${{ github.sha }}
# echo "COMMIT_HASH=$latest_commit" >> $GITHUB_ENV
# run_id=${{ github.run_id }}
# echo "RUN_ID=$run_id" >> $GITHUB_ENV

- name: Display latest commit
run: |
echo "Latest commit on target branch: ${{ env.COMMIT_HASH }}"
echo "Latest run id: ${{ env.RUN_ID }}"
# - name: Display latest commit
# run: |
# echo "Latest commit on target branch: ${{ env.COMMIT_HASH }}"
# echo "Latest run id: ${{ env.RUN_ID }}"

- name: Run Py.Cafe tool
run: |
python tools/pycafe/create_py_cafe_url.py
# - name: Run Py.Cafe tool
# run: |
# python tools/pycafe/create_py_cafe_url.py

- name: Run Github Tool
env:
GH_TOKEN: ${{ github.token }}
GITHUB_TOKEN: ${{ github.token }}
RUN_ID: ${{ github.run_id }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.inputs.pr_number }}
GITHUB_OWNER: mckinsey

run: |
cd vizro-core
hatch run python ../tools/pycafe/test.py mckinsey/vizro 766
- name: Create Check Run
env:
GH_TOKEN: ${{ github.token }}
run: |
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/check-runs \
-d '{"name":"Scratch Example", "head_sha":"${{ github.event.pull_request.head.sha }}", "status":"in_progress","details_url":"https://github.com/mckinsey/vizro","output":{"title":"Dev example to try out things","summary":"","text":""}}'
# check why details url not working, check why it runs under secret scans
# test_something:
# runs-on: ubuntu-latest
# permissions:
# checks: write
# contents: read
# steps:
# - uses: actions/checkout@v1
# - uses: setup-build
# id: test
# - uses: LouisBrunner/[email protected]
# if: always()
# with:
# token: ${{ secrets.GITHUB_TOKEN }}
# name: Test XYZ
# conclusion: ${{ job.status }}
# output: |
# {"summary":"${{ steps.test.outputs.summary }}"}

# why not build wheel now?
# see why not direct again?
hatch run python ../tools/pycafe/test.py vizro-core/examples/scratch_dev/
83 changes: 58 additions & 25 deletions tools/pycafe/test.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
import os
import sys

# Authenticate with GitHub
from github import Auth, Github
import subprocess
from pathlib import Path
import textwrap
import base64
import gzip
import json
from urllib.parse import quote, urlencode


GITHUB_TOKEN = str(os.getenv('GITHUB_TOKEN'))
REPO_NAME = str(os.getenv('GITHUB_REPOSITORY'))
PR_NUMBER = int(os.getenv('PR_NUMBER'))

# using an access token
# COMMIT_HASH = str(os.getenv("COMMIT_HASH"))
RUN_ID = str(os.getenv("RUN_ID"))
PACKAGE_VERSION = subprocess.check_output(["hatch", "version"], cwd="vizro-core").decode("utf-8").strip()
PYCAFE_URL = "https://py.cafe"

access_token = str(os.getenv("GH_TOKEN"))
auth = Auth.Token(access_token)
DIRECTORY = sys.argv[1]

# Access
auth = Auth.Token(GITHUB_TOKEN)
g = Github(auth=auth)

# for repo in g.get_user().get_repos():
# print(repo.name)
# Get PR and commits
repo = g.get_repo(REPO_NAME)
pr = repo.get_pull(PR_NUMBER)
COMMIT_SHA = pr.head.sha



def generate_link(directory):
base_url = f"https://raw.githubusercontent.com/mckinsey/vizro/{COMMIT_SHA}/{DIRECTORY}"

app_file_path = os.path.join(directory, "app.py")
app_content = Path(app_file_path).read_text()
app_content_split = app_content.split('if __name__ == "__main__":')
app_content = app_content_split[0] + textwrap.dedent(app_content_split[1])

json_object = {
"code": str(app_content),
"requirements": f"{PYCAFE_URL}/gh/artifact/mckinsey/vizro/actions/runs/{RUN_ID}/pip/vizro-{PACKAGE_VERSION}-py3-none-any.whl",
"files": [],
}
for root, _, files in os.walk("./" + directory):
for file in files:
# print(root, file)
if "yaml" in root or "app.py" in file:
continue
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory)
file_url = f"{base_url}{relative_path.replace(os.sep, '/')}"
json_object["files"].append({"name": relative_path, "url": file_url})

print(sys.argv)
repo = sys.argv[1]
pr = sys.argv[2]
# type = sys.argv[3]
# code = sys.argv[4]
# requirements = sys.argv[5]
# Final JSON object logging
print(f"Final JSON object: {json.dumps(json_object, indent=2)}")

# Get the repository
repo = g.get_repo(repo)
json_text = json.dumps(json_object)
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)
return f"{PYCAFE_URL}/snippet/vizro/v1?{query}"

# Get the pull request
pr_number = int(pr)
pr = repo.get_pull(pr_number)
print(pr)

# base_url = f"https://py.cafe/snippet/{type}/v1"
url = "https://py.cafe/snippet/vizro/v1" # f"{base_url}#code={quote(code)}&requirements={quote(requirements)}"

# # Get the latest commit SHA from the PR
# if 1:
commit_sha = pr.head.sha
print(commit_sha)

pr.create_issue_comment("Foo bar")

Expand All @@ -44,6 +77,6 @@
context = "PyCafe"

# Create the status on the commit
commit = repo.get_commit(commit_sha)
commit = repo.get_commit(COMMIT_SHA)
commit.create_status(state=state, target_url=url, description=description, context=context)
print(f"Deployment status added to commit {commit_sha}")
print(f"Deployment status added to commit {COMMIT_SHA}")

0 comments on commit 3ecd872

Please sign in to comment.