Skip to content

feat: adds support for Discord Webhooks #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Media/*
Media/PGNs/*
Media/Networks/*
Media/Events/*

webhook
35 changes: 33 additions & 2 deletions OpenBench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@

import datetime
import hashlib
import json
import math
import os
import random
import re
import requests

Expand All @@ -41,6 +39,7 @@
from OpenBench.config import OPENBENCH_CONFIG
from OpenBench.models import *
from OpenBench.stats import TrinomialSPRT, PentanomialSPRT
from OpenBench.templatetags.mytags import longStatBlock


import OpenBench.views
Expand Down Expand Up @@ -396,6 +395,34 @@ def network_edit(request, engine, network):

return OpenBench.views.redirect(request, '/networks/%s' % (network.engine), status='Applied changes')

def notify_webhook(request, test_id):
webhook = open('webhook').read().strip() # Removing trailing whitespace/newline, if present
test = Test.objects.get(id=test_id)

# Compute stats
lower, elo, upper = OpenBench.stats.Elo(test.results())
error = max(upper - elo, elo - lower)
elo = OpenBench.templatetags.mytags.twoDigitPrecision(elo)
error = OpenBench.templatetags.mytags.twoDigitPrecision(error)
outcome = 'passed' if test.passed else 'failed'

# Green if passing, red if failing.
color = 0xFEFF58
if test.passed:
color = 0x37F769
elif test.wins < test.losses:
color = 0xFA4E4E

return requests.post(webhook, json={
'username': test.dev_engine,
'embeds': [{
'author': { 'name': test.author },
'title': f'Test `{test.dev.name}` vs `{test.base.name}` {outcome}',
'url': request.build_absolute_uri(f'/test/{test_id}'),
'color': color,
'description': f'```\n{longStatBlock(test)}\n```',
}]
})

def update_test(request, machine):

Expand Down Expand Up @@ -502,5 +529,9 @@ def update_test(request, machine):
Machine.objects.filter(id=machine_id).update(
updated=timezone.now()
)

# Send update to webhook, if it exists
if test.finished and os.path.exists('webhook'):
notify_webhook(request, test_id)

return [{}, { 'stop' : True }][test.finished]