Skip to content

Commit e3ff5d9

Browse files
committed
Add api for "ok to preview" comments
1 parent 691d319 commit e3ff5d9

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

app/github.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
from .config import GITHUB_TOKEN
66
from .release import ReleaseInfo
77

8-
SIGNATURE = "<!-- action-check: release-file -->"
8+
SIGNATURE_TEMPLATE = "<!-- action-check: {slug} -->"
99
API_BASE = "https://api.github.com"
1010

1111

12-
def is_release_check_comment(comment: dict) -> bool:
12+
def has_signature(comment: dict, slug: str) -> bool:
1313
return (
1414
comment["user"]["login"] in ["github-actions[bot]", "botberry"]
15-
and SIGNATURE in comment["body"]
15+
and SIGNATURE_TEMPLATE.format(slug) in comment["body"]
1616
)
1717

1818

@@ -44,11 +44,11 @@ def get_labels(pr_number) -> typing.List[dict]:
4444
return response.json()
4545

4646

47-
def add_or_edit_comment(pr_number: int, comment: str):
47+
def add_or_edit_comment(pr_number: int, comment: str, slug: str):
4848
current_comments = get_comments(pr_number)
4949

5050
previous_comment = next(
51-
(comment for comment in current_comments if is_release_check_comment(comment)),
51+
(comment for comment in current_comments if has_signature(comment, slug)),
5252
None,
5353
)
5454

@@ -58,7 +58,7 @@ def add_or_edit_comment(pr_number: int, comment: str):
5858
response = method(
5959
url,
6060
headers={"Authorization": f"token {GITHUB_TOKEN}"},
61-
json={"body": comment + SIGNATURE},
61+
json={"body": comment + SIGNATURE_TEMPLATE.format(slug)},
6262
)
6363

6464
response.raise_for_status()

app/schema.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
import typing
21
from enum import Enum
2+
from typing import List, Optional
33

44
import strawberry
55

66
from .github import add_or_edit_comment, update_labels
77
from .release import ReleaseInfo
8-
from .templates import INVALID_RELEASE_FILE, MISSING_RELEASE_FILE, RELEASE_FILE_ADDED
8+
from .templates import (
9+
INVALID_RELEASE_FILE,
10+
MISSING_RELEASE_FILE,
11+
OK_TO_PREVIEW,
12+
RELEASE_FILE_ADDED,
13+
)
914

1015

1116
@strawberry.type
@@ -26,11 +31,34 @@ class ReleaseFileStatus(Enum):
2631
class AddReleaseFileCommentInput:
2732
pr_number: int
2833
status: ReleaseFileStatus
29-
release_info: typing.Optional[ReleaseInfo] = None
34+
release_info: Optional[ReleaseInfo] = None
35+
36+
37+
@strawberry.input
38+
class AddOkToPreviewCommentInput:
39+
pr_number: int
40+
paths: List[str]
3041

3142

3243
@strawberry.type
3344
class Mutation:
45+
@strawberry.mutation
46+
def add_ok_to_preview_comment(self, input: AddOkToPreviewCommentInput) -> str:
47+
paths = [
48+
p.replace(".md", "").replace("docs/", "").replace("README", "")
49+
for p in input.paths
50+
]
51+
links = [
52+
f"https://strawberry.rocks/docs/pr/{input.pr_number}/{path}"
53+
for path in paths
54+
]
55+
56+
comment = OK_TO_PREVIEW.format(links=links)
57+
58+
add_or_edit_comment(input.pr_number, comment, slug="ok-to-preview")
59+
60+
return "ok"
61+
3462
@strawberry.mutation
3563
def add_release_file_comment(self, input: AddReleaseFileCommentInput) -> str:
3664
"""Adds a comment about the release file to a PR."""
@@ -46,7 +74,7 @@ def add_release_file_comment(self, input: AddReleaseFileCommentInput) -> str:
4674
if input.release_info:
4775
comment = comment.format(changelog_preview=input.release_info.changelog)
4876

49-
add_or_edit_comment(input.pr_number, comment)
77+
add_or_edit_comment(input.pr_number, comment, slug="release-file")
5078
update_labels(input.pr_number, input.release_info)
5179

5280
return "ok"

app/templates.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
OK_TO_PREVIEW = """\
2+
Hi 👋 You can find a preview of the docs here:
3+
4+
{links}
5+
"""
6+
17
MISSING_RELEASE_FILE = """\
28
Hi, thanks for contributing to Strawberry 🍓!
39

0 commit comments

Comments
 (0)