Skip to content

Commit

Permalink
Iterating some more on how to structure actions
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Oct 31, 2024
1 parent 04a9cd7 commit 1e47ccb
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 96 deletions.
2 changes: 1 addition & 1 deletion lazy_github/lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AppearanceSettings(BaseModel):
dark_mode: bool = True
# Settings to configure which UI elements to display by default
show_command_log: bool = True
show_actions: bool = True
show_workflows: bool = True
show_issues: bool = True
show_pull_requests: bool = True

Expand Down
16 changes: 0 additions & 16 deletions lazy_github/lib/github/actions.py

This file was deleted.

30 changes: 30 additions & 0 deletions lazy_github/lib/github/workflows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from lazy_github.lib.context import LazyGithubContext
from lazy_github.models.github import Repository, Workflow, WorkflowRun


async def list_workflows(repository: Repository, page: int = 1, per_page: int = 30) -> list[Workflow]:
"""Lists available Github action workflows on the specified repo"""
query_params = {"page": page, "per_page": per_page}
url = f"/repos/{repository.owner.login}/{repository.name}/actions/workflows"
response = await LazyGithubContext.client.get(url, params=query_params)
response.raise_for_status()
raw_json = response.json()

if workflows := raw_json.get("workflows"):
return [Workflow(**w) for w in workflows]
else:
return []


async def list_workflow_runs(repository: Repository, page: int = 1, per_page: int = 30) -> list[WorkflowRun]:
"""Lists github workflows runs on the specified repo"""
query_params = {"page": page, "per_page": per_page}
url = f"/repos/{repository.owner.login}/{repository.name}/actions/runs"
response = await LazyGithubContext.client.get(url, params=query_params)
response.raise_for_status()
raw_json = response.json()

if workflows := raw_json.get("workflow_runs"):
return [WorkflowRun(**w) for w in workflows]
else:
return []
16 changes: 16 additions & 0 deletions lazy_github/models/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,19 @@ class Workflow(BaseModel):
created_at: datetime
updated_at: datetime
url: str


class WorkflowRun(BaseModel):
name: str
display_title: str
path: str
run_number: int
head_branch: str
status: str
conclusion: str
event: str
actor: User
triggering_actor: User
repository: Repository
created_at: datetime
updated_at: datetime
24 changes: 16 additions & 8 deletions lazy_github/ui/screens/primary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from lazy_github.ui.screens.new_issue import NewIssueModal
from lazy_github.ui.screens.new_pull_request import NewPullRequestModal
from lazy_github.ui.screens.settings import SettingsModal
from lazy_github.ui.widgets.actions import ActionsContainer
from lazy_github.ui.widgets.command_log import CommandLogSection
from lazy_github.ui.widgets.common import LazyGithubContainer
from lazy_github.ui.widgets.info import LazyGithubInfoTabPane
Expand All @@ -37,6 +36,7 @@
pull_request_to_cell,
)
from lazy_github.ui.widgets.repositories import ReposContainer
from lazy_github.ui.widgets.workflows import WorkflowsContainer


class CurrentlySelectedRepo(Widget):
Expand Down Expand Up @@ -121,9 +121,9 @@ def compose(self) -> ComposeResult:
issues.display = LazyGithubContext.config.appearance.show_issues
yield issues

actions = ActionsContainer(id="actions")
actions.display = LazyGithubContext.config.appearance.show_actions
yield actions
workflows = WorkflowsContainer(id="workflows")
workflows.display = LazyGithubContext.config.appearance.show_workflows
yield workflows

def action_open_issue(self) -> None:
self.trigger_issue_creation_flow()
Expand Down Expand Up @@ -160,8 +160,8 @@ def issues(self) -> IssuesContainer:
return self.query_one("#issues", IssuesContainer)

@property
def actions(self) -> ActionsContainer:
return self.query_one("#actions", ActionsContainer)
def actions(self) -> WorkflowsContainer:
return self.query_one("#workflows", WorkflowsContainer)

async def on_repo_selected(self, message: RepoSelected) -> None:
try:
Expand Down Expand Up @@ -196,14 +196,20 @@ class MainViewPane(Container):
("1", "focus_section('#repos_table')"),
("2", "focus_section('#pull_requests_table')"),
("3", "focus_section('#issues_table')"),
("4", "focus_section('#actions_table')"),
# ("4", "focus_section('#actions_table')"),
("4", "focus_workflow_tabs"),
("5", "focus_tabs"),
("6", "focus_section('LazyGithubCommandLog')"),
]

def action_focus_section(self, selector: str) -> None:
self.query_one(selector).focus()

def action_focus_workflow_tabs(self) -> None:
tabs = self.query_one("#workflow_tabs", TabbedContent)
if tabs.children and tabs.tab_count > 0:
tabs.children[0].focus()

def action_focus_tabs(self) -> None:
tabs = self.query_one("#selection_detail_tabs", TabbedContent)
if tabs.children and tabs.tab_count > 0:
Expand Down Expand Up @@ -253,7 +259,9 @@ def commands(self) -> tuple[LazyGithubCommand, ...]:
LazyGithubCommand(
"Toggle Command Log", partial(toggle_ui, "command_log"), "Toggle showing or hiding the command log"
),
LazyGithubCommand("Toggle Actions", partial(toggle_ui, "actions"), "Toggle showing or hiding repo actions"),
LazyGithubCommand(
"Toggle Workflows", partial(toggle_ui, "actions"), "Toggle showing or hiding repo actions"
),
LazyGithubCommand("Toggle Issues", partial(toggle_ui, "issues"), "Toggle showing or hiding repo issues"),
LazyGithubCommand(
"Toggle Pull Requests",
Expand Down
71 changes: 0 additions & 71 deletions lazy_github/ui/widgets/actions.py

This file was deleted.

89 changes: 89 additions & 0 deletions lazy_github/ui/widgets/workflows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from functools import partial

from textual.app import ComposeResult
from textual.containers import Container
from textual.widgets import DataTable, Label, TabbedContent, TabPane

from lazy_github.lib.github.workflows import list_workflows
from lazy_github.lib.messages import RepoSelected
from lazy_github.models.github import Repository, Workflow
from lazy_github.ui.widgets.command_log import log_event
from lazy_github.ui.widgets.common import LazilyLoadedDataTable, LazyGithubContainer


def workflow_to_cell(workflow: Workflow) -> tuple[str | int, ...]:
return (workflow.name, workflow.created_at.strftime("%c"), workflow.updated_at.strftime("%c"), workflow.path)


class AvailableWorkflowsContainers(Container):
workflows: dict[str, Workflow] = {}

def compose(self) -> ComposeResult:
yield LazilyLoadedDataTable(
id="searchable_workflows_table",
table_id="workflows_table",
search_input_id="workflows_search",
sort_key="name",
load_function=None,
batch_size=30,
reverse_sort=True,
)

@property
def searchable_table(self) -> LazilyLoadedDataTable:
return self.query_one("#searchable_workflows_table", LazilyLoadedDataTable)

@property
def table(self) -> DataTable:
return self.query_one("#workflows_table", DataTable)

def on_mount(self) -> None:
self.table.cursor_type = "row"
self.table.add_column("Name", key="name")
self.table.add_column("Created", key="created")
self.table.add_column("Updated", key="updated")
self.table.add_column("Path", key="path")

async def fetch_more_workflows(
self, repo: Repository, batch_size: int, batch_to_fetch: int
) -> list[tuple[str | int, ...]]:
next_page = await list_workflows(repo, page=batch_to_fetch, per_page=batch_size)
new_workflows = [w for w in next_page if not isinstance(w, Workflow)]
self.workflows.update({w.number: w for w in new_workflows})

return [workflow_to_cell(w) for w in new_workflows]

async def on_repo_selected(self, message: RepoSelected) -> None:
log_event("Repo selected")
workflows = await list_workflows(message.repo)
self.workflows = {}
rows = []
for workflow in workflows:
self.workflows[workflow.name] = workflow
rows.append(workflow_to_cell(workflow))

self.searchable_table.set_rows(rows)
self.searchable_table.change_load_function(partial(self.fetch_more_workflows, message.repo))
self.searchable_table.can_load_more = True
self.searchable_table.current_batch = 1


class WorkflowRunsContainer(Container):
def compose(self) -> ComposeResult:
yield Label("List of workflow runs")


class WorkflowsContainer(LazyGithubContainer):
def compose(self) -> ComposeResult:
self.border_title = "[4] Workflows"
with TabbedContent(id="workflow_tabs"):
with TabPane("Workflows", id="workflows_tab"):
yield AvailableWorkflowsContainers(id="workflows")
with TabPane("Runs", id="runs_tab"):
yield WorkflowRunsContainer(id="workflow_runs")

def on_repo_selected(self, message: RepoSelected) -> None:
self.query_one("#workflows", AvailableWorkflowsContainers).post_message(message)
self.query_one("#workflow_runs", WorkflowRunsContainer).post_message(message)
# This prevents the message from endlessly cycling and DDOSing the app :)
message.stop()

0 comments on commit 1e47ccb

Please sign in to comment.