1
|
|
|
# coding: utf-8 |
2
|
|
|
# github |
3
|
|
|
from github3 import GitHub |
4
|
|
|
|
5
|
|
|
# django classes |
6
|
|
|
from django.conf import settings |
7
|
|
|
from django.utils.log import getLogger |
8
|
|
|
from django.core.cache import caches |
9
|
|
|
|
10
|
|
|
# django_th classes |
11
|
|
|
from django_th.services.services import ServicesMgr |
12
|
|
|
|
13
|
|
|
""" |
14
|
|
|
handle process with github |
15
|
|
|
put the following in settings.py |
16
|
|
|
|
17
|
|
|
TH_GITHUB = { |
18
|
|
|
'username': 'username', |
19
|
|
|
'password': 'password', |
20
|
|
|
'consumer_key': 'my key', |
21
|
|
|
'consumer_secret': 'my secret' |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
TH_SERVICES = ( |
26
|
|
|
... |
27
|
|
|
'th_github.my_github.ServiceGithub', |
28
|
|
|
... |
29
|
|
|
) |
30
|
|
|
|
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
logger = getLogger('django_th.trigger_happy') |
34
|
|
|
|
35
|
|
|
cache = caches['th_github'] |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class ServiceGithub(ServicesMgr): |
39
|
|
|
|
40
|
|
|
def __init__(self, token=None, **kwargs): |
41
|
|
|
super(ServiceGithub, self).__init__(token, **kwargs) |
42
|
|
|
self.scope = ['public_repo'] |
43
|
|
|
self.REQ_TOKEN = 'https://github.com/login/oauth/authorize' |
44
|
|
|
self.AUTH_URL = 'https://github.com/login/oauth/authorize' |
45
|
|
|
self.ACC_TOKEN = 'https://github.com/login/oauth/access_token' |
46
|
|
|
self.username = settings.TH_GITHUB['username'] |
47
|
|
|
self.password = settings.TH_GITHUB['password'] |
48
|
|
|
self.consumer_key = settings.TH_GITHUB['consumer_key'] |
49
|
|
|
self.consumer_secret = settings.TH_GITHUB['consumer_secret'] |
50
|
|
|
self.token = token |
51
|
|
|
self.oauth = 'oauth1' |
52
|
|
|
self.service = 'ServiceGithub' |
53
|
|
|
if self.token: |
54
|
|
|
token_key, token_secret = self.token.split('#TH#') |
55
|
|
|
self.gh = GitHub(token=token_key) |
56
|
|
|
else: |
57
|
|
|
self.gh = GitHub(username=self.username, password=self.password) |
58
|
|
|
|
59
|
|
|
def read_data(self, **kwargs): |
60
|
|
|
""" |
61
|
|
|
get the data from the service |
62
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
63
|
|
|
:type kwargs: dict |
64
|
|
|
:rtype: list |
65
|
|
|
""" |
66
|
|
|
trigger_id = kwargs.get('trigger_id') |
67
|
|
|
data = list() |
68
|
|
|
cache.set('th_github_' + str(trigger_id), data) |
69
|
|
|
|
70
|
|
|
def save_data(self, trigger_id, **data): |
71
|
|
|
""" |
72
|
|
|
let's save the data |
73
|
|
|
:param trigger_id: trigger ID from which to save data |
74
|
|
|
:param data: the data to check to be used and save |
75
|
|
|
:type trigger_id: int |
76
|
|
|
:type data: dict |
77
|
|
|
:return: the status of the save statement |
78
|
|
|
:rtype: boolean |
79
|
|
|
""" |
80
|
|
|
from th_github.models import Github |
81
|
|
|
if self.token: |
82
|
|
|
title = self.set_title(data) |
83
|
|
|
body = self.set_content(data) |
84
|
|
|
# get the details of this trigger |
85
|
|
|
trigger = Github.objects.get(trigger_id=trigger_id) |
86
|
|
|
|
87
|
|
|
# check if it remains more than 1 access |
88
|
|
|
# then we can create an issue |
89
|
|
|
limit = self.gh.ratelimit_remaining |
90
|
|
|
if limit > 1: |
91
|
|
|
# repo goes to "owner" |
92
|
|
|
# project goes to "repository" |
93
|
|
|
r = self.gh.create_issue(trigger.repo, |
94
|
|
|
trigger.project, |
95
|
|
|
title, |
96
|
|
|
body) |
97
|
|
|
else: |
98
|
|
|
# rate limit reach |
99
|
|
|
logger.warn("Rate limit reached") |
100
|
|
|
# put again in cache the data that could not be |
101
|
|
|
# published in Github yet |
102
|
|
|
cache.set('th_github_' + str(trigger_id), data, version=2) |
103
|
|
|
return True |
104
|
|
|
sentence = str('github {} created').format(r) |
105
|
|
|
logger.debug(sentence) |
106
|
|
|
status = True |
107
|
|
|
else: |
108
|
|
|
sentence = "no token or link provided for trigger ID {} " |
109
|
|
|
logger.critical(sentence.format(trigger_id)) |
110
|
|
|
status = False |
111
|
|
|
|
112
|
|
|
return status |
113
|
|
|
|
114
|
|
|
def auth(self, request): |
115
|
|
|
""" |
116
|
|
|
let's auth the user to the Service |
117
|
|
|
:param request: request object |
118
|
|
|
:return: callback url |
119
|
|
|
:rtype: string that contains the url to redirect after auth |
120
|
|
|
""" |
121
|
|
|
auth = self.gh.authorize(self.username, |
122
|
|
|
self.password, |
123
|
|
|
self.scope, |
124
|
|
|
'', |
125
|
|
|
'', |
126
|
|
|
self.consumer_key, |
127
|
|
|
self.consumer_secret) |
128
|
|
|
request.session['oauth_token'] = auth.token |
129
|
|
|
request.session['oauth_id'] = auth.id |
130
|
|
|
return self.callback_url(request) |
131
|
|
|
|
132
|
|
|
def callback(self, request, **kwargs): |
133
|
|
|
""" |
134
|
|
|
Called from the Service when the user accept to activate it |
135
|
|
|
:param request: request object |
136
|
|
|
:return: callback url |
137
|
|
|
:rtype: string , path to the template |
138
|
|
|
""" |
139
|
|
|
access_token = request.session['oauth_token'] + "#TH#" |
140
|
|
|
access_token += str(request.session['oauth_id']) |
141
|
|
|
kwargs = {'access_token': access_token} |
142
|
|
|
return super(ServiceGithub, self).callback(request, **kwargs) |
143
|
|
|
|