forked from chihacknight/civic-json-worker
-
Notifications
You must be signed in to change notification settings - Fork 53
/
run_update.py
1423 lines (1117 loc) · 52.8 KB
/
run_update.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import logging
from csv import DictReader
from itertools import groupby
from operator import itemgetter
from StringIO import StringIO
from datetime import datetime
from urllib2 import HTTPError, URLError
from urlparse import urlparse
from random import shuffle
from argparse import ArgumentParser
from time import time
from re import match, sub
from dateutil.tz import tzoffset
import feedparser
import json
from raven import Client as SentryClient
from requests import get, exceptions
from app import db, Project, Organization, Story, Event, Error, Issue, Label, Attendance
from feeds import get_first_working_feed_link
from utils import is_safe_name, safe_name, raw_name
# Logging Setup
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
# :NOTE: debug
# import warnings
# warnings.filterwarnings('error')
THREE_HOURS_IN_MS = 3 * 60 * 60 * 1000
# org sources filenames
ORG_SOURCES_FILENAME = 'org_sources.csv'
TEST_ORG_SOURCES_FILENAME = 'test_org_sources.csv'
# API URL templates
# TODO: use a Meetup client library with pagination
MEETUP_API_URL = "https://api.meetup.com/2/events?status=past,upcoming&format=json&group_urlname={group_urlname}&key={key}&desc=true&page=200"
MEETUP_COUNT_API_URL = "https://api.meetup.com/2/groups?group_urlname={group_urlname}&key={key}"
GITHUB_USER_API_URL = 'https://api.github.com/users/{username}'
GITHUB_USER_REPOS_API_URL = 'https://api.github.com/users/{username}/repos'
GITHUB_REPOS_API_URL = 'https://api.github.com/repos{repo_path}'
GITHUB_ISSUES_API_URL = 'https://api.github.com/repos{repo_path}/issues'
GITHUB_CONTENT_API_URL = 'https://api.github.com/repos{repo_path}/contents/{file_path}'
GITHUB_COMMIT_STATUS_URL = 'https://api.github.com/repos{repo_path}/commits/{default_branch}/status'
GITHUB_AUTH = None
if 'GITHUB_TOKEN' in os.environ:
GITHUB_AUTH = (os.environ['GITHUB_TOKEN'], '')
MEETUP_KEY = None
if 'MEETUP_KEY' in os.environ:
MEETUP_KEY = os.environ['MEETUP_KEY']
SENTRY = None
if 'SENTRY_DSN' in os.environ:
SENTRY = SentryClient(os.environ['SENTRY_DSN'])
GITHUB_THROTTLING = False
def get_github_api(url, headers=None):
'''
Make authenticated GitHub requests.
'''
global GITHUB_THROTTLING
got = get(url, auth=GITHUB_AUTH, headers=headers)
limit_hit, remaining = get_hit_github_ratelimit(got.headers)
logging.info(u'-{}- Asked Github for {}{}'.format(remaining, url, u' ({})'.format(headers) if headers and headers != {} else u''))
# check for throttling
if got.status_code == 403 and limit_hit:
# we've been throttled
GITHUB_THROTTLING = True
# log the error
logging.error(u"GitHub Rate Limit Remaining: {}".format(got.headers["X-Ratelimit-Remaining"]))
# save the error in the db
error_dict = {
"error": u'IOError: We done got throttled by GitHub',
"time": datetime.now()
}
new_error = Error(**error_dict)
# commit the error
db.session.add(new_error)
db.session.commit()
return got
def get_hit_github_ratelimit(headers):
''' Return True if we've hit the GitHub rate limit,
False if we haven't or if we can't figure it out.
Also return the remaining requests reported by GitHub.
'''
try:
remaining_str = headers['X-Ratelimit-Remaining']
except KeyError:
# no header by that name
return False, 0
try:
remaining_int = int(remaining_str)
except ValueError:
# value can't be converted into an integer
return False, 0
# return True if we've hit the limit
return remaining_int <= 0, remaining_int
def format_date(time_in_milliseconds, utc_offset_msec):
'''
Create a datetime object from a time in milliseconds from the epoch
'''
tz = tzoffset(None, utc_offset_msec / 1000.0)
dt = datetime.fromtimestamp(time_in_milliseconds / 1000.0, tz)
return datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
def format_location(venue):
if 'address_1' not in venue:
return venue['name']
address = venue['address_1']
if 'address_2' in venue and venue['address_2'] != '':
address = address + ', ' + venue['address_2']
return u'{name}\n{address}'.format(name=venue['name'], address=address)
def get_meetup_events(organization, group_urlname):
''' Get events associated with a group
'''
events = []
if not MEETUP_KEY:
logging.error("No meetup.com key set.")
return events
meetup_url = MEETUP_API_URL.format(group_urlname=group_urlname, key=MEETUP_KEY)
got = get(meetup_url)
if got.status_code in range(400, 499):
logging.error(u"{}'s meetup page cannot be found".format(organization.name))
return events
else:
try:
results = got.json()['results']
for event in results:
# "Scheduled event duration in milliseconds, if an end time is
# specified by the organizer. When not present, a default of 3
# hours may be assumed by applications"
# see: https://www.meetup.com/meetup_api/docs/:urlname/events/#list
duration = event.get('duration', THREE_HOURS_IN_MS)
eventdict = dict(
organization_name=organization.name,
name=event['name'],
event_url=event['event_url'],
start_time_notz=format_date(event['time'], event['utc_offset']),
end_time_notz=format_date(event['time'] + duration, event['utc_offset']),
created_at=format_date(event['created'], event['utc_offset']),
utc_offset=event['utc_offset'] / 1000.0,
rsvps=event['yes_rsvp_count'],
description=event.get('description')
)
# Some events don't have locations.
if 'venue' in event:
eventdict['location'] = format_location(event['venue'])
eventdict['lat'] = event['venue']['lat']
eventdict['lon'] = event['venue']['lon']
events.append(eventdict)
return events
except (TypeError, ValueError):
return events
def get_meetup_count(organization, identifier):
''' Get the count of meetup members
'''
meetup_url = MEETUP_COUNT_API_URL.format(group_urlname=identifier, key=MEETUP_KEY)
got = get(meetup_url)
members = None
if got and got.status_code // 100 == 2:
try:
response = got.json()
if response:
if response["results"]:
members = response["results"][0]["members"]
except ValueError: # meetup API returned non-JSON response
return None
return members
def get_organizations(org_sources):
''' Collate all organizations from different sources.
'''
organizations = []
with open(org_sources) as file:
for org_source in file.read().splitlines():
scheme, netloc, path, _, _, _ = urlparse(org_source)
is_json = os.path.splitext(path)[1] == '.json'
# if it's a local file...
if not scheme and not netloc:
if is_json:
organizations.extend(get_organizations_from_local_json(org_source))
else:
organizations.extend(get_organizations_from_local_csv(org_source))
elif is_json:
organizations.extend(get_organizations_from_json(org_source))
elif 'docs.google.com' in org_source:
organizations.extend(get_organizations_from_spreadsheet(org_source))
return organizations
def get_organizations_from_json(org_source):
''' Get a row for each organization from a remote JSON file.
'''
got = get(org_source)
return got.json()
def get_organizations_from_spreadsheet(org_source):
'''
Get a row for each organization from the Brigade Info spreadsheet.
Return a list of dictionaries, one for each row past the header.
'''
got = get(org_source)
#
# Requests response.text is a lying liar, with its UTF8 bytes as unicode()?
# Use response.content to plain bytes, then decode everything.
#
organizations = list(DictReader(StringIO(got.content)))
return decode_organizations_list(organizations)
def get_organizations_from_local_csv(org_source):
''' Get a row for each organization from a local CSV file.
Return a list of dictionaries, one for each row past the header.
'''
organizations = list(DictReader(open(org_source, 'rb')))
return decode_organizations_list(organizations)
def get_organizations_from_local_json(org_source):
''' Get a row for each organization from a local JSON file.
Return a list of dictionaries, one for each row past the header.
'''
with open(org_source, 'rb') as org_data:
organizations = json.load(org_data)
return organizations
def decode_organizations_list(organizations):
'''
Decode keys and values in a list of organizations
'''
for (index, org) in enumerate(organizations):
organizations[index] = dict([(k.decode('utf8'), v.decode('utf8'))
for (k, v) in org.items()])
return organizations
def get_stories(organization):
''' Get two recent stories from an rss feed.
'''
# If there is no given rss link, try the website url.
if organization.rss:
rss = organization.rss
else:
rss = organization.website
stories = []
# Extract a valid RSS feed from the URL
try:
url = get_first_working_feed_link(rss)
# If no feed found then give up
if not url:
return stories
except (HTTPError, ValueError, URLError):
return stories
try:
logging.info('Asking cyberspace for ' + url)
d = feedparser.parse(get(url).text)
except (HTTPError, URLError, exceptions.SSLError):
return stories
#
# Return dictionaries for the two most recent entries.
#
stories = [dict(title=e.title, link=e.link, type=u'blog', organization_name=organization.name) for e in d.entries[:2]]
return stories
def get_adjoined_json_lists(response, headers=None):
''' Github uses the Link header (RFC 5988) to do pagination.
If we see a Link header, assume we're dealing with lists
and concat them all together.
'''
result = response.json()
status_code = response.status_code
if type(result) is list:
while 'next' in response.links:
response = get_github_api(response.links['next']['url'], headers=headers)
status_code = response.status_code
# Consider any status other than 2xx an error
if not status_code // 100 == 2:
break
result += response.json()
return result, status_code
def parse_github_user(url):
''' given a URL, returns the github username or None if it is not a Github URL '''
_, host, path, _, _, _ = urlparse(url)
matched = match(r'(/orgs)?/(?P<name>[^/]+)/?$', path)
if host in ('www.github.com', 'github.com') and matched:
return matched.group('name')
def is_official_brigade(org_info):
'''
Given an entry in the org info source (e.g. brigade-information), returns
true if the org is an "official" CfA brigade
'''
tags = org_info.get('tags', [])
return 'Code for America' in tags and 'Official' in tags
def get_projects(organization):
'''
Get a list of projects from CSV, TSV, JSON, or Github URL.
Convert to a dict.
TODO: Have this work for GDocs.
'''
# don't try to process an empty projects_list_url
if not organization.projects_list_url:
return []
# If projects_list is a GitHub organization
# Use the GitHub auth to request all the included repos.
# Follow next page links
github_username = parse_github_user(organization.projects_list_url)
if github_username:
projects_url = GITHUB_USER_REPOS_API_URL.format(username=github_username)
try:
got = get_github_api(projects_url)
# Consider any status other than 2xx an error
if not got.status_code // 100 == 2:
return []
projects, _ = get_adjoined_json_lists(got)
except exceptions.RequestException:
# Something has gone wrong, probably a bad URL or site is down.
return []
# Else its a csv or json of projects
else:
projects_url = organization.projects_list_url
logging.info('Asking for ' + projects_url)
try:
response = get(projects_url)
# Consider any status other than 2xx an error
if not response.status_code // 100 == 2:
return []
# If its a csv
if "csv" in projects_url and (('content-type' in response.headers and 'text/csv' in response.headers['content-type']) or 'content-type' not in response.headers):
data = response.content.splitlines()
projects = list(DictReader(data, dialect='excel'))
# convert all the values to unicode
for project in projects:
for project_key, project_value in project.items():
if project_key:
project_key = project_key.lower()
# some values might be lists
if type(project_value) is list:
project_value = [unicode(item.decode('utf8')) for item in project_value]
project[project_key] = project_value
# some values might be empty strings
elif type(project_value) in (str, unicode) and unicode(project_value.decode('utf8')) == u'':
project[project_key] = None
# we want tags to be a list with no whitespace
elif project_key == 'tags':
project_value = unicode(project_value.decode('utf8'))
project[project_key] = [tag.strip() for tag in project_value.split(',')]
else:
project[project_key] = unicode(project_value.decode('utf8'))
# Else just grab it as json
else:
try:
projects = response.json()
except ValueError:
# Not a json file.
return []
except exceptions.RequestException:
# Something has gone wrong, probably a bad URL or site is down.
return []
# If projects is just a list of GitHub urls, like Open Gov Hack Night
# turn it into a list of dicts with minimal project information
if len(projects) and type(projects[0]) in (str, unicode):
projects = [dict(code_url=item, organization_name=organization.name) for item in projects]
# If data is list of dicts, like BetaNYC or a GitHub org
elif len(projects) and type(projects[0]) is dict:
for project in projects:
project['organization_name'] = organization.name
if "homepage" in project:
project["link_url"] = project["homepage"]
if "html_url" in project:
project["code_url"] = project["html_url"]
for key in project.keys():
if key not in ['name', 'description', 'link_url', 'code_url', 'type', 'categories', 'tags', 'organization_name', 'status']:
del project[key]
# Get any updates on the projects
projects = [update_project_info(proj) for proj in projects]
# Drop projects with no updates
projects = filter(None, projects)
# Add organization names along the way.
for project in projects:
project['organization_name'] = organization.name
return projects
def github_latest_update_time(github_details):
'''
Use `pushed_at` date, if present, which is updated any time any
branch is pushed. This tends to be more similar to the first
date visible when users click through to the project -- the
last commit's date. If there is no pushed_at date then fall back to `updated_at` date.
It's still not perfect, but this will be a quick improvement to avoid the
confusion of seeing a "last modified yesterday" project that actually
hasn't seen a commit in three years.
(See issue #245 for some context, but we ripped it out)
'''
import dateutil.parser
datetime_format = '%a, %d %b %Y %H:%M:%S %Z'
if 'pushed_at' in github_details:
update_time = github_details['pushed_at']
elif 'updated_at' in github_details:
update_time = github_details['updated_at']
else:
return datetime.now()
return dateutil.parser.parse(update_time).strftime(datetime_format)
def non_github_project_update_time(project):
''' If its a non-github project, we should check if any of the fields
have been updated, such as the description.
Set the last_updated timestamp.
'''
filters = [Project.name == project['name'], Project.organization_name == project['organization_name']]
existing_project = db.session.query(Project).filter(*filters).first()
if existing_project:
# project gets existing last_updated
project['last_updated'] = existing_project.last_updated
# unless one of the fields has been updated
for key, value in project.iteritems():
if project[key] != existing_project.__dict__[key]:
project['last_updated'] = datetime.now().strftime("%a, %d %b %Y %H:%M:%S %Z")
else:
# Set a date when we first see a non-github project
project['last_updated'] = datetime.now().strftime("%a, %d %b %Y %H:%M:%S %Z")
return project
def make_root_github_project_path(path):
''' Strip anything extra off the end of a github path
'''
path_split = path.split('/')
path = '/'.join(path_split[0:3])
# some URLs have been passed to us with '.git' at the end
path = sub(ur'\.git$', '', path)
return path
def update_project_info(project):
''' Update info from Github, if it's missing.
Modify the project in-place and return nothing.
Complete repository project details go into extras, for example
project details from Github can be found under "github_details".
Github_details is specifically expected to be used on this page:
http://opengovhacknight.org/projects.html
'''
if 'code_url' not in project or not project['code_url']:
project = non_github_project_update_time(project)
return project
_, host, path, _, _, _ = urlparse(project['code_url'])
if host != 'github.com':
project = non_github_project_update_time(project)
return project
# Get the Github attributes
if host == 'github.com':
path = sub(r"[\s\/]+?$", "", path)
# make sure we're working with the main github URL
path = make_root_github_project_path(path)
repo_url = GITHUB_REPOS_API_URL.format(repo_path=path)
# find an existing project, filtering on code_url, organization_name, and project name (if we know it)
existing_filter = [Project.code_url == project['code_url'], Project.organization_name == project['organization_name']]
if 'name' in project and project['name']:
existing_filter.append(Project.name == project['name'])
existing_project = db.session.query(Project).filter(*existing_filter).first()
# if we're throttled, make sure an existing project is kept and return none
if GITHUB_THROTTLING:
if existing_project:
# :::here (project/true)
existing_project.keep = True
# commit the project
db.session.commit()
return None
# keep track of org spreadsheet values
spreadsheet_is_updated = False
if existing_project:
# copy 'last_updated' values from the existing project to the project dict
project['last_updated'] = existing_project.last_updated
project['last_updated_issues'] = existing_project.last_updated_issues
project['last_updated_civic_json'] = existing_project.last_updated_civic_json
project['last_updated_root_files'] = existing_project.last_updated_root_files
# check whether any of the org spreadsheet values for the project have changed
for project_key in project:
check_value = project[project_key]
existing_value = existing_project.__dict__[project_key]
if check_value and check_value != existing_value:
spreadsheet_is_updated = True
project[project_key] = check_value
elif not check_value and existing_value:
project[project_key] = existing_value
# request project info from GitHub with the If-Modified-Since header
if existing_project.last_updated:
last_updated = datetime.strftime(existing_project.last_updated, "%a, %d %b %Y %H:%M:%S GMT")
got = get_github_api(repo_url, headers={"If-Modified-Since": last_updated})
# In rare cases, a project can be saved without a last_updated.
else:
got = get_github_api(repo_url)
else:
got = get_github_api(repo_url)
if got.status_code in range(400, 499):
if got.status_code == 404:
# It's a bad GitHub link
logging.error(u"{} doesn't exist.".format(repo_url))
# If there's an existing project in the database, get rid of it
if existing_project:
# this is redundant, but let's make sure
# :::here (project/false)
existing_project.keep = False
db.session.commit()
# Take the project out of the loop by returning None
return None
elif got.status_code == 403:
# Throttled by GitHub
if existing_project:
# :::here (project/true)
existing_project.keep = True
# commit the project
db.session.commit()
return None
else:
raise IOError
# If the project has not been modified...
elif got.status_code == 304:
logging.info(u'Project {} has not been modified since last update'.format(repo_url))
# if values have changed, copy untouched values from the existing project object and return it
if spreadsheet_is_updated:
logging.info('Project %s has been modified via spreadsheet.', repo_url)
project['github_details'] = existing_project.github_details
return project
# nothing was updated, but make sure we keep the project
# :::here (project/true)
existing_project.keep = True
# commit the project
db.session.commit()
return None
# the project has been modified
all_github_attributes = got.json()
github_details = {}
for field in ('contributors_url', 'created_at', 'forks_count', 'homepage',
'html_url', 'id', 'open_issues', 'pushed_at',
'updated_at', 'watchers_count', 'name', 'description',
'stargazers_count', 'subscribers_count'):
github_details[field] = all_github_attributes[field]
github_details['owner'] = dict()
for field in ('avatar_url', 'html_url', 'login', 'type'):
github_details['owner'][field] = all_github_attributes['owner'][field]
project['github_details'] = github_details
if 'name' not in project or not project['name']:
project['name'] = all_github_attributes['name']
if 'description' not in project or not project['description']:
project['description'] = all_github_attributes['description']
if 'link_url' not in project or not project['link_url']:
project['link_url'] = all_github_attributes['homepage']
project['last_updated'] = github_latest_update_time(github_details)
# Grab the list of project languages
got = get_github_api(all_github_attributes['languages_url'])
languages_json = got.json()
if got.status_code // 100 == 2 and languages_json.keys():
project['languages'] = languages_json.keys()
else:
project['languages'] = None
#
# Populate project contributors from github_details[contributors_url]
#
project['github_details']['contributors'] = []
got = get_github_api(all_github_attributes['contributors_url'])
try:
contributors_json = got.json()
for contributor in contributors_json:
# we don't want people without email addresses?
if contributor['login'] == 'invalid-email-address':
break
project['github_details']['contributors'].append(dict())
for field in ('login', 'url', 'avatar_url', 'html_url', 'contributions'):
project['github_details']['contributors'][-1][field] = contributor[field]
# flag the owner with a boolean value
project['github_details']['contributors'][-1]['owner'] \
= bool(contributor['login'] == project['github_details']['owner']['login'])
except:
pass
#
# Populate project participation from github_details[url] + "/stats/participation"
# Sometimes GitHub returns a blank dict instead of no participation.
#
got = get_github_api(all_github_attributes['url'] + '/stats/participation')
try:
participation_json = got.json()
project['github_details']['participation'] = participation_json['all']
except:
project['github_details']['participation'] = [0] * 50
#
# Populate values from the civic.json if it exists/is updated
#
project, civic_json_is_updated = update_project_from_civic_json(project_dict=project, force=spreadsheet_is_updated)
# Get the lastest commit status
# First build up the url to use
if "default_branch" in all_github_attributes:
commit_status_url = GITHUB_COMMIT_STATUS_URL.format(repo_path=path, default_branch=all_github_attributes['default_branch'])
got = get_github_api(commit_status_url)
project["commit_status"] = got.json().get('state', None)
return project
def extract_tag_value(tag_candidate):
''' Extract the value of a tag from a string or object. tag_candidate must
be in the form of either u'tag value' or {'tag': u'tag value'}
'''
if (type(tag_candidate) is str or type(tag_candidate) is unicode) and len(tag_candidate) > 0:
# unicodeify
tag_candidate = unicode(tag_candidate)
# escape csv characters
tag_candidate = sub(u'"', u'""', tag_candidate)
tag_candidate = u'"{}"'.format(tag_candidate) if u',' in tag_candidate or u'"' in tag_candidate else tag_candidate
return tag_candidate
if type(tag_candidate) is dict and 'tag' in tag_candidate:
return extract_tag_value(tag_candidate['tag'])
return None
def get_tags_from_civic_json_object(tags_in):
''' Extract and return tags in the correct format from the passed object
'''
# the in object should be a list with something in it
if type(tags_in) is not list or not len(tags_in):
return None
# get the tags
extracted = [extract_tag_value(item) for item in tags_in]
# strip None values and return as a list
return [item for item in extracted if item is not None]
def update_project_from_civic_json(project_dict, force=False):
''' Update and return the passed project dict with values from civic.json
'''
civic_json = get_civic_json_for_project(project_dict, force)
is_updated = False
# get status
existing_status = project_dict['status'] if 'status' in project_dict else None
if 'status' in civic_json and existing_status != civic_json['status']:
project_dict['status'] = civic_json['status'] if civic_json['status'].strip() else None
is_updated = True
# get tags
existing_tags = project_dict['tags'] if 'tags' in project_dict else None
civic_tags = get_tags_from_civic_json_object(civic_json['tags']) if 'tags' in civic_json else None
if civic_tags and existing_tags != civic_tags:
project_dict['tags'] = civic_tags
is_updated = True
# add other attributes from civic.json here
return project_dict, is_updated
def get_issues_for_project(project):
''' get the issues for a single project in dict format
without touching the database (used for testing)
'''
issues = []
if not project.code_url:
return issues
# Get github issues api url
_, host, path, _, _, _ = urlparse(project.code_url)
path = sub(r"[\s\/]+?$", "", path)
# make sure we're working with the main github URL
path = make_root_github_project_path(path)
issues_url = GITHUB_ISSUES_API_URL.format(repo_path=path)
# Ping github's api for project issues
got = get_github_api(issues_url, headers={'If-None-Match': project.last_updated_issues})
if got.status_code // 100 != 2:
return issues
# Save each issue in response
responses, _ = get_adjoined_json_lists(got, headers={'If-None-Match': project.last_updated_issues})
for issue in responses:
# Type check the issue, we are expecting a dictionary
if isinstance(issue, dict):
# Pull requests are returned along with issues. Skip them.
if "/pull/" in issue['html_url']:
continue
issue_dict = dict(project_id=project.id)
for field in (
'title', 'html_url', 'body',
'labels', 'created_at', 'updated_at'):
issue_dict[field] = issue.get(field, None)
issues.append(issue_dict)
else:
logging.error('Issue for project %s is not a dictionary', project.name)
return issues
def get_issues(project):
''' Get github issues associated with the passed Project.
'''
issues = []
# don't try to parse an empty code_url
if not project.code_url:
return issues
# Mark this project's issues for deletion
# :::here (issue/false)
db.session.execute(db.update(Issue, values={'keep': False}).where(Issue.project_id == project.id))
# Get github issues api url
_, host, path, _, _, _ = urlparse(project.code_url)
# Only check issues if its a github project
if host != 'github.com':
return issues
path = sub(r"[\s\/]+?$", "", path)
# make sure we're working with the main github URL
path = make_root_github_project_path(path)
issues_url = GITHUB_ISSUES_API_URL.format(repo_path=path)
# Ping github's api for project issues
# :TODO: non-github projects are hitting here and shouldn't be!
got = get_github_api(issues_url, headers={'If-None-Match': project.last_updated_issues})
# A 304 means that issues have not been modified since we last checked
if got.status_code == 304:
# :::here (issue/true)
db.session.execute(db.update(Issue, values={'keep': True}).where(Issue.project_id == project.id))
logging.info('Issues %s have not changed since last update', issues_url)
elif got.status_code not in range(400, 499):
# Update the project's last_updated_issue field
project.last_updated_issues = unicode(got.headers['ETag'])
db.session.add(project)
# Get all the pages of issues
responses, _ = get_adjoined_json_lists(got)
# Save each issue in response
for issue in responses:
# Type check the issue, we are expecting a dictionary
if isinstance(issue, dict):
# Pull requests are returned along with issues. Skip them.
if "/pull/" in issue['html_url']:
continue
issue_dict = dict(project_id=project.id)
for field in (
'title', 'html_url', 'body',
'labels', 'created_at', 'updated_at'):
issue_dict[field] = issue.get(field, None)
issues.append(issue_dict)
else:
logging.error('Issue for project %s is not a dictionary', project.name)
return issues
def get_root_directory_listing_for_project(project_dict, force=False):
''' Get a listing of the project's github repo root directory. Will return
an empty list if the listing hasn't changed since the last time we asked
unless force is True.
'''
listing = []
if 'code_url' not in project_dict or not project_dict['code_url']:
return listing
# Get the API URL
_, host, path, _, _, _ = urlparse(project_dict['code_url'])
path = sub(r"[\s\/]+?$", "", path)
# make sure we're working with the main github URL
path = make_root_github_project_path(path)
directory_url = GITHUB_CONTENT_API_URL.format(repo_path=path, file_path='')
# Request the directory listing
request_headers = {}
if 'last_updated_root_files' in project_dict and not force:
request_headers['If-None-Match'] = project_dict['last_updated_root_files']
got = get_github_api(directory_url, headers=request_headers)
# Verify that content has not been modified since last run
if got.status_code == 304:
logging.info(u'root directory listing has not changed since last update for {}'.format(directory_url))
elif got.status_code not in range(400, 499):
logging.info(u'root directory listing has changed for {}'.format(directory_url))
# Update the project's last_updated_root_files field
project_dict['last_updated_root_files'] = unicode(got.headers['ETag'])
# get the contents of the file
listing = got.json()
else:
logging.info(u'NO root directory listing found for {}'.format(directory_url))
return listing
def get_civic_json_exists_for_project(project_dict, force=False):
''' Return True if the passed project has a civic.json file in its root directory.
'''
directory_listing = get_root_directory_listing_for_project(project_dict, force)
exists = 'civic.json' in [item['name'] for item in directory_listing]
return exists
def get_civic_json_for_project(project_dict, force=False):
''' Get the contents of the civic.json at the project's github repo root, if it exists.
'''
civic = {}
# return an empty dict if civic.json doesn't exist (or hasn't been updated)
if not get_civic_json_exists_for_project(project_dict, force):
return civic
# Get the API URL (if 'code_url' wasn't in project_dict, it would've been caught upstream)
_, host, path, _, _, _ = urlparse(project_dict['code_url'])
path = sub(r"[\s\/]+?$", "", path)
# make sure we're working with the main github URL
path = make_root_github_project_path(path)
civic_url = GITHUB_CONTENT_API_URL.format(repo_path=path, file_path='civic.json')
# Request the contents of the civic.json file
# without the 'Accept' header we'd get information about the
# file rather than the contents of the file
request_headers = {'Accept': 'application/vnd.github.v3.raw'}
if 'last_updated_civic_json' in project_dict and not force:
request_headers['If-None-Match'] = project_dict['last_updated_civic_json']
got = get_github_api(civic_url, headers=request_headers)
# Verify that content has not been modified since last run
if got.status_code == 304:
logging.info(u'Unchanged civic.json at {}'.format(civic_url))
elif got.status_code not in range(400, 499):
logging.info(u'New civic.json at {}'.format(civic_url))
# Update the project's last_updated_civic_json field
project_dict['last_updated_civic_json'] = unicode(got.headers['ETag'])
try:
# get the contents of the file
civic = got.json()
except ValueError:
logging.error(u'Malformed civic.json at {}'.format(civic_url))
else:
logging.info(u'No civic.json at {}'.format(civic_url))
return civic
def count_people_totals(all_projects):
''' Create a list of people details based on project details.
Request additional data from Github API for each person.
See discussion at
https://github.com/codeforamerica/civic-json-worker/issues/18
'''
users, contributors = [], []
for project in all_projects:
contributors.extend(project['contributors'])
#
# Sort by login; there will be duplicates!
#
contributors.sort(key=itemgetter('login'))
#
# Populate users array with groups of contributors.
#