From 5910ced095743d4806f65a7d32ff67dc76196ba9 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Wed, 24 Jul 2024 19:34:26 -0400 Subject: [PATCH] refactor: tidy composite action into docker-based action --- Dockerfile | 12 ++++++++++++ action.yml | 37 ++++++++++++++----------------------- entrypoint.sh | 3 +++ main.py | 38 +++++++++++++++++++++++++------------- requirements.txt | 1 + 5 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 Dockerfile create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b1bfe25 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/action.yml b/action.yml index 4c00325..e8fa00f 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..8cbe8a3 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +python ./main.py "$@" diff --git a/main.py b/main.py index 603f1d6..a37df2f 100644 --- a/main.py +++ b/main.py @@ -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}") diff --git a/requirements.txt b/requirements.txt index 610f441..1ee01ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ httpx==0.27 +pydantic>=2.0.0