-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathstreams.py
1702 lines (1382 loc) · 74.7 KB
/
streams.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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import re
from abc import ABC, abstractmethod
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, Union
from urllib import parse
import pendulum
import requests
from airbyte_cdk import BackoffStrategy, StreamSlice
from airbyte_cdk.models import AirbyteLogMessage, AirbyteMessage, Level, SyncMode
from airbyte_cdk.models import Type as MessageType
from airbyte_cdk.sources.streams.availability_strategy import AvailabilityStrategy
from airbyte_cdk.sources.streams.checkpoint.substream_resumable_full_refresh_cursor import SubstreamResumableFullRefreshCursor
from airbyte_cdk.sources.streams.core import CheckpointMixin, Stream
from airbyte_cdk.sources.streams.http import HttpStream
from airbyte_cdk.sources.streams.http.error_handlers import ErrorHandler, ErrorResolution, HttpStatusErrorHandler, ResponseAction
from airbyte_cdk.sources.streams.http.exceptions import DefaultBackoffException, UserDefinedBackoffException
from airbyte_cdk.utils import AirbyteTracedException
from airbyte_protocol.models import FailureType
from . import constants
from .backoff_strategies import ContributorActivityBackoffStrategy, GithubStreamABCBackoffStrategy
from .errors_handlers import (
GITHUB_DEFAULT_ERROR_MAPPING,
ContributorActivityErrorHandler,
GitHubGraphQLErrorHandler,
GithubStreamABCErrorHandler,
)
from .graphql import (
CursorStorage,
QueryReactions,
get_query_issue_reactions,
get_query_projectsV2,
get_query_pull_requests,
get_query_reviews,
)
from .utils import GitHubAPILimitException, getter
class GithubStreamABC(HttpStream, ABC):
primary_key = "id"
# Detect streams with high API load
large_stream = False
max_retries: int = 5
stream_base_params = {}
def __init__(self, api_url: str = "https://api.github.com", access_token_type: str = "", **kwargs):
if kwargs.get("authenticator"):
kwargs["authenticator"].max_time = kwargs.pop("max_waiting_time", self.max_time)
super().__init__(**kwargs)
self.access_token_type = access_token_type
self.api_url = api_url
self.state = {}
if not self.supports_incremental:
self.cursor = SubstreamResumableFullRefreshCursor()
@property
def url_base(self) -> str:
return self.api_url
@property
def availability_strategy(self) -> Optional["AvailabilityStrategy"]:
return None
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
links = response.links
if "next" in links:
next_link = links["next"]["url"]
parsed_link = parse.urlparse(next_link)
page = dict(parse.parse_qsl(parsed_link.query)).get("page")
return {"page": page}
def request_params(
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None
) -> MutableMapping[str, Any]:
params = {"per_page": self.page_size}
if next_page_token:
params.update(next_page_token)
params.update(self.stream_base_params)
return params
def request_headers(self, **kwargs) -> Mapping[str, Any]:
# Without sending `User-Agent` header we will be getting `403 Client Error: Forbidden for url` error.
return {"User-Agent": "PostmanRuntime/7.28.0"}
def parse_response(
self,
response: requests.Response,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> Iterable[Mapping]:
for record in response.json(): # GitHub puts records in an array.
yield self.transform(record=record, stream_slice=stream_slice)
def get_error_handler(self) -> Optional[ErrorHandler]:
return GithubStreamABCErrorHandler(
logger=self.logger, max_retries=self.max_retries, error_mapping=GITHUB_DEFAULT_ERROR_MAPPING, stream=self
)
def get_backoff_strategy(self) -> Optional[Union[BackoffStrategy, List[BackoffStrategy]]]:
return GithubStreamABCBackoffStrategy(stream=self)
@staticmethod
def check_graphql_rate_limited(response_json: dict) -> bool:
errors = response_json.get("errors")
if errors:
for error in errors:
if error.get("type") == "RATE_LIMITED":
return True
return False
def read_records(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping[str, Any]]:
# get out the stream_slice parts for later use.
organisation = stream_slice.get("organization", "")
repository = stream_slice.get("repository", "")
# Reading records while handling the errors
try:
yield from super().read_records(stream_slice=stream_slice, **kwargs)
except DefaultBackoffException as e:
# This whole try/except situation in `read_records()` isn't good but right now in `self._send_request()`
# function we have `response.raise_for_status()` so we don't have much choice on how to handle errors.
# Bocked on https://github.com/airbytehq/airbyte/issues/3514.
if e.response.status_code == requests.codes.NOT_FOUND:
# A lot of streams are not available for repositories owned by a user instead of an organization.
if isinstance(self, Organizations):
error_msg = f"Syncing `{self.__class__.__name__}` stream isn't available for organization `{organisation}`."
elif isinstance(self, TeamMemberships):
error_msg = f"Syncing `{self.__class__.__name__}` stream for organization `{organisation}`, team `{stream_slice.get('team_slug')}` and user `{stream_slice.get('username')}` isn't available: User has no team membership. Skipping..."
else:
error_msg = f"Syncing `{self.__class__.__name__}` stream isn't available for repository `{repository}`."
elif e.response.status_code == requests.codes.FORBIDDEN:
error_msg = str(e.response.json().get("message"))
# When using the `check_connection` method, we should raise an error if we do not have access to the repository.
if isinstance(self, Repositories):
raise e
# When `403` for the stream, that has no access to the organization's teams, based on OAuth Apps Restrictions:
# https://docs.github.com/en/organizations/restricting-access-to-your-organizations-data/enabling-oauth-app-access-restrictions-for-your-organization
# For all `Organisation` based streams
elif isinstance(self, Organizations) or isinstance(self, Teams) or isinstance(self, Users):
error_msg = (
f"Syncing `{self.name}` stream isn't available for organization `{organisation}`. Full error message: {error_msg}"
)
# For all other `Repository` base streams
else:
error_msg = (
f"Syncing `{self.name}` stream isn't available for repository `{repository}`. Full error message: {error_msg}"
)
elif e.response.status_code == requests.codes.UNAUTHORIZED:
if self.access_token_type == constants.PERSONAL_ACCESS_TOKEN_TITLE:
error_msg = str(e.response.json().get("message"))
self.logger.error(f"{self.access_token_type} renewal is required: {error_msg}")
raise e
elif e.response.status_code == requests.codes.GONE and isinstance(self, Projects):
# Some repos don't have projects enabled and we we get "410 Client Error: Gone for
# url: https://api.github.com/repos/xyz/projects?per_page=100" error.
error_msg = f"Syncing `Projects` stream isn't available for repository `{stream_slice['repository']}`."
elif e.response.status_code == requests.codes.CONFLICT:
error_msg = (
f"Syncing `{self.name}` stream isn't available for repository "
f"`{stream_slice['repository']}`, it seems like this repository is empty."
)
elif e.response.status_code == requests.codes.SERVER_ERROR and isinstance(self, WorkflowRuns):
error_msg = f"Syncing `{self.name}` stream isn't available for repository `{stream_slice['repository']}`."
elif e.response.status_code == requests.codes.BAD_GATEWAY:
error_msg = f"Stream {self.name} temporary failed. Try to re-run sync later"
else:
# most probably here we're facing a 500 server error and a risk to get a non-json response, so lets output response.text
self.logger.error(f"Undefined error while reading records: {e.response.text}")
raise e
self.logger.warning(error_msg)
except GitHubAPILimitException as e:
internal_message = (
f"Stream: `{self.name}`, slice: `{stream_slice}`. Limits for all provided tokens are reached, please try again later"
)
message = "Rate Limits for all provided tokens are reached. For more information please refer to documentation: https://docs.airbyte.com/integrations/sources/github#limitations--troubleshooting"
raise AirbyteTracedException(internal_message=internal_message, message=message, failure_type=FailureType.config_error) from e
class GithubStream(GithubStreamABC):
def __init__(self, repositories: List[str], page_size_for_large_streams: int, **kwargs):
super().__init__(**kwargs)
self.repositories = repositories
# GitHub pagination could be from 1 to 100.
# This parameter is deprecated and in future will be used sane default, page_size: 10
self.page_size = page_size_for_large_streams if self.large_stream else constants.DEFAULT_PAGE_SIZE
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/{self.name}"
def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
for repository in self.repositories:
yield {"repository": repository}
def get_error_display_message(self, exception: BaseException) -> Optional[str]:
if (
isinstance(exception, DefaultBackoffException)
and exception.response.status_code == requests.codes.BAD_GATEWAY
and self.large_stream
and self.page_size > 1
):
return f'Please try to decrease the "Page size for large streams" below {self.page_size}. The stream "{self.name}" is a large stream, such streams can fail with 502 for high "page_size" values.'
return super().get_error_display_message(exception)
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record["repository"] = stream_slice["repository"]
return record
class SemiIncrementalMixin(CheckpointMixin):
"""
Semi incremental streams are also incremental but with one difference, they:
- read all records;
- output only new records.
This means that semi incremental streams read all records (like full_refresh streams) but do filtering directly
in the code and output only latest records (like incremental streams).
"""
cursor_field = "updated_at"
# This flag is used to indicate that current stream supports `sort` and `direction` request parameters and that
# we should break processing records if possible. If `sort` is set to `updated` and `direction` is set to `desc`
# this means that latest records will be at the beginning of the response and after we processed those latest
# records we can just stop and not process other record. This will increase speed of each incremental stream
# which supports those 2 request parameters. Currently only `IssueMilestones` and `PullRequests` streams are
# supporting this.
is_sorted = False
def __init__(self, start_date: str = "", **kwargs):
super().__init__(**kwargs)
self._start_date = start_date
self._starting_point_cache = {}
@property
def state(self) -> MutableMapping[str, Any]:
return self._state
@state.setter
def state(self, value: MutableMapping[str, Any]):
self._state = value
@property
def slice_keys(self):
if hasattr(self, "repositories"):
return ["repository"]
return ["organization"]
record_slice_key = slice_keys
def convert_cursor_value(self, value):
return value
@property
def state_checkpoint_interval(self) -> Optional[int]:
if self.is_sorted == "asc":
return self.page_size
def _get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]):
"""
Return the latest state by comparing the cursor value in the latest record with the stream's most recent state
object and returning an updated state object.
"""
slice_value = getter(latest_record, self.record_slice_key)
updated_state = self.convert_cursor_value(latest_record[self.cursor_field])
stream_state_value = current_stream_state.get(slice_value, {}).get(self.cursor_field)
if stream_state_value:
updated_state = max(updated_state, stream_state_value)
current_stream_state.setdefault(slice_value, {})[self.cursor_field] = updated_state
return current_stream_state
def _get_starting_point(self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any]) -> str:
if stream_state:
state_path = [stream_slice[k] for k in self.slice_keys] + [self.cursor_field]
stream_state_value = getter(stream_state, state_path, strict=False)
if stream_state_value:
if self._start_date:
return max(self._start_date, stream_state_value)
return stream_state_value
return self._start_date
def get_starting_point(self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any]) -> str:
cache_key = tuple([stream_slice[k] for k in self.slice_keys])
if cache_key not in self._starting_point_cache:
self._starting_point_cache[cache_key] = self._get_starting_point(stream_state, stream_slice)
return self._starting_point_cache[cache_key]
def read_records(
self,
sync_mode: SyncMode,
cursor_field: List[str] = None,
stream_slice: Mapping[str, Any] = None,
stream_state: Mapping[str, Any] = None,
) -> Iterable[Mapping[str, Any]]:
start_point = self.get_starting_point(stream_state=stream_state, stream_slice=stream_slice)
for record in super().read_records(
sync_mode=sync_mode, cursor_field=cursor_field, stream_slice=stream_slice, stream_state=stream_state
):
cursor_value = self.convert_cursor_value(record[self.cursor_field])
if not start_point or cursor_value > start_point:
yield record
self.state = self._get_updated_state(self.state, record)
elif self.is_sorted == "desc" and cursor_value < start_point:
break
def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
self._starting_point_cache.clear()
yield from super().stream_slices(**kwargs)
class IncrementalMixin(SemiIncrementalMixin):
def request_params(self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, **kwargs) -> MutableMapping[str, Any]:
params = super().request_params(stream_state=stream_state, **kwargs)
since_params = self.get_starting_point(stream_state=stream_state, stream_slice=stream_slice)
if since_params:
params["since"] = since_params
return params
# Below are full refresh streams
class RepositoryStats(GithubStream):
"""
This stream is technical and not intended for the user, we use it for checking connection with the repository.
API docs: https://docs.github.com/en/rest/reference/repos#get-a-repository
"""
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}"
def parse_response(self, response: requests.Response, stream_slice: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping]:
yield response.json()
class Assignees(GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/assignees?apiVersion=2022-11-28#list-assignees
"""
class Branches(GithubStream):
"""
API docs: https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#list-branches
"""
primary_key = ["repository", "name"]
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/branches"
class Collaborators(GithubStream):
"""
API docs: https://docs.github.com/en/rest/collaborators/collaborators?apiVersion=2022-11-28#list-repository-collaborators
"""
class IssueLabels(GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#list-labels-for-a-repository
"""
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/labels"
class Organizations(GithubStreamABC):
"""
API docs: https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-organizations
"""
# GitHub pagination could be from 1 to 100.
page_size = 100
def __init__(self, organizations: List[str], access_token_type: str = "", **kwargs):
super().__init__(**kwargs)
self.organizations = organizations
self.access_token_type = access_token_type
def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
for organization in self.organizations:
yield {"organization": organization}
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"orgs/{stream_slice['organization']}"
def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
yield response.json()
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record["organization"] = stream_slice["organization"]
return record
class Repositories(SemiIncrementalMixin, Organizations):
"""
API docs: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-organization-repositories
"""
is_sorted = "desc"
stream_base_params = {
"sort": "updated",
"direction": "desc",
}
def __init__(self, *args, pattern: Optional[str] = None, **kwargs):
self._pattern = re.compile(pattern) if pattern else pattern
super().__init__(*args, **kwargs)
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"orgs/{stream_slice['organization']}/repos"
def parse_response(self, response: requests.Response, stream_slice: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping]:
for record in response.json(): # GitHub puts records in an array.
record = self.transform(record=record, stream_slice=stream_slice)
if not self._pattern or self._pattern.match(record["full_name"]):
yield record
class Tags(GithubStream):
"""
API docs: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#list-repository-tags
"""
primary_key = ["repository", "name"]
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/tags"
class Teams(Organizations):
"""
API docs: https://docs.github.com/en/rest/teams/teams?apiVersion=2022-11-28#list-teams
"""
use_cache = True
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"orgs/{stream_slice['organization']}/teams"
def parse_response(self, response: requests.Response, stream_slice: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping]:
for record in response.json():
yield self.transform(record=record, stream_slice=stream_slice)
class Users(Organizations):
"""
API docs: https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#list-organization-members
"""
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"orgs/{stream_slice['organization']}/members"
def parse_response(self, response: requests.Response, stream_slice: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping]:
for record in response.json():
yield self.transform(record=record, stream_slice=stream_slice)
# Below are semi incremental streams
class Releases(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#list-releases
"""
cursor_field = "created_at"
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record = super().transform(record=record, stream_slice=stream_slice)
assets = record.get("assets", [])
for asset in assets:
uploader = asset.pop("uploader", None)
asset["uploader_id"] = uploader.get("id") if uploader else None
return record
class Events(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/activity/events?apiVersion=2022-11-28#list-repository-events
"""
cursor_field = "created_at"
class PullRequests(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests
"""
use_cache = True
large_stream = True
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._first_read = True
def read_records(self, stream_state: Mapping[str, Any] = None, **kwargs) -> Iterable[Mapping[str, Any]]:
"""
Decide if this a first read or not by the presence of the state object
"""
self._first_read = not bool(stream_state)
yield from super().read_records(stream_state=stream_state, **kwargs)
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/pulls"
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record = super().transform(record=record, stream_slice=stream_slice)
for nested in ("head", "base"):
entry = record.get(nested, {})
entry["repo_id"] = (record.get("head", {}).pop("repo", {}) or {}).get("id")
return record
def request_params(self, **kwargs) -> MutableMapping[str, Any]:
base_params = super().request_params(**kwargs)
# The very first time we read this stream we want to read ascending so we can save state in case of
# a halfway failure. But if there is state, we read descending to allow incremental behavior.
params = {"state": "all", "sort": "updated", "direction": self.is_sorted}
return {**base_params, **params}
@property
def is_sorted(self) -> str:
"""
Depending if there any state we read stream in ascending or descending order.
"""
if self._first_read:
return "asc"
return "desc"
class CommitComments(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/commits/comments?apiVersion=2022-11-28#list-commit-comments-for-a-repository
"""
use_cache = True
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/comments"
class IssueMilestones(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/milestones?apiVersion=2022-11-28#list-milestones
"""
is_sorted = "desc"
stream_base_params = {
"state": "all",
"sort": "updated",
"direction": "desc",
}
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/milestones"
class Stargazers(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/activity/starring?apiVersion=2022-11-28#list-stargazers
"""
primary_key = "user_id"
cursor_field = "starred_at"
def request_headers(self, **kwargs) -> Mapping[str, Any]:
base_headers = super().request_headers(**kwargs)
# We need to send below header if we want to get `starred_at` field. See docs (Alternative response with
# star creation timestamps) - https://docs.github.com/en/rest/reference/activity#list-stargazers.
headers = {"Accept": "application/vnd.github.v3.star+json"}
return {**base_headers, **headers}
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
"""
We need to provide the "user_id" for the primary_key attribute
and don't remove the whole "user" block from the record.
"""
record = super().transform(record=record, stream_slice=stream_slice)
record["user_id"] = record.get("user").get("id")
return record
class Projects(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/projects/projects?apiVersion=2022-11-28#list-repository-projects
"""
use_cache = True
stream_base_params = {
"state": "all",
}
def request_headers(self, **kwargs) -> Mapping[str, Any]:
base_headers = super().request_headers(**kwargs)
# Projects stream requires sending following `Accept` header. If we won't sent it
# we'll get `415 Client Error: Unsupported Media Type` error.
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
return {**base_headers, **headers}
class IssueEvents(SemiIncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/events?apiVersion=2022-11-28#list-issue-events-for-a-repository
"""
cursor_field = "created_at"
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/issues/events"
# Below are incremental streams
class Comments(IncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#list-issue-comments-for-a-repository
"""
use_cache = True
large_stream = True
max_retries = 7
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/issues/comments"
class Commits(IncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-commits
Pull commits from each branch of each repository, tracking state for each branch
"""
primary_key = "sha"
cursor_field = "created_at"
slice_keys = ["repository", "branch"]
def __init__(self, branches_to_pull: List[str], **kwargs):
super().__init__(**kwargs)
kwargs.pop("start_date")
self.branches_to_repos = {}
self.branches_to_pull = set(branches_to_pull)
self.branches_stream = Branches(**kwargs)
self.repositories_stream = RepositoryStats(**kwargs)
def request_params(self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, **kwargs) -> MutableMapping[str, Any]:
params = super(IncrementalMixin, self).request_params(stream_state=stream_state, stream_slice=stream_slice, **kwargs)
since = self.get_starting_point(stream_state=stream_state, stream_slice=stream_slice)
if since:
params["since"] = since
params["sha"] = stream_slice["branch"]
return params
def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
self._validate_branches_to_pull()
for stream_slice in super().stream_slices(**kwargs):
repository = stream_slice["repository"]
for branch in self.branches_to_repos.get(repository, []):
yield {"branch": branch, "repository": repository}
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record = super().transform(record=record, stream_slice=stream_slice)
# Record of the `commits` stream doesn't have an updated_at/created_at field at the top level (so we could
# just write `record["updated_at"]` or `record["created_at"]`). Instead each record has such value in
# `commit.author.date`. So the easiest way is to just enrich the record returned from API with top level
# field `created_at` and use it as cursor_field.
# Include the branch in the record
record["created_at"] = record["commit"]["author"]["date"]
record["branch"] = stream_slice["branch"]
return record
def _get_updated_state(self, current_stream_state: MutableMapping[str, Any], latest_record: Mapping[str, Any]):
repository = latest_record["repository"]
branch = latest_record["branch"]
updated_state = latest_record[self.cursor_field]
stream_state_value = current_stream_state.get(repository, {}).get(branch, {}).get(self.cursor_field)
if stream_state_value:
updated_state = max(updated_state, stream_state_value)
current_stream_state.setdefault(repository, {}).setdefault(branch, {})[self.cursor_field] = updated_state
return current_stream_state
def _validate_branches_to_pull(self):
# Get the default branch for each repository
default_branches = {}
for stream_slice in self.repositories_stream.stream_slices(sync_mode=SyncMode.full_refresh):
for repo_stats in self.repositories_stream.read_records(stream_slice=stream_slice, sync_mode=SyncMode.full_refresh):
default_branches[repo_stats["full_name"]] = repo_stats["default_branch"]
all_branches = []
for stream_slice in self.branches_stream.stream_slices(sync_mode=SyncMode.full_refresh):
for branch in self.branches_stream.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream_slice):
all_branches.append(f"{branch['repository']}/{branch['name']}")
# Create mapping of repository to list of branches to pull commits for
# If no branches are specified for a repo, use its default branch
for repo in self.repositories:
repo_branches = []
for branch in self.branches_to_pull:
branch_parts = branch.split("/", 2)
if "/".join(branch_parts[:2]) == repo and branch in all_branches:
repo_branches.append(branch_parts[-1])
if not repo_branches:
repo_branches = [default_branches[repo]]
self.branches_to_repos[repo] = repo_branches
class Issues(IncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/issues/issues?apiVersion=2022-11-28#list-repository-issues
"""
use_cache = True
large_stream = True
is_sorted = "asc"
stream_base_params = {
"state": "all",
"sort": "updated",
"direction": "asc",
}
class ReviewComments(IncrementalMixin, GithubStream):
"""
API docs: https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#list-review-comments-in-a-repository
"""
use_cache = True
large_stream = True
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/pulls/comments"
class GitHubGraphQLStream(GithubStream, ABC):
http_method = "POST"
def path(
self, *, stream_state: Mapping[str, Any] = None, stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None
) -> str:
return "graphql"
def get_error_handler(self) -> Optional[ErrorHandler]:
return GitHubGraphQLErrorHandler(
logger=self.logger, max_retries=self.max_retries, error_mapping=GITHUB_DEFAULT_ERROR_MAPPING, stream=self
)
def _get_repository_name(self, repository: Mapping[str, Any]) -> str:
return repository["owner"]["login"] + "/" + repository["name"]
def request_params(
self, stream_state: Mapping[str, Any], stream_slice: Mapping[str, Any] = None, next_page_token: Mapping[str, Any] = None
) -> MutableMapping[str, Any]:
return {}
class PullRequestStats(SemiIncrementalMixin, GitHubGraphQLStream):
"""
API docs: https://docs.github.com/en/graphql/reference/objects#pullrequest
"""
large_stream = True
is_sorted = "asc"
def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
repository = response.json()["data"]["repository"]
if repository:
nodes = repository["pullRequests"]["nodes"]
for record in nodes:
record["review_comments"] = sum([node["comments"]["totalCount"] for node in record["review_comments"]["nodes"]])
record["comments"] = record["comments"]["totalCount"]
record["commits"] = record["commits"]["totalCount"]
record["repository"] = self._get_repository_name(repository)
if record["merged_by"]:
record["merged_by"]["type"] = record["merged_by"].pop("__typename")
yield record
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
repository = response.json()["data"]["repository"]
if repository:
pageInfo = repository["pullRequests"]["pageInfo"]
if pageInfo["hasNextPage"]:
return {"after": pageInfo["endCursor"]}
def request_body_json(
self,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> Optional[Mapping]:
organization, name = stream_slice["repository"].split("/")
if next_page_token:
next_page_token = next_page_token["after"]
query = get_query_pull_requests(
owner=organization, name=name, first=self.page_size, after=next_page_token, direction=self.is_sorted.upper()
)
return {"query": query}
def request_headers(self, **kwargs) -> Mapping[str, Any]:
base_headers = super().request_headers(**kwargs)
# https://docs.github.com/en/graphql/overview/schema-previews#merge-info-preview
headers = {"Accept": "application/vnd.github.merge-info-preview+json"}
return {**base_headers, **headers}
class Reviews(SemiIncrementalMixin, GitHubGraphQLStream):
"""
API docs: https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#list-reviews-for-a-pull-request
"""
is_sorted = False
cursor_field = "updated_at"
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.pull_requests_cursor = {}
self.reviews_cursors = {}
def _get_records(self, pull_request, repository_name):
"yield review records from pull_request"
for record in pull_request["reviews"]["nodes"]:
record["repository"] = repository_name
record["pull_request_url"] = pull_request["url"]
if record["commit"]:
record["commit_id"] = record.pop("commit")["oid"]
if record["user"]:
record["user"]["type"] = record["user"].pop("__typename")
# for backward compatibility with REST API response
record["_links"] = {
"html": {"href": record["html_url"]},
"pull_request": {"href": record["pull_request_url"]},
}
yield record
def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
repository = response.json()["data"]["repository"]
if repository:
repository_name = self._get_repository_name(repository)
if "pullRequests" in repository:
for pull_request in repository["pullRequests"]["nodes"]:
yield from self._get_records(pull_request, repository_name)
elif "pullRequest" in repository:
yield from self._get_records(repository["pullRequest"], repository_name)
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
repository = response.json()["data"]["repository"]
if repository:
repository_name = self._get_repository_name(repository)
reviews_cursors = self.reviews_cursors.setdefault(repository_name, {})
if "pullRequests" in repository:
if repository["pullRequests"]["pageInfo"]["hasNextPage"]:
self.pull_requests_cursor[repository_name] = repository["pullRequests"]["pageInfo"]["endCursor"]
for pull_request in repository["pullRequests"]["nodes"]:
if pull_request["reviews"]["pageInfo"]["hasNextPage"]:
pull_request_number = pull_request["number"]
reviews_cursors[pull_request_number] = pull_request["reviews"]["pageInfo"]["endCursor"]
elif "pullRequest" in repository:
if repository["pullRequest"]["reviews"]["pageInfo"]["hasNextPage"]:
pull_request_number = repository["pullRequest"]["number"]
reviews_cursors[pull_request_number] = repository["pullRequest"]["reviews"]["pageInfo"]["endCursor"]
if reviews_cursors:
number, after = reviews_cursors.popitem()
return {"after": after, "number": number}
if repository_name in self.pull_requests_cursor:
return {"after": self.pull_requests_cursor.pop(repository_name)}
def request_body_json(
self,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> Optional[Mapping]:
organization, name = stream_slice["repository"].split("/")
if not next_page_token:
next_page_token = {"after": None}
query = get_query_reviews(owner=organization, name=name, first=self.page_size, **next_page_token)
return {"query": query}
class PullRequestCommits(GithubStream):
"""
API docs: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-commits-on-a-pull-request
"""
primary_key = "sha"
def __init__(self, parent: HttpStream, **kwargs):
super().__init__(**kwargs)
self.parent = parent
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
return f"repos/{stream_slice['repository']}/pulls/{stream_slice['pull_number']}/commits"
def stream_slices(
self, sync_mode: SyncMode, cursor_field: List[str] = None, stream_state: Mapping[str, Any] = None
) -> Iterable[Optional[Mapping[str, Any]]]:
parent_stream_slices = self.parent.stream_slices(
sync_mode=SyncMode.full_refresh, cursor_field=cursor_field, stream_state=stream_state
)
for stream_slice in parent_stream_slices:
parent_records = self.parent.read_records(
sync_mode=SyncMode.full_refresh, cursor_field=cursor_field, stream_slice=stream_slice, stream_state=stream_state
)
for record in parent_records:
yield {"repository": record["repository"], "pull_number": record["number"]}
def transform(self, record: MutableMapping[str, Any], stream_slice: Mapping[str, Any]) -> MutableMapping[str, Any]:
record = super().transform(record=record, stream_slice=stream_slice)
record["pull_number"] = stream_slice["pull_number"]
return record
class ProjectsV2(SemiIncrementalMixin, GitHubGraphQLStream):
"""
API docs: https://docs.github.com/en/graphql/reference/objects#projectv2
"""
is_sorted = "asc"
def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapping]:
repository = response.json()["data"]["repository"]
if repository:
nodes = repository["projectsV2"]["nodes"]
for record in nodes:
record["owner_id"] = record.pop("owner").get("id")
record["repository"] = self._get_repository_name(repository)
yield record
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
repository = response.json()["data"]["repository"]
if repository:
page_info = repository["projectsV2"]["pageInfo"]
if page_info["hasNextPage"]:
return {"after": page_info["endCursor"]}
def request_body_json(
self,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> Optional[Mapping]:
organization, name = stream_slice["repository"].split("/")
if next_page_token:
next_page_token = next_page_token["after"]
query = get_query_projectsV2(
owner=organization, name=name, first=self.page_size, after=next_page_token, direction=self.is_sorted.upper()
)
return {"query": query}
# Reactions streams
class ReactionStream(GithubStream, CheckpointMixin, ABC):
parent_key = "id"
copy_parent_key = "comment_id"
cursor_field = "created_at"
def __init__(self, start_date: str = "", **kwargs):
super().__init__(**kwargs)
kwargs["start_date"] = start_date
self._parent_stream = self.parent_entity(**kwargs)
self._start_date = start_date
@property
@abstractmethod
def parent_entity(self):
"""
Specify the class of the parent stream for which receive reactions
"""
@property
def state(self) -> MutableMapping[str, Any]:
return self._state
@state.setter
def state(self, value: MutableMapping[str, Any]):
self._state = value