forked from gitcoinco/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
62 lines (49 loc) · 2.36 KB
/
views.py
File metadata and controls
62 lines (49 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
"""Define the Gitcoin Bot views.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import json
from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from .actions import determine_response
@csrf_exempt
@require_POST
def payload(request):
"""Handle the Github bot payload.
Parse request.body bytes from github into json, retrieve relevant info
and respond with appropriate message from gitcoinbot actions.
Returns:
HttpResponse: The confirmation of Github payload acceptance.
"""
request_json = json.loads(request.body.decode('utf8'))
comment_dict = request_json.get('comment', {})
repo_dict = request_json.get('repository', {})
sender_dict = request_json.get('sender', {})
action = request_json.get('action', '')
does_address_gitcoinbot = f"@{settings.GITHUB_API_USER}" in comment_dict.get('body', '')
if (action == 'deleted') or sender_dict.get('login', '') == 'gitcoinbot[bot]' or not does_address_gitcoinbot:
# Gitcoinbot shoulsd not process these actions
return HttpResponse(status=204)
else:
owner = repo_dict.get('owner', {}).get('login')
repo = repo_dict.get('name')
comment_id = comment_dict.get('id')
comment_text = comment_dict.get('body')
issue_id = request_json.get('issue', {}).get('number')
installation_id = request_json.get('installation', {}).get('id')
# sender = request_json['sender']['login']
# issueURL = request_json['comment']['url']
determine_response(owner, repo, comment_id, comment_text, issue_id, installation_id)
return HttpResponse('Gitcoinbot Responded')