4
4
import json
5
5
import re
6
6
7
+ try :
8
+ from urlparse import urljoin , urlparse
9
+ except ImportError :
10
+ from urllib .parse import urljoin , urlparse # noqa
11
+
7
12
from django .conf import settings
8
13
from requests .exceptions import RequestException
9
14
from allauth .socialaccount .models import SocialToken
10
15
from allauth .socialaccount .providers .gitlab .views import GitLabOAuth2Adapter
11
- from urlparse import urljoin
12
16
13
17
from readthedocs .restapi .client import api
14
18
@@ -23,7 +27,9 @@ class GitLabService(Service):
23
27
"""Provider service for GitLab"""
24
28
25
29
adapter = GitLabOAuth2Adapter
26
- url_pattern = re .compile (re .escape (adapter .provider_base_url ))
30
+ # Just use the network location to determine if it's a GitLab project
31
+ # because private repos have another base url, eg. [email protected]
32
+ url_pattern = re .compile (re .escape (urlparse (adapter .provider_base_url ).netloc ))
27
33
default_avatar = {
28
34
'repo' : urljoin (settings .MEDIA_URL , 'images/fa-bookmark.svg' ),
29
35
'org' : urljoin (settings .MEDIA_URL , 'images/fa-users.svg' ),
@@ -172,24 +178,26 @@ def setup_webhook(self, project):
172
178
:rtype: bool
173
179
"""
174
180
session = self .get_session ()
181
+ resp = None
182
+ repositories = RemoteRepository .objects .filter (clone_url = project .vcs_repo ().repo_url )
175
183
184
+ if not repositories .exists ():
185
+ log .error ('GitLab remote repository not found' )
186
+ return False , resp
187
+
188
+ repo_id = repositories [0 ].get_serialized ()['id' ]
176
189
# See: http://doc.gitlab.com/ce/api/projects.html#add-project-hook
177
190
data = json .dumps ({
178
- 'id' : 'readthedocs' ,
191
+ 'id' : repo_id ,
179
192
'push_events' : True ,
180
193
'issues_events' : False ,
181
194
'merge_requests_events' : False ,
182
195
'note_events' : False ,
183
196
'tag_push_events' : True ,
184
197
'url' : u'https://{0}/gitlab' .format (settings .PRODUCTION_DOMAIN ),
185
198
})
186
- resp = None
199
+
187
200
try :
188
- repositories = RemoteRepository .objects .filter (
189
- clone_url = project .vcs_repo ().repo_url
190
- )
191
- assert repositories
192
- repo_id = repositories [0 ].get_serialized ()['id' ]
193
201
resp = session .post (
194
202
u'{url}/api/v3/projects/{repo_id}/hooks' .format (
195
203
url = self .adapter .provider_base_url ,
@@ -199,19 +207,13 @@ def setup_webhook(self, project):
199
207
headers = {'content-type' : 'application/json' }
200
208
)
201
209
if resp .status_code == 201 :
202
- log .info ('GitLab webhook creation successful for project: %s' , # noqa
203
- project )
204
- return (True , resp )
205
- except (AssertionError , RemoteRepository .DoesNotExist ) as ex :
206
- log .error ('GitLab remote repository not found' , exc_info = ex )
207
- except RequestException as ex :
208
- pass
210
+ log .info ('GitLab webhook creation successful for project: %s' , project )
211
+ return True , resp
212
+ except RequestException :
213
+ log .error ('GitLab webhook creation failed for project: %s' , project , exc_info = True )
209
214
else :
210
- ex = False
211
-
212
- log .error ('GitLab webhook creation failed for project: %s' , # noqa
213
- project , exc_info = ex )
214
- return (False , resp )
215
+ log .error ('GitLab webhook creation failed for project: %s' , project )
216
+ return False , resp
215
217
216
218
@classmethod
217
219
def get_token_for_project (cls , project , force_local = False ):
0 commit comments