Skip to content

Commit 5cbc8bc

Browse files
authored
compress github action payload (#290)
* compress github action payload * allow explicit specification of gh branch * allow backslashes in code content
1 parent c7cf3f3 commit 5cbc8bc

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

.github/workflows/runner.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import base64
12
import json
23
import sys
4+
import zlib
35
from dataclasses import asdict
46
from datetime import datetime
57
from pathlib import Path
@@ -8,8 +10,10 @@
810

911
from run_eval import run_config
1012

11-
config = json.loads(Path("payload.json").read_text())
13+
payload = Path("payload.json").read_text()
1214
Path("payload.json").unlink()
15+
payload = zlib.decompress(base64.b64decode(payload)).decode("utf-8")
16+
config = json.loads(payload)
1317

1418
result = asdict(run_config(config))
1519

src/discord-cluster-manager/bot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ async def setup_hook(self):
7979
# Load cogs
8080
submit_cog = SubmitCog(self)
8181
submit_cog.register_launcher(ModalLauncher(consts.MODAL_CUDA_INCLUDE_DIRS))
82-
submit_cog.register_launcher(GitHubLauncher(env.GITHUB_REPO, env.GITHUB_TOKEN))
82+
submit_cog.register_launcher(
83+
GitHubLauncher(env.GITHUB_REPO, env.GITHUB_TOKEN, env.GITHUB_WORKFLOW_BRANCH)
84+
)
8385
await self.add_cog(submit_cog)
8486
await self.add_cog(BotManagerCog(self))
8587
await self.add_cog(LeaderboardCog(self))

src/discord-cluster-manager/env.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
from dotenv import load_dotenv
4+
from utils import get_github_branch_name
45

56

67
def init_environment():
@@ -33,6 +34,7 @@ def init_environment():
3334
# GitHub-specific constants
3435
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
3536
GITHUB_REPO = os.getenv("GITHUB_REPO")
37+
GITHUB_WORKFLOW_BRANCH = os.getenv("GITHUB_WORKFLOW_BRANCH", get_github_branch_name())
3638
PROBLEMS_REPO = os.getenv("PROBLEMS_REPO")
3739

3840
# Directory that will be used for local problem development.

src/discord-cluster-manager/launchers/github.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import asyncio
2+
import base64
23
import datetime
34
import json
45
import math
56
import pprint
67
import tempfile
78
import zipfile
9+
import zlib
810
from typing import Awaitable, Callable, Optional
911

1012
import requests
@@ -20,7 +22,7 @@
2022
from github import Github, UnknownObjectException, WorkflowRun
2123
from report import RunProgressReporter
2224
from run_eval import CompileResult, EvalResult, FullResult, RunResult, SystemInfo
23-
from utils import get_github_branch_name, setup_logging
25+
from utils import setup_logging
2426

2527
from .launcher import Launcher
2628

@@ -39,10 +41,11 @@ def get_timeout(config: dict) -> int:
3941

4042

4143
class GitHubLauncher(Launcher):
42-
def __init__(self, repo: str, token: str):
44+
def __init__(self, repo: str, token: str, branch: str):
4345
super().__init__(name="GitHub", gpus=GitHubGPU)
4446
self.repo = repo
4547
self.token = token
48+
self.branch = branch
4649
self.trigger_limit = asyncio.Semaphore(1)
4750

4851
async def run_submission(
@@ -71,10 +74,12 @@ async def run_submission(
7174
lang_name = {"py": "Python", "cu": "CUDA"}[lang]
7275

7376
logger.info(f"Attempting to trigger GitHub action for {lang_name} on {selected_workflow}")
74-
run = GitHubRun(self.repo, self.token, selected_workflow)
77+
run = GitHubRun(self.repo, self.token, self.branch, selected_workflow)
7578
logger.info(f"Successfully created GitHub run: {run.run_id}")
7679

77-
payload = json.dumps(config)
80+
payload = base64.b64encode(zlib.compress(json.dumps(config).encode("utf-8"))).decode(
81+
"utf-8"
82+
)
7883

7984
inputs = {"payload": payload}
8085
if lang == "py":
@@ -143,10 +148,11 @@ async def wait_callback(self, run: "GitHubRun", status: RunProgressReporter):
143148

144149

145150
class GitHubRun:
146-
def __init__(self, repo: str, token: str, workflow_file: str):
151+
def __init__(self, repo: str, token: str, branch: str, workflow_file: str):
147152
gh = Github(token)
148153
self.repo = gh.get_repo(repo)
149154
self.token = token
155+
self.branch = branch
150156
self.workflow_file = workflow_file
151157
self.run: Optional[WorkflowRun.WorkflowRun] = None
152158
self.start_time = None
@@ -189,14 +195,14 @@ async def trigger(self, inputs: dict) -> bool:
189195
logger.error(f"Could not find workflow {self.workflow_file}", exc_info=e)
190196
raise ValueError(f"Could not find workflow {self.workflow_file}") from e
191197

192-
branch_name = get_github_branch_name()
198+
logger.info("Dispatching workflow %s on branch %s", self.workflow_file, self.branch)
193199
logger.debug(
194200
"Dispatching workflow %s on branch %s with inputs %s",
195201
self.workflow_file,
196-
branch_name,
202+
self.branch,
197203
pprint.pformat(inputs),
198204
)
199-
success = await asyncio.to_thread(workflow.create_dispatch, branch_name, inputs=inputs)
205+
success = await asyncio.to_thread(workflow.create_dispatch, self.branch, inputs=inputs)
200206

201207
if success:
202208
wait_seconds = 5
@@ -248,7 +254,7 @@ async def trigger(self, inputs: dict) -> bool:
248254
return False
249255
else:
250256
logger.error(
251-
f"Failed to dispatch workflow {self.workflow_file} on branch {branch_name}."
257+
f"Failed to dispatch workflow {self.workflow_file} on branch {self.branch}."
252258
)
253259
return False
254260

src/discord-cluster-manager/leaderboard_db.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def create_submission(
226226
"""
227227
SELECT id, code
228228
FROM leaderboard.code_files
229-
WHERE hash = encode(sha256(%s::bytea), 'hex')
229+
WHERE hash = encode(sha256(%s), 'hex')
230230
""",
231-
(code,),
231+
(code.encode("utf-8"),),
232232
)
233233

234234
code_id = None
@@ -245,7 +245,7 @@ def create_submission(
245245
VALUES (%s)
246246
RETURNING id
247247
""",
248-
(code,),
248+
(code.encode("utf-8"),),
249249
)
250250
code_id = self.cursor.fetchone()
251251
# Check if user exists in user_info, if not add them

0 commit comments

Comments
 (0)