1
|
|
|
from github import Github |
2
|
|
|
import requests |
3
|
|
|
from bs4 import BeautifulSoup |
4
|
|
|
import json |
5
|
|
|
|
6
|
|
|
from st2actions.runners.pythonrunner import Action |
7
|
|
|
|
8
|
|
|
__all__ = [ |
9
|
|
|
'BaseGithubAction' |
10
|
|
|
] |
11
|
|
|
|
12
|
|
|
# Default Github web URL (used by tasks which directly scrape data from HTML) |
13
|
|
|
# pages |
14
|
|
|
DEFAULT_WEB_URL = 'https://github.com' |
15
|
|
|
|
16
|
|
|
# Default Github API url |
17
|
|
|
DEFAULT_API_URL = 'https://api.github.com' |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class BaseGithubAction(Action): |
21
|
|
|
def __init__(self, config): |
22
|
|
|
super(BaseGithubAction, self).__init__(config=config) |
23
|
|
|
token = self.config.get('token', None) |
24
|
|
|
self.token = token or None |
25
|
|
|
self.base_url = self.config.get('base_url', DEFAULT_API_URL) |
26
|
|
|
|
27
|
|
|
self._client = Github(self.token, base_url=self.base_url) |
28
|
|
|
self._session = requests.Session() |
29
|
|
|
|
30
|
|
|
def _web_session(self): |
31
|
|
|
'''Returns a requests session to scrape off the web''' |
32
|
|
|
login_url = DEFAULT_WEB_URL + '/login' |
33
|
|
|
session = requests.Session() |
34
|
|
|
request = session.get(login_url).text |
35
|
|
|
html = BeautifulSoup(request) |
36
|
|
|
token = html.find('input', {'name': 'authenticity_token'}).attrs['value'] |
37
|
|
|
commit_value = html.find('input', {'name': 'commit'}).attrs['value'] |
38
|
|
|
session_path = html.find('form', {'method': 'post'}).attrs['action'] |
39
|
|
|
|
40
|
|
|
login_data = { |
41
|
|
|
'login': self.config['user'], |
42
|
|
|
'password': self.config['password'], |
43
|
|
|
'commit': commit_value, |
44
|
|
|
'authenticity_token': token |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
session_url = DEFAULT_WEB_URL + session_path |
48
|
|
|
session.post(session_url, data=login_data) |
49
|
|
|
return session |
50
|
|
|
|
51
|
|
|
def _get_analytics(self, category, repo): |
52
|
|
|
url = DEFAULT_WEB_URL + repo + '/graphs/' + category + '.json' |
53
|
|
|
s = self._web_session() |
54
|
|
|
response = s.get(url) |
55
|
|
|
return response.json() |
56
|
|
|
|
57
|
|
|
def _get_user_token(self, user): |
58
|
|
|
return self.action_service.get_value( |
59
|
|
|
name="token_{}".format(user)) |
60
|
|
|
|
61
|
|
|
def _change_to_user_token(self, user): |
62
|
|
|
token = self.action_service.get_value( |
63
|
|
|
name="token_{}".format(user)) |
64
|
|
|
|
65
|
|
|
self._client = Github(token, base_url=self.base_url) |
66
|
|
|
|
67
|
|
|
return True |
68
|
|
|
|
69
|
|
|
def _request(self, method, uri, payload, token): |
70
|
|
|
headers = {'Authorization': 'token {}'.format(token)} |
71
|
|
|
|
72
|
|
|
url = "{}{}".format(self.base_url, |
73
|
|
|
uri) |
74
|
|
|
|
75
|
|
|
try: |
76
|
|
|
r = self._session.request(method, |
77
|
|
|
url, |
78
|
|
|
data=json.dumps(payload), |
79
|
|
|
headers=headers, |
80
|
|
|
verify=False) |
81
|
|
|
r.raise_for_status() |
82
|
|
|
except requests.exceptions.HTTPError: |
83
|
|
|
raise Exception( |
84
|
|
|
"ERROR: '{}'ing to '{}' - status code: {} payload: {}".format( |
85
|
|
|
method, url, r.status_code, json.dumps(payload))) |
86
|
|
|
except requests.exceptions.ConnectionError, e: |
87
|
|
|
raise Exception("Could not connect to: {} : {}".format(url, e)) |
88
|
|
|
else: |
89
|
|
|
if r.status_code == 204: |
90
|
|
|
return None |
91
|
|
|
else: |
92
|
|
|
return r.json() |
93
|
|
|
|