Skip to content
Open
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/*

webhooks.json
40 changes: 40 additions & 0 deletions OpenBench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 +397,41 @@ def network_edit(request, engine, network):

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

def notify_webhook(request, test_id):
test = Test.objects.get(id=test_id)
with open('webhooks.json') as webhooks:
webhooks = json.load(webhooks)
# If the test author does not have a webhook, exit now
if test.author.lower() not in webhooks:
return

# Fetch the specific webhook for this test author
webhook = webhooks[test.author.lower()]

# 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 @@ -503,4 +539,8 @@ def update_test(request, machine):
updated=timezone.now()
)

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

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