def test_get_incidents_pagination(self, mock_query_resp): """ Test getting a paginated response of incidents """ mock_query_resp.side_effect = [ { "incidents": [ { "id": "PT4KHLK", "type": "incident", "summary": "[#1234] The server is on fire.", "incident_number": 1234, "status": "resolved", "title": "The server is on fire.", "incident_key": "baf7cf21b1da41b4b0221008339ff357", "urgency": "high" } ], "limit": 25, "offset": 0, "total": None, "more": True }, { "incidents": [ { "id": "PT4KHLK", "type": "incident", "summary": "[#1234] The server is on fire.", "incident_number": 1234, "status": "resolved", "title": "The server is on fire.", "incident_key": "baf7cf21b1da41b4b0221008339ff357", "urgency": "high" } ], "limit": 25, "offset": 0, "total": None, "more": False } ] pyduty = PagerDuty('abc123') incidents = pyduty.get_incidents( 'ABCXYZ', since=dateutil.parser.parse('2019-01-01T06:42:09.668417+00:00'), until=dateutil.parser.parse('2019-01-01T06:52:09.668417+00:00') ) self.assertTrue(isinstance(incidents, types.GeneratorType)) # Skip the two pagination pages next(incidents) next(incidents) self.assertRaises(StopIteration, next, incidents)
def test_get_incidents(self, mock_query_resp): """ Test getting a single pagination of incidents """ mock_query_resp.return_value = { "incidents": [ { "id": "PT4KHLK", "type": "incident", "summary": "[#1234] The server is on fire.", "incident_number": 1234, "status": "resolved", "title": "The server is on fire.", "incident_key": "baf7cf21b1da41b4b0221008339ff357", "urgency": "high" } ], "limit": 25, "offset": 0, "total": None, "more": False } pyduty = PagerDuty('abc123') incidents = pyduty.get_incidents( 'ABCXYZ', since=dateutil.parser.parse('2019-01-01T06:42:09.668417+00:00'), until=dateutil.parser.parse('2019-01-01T06:52:09.668417+00:00') ) self.assertTrue(isinstance(incidents, types.GeneratorType)) next(incidents) mock_query_resp.assert_called_once_with( endpoint='incidents', method='GET', payload={ 'team_ids[]': 'ABCXYZ', 'time_zone': 'UTC', 'since': '2019-01-01T06:42:09.668417+00:00', 'until': '2019-01-01T06:52:09.668417+00:00', 'offset': 0 } )
def _populate_alerts(self, team_id, since, until): """ Populate team alerts :param team: PagerDuty Team ID :param since: :param until: :return: (bool) successful """ pyduty = PagerDuty(os.getenv('PAGERDUTY_KEY')) team = Team.objects.get(id=team_id) try: for incidents in pyduty.get_incidents( team_id=team.team_id, since=dateutil.parser.parse(since), until=dateutil.parser.parse(until)): for incident in incidents: _, created = Incidents.objects.get_or_create( title=incident.get('title', 'No title'), description=incident.get('description', 'No description'), summary=incident.get('summary', 'No summary'), status=incident.get('status', 'No status'), created_at=incident['created_at'], incident_id=incident['id'], urgency=incident['urgency'], team=team, ) if created: logger.info(f"{incident['id']} has been created") except RequestFailure as err: logger.error(f'Failed to query PagerDuty: {err}') return False team.save() return True