-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.py
101 lines (72 loc) · 3.25 KB
/
github.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Get data from github hook server and parse it."""
import copy
import os
import requests
from werkzeug.exceptions import BadRequest
def isValidPullRequest(data):
"""Check if pull request is valid."""
isValidRequest = validatePullRequest(data)
isValidAction = data.get('action') == 'review_requested' or data.get(
'action') == 'assigned'
return isValidRequest and isValidAction
def validatePullRequest(data):
"""Validate pull request by action."""
if 'action' not in data:
raise BadRequest('no event supplied')
if 'pull_request' not in data or 'html_url' not in data.get('pull_request'):
raise BadRequest('payload.pull_request.html_url missing')
return True
def getRecipientGithubUserNameByAction(data):
"""Get github user name by github action."""
payloadParser = GithubWebhookPayloadParser(data)
if data.get('action') == 'review_requested':
username = payloadParser.getRequestReviewerUserName()
elif data.get('action') == 'assigned':
username = payloadParser.getAssigneeUserName()
else:
raise BadRequest('Github username not found')
return username
def lookupGithubFullName(gh_username):
"""Get full name of the user."""
url = 'https://api.github.com/users/{}'.format(gh_username)
request = requests.get(url, auth=(os.environ.get(
'GITHUB_API_USER', ''), os.environ.get('GITHUB_API_TOKEN', '')))
user = request.json()
return user.get('name', '')
def notifyRecipient(data):
"""Notify PR author about reviews."""
# Work in progress
return data
class GithubWebhookPayloadParser:
"""A class to parse a github payload and return specific elements."""
def __init__(self, data=None):
if data is None:
data = {}
self._data = copy.deepcopy(data)
def getRequestReviewerUserName(self):
"""Parse and retrieve the requested reviewer username."""
return self._data.get('requested_reviewer', {}).get('login')
def getAssigneeUserName(self):
"""Parse and retrieve the assignee's username."""
return self._data.get('assignee', {}).get('login')
def getPullRequestTitle(self):
"""Parse and retrieve the pull request title."""
return self._data.get('pull_request', {}).get('title')
def getPullRequestUrl(self):
"""Parse and retrieve the pull request html url."""
return self._data.get('pull_request', {}).get('html_url')
def getPullRequestRepo(self):
"""Parse and retrieve the pull request repository name."""
return self._data.get('repository', {}).get('full_name')
def getPullRequestNumber(self):
"""Parse and retrieve the pull request number."""
return self._data.get('number')
def getPullRequestAuthor(self):
"""Parse and retrieve the pull request author."""
return self._data.get('pull_request', {}).get('user', {}).get('login')
def getPullRequestAuthorImage(self):
"""Parse and retrieve the pull request author image."""
return self._data.get('pull_request', {}).get('user', {}).get('avatar_url')
def getPullRequestDescription(self):
"""Parse and retrieve the pull request repository description."""
return self._data.get('pull_request', {}).get('body')