Skip to content

Commit

Permalink
Merge pull request #350 from BSd3v/updating-test-flow
Browse files Browse the repository at this point in the history
Updating Test Flow
  • Loading branch information
AnnMarieW authored Dec 10, 2024
2 parents 5292318 + cd96700 commit 6d3a1e5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 44 deletions.
9 changes: 3 additions & 6 deletions .github/workflows/host-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ jobs:
| jq -r '.artifacts[0].id')
echo "artifact_id=$artifact_id" >> $GITHUB_ENV
- name: Get download URL
run: echo "artifact_url=https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/artifacts/${{ env.artifact_id }}" >> $GITHUB_ENV

- name: Post Link
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
PR_NUMBER: ${{ github.event.inputs.pr_number }}
ARTIFACT_URL: ${{ env.artifact_url }}
ARTIFACT_ID: ${{ env.artifact_id }}
PR_NUMBER: ${{ github.event.inputs.pr_number }}
WORKFLOW_NAME: Host%20Test%20App
ARTIFACT_NAME: dist-artifact
GITHUB_OWNER: snehilvj
FILE_FULLNAME: dash_mantine_components-${{ env.version }}-py3-none-any.whl
PACKAGE_NAME: dash_mantine_components
Expand Down
125 changes: 87 additions & 38 deletions tmp_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,47 @@
from urllib.parse import quote, urlencode
import os
from github import Github
from datetime import datetime

GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
REPO_NAME = os.getenv("GITHUB_REPOSITORY")
PR_NUMBER = int(os.getenv("PR_NUMBER"))

files = []
code = ''
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:
for root, dirs, filenames in os.walk(f"PRs"):
for f in filenames:
file_path = os.path.join(root, f)
if f not in ["app.py", "requirements.txt"]:
with open(file_path, "rb") as file:
files.append(
{
"name": f,
"name": os.path.relpath(file_path, "PRs"),
"content": base64.b64encode(file.read()).decode("utf8"),
"encoding": "base64"
"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()
}

new_package = f'{os.getenv("PACKAGE_NAME")} @ https://py.cafe/gh/artifact/{os.getenv("GITHUB_REPOSITORY")}/{os.getenv("ARTIFACT_ID")}/{os.getenv("FILE_FULLNAME")}'
if os.getenv("PACKAGE_NAME") in reqs['content']:
reqs['content'] = reqs['content'].replace(os.getenv("PACKAGE_NAME"), new_package)
elif f == "app.py":
with open(file_path, "r") as file:
code = file.read()
elif f == "requirements.txt":
with open(file_path, "r") as file:
reqs = {
"name": os.path.relpath(file_path, "PRs"),
"content": file.read(),
}

new_package = f'{os.getenv("PACKAGE_NAME")} @ https://py.cafe/gh/artifact/{os.getenv("GITHUB_REPOSITORY")}/pull/{PR_NUMBER}/{os.getenv("WORKFLOW_NAME")}/{os.getenv("ARTIFACT_NAME")}/{os.getenv("FILE_FULLNAME")}'
if os.getenv("PACKAGE_NAME") in reqs["content"]:
reqs["content"] = reqs["content"].replace(os.getenv("PACKAGE_NAME"), new_package)
else:
reqs['content'] += f'\n{new_package}'
reqs["content"] += f"\n{new_package}"


def generate_link(files, code):
json_object = {
"requirements": reqs['content'],
"requirements": reqs["content"],
"code": code,
"files": files,
}
Expand All @@ -50,30 +57,72 @@ def generate_link(files, code):
type = "dash" # replace by dash, or streamlit
return f"{base_url}/snippet/{type}/v1?{query}"


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

# 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)


# 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'))
# Find existing comments by the bot
comments = pull_request.get_issue_comments()
bot_comment = None

for comment in comments:
if comment.body.startswith("Test Environment for ["):
bot_comment = comment
break

# Get current UTC datetime
current_utc_time = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")

# Define the comment body with datetime
comment_body = f"Test Environment for [{REPO_NAME}-{PR_NUMBER}]({link})\nUpdated on: {current_utc_time}"

# Update the existing comment or create a new one
if bot_comment:
bot_comment.edit(comment_body)
print("Comment updated on the pull request.")
else:
pull_request.create_issue_comment(comment_body)
print("Comment added to the pull request.")

# Initialize Github object
g = Github(GITHUB_TOKEN)

# Get the repository
repo = g.get_repo(REPO_NAME)
# Create deployment message for a status
def create_deployment_message(link):
# Create a deployment
deployment = repo.create_deployment(
ref=pull_request.head.sha,
task="deploy",
auto_merge=False,
required_contexts=[],
payload={},
environment="staging",
description=f"Deploying PR #{PR_NUMBER} to PyCafe",
transient_environment=True,
production_environment=False,
)

# Get the pull request
pull_request = repo.get_pull(PR_NUMBER)
# Update the deployment status
deployment.create_status(
state="success",
target_url="https://py.cafe/",
description="Deployment to staging succeeded!",
environment_url=link,
auto_inactive=True,
)

# 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(f"Deployment message added to PR #{PR_NUMBER}")

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

# Call the function to post the comment
post_comment(link)
post_comment(link)
create_deployment_message(link)

0 comments on commit 6d3a1e5

Please sign in to comment.