|
5 | 5 | from discord.ui import ( |
6 | 6 | ActionRow, |
7 | 7 | Button, |
8 | | - Container, |
9 | 8 | LayoutView, |
10 | 9 | Section, |
11 | 10 | TextDisplay, |
|
15 | 14 | from yarl import URL |
16 | 15 |
|
17 | 16 | from ghutils.core.bot import GHUtilsBot |
18 | | -from ghutils.core.types import CustomEmoji |
| 17 | +from ghutils.ui.components.artifacts import WORKFLOW_PREFIX, ArtifactContainer |
19 | 18 | from ghutils.ui.components.paginated_select import ( |
20 | 19 | MAX_PAGE_LENGTH, |
21 | 20 | PaginatedSelect, |
22 | 21 | paginated_select, |
23 | 22 | ) |
24 | 23 | from ghutils.ui.components.visibility import add_visibility_buttons |
25 | 24 | from ghutils.utils.discord.commands import AnyInteractionCommand |
26 | | -from ghutils.utils.discord.mentions import relative_timestamp |
27 | 25 | from ghutils.utils.github import gh_request |
28 | 26 | from ghutils.utils.strings import join_truthy, truncate_str |
29 | 27 |
|
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 | | - |
121 | 28 |
|
122 | 29 | class SelectArtifactView(LayoutView): |
123 | 30 | bot: GHUtilsBot |
|
0 commit comments