diff --git a/.forgejo/actions/push-status-to-discord/action.yml b/.forgejo/actions/push-status-to-discord/action.yml index 05afb38..44395a0 100644 --- a/.forgejo/actions/push-status-to-discord/action.yml +++ b/.forgejo/actions/push-status-to-discord/action.yml @@ -8,13 +8,15 @@ inputs: status: description: "Status to report." required: true + message-id: + description: "Message ID to edit." outputs: message-id: description: "ID of the message that was posted, for editing purposes in further calls." - value: "" + value: ${{ steps.post-message.outputs.message-id }} -run: +runs: using: "composite" steps: - name: Setup Python @@ -22,5 +24,7 @@ run: with: python-version: 3.12 - name: Install dependencies - run: pip install . - - run: python ./main.py ${{ inputs.webhook_url }} ${{ inputs.project_name }} ${{inputs.status }} + run: pip install -r ./.forgejo/actions/push-status-to-discord/requirements.txt + - id: post-message + run: | + echo "message-id=$(python ./.forgejo/actions/push-status-to-discord/main.py ${{ inputs.webhook-url }} ${{ inputs.project-name }} ${{ inputs.status }} ${{ inputs.message-id }})" >> $GITHUB_OUTPUT diff --git a/.forgejo/actions/push-status-to-discord/main.py b/.forgejo/actions/push-status-to-discord/main.py index 115ff11..392e0c7 100644 --- a/.forgejo/actions/push-status-to-discord/main.py +++ b/.forgejo/actions/push-status-to-discord/main.py @@ -1,8 +1,44 @@ import httpx +import typing import sys +COLORS = { + "failure": 0xCB2431, + "success": 0x28A745, +} + +class EmbedField(typing.TypedDict): + name: str + value: str + inline: bool + +class Embed(typing.TypedDict): + color: str + title: str + fields: list[EmbedField] + if __name__ == "__main__": - webhook_url, project_name, status = sys.argv[1:] - response = httpx.post(webhook_url, json={"content": f"{project_name} - {status}"}) - print(response.json()) + webhook_url, project_name, status = sys.argv[1:4] + message_id = None + if len(sys.argv) > 4: + message_id = sys.argv[4] + + embed_data = Embed( + color=COLORS["success"], + title=f"{project_name}: build #", + fields=[ + EmbedField( + name="Status", + value=status, + inline=True + ) + ] + ) + + if message_id: + response = httpx.patch(f"{webhook_url}/messages/{message_id}?wait=true", json={"embeds": [embed_data]}) + else: + response = httpx.post(f"{webhook_url}?wait=true", json={"embeds": [embed_data]}) + + print(response.json()["id"]) diff --git a/.forgejo/actions/push-status-to-discord/requirements.txt b/.forgejo/actions/push-status-to-discord/requirements.txt new file mode 100644 index 0000000..610f441 --- /dev/null +++ b/.forgejo/actions/push-status-to-discord/requirements.txt @@ -0,0 +1 @@ +httpx==0.27 diff --git a/.forgejo/workflows/ci.yml b/.forgejo/workflows/ci.yml index de5e23f..c30672d 100644 --- a/.forgejo/workflows/ci.yml +++ b/.forgejo/workflows/ci.yml @@ -5,12 +5,12 @@ jobs: pre-run-notify: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v4 - uses: ./.forgejo/actions/push-status-to-discord with: webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }} project-name: ${{ github.repository }} status: "Started" - sast: runs-on: ubuntu-latest steps: @@ -22,3 +22,14 @@ jobs: run: pipx run black . --check - name: Check import ordering run: pipx run isort **/*.py --check + post-run-notify: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ./.forgejo/actions/push-status-to-discord + with: + webhook-url: ${{ secrets.DISCORD_WEBHOOK_URL }} + project-name: ${{ github.repository }} + status: "Success" + message-id: ${{ steps.pre-run-notify.outputs.message-id }} +