104 lines
2.4 KiB
Python
104 lines
2.4 KiB
Python
"""
|
|
Push status to Discord action
|
|
|
|
Pushes provided build status data to Discord as a new message or as
|
|
an edit to an existing message.
|
|
"""
|
|
|
|
import sys
|
|
import typing
|
|
import datetime
|
|
import os
|
|
|
|
import httpx
|
|
|
|
COLORS = {
|
|
"failure": 0xCB2431,
|
|
"success": 0x28A745,
|
|
"info": 0x0000FF,
|
|
}
|
|
|
|
|
|
class EmbedField(typing.TypedDict):
|
|
"""
|
|
Single embedded field
|
|
See: https://discord.com/developers/docs/resources/message#embed-object-embed-field-structure
|
|
"""
|
|
|
|
name: str
|
|
value: str
|
|
inline: bool
|
|
|
|
|
|
class Embed(typing.TypedDict):
|
|
"""
|
|
Single message embed
|
|
See: https://discord.com/developers/docs/resources/message#embed-object-embed-field-structure
|
|
"""
|
|
|
|
color: str
|
|
title: str
|
|
fields: list[EmbedField]
|
|
|
|
|
|
class MessageState(typing.TypedDict):
|
|
"""
|
|
Partial mapping of Discord message information.
|
|
|
|
See: https://discord.com/developers/docs/resources/channel#message-object
|
|
"""
|
|
|
|
id: int
|
|
timestamp: str
|
|
edited_timestamp: str
|
|
embeds: list[Embed]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
(
|
|
webhook_url,
|
|
status,
|
|
variant,
|
|
title,
|
|
) = sys.argv[1:5]
|
|
|
|
message_id = sys.argv[5] if len(sys.argv) == 6 else None
|
|
|
|
previous_message_data = (
|
|
httpx.get(f"{webhook_url}/messages/{message_id}").json()
|
|
if message_id is not None
|
|
else None
|
|
)
|
|
|
|
embed_data = Embed(
|
|
color=COLORS[variant],
|
|
title=title,
|
|
fields=[
|
|
EmbedField(name="Status", value=status, inline=True),
|
|
EmbedField(
|
|
name="Updated at",
|
|
value=str(datetime.datetime.now().ctime()),
|
|
inline=True,
|
|
),
|
|
],
|
|
)
|
|
|
|
if message_id:
|
|
previous_message_data = httpx.get(f"{webhook_url}/messages/{message_id}").json()
|
|
|
|
response = httpx.patch(
|
|
f"{webhook_url}/messages/{message_id}?wait=true",
|
|
json={"embeds": [*previous_message_data["embeds"], embed_data]},
|
|
)
|
|
message_id = response.json()["id"]
|
|
else:
|
|
response = httpx.post(f"{webhook_url}?wait=true", json={"embeds": [embed_data]})
|
|
message_id = response.json()["id"]
|
|
|
|
outputs_path = os.environ.get("GITHUB_OUTPUT")
|
|
|
|
if not outputs_path:
|
|
raise RuntimeError("Cannot write to outputs, no GITHUB_ENV set.")
|
|
|
|
with open(outputs_path, "a", encoding="utf-8") as outputs_file:
|
|
outputs_file.write(f"message-id={message_id}\n")
|