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: variant:
description: "Style to attach to the message element." description: "Style to attach to the message element."
default: "info" default: "info"
init: message-id:
description: "Marks the first status push, prevents pulling context." description: "Message ID to post to."
outputs:
message-id:
description: "Id of the message created, for later edits."
runs: runs:
using: "composite" using: "docker"
steps: image: "Dockerfile"
- name: Setup Python args:
uses: actions/setup-python@v5 - ${{ inputs.webhook-url }}
with: - ${{ inputs.status }}
python-version: 3.12 - ${{ inputs.variant }}
- uses: actions/download-artifact@v3 - ${{ inputs.title }}
if: ${{ !inputs.init }} - ${{ inputs.message-id }}
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

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