5
5
from django .conf import settings
6
6
from requests_oauthlib import OAuth1Session , OAuth2Session
7
7
8
- from .models import GithubProject , GithubOrganization , BitbucketProject , BitbucketTeam
8
+ from .models import GithubProject , GithubOrganization , BitbucketProject
9
9
from tastyapi import apiv2
10
10
11
11
log = logging .getLogger (__name__ )
@@ -37,45 +37,6 @@ def get_oauth_session(user, provider):
37
37
return session or None
38
38
39
39
40
- def make_github_project (user , org , privacy , repo_json ):
41
- log .info ('Trying GitHub: %s' % repo_json ['full_name' ])
42
- if (repo_json ['private' ] is True and privacy == 'private' or
43
- repo_json ['private' ] is False and privacy == 'public' ):
44
- project , created = GithubProject .objects .get_or_create (
45
- full_name = repo_json ['full_name' ],
46
- users__pk = user .pk ,
47
- )
48
- if project .organization and project .organization != org :
49
- log .debug ('Not importing %s because mismatched orgs' % repo_json ['name' ])
50
- return None
51
- else :
52
- project .organization = org
53
- project .users .add (user )
54
- project .name = repo_json ['name' ]
55
- project .description = repo_json ['description' ]
56
- project .git_url = repo_json ['git_url' ]
57
- project .ssh_url = repo_json ['ssh_url' ]
58
- project .html_url = repo_json ['html_url' ]
59
- project .json = repo_json
60
- project .save ()
61
- return project
62
- else :
63
- log .debug ('Not importing %s because mismatched type' % repo_json ['name' ])
64
-
65
-
66
- def make_github_organization (user , org_json ):
67
- org , created = GithubOrganization .objects .get_or_create (
68
- login = org_json .get ('login' ),
69
- )
70
- org .html_url = org_json .get ('html_url' )
71
- org .name = org_json .get ('name' )
72
- org .email = org_json .get ('email' )
73
- org .json = org_json
74
- org .users .add (user )
75
- org .save ()
76
- return org
77
-
78
-
79
40
def get_token_for_project (project , force_local = False ):
80
41
if not getattr (settings , 'ALLOW_PRIVATE_REPOS' , False ):
81
42
return None
@@ -118,14 +79,13 @@ def github_paginate(session, url):
118
79
def import_github (user , sync ):
119
80
""" Do the actual github import """
120
81
121
- repo_type = getattr (settings , 'GITHUB_PRIVACY' , 'public' )
122
82
session = get_oauth_session (user , provider = 'github' )
123
83
if sync and session :
124
84
# Get user repos
125
85
owner_resp = github_paginate (session , 'https://api.github.com/user/repos?per_page=100' )
126
86
try :
127
87
for repo in owner_resp :
128
- make_github_project ( user = user , org = None , privacy = repo_type , repo_json = repo )
88
+ GithubProject . objects . create_from_api ( repo , user = user )
129
89
except TypeError , e :
130
90
print e
131
91
@@ -134,11 +94,13 @@ def import_github(user, sync):
134
94
resp = session .get ('https://api.github.com/user/orgs' )
135
95
for org_json in resp .json ():
136
96
org_resp = session .get ('https://api.github.com/orgs/%s' % org_json ['login' ])
137
- org_obj = make_github_organization (user = user , org_json = org_resp .json ())
97
+ org_obj = GithubOrganization .objects .create_from_api (
98
+ org_resp .json (), user = user )
138
99
# Add repos
139
100
org_repos_resp = github_paginate (session , 'https://api.github.com/orgs/%s/repos?per_page=100' % org_json ['login' ])
140
101
for repo in org_repos_resp :
141
- make_github_project (user = user , org = org_obj , privacy = repo_type , repo_json = repo )
102
+ GithubProject .objects .create_from_api (
103
+ repo , user = user , organization = org_obj )
142
104
except TypeError , e :
143
105
print e
144
106
@@ -171,58 +133,31 @@ def bitbucket_paginate(session, url):
171
133
return result
172
134
173
135
174
- def make_bitbucket_project (user , org , privacy , repo_json ):
175
- log .info ('Trying Bitbucket: %s' % repo_json ['full_name' ])
176
- if (repo_json ['is_private' ] is True and privacy == 'private' or
177
- repo_json ['is_private' ] is False and privacy == 'public' ):
178
- project , created = BitbucketProject .objects .get_or_create (
179
- full_name = repo_json ['full_name' ],
180
- )
181
- if project .organization and project .organization != org :
182
- log .debug ('Not importing %s because mismatched orgs' % repo_json ['name' ])
183
- return None
184
- else :
185
- project .organization = org
186
- project .users .add (user )
187
- project .name = repo_json ['name' ]
188
- project .description = repo_json ['description' ]
189
- project .git_url = repo_json ['links' ]['clone' ][0 ]['href' ]
190
- project .ssh_url = repo_json ['links' ]['clone' ][1 ]['href' ]
191
- project .html_url = repo_json ['links' ]['html' ]['href' ]
192
- project .vcs = repo_json ['scm' ]
193
- project .json = repo_json
194
- project .save ()
195
- return project
196
- else :
197
- log .debug ('Not importing %s because mismatched type' % repo_json ['name' ])
198
-
199
-
200
- def process_bitbucket_json (user , json , repo_type ):
136
+ def process_bitbucket_json (user , json ):
201
137
try :
202
138
for page in json :
203
139
for repo in page ['values' ]:
204
- make_bitbucket_project ( user = user , org = None , privacy = repo_type , repo_json = repo )
140
+ BitbucketProject . objects . create_from_api ( repo , user = user )
205
141
except TypeError , e :
206
142
print e
207
143
208
144
209
145
def import_bitbucket (user , sync ):
210
146
""" Do the actual github import """
211
147
212
- repo_type = getattr (settings , 'GITHUB_PRIVACY' , 'public' )
213
148
session = get_oauth_session (user , provider = 'bitbucket' )
214
149
if sync and session :
215
150
# Get user repos
216
151
try :
217
152
owner_resp = bitbucket_paginate (session , 'https://bitbucket.org/api/2.0/repositories/{owner}' .format (owner = user .username ))
218
- process_bitbucket_json (user , owner_resp , repo_type )
153
+ process_bitbucket_json (user , owner_resp )
219
154
except TypeError , e :
220
155
print e
221
156
222
157
# Get org repos
223
158
resp = session .get ('https://bitbucket.org/api/1.0/user/privileges/' )
224
159
for team in resp .json ()['teams' ].keys ():
225
160
org_resp = bitbucket_paginate (session , 'https://bitbucket.org/api/2.0/teams/{team}/repositories' .format (team = team ))
226
- process_bitbucket_json (user , org_resp , repo_type )
161
+ process_bitbucket_json (user , org_resp )
227
162
228
163
return session is not None
0 commit comments