-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
tmp_deploy.py
128 lines (106 loc) · 3.86 KB
/
tmp_deploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import gzip
import base64
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 = ""
reqs = {}
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": os.path.relpath(file_path, "PRs"),
"content": base64.b64encode(file.read()).decode("utf8"),
"encoding": "base64",
}
)
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")}/{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)
else:
reqs["content"] += f"\n{new_package}"
def generate_link(files, code):
json_object = {
"requirements": reqs["content"],
"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)
# 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):
# 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.")
# 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,
)
# 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,
)
print(f"Deployment message added to PR #{PR_NUMBER}")
post_comment(link)
create_deployment_message(link)