Skip to content

Commit 218a4db

Browse files
committed
Move ArtifactContainer into its own file
1 parent d202005 commit 218a4db

File tree

2 files changed

+107
-94
lines changed

2 files changed

+107
-94
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from typing import Any
2+
3+
import humanize
4+
from discord.ui import (
5+
ActionRow,
6+
Button,
7+
Container,
8+
TextDisplay,
9+
)
10+
from githubkit.rest import Artifact, FullRepository, Workflow, WorkflowRun
11+
from yarl import URL
12+
13+
from ghutils.core.bot import GHUtilsBot
14+
from ghutils.core.types import CustomEmoji
15+
from ghutils.utils.discord.mentions import relative_timestamp
16+
17+
WORKFLOW_PREFIX = ".github/workflows/"
18+
19+
20+
class ArtifactContainer(Container[Any]):
21+
description_text = TextDisplay[Any]("")
22+
23+
button_row = ActionRow[Any]()
24+
25+
footer_text = TextDisplay[Any]("")
26+
27+
def set_artifact(
28+
self,
29+
bot: GHUtilsBot,
30+
repo: FullRepository,
31+
branch: str | None,
32+
workflow: Workflow,
33+
workflow_run: WorkflowRun,
34+
artifact: Artifact,
35+
):
36+
if not branch and "pull_request" not in workflow_run.event:
37+
branch = workflow_run.head_branch
38+
39+
is_normal = workflow.path.startswith(WORKFLOW_PREFIX)
40+
stripped_workflow_path = workflow.path.removeprefix(WORKFLOW_PREFIX)
41+
repo_url = URL("https://github.com") / repo.owner.login / repo.name
42+
workflow_url = repo_url / "actions/workflows" / stripped_workflow_path
43+
44+
description = [
45+
f"## `{artifact.name}.zip`",
46+
f"**Workflow:** [{workflow.name}]({workflow_url})"
47+
+ (
48+
f" ([{stripped_workflow_path}]({workflow.html_url}))"
49+
if is_normal
50+
else ""
51+
),
52+
f"**Workflow run:** [{workflow_run.display_title}]({workflow_run.html_url})",
53+
]
54+
if artifact.created_at:
55+
description.append(
56+
f"**Created:** {relative_timestamp(artifact.created_at)}"
57+
)
58+
if (
59+
artifact.updated_at
60+
and (artifact.updated_at - artifact.created_at).total_seconds() >= 1
61+
):
62+
description.append(
63+
f"**Updated:** {relative_timestamp(artifact.updated_at)}"
64+
)
65+
if artifact.expires_at:
66+
d_s = "d" if artifact.expired else "s"
67+
description.append(
68+
f"**Expire{d_s}:** {relative_timestamp(artifact.expires_at)}"
69+
)
70+
self.description_text.content = "\n".join(description)
71+
72+
self.button_row.clear_items()
73+
self.button_row.add_item(
74+
Button(
75+
emoji="⬇️",
76+
label=f"Download ({humanize.naturalsize(artifact.size_in_bytes, binary=True)})",
77+
url=str(
78+
repo_url
79+
/ "actions/runs"
80+
/ str(workflow_run.id)
81+
/ "artifacts"
82+
/ str(artifact.id)
83+
),
84+
disabled=artifact.expired,
85+
)
86+
)
87+
if branch and is_normal:
88+
self.button_row.add_item(
89+
Button(
90+
emoji=bot.get_custom_emoji(CustomEmoji.nightly_link),
91+
label="nightly.link",
92+
url=str(
93+
URL("https://nightly.link")
94+
/ repo.owner.login
95+
/ repo.name
96+
/ "workflows"
97+
/ stripped_workflow_path
98+
/ branch
99+
/ artifact.name
100+
),
101+
)
102+
)
103+
104+
self.footer_text.content = f"-# {repo.owner.login}/{repo.name}"
105+
if branch:
106+
self.footer_text.content += f"@{branch}"

bot/src/ghutils/ui/views/select_artifact.py

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from discord.ui import (
66
ActionRow,
77
Button,
8-
Container,
98
LayoutView,
109
Section,
1110
TextDisplay,
@@ -15,109 +14,17 @@
1514
from yarl import URL
1615

1716
from ghutils.core.bot import GHUtilsBot
18-
from ghutils.core.types import CustomEmoji
17+
from ghutils.ui.components.artifacts import WORKFLOW_PREFIX, ArtifactContainer
1918
from ghutils.ui.components.paginated_select import (
2019
MAX_PAGE_LENGTH,
2120
PaginatedSelect,
2221
paginated_select,
2322
)
2423
from ghutils.ui.components.visibility import add_visibility_buttons
2524
from ghutils.utils.discord.commands import AnyInteractionCommand
26-
from ghutils.utils.discord.mentions import relative_timestamp
2725
from ghutils.utils.github import gh_request
2826
from ghutils.utils.strings import join_truthy, truncate_str
2927

30-
WORKFLOW_PREFIX = ".github/workflows/"
31-
32-
33-
class ArtifactContainer(Container[Any]):
34-
description_text = TextDisplay[Any]("")
35-
36-
button_row = ActionRow[Any]()
37-
38-
footer_text = TextDisplay[Any]("")
39-
40-
def set_artifact(
41-
self,
42-
bot: GHUtilsBot,
43-
repo: FullRepository,
44-
branch: str | None,
45-
workflow: Workflow,
46-
workflow_run: WorkflowRun,
47-
artifact: Artifact,
48-
):
49-
if not branch and "pull_request" not in workflow_run.event:
50-
branch = workflow_run.head_branch
51-
52-
is_normal = workflow.path.startswith(WORKFLOW_PREFIX)
53-
stripped_workflow_path = workflow.path.removeprefix(WORKFLOW_PREFIX)
54-
repo_url = URL("https://github.com") / repo.owner.login / repo.name
55-
workflow_url = repo_url / "actions/workflows" / stripped_workflow_path
56-
57-
description = [
58-
f"## `{artifact.name}.zip`",
59-
f"**Workflow:** [{workflow.name}]({workflow_url})"
60-
+ (
61-
f" ([{stripped_workflow_path}]({workflow.html_url}))"
62-
if is_normal
63-
else ""
64-
),
65-
f"**Workflow run:** [{workflow_run.display_title}]({workflow_run.html_url})",
66-
]
67-
if artifact.created_at:
68-
description.append(
69-
f"**Created:** {relative_timestamp(artifact.created_at)}"
70-
)
71-
if (
72-
artifact.updated_at
73-
and (artifact.updated_at - artifact.created_at).total_seconds() >= 1
74-
):
75-
description.append(
76-
f"**Updated:** {relative_timestamp(artifact.updated_at)}"
77-
)
78-
if artifact.expires_at:
79-
d_s = "d" if artifact.expired else "s"
80-
description.append(
81-
f"**Expire{d_s}:** {relative_timestamp(artifact.expires_at)}"
82-
)
83-
self.description_text.content = "\n".join(description)
84-
85-
self.button_row.clear_items()
86-
self.button_row.add_item(
87-
Button(
88-
emoji="⬇️",
89-
label=f"Download ({humanize.naturalsize(artifact.size_in_bytes, binary=True)})",
90-
url=str(
91-
repo_url
92-
/ "actions/runs"
93-
/ str(workflow_run.id)
94-
/ "artifacts"
95-
/ str(artifact.id)
96-
),
97-
disabled=artifact.expired,
98-
)
99-
)
100-
if branch and is_normal:
101-
self.button_row.add_item(
102-
Button(
103-
emoji=bot.get_custom_emoji(CustomEmoji.nightly_link),
104-
label="nightly.link",
105-
url=str(
106-
URL("https://nightly.link")
107-
/ repo.owner.login
108-
/ repo.name
109-
/ "workflows"
110-
/ stripped_workflow_path
111-
/ branch
112-
/ artifact.name
113-
),
114-
)
115-
)
116-
117-
self.footer_text.content = f"-# {repo.owner.login}/{repo.name}"
118-
if branch:
119-
self.footer_text.content += f"@{branch}"
120-
12128

12229
class SelectArtifactView(LayoutView):
12330
bot: GHUtilsBot

0 commit comments

Comments
 (0)