refactor: tidy composite action into docker-based action

This commit is contained in:
Marc 2024-07-24 19:34:26 -04:00
parent 0885f3a53e
commit 5910ced095
Signed by: marc
GPG key ID: 048E042F22B5DC79
5 changed files with 55 additions and 36 deletions

12
Dockerfile Normal file
View file

@ -0,0 +1,12 @@
FROM python:3.12-alpine AS base
RUN apk update && apk upgrade
COPY ./requirements.txt ./requirements.txt
RUN pip install --upgrade pip && pip install -r ./requirements.txt
COPY ./main.py ./main.py
COPY ./entrypoint.sh ./entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

View file

@ -13,28 +13,19 @@ inputs:
variant:
description: "Style to attach to the message element."
default: "info"
init:
description: "Marks the first status push, prevents pulling context."
message-id:
description: "Message ID to post to."
outputs:
message-id:
description: "Id of the message created, for later edits."
runs:
using: "composite"
steps:
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- uses: actions/download-artifact@v3
if: ${{ !inputs.init }}
with:
name: "${{github.sha}}-${{github.run_number}}-${{github.run_attempt}}-discord-context.zip"
path: /tmp
- name: Install dependencies
if: ${{ always() }}
run: pip install -r ${{ github.action_path }}/requirements.txt
- run: python ${{ github.action_path }}/main.py ${{ inputs.webhook-url }} "${{inputs.title}}" ${{ inputs.status }} ${{ inputs.variant }} /tmp/discord-context
- uses: actions/upload-artifact@v3
with:
path: /tmp/discord-context
name: "${{github.sha}}-${{github.run_number}}-${{github.run_attempt}}-discord-context.zip"
retention-days: 1
overwrite: true
using: "docker"
image: "Dockerfile"
args:
- ${{ inputs.webhook-url }}
- ${{ inputs.status }}
- ${{ inputs.variant }}
- ${{ inputs.title }}
- ${{ inputs.message-id }}

3
entrypoint.sh Normal file
View file

@ -0,0 +1,3 @@
#!/bin/bash
python ./main.py "$@"

38
main.py
View file

@ -1,8 +1,7 @@
import sys
import typing
import json
import pathlib
import datetime
import os
import httpx
@ -43,11 +42,20 @@ class Context(typing.TypedDict):
if __name__ == "__main__":
webhook_url, title, status, variant, context_path = sys.argv[1:6]
context = None
if pathlib.Path(context_path).exists():
with open(context_path, "r", encoding="utf-8") as context_file:
context = Context(**json.load(context_file))
(
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],
@ -62,15 +70,19 @@ if __name__ == "__main__":
],
)
if context:
message_id = context["message"]["id"]
if message_id is not None:
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": [*context["message"]["embeds"], embed_data]},
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]})
context = Context(message=response.json())
message_id = response.json()["id"]
with open(context_path, "w", encoding="utf-8") as context_file:
json.dump(context, context_file)
outputs_path = os.environ.get("GITHUB_ENV")
with open(outputs_path, "a", encoding="utf-8") as outputs_file:
outputs_file.write(f"message-id={message_id}")

View file

@ -1 +1,2 @@
httpx==0.27
pydantic>=2.0.0