diff --git a/.gitignore b/.gitignore index d79145a3..ae0e3c57 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,5 @@ Media/* Media/PGNs/* Media/Networks/* Media/Events/* + +webhooks.json diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 7a9eed44..204a24ae 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -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 @@ -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): @@ -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]