forked from foxmask/django-th
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_github.py
173 lines (148 loc) · 5.81 KB
/
my_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# coding: utf-8
# github
from github3 import GitHub
# django classes
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.log import getLogger
from django.core.cache import caches
# django_th classes
from django_th.services.services import ServicesMgr
from django_th.models import UserService, ServicesActivated
"""
handle process with github
put the following in settings.py
TH_GITHUB = {
'username': 'username',
'password': 'password',
'consumer_key': 'my key',
'consumer_secret': 'my secret'
}
TH_SERVICES = (
...
'th_github.my_github.ServiceGithub',
...
)
"""
logger = getLogger('django_th.trigger_happy')
cache = caches['th_github']
class ServiceGithub(ServicesMgr):
def __init__(self, token=None):
super(ServiceGithub, self).__init__(token)
self.scope = ['public_repo']
self.REQ_TOKEN = 'https://github.com/login/oauth/authorize'
self.AUTH_URL = 'https://github.com/login/oauth/authorize'
self.ACC_TOKEN = 'https://github.com/login/oauth/access_token'
self.username = settings.TH_GITHUB['username']
self.password = settings.TH_GITHUB['password']
self.consumer_key = settings.TH_GITHUB['consumer_key']
self.consumer_secret = settings.TH_GITHUB['consumer_secret']
self.token = token
if self.token:
token_key, token_secret = self.token.split('#TH#')
self.gh = GitHub(token=token_key)
else:
self.gh = GitHub(username=self.username, password=self.password)
def read_data(self, **kwargs):
"""
get the data from the service
:param kwargs: contain keyword args : trigger_id at least
:type kwargs: dict
:rtype: list
"""
# date_triggered = kwargs['date_triggered']
trigger_id = kwargs['trigger_id']
data = list()
cache.set('th_github_' + str(trigger_id), data)
def process_data(self, **kwargs):
"""
get the data from the cache
:param kwargs: contain keyword args : trigger_id at least
:type kwargs: dict
"""
kw = {'cache_stack': 'th_github',
'trigger_id': str(kwargs['trigger_id'])}
return super(ServiceGithub, self).process_data(**kw)
def save_data(self, trigger_id, **data):
"""
let's save the data
:param trigger_id: trigger ID from which to save data
:param data: the data to check to be used and save
:type trigger_id: int
:type data: dict
:return: the status of the save statement
:rtype: boolean
"""
from th_github.models import Github
status = False
if self.token:
title = self.set_title(data)
body = self.set_content(data)
# get the details of this trigger
trigger = Github.objects.get(trigger_id=trigger_id)
# check if it remains more than 1 access
# then we can create an issue
limit = self.gh.ratelimit_remaining
if limit > 1:
# repo goes to "owner"
# project goes to "repository"
r = self.gh.create_issue(trigger.repo,
trigger.project,
title,
body)
else:
# rate limit reach
logger.warn("Rate limit reached")
# put again in cache the data that could not be
# published in Github yet
cache.set('th_github_' + str(trigger_id), data, version=2)
return True
sentance = str('github {} created').format(r)
logger.debug(sentance)
status = True
else:
sentance = "no token or link provided for trigger ID {} "
logger.critical(sentance.format(trigger_id))
status = False
return status
def auth(self, request):
"""
let's auth the user to the Service
"""
callback_url = 'http://%s%s' % (
request.get_host(), reverse('github_callback'))
auth = self.gh.authorize(self.username,
self.password,
self.scope,
'',
'',
self.consumer_key,
self.consumer_secret)
request.session['oauth_token'] = auth.token
request.session['oauth_id'] = auth.id
return callback_url
def callback(self, request, **kwargs):
"""
Called from the Service when the user accept to activate it
"""
try:
# finally we save the user auth token
# As we already stored the object ServicesActivated
# from the UserServiceCreateView now we update the same
# object to the database so :
# 1) we get the previous objet
us = UserService.objects.get(
user=request.user,
name=ServicesActivated.objects.get(name='ServiceGithub'))
# 2) Readability API require to use 4 parms consumer_key/secret +
# token_key/secret instead of usually get just the token
# from an access_token request. So we need to add a string
# seperator for later use to slpit on this one
access_token = request.session['oauth_token'] + "#TH#"
access_token += str(request.session['oauth_id'])
us.token = access_token
# 3) and save everything
us.save()
except KeyError:
return '/'
return 'github/callback.html'