-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into cast-default
- Loading branch information
Showing
17 changed files
with
220 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,67 @@ | ||
from enum import Enum | ||
|
||
import marvin | ||
from gh_util.functions import add_labels_to_issue, fetch_repo_labels | ||
from gh_util.types import GitHubIssueEvent | ||
from gh_util.types import GitHubIssueEvent, GitHubLabel | ||
from prefect import flow, task | ||
from prefect.events.schemas import DeploymentTrigger | ||
|
||
|
||
@flow(log_prints=True) | ||
async def label_issues( | ||
event_body_str: str, | ||
): # want to do {{ event.payload.body | from_json }} but not supported | ||
"""Label issues based on their action""" | ||
issue_event = GitHubIssueEvent.model_validate_json(event_body_str) | ||
print( | ||
f"Issue '#{issue_event.issue.number} - {issue_event.issue.title}' was {issue_event.action}" | ||
@task | ||
async def get_appropriate_labels( | ||
issue_body: str, label_options: set[GitHubLabel], existing_labels: set[GitHubLabel] | ||
) -> set[str]: | ||
LabelOption = Enum( | ||
"LabelOption", | ||
{label.name: label.name for label in label_options.union(existing_labels)}, | ||
) | ||
|
||
issue_body = issue_event.issue.body | ||
@marvin.fn | ||
async def get_labels( | ||
body: str, existing_labels: list[GitHubLabel] | ||
) -> set[LabelOption]: # type: ignore | ||
"""Return appropriate labels for a GitHub issue based on its body. | ||
If existing labels are sufficient, return them. | ||
""" | ||
|
||
return {i.value for i in await get_labels(issue_body, existing_labels)} | ||
|
||
|
||
@flow(log_prints=True) | ||
async def label_issues(event_body_json: str): | ||
"""Label issues based on incoming webhook events from GitHub.""" | ||
event = GitHubIssueEvent.model_validate_json(event_body_json) | ||
|
||
print(f"Issue '#{event.issue.number} - {event.issue.title}' was {event.action}") | ||
|
||
owner, repo = event.repository.owner.login, event.repository.name | ||
|
||
owner, repo = issue_event.repository.owner.login, issue_event.repository.name | ||
label_options = await task(fetch_repo_labels)(owner, repo) | ||
|
||
repo_labels = await task(fetch_repo_labels)(owner, repo) | ||
labels = await get_appropriate_labels( | ||
issue_body=event.issue.body, | ||
label_options=label_options, | ||
existing_labels=set(event.issue.labels), | ||
) | ||
|
||
label = task(marvin.classify)( | ||
issue_body, labels=[label.name for label in repo_labels] | ||
await task(add_labels_to_issue)( | ||
owner=owner, | ||
repo=repo, | ||
issue_number=event.issue.number, | ||
new_labels=labels, | ||
) | ||
|
||
await task(add_labels_to_issue)(owner, repo, issue_event.issue.number, {label}) | ||
print(f"Labeled issue with {' | '.join(labels)!r}") | ||
|
||
|
||
print(f"Labeled issue with '{label}'") | ||
if __name__ == "__main__": | ||
label_issues.serve( | ||
name="Label GitHub Issues", | ||
triggers=[ | ||
DeploymentTrigger( | ||
expect={"marvin.issue*"}, | ||
parameters={"event_body_json": "{{ event.payload.body }}"}, | ||
) | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.