Skip to content

Commit db8e7db

Browse files
committed
Add codegen/utils module
1 parent cfaca9f commit db8e7db

35 files changed

+942
-21
lines changed

src/codegen/cli/codemod/convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def convert_to_cli(input: str, language: str, name: str) -> str:
77
# from app.codemod.compilation.models.context import CodemodContext
88
#from app.codemod.compilation.models.pr_options import PROptions
99
10-
from graph_sitter import {codebase_type}
10+
from codegen.sdk import {codebase_type}
1111
1212
context: Any
1313

src/codegen/cli/utils/count_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# from app.codemod.compilation.models.context import CodemodContext
77
# from app.codemod.compilation.models.pr_options import PROptions
8-
# from graph_sitter import PyCodebaseType
8+
# from codegen.sdk import PyCodebaseType
99

1010
# context: CodemodContext
1111

src/codegen/git/__init__.py

Whitespace-only changes.

src/codegen/git/configs/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
CODEGEN_BOT_NAME = "codegen-bot"
44
CODEGEN_BOT_EMAIL = "team+codegenbot@codegen.sh"
55
CODEOWNERS_FILEPATHS = [".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS"]
6+
HIGHSIDE_REMOTE_NAME = "highside"
7+
LOWSIDE_REMOTE_NAME = "lowside"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
from typing import Any
5+
6+
from pydantic import BaseModel, Field
7+
8+
from codegen.git.models.pull_request_context import PullRequestContext
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
class CodemodContext(BaseModel):
14+
CODEMOD_ID: int | None = None
15+
CODEMOD_LINK: str | None = None
16+
CODEMOD_AUTHOR: str | None = None
17+
TEMPLATE_ARGS: dict[str, Any] = Field(default_factory=dict)
18+
19+
# TODO: add fields for version
20+
# CODEMOD_VERSION_ID: int | None = None
21+
# CODEMOD_VERSION_AUTHOR: str | None = None
22+
23+
PULL_REQUEST: PullRequestContext | None = None
24+
25+
@classmethod
26+
def _render_template(cls, template_schema: dict[str, str], template_values: dict[str, Any]) -> dict[str, Any]:
27+
template_data: dict[str, Any] = {}
28+
for var_name, var_value in template_values.items():
29+
var_type = template_schema.get(var_name)
30+
31+
if var_type == "list":
32+
template_data[var_name] = [str(v).strip() for v in var_value.split(",")]
33+
else:
34+
template_data[var_name] = str(var_value)
35+
return template_data
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pydantic import BaseModel
2+
3+
4+
class GithubNamedUserContext(BaseModel):
5+
"""Represents a GitHub user parsed from a webhook payload"""
6+
7+
login: str
8+
email: str | None = None
9+
10+
@classmethod
11+
def from_payload(cls, payload: dict) -> "GithubNamedUserContext":
12+
return cls(login=payload.get("login"), email=payload.get("email"))

src/codegen/git/models/pr_options.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pydantic import BaseModel
2+
3+
from codegen.utils.decorators.docs import apidoc
4+
5+
6+
@apidoc
7+
class PROptions(BaseModel):
8+
"""Options for generating a PR."""
9+
10+
title: str | None = None
11+
body: str | None = None
12+
labels: list[str] | None = None # TODO: not used until we add labels to GithubPullRequestModel
13+
force_push_head_branch: bool | None = None
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pydantic import BaseModel
2+
3+
4+
class PRPartContext(BaseModel):
5+
"""Represents a GitHub pull request part parsed from a webhook payload"""
6+
7+
ref: str
8+
sha: str
9+
10+
@classmethod
11+
def from_payload(cls, payload: dict) -> "PRPartContext":
12+
return cls(ref=payload.get("ref"), sha=payload.get("sha"))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from pydantic import BaseModel
2+
3+
from codegen.git.models.github_named_user_context import GithubNamedUserContext
4+
from codegen.git.models.pr_part_context import PRPartContext
5+
from codegen.git.schemas.github import GithubType
6+
7+
8+
class PullRequestContext(BaseModel):
9+
"""Represents a GitHub pull request"""
10+
11+
id: int
12+
url: str
13+
html_url: str
14+
number: int
15+
state: str
16+
title: str
17+
user: GithubNamedUserContext
18+
body: str
19+
draft: bool
20+
head: PRPartContext
21+
base: PRPartContext
22+
merged: bool | None
23+
merged_by: dict | None
24+
additions: int | None
25+
deletions: int | None
26+
changed_files: int | None
27+
github_type: GithubType | None = None
28+
webhook_data: dict | None = None
29+
30+
@classmethod
31+
def from_payload(cls, webhook_payload: dict) -> "PullRequestContext":
32+
webhook_data = webhook_payload.get("pull_request", {})
33+
return cls(
34+
id=webhook_data.get("id"),
35+
url=webhook_data.get("url"),
36+
html_url=webhook_data.get("html_url"),
37+
number=webhook_data.get("number"),
38+
state=webhook_data.get("state"),
39+
title=webhook_data.get("title"),
40+
user=GithubNamedUserContext.from_payload(webhook_data.get("user", {})),
41+
body=webhook_data.get("body"),
42+
draft=webhook_data.get("draft"),
43+
head=PRPartContext.from_payload(webhook_data.get("head", {})),
44+
base=PRPartContext.from_payload(webhook_data.get("base", {})),
45+
merged=webhook_data.get("merged"),
46+
merged_by=webhook_data.get("merged_by", {}),
47+
additions=webhook_data.get("additions"),
48+
deletions=webhook_data.get("deletions"),
49+
changed_files=webhook_data.get("changed_files"),
50+
github_type=GithubType.from_url(webhook_data.get("html_url")),
51+
webhook_data=webhook_data,
52+
)

src/codegen/gscli/generate/runner_imports.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import plotly
1313
""".strip()
1414
CODEGEN_IMPORTS = """
15-
from app.codemod.compilation.models.context import CodemodContext
16-
from app.codemod.compilation.models.github_named_user_context import GithubNamedUserContext
17-
from app.codemod.compilation.models.pr_part_context import PRPartContext
18-
from app.codemod.compilation.models.pull_request_context import PullRequestContext
15+
from codegen.git.models.codemod_context import CodemodContext
16+
from codegen.git.models.github_named_user_context import GithubNamedUserContext
17+
from codegen.git.models.pr_part_context import PRPartContext
18+
from codegen.git.models.pull_request_context import PullRequestContext
1919
"""
2020
# TODO: these should also be made public (i.e. included in the docs site)
2121
GS_PRIVATE_IMPORTS = """

0 commit comments

Comments
 (0)