diff --git a/CHANGELOG b/CHANGELOG index c59ae487..776f02a1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ # Change Log All notable changes to this project will be documented in this file. +## [3.6.12] - 2022-05-04 +# Fixed +- Fix Key error when retrieve html_url for checksuites ## [3.6.11] - 2022-04-28 # Fixed diff --git a/bert_e/git_host/github/__init__.py b/bert_e/git_host/github/__init__.py index 4c38dd3f..3a2d687c 100644 --- a/bert_e/git_host/github/__init__.py +++ b/bert_e/git_host/github/__init__.py @@ -544,20 +544,20 @@ def __init__(self, *args, **kwargs): @property def url(self): + self.remove_unwanted_workflows() + def sort_f(check_suite): date = check_suite.get('created_at') - if date: - return datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ') - return datetime(year=1962, month=1, day=1) + return date if date else datetime(year=1962, month=1, day=1) - self.data['check_suites'].sort(key=sort_f, reverse=True) + self._check_suites.sort(key=sort_f, reverse=True) - failed_c = [elem for elem in self.data['check_suites'] + failed_c = [elem for elem in self._check_suites if elem['conclusion'] != 'success'] if len(failed_c): return failed_c[0]['html_url'] - elif len(failed_c) and len(self.data['check_suites']): - return self.data['check_suites'][0]['html_url'] + elif len(failed_c) and len(self._check_suites): + return self._check_suites[0]['html_url'] return '' def is_pending(self): @@ -566,8 +566,10 @@ def is_pending(self): ]) > 0 def _get_check_suite_ids(self, workflow_runs): - return list( - map(lambda elem: elem['check_suite_id'], workflow_runs) + return dict(map(lambda elem: ( + elem['check_suite_id'], + {'html_url': elem['html_url'], 'event': elem['event']} + ), workflow_runs) ) def _get_aggregate_workflow_dispatched(self, page): @@ -579,9 +581,9 @@ def _get_aggregate_workflow_dispatched(self, page): client=self.client, owner=owner, repo=repo, ref=ref, params={ - 'event': 'workflow_dispatch', 'branch': self._check_suites[0]['head_branch'], - 'page': page + 'page': page, + 'per_page': 100 }) def remove_unwanted_workflows(self): @@ -597,13 +599,20 @@ def remove_unwanted_workflows(self): response = self._get_aggregate_workflow_dispatched(page) dispatched = self._get_check_suite_ids(response.workflow_runs) - while len(dispatched) < response.total_count: + while len(dispatched.keys()) < response.total_count: page += 1 response = self._get_aggregate_workflow_dispatched(page) - dispatched += self._get_check_suite_ids(response.workflow_runs) + dispatched.update( + self._get_check_suite_ids(response.workflow_runs) + ) + + workflow_dispatches = [ + key for key, value in dispatched.items() + if value['event'] == 'workflow_dispatch' + ] self._check_suites = list(filter( - lambda elem: elem['id'] not in dispatched, + lambda elem: elem['id'] not in workflow_dispatches, self._check_suites )) @@ -612,6 +621,12 @@ def remove_unwanted_workflows(self): self._check_suites )) + self._check_suites = list(map(lambda elem: { + **elem, + 'html_url': + dispatched.get(elem['id'], {'html_url': ''})['html_url'] + }, self._check_suites)) + @property def state(self): self.remove_unwanted_workflows() diff --git a/bert_e/git_host/github/schema.py b/bert_e/git_host/github/schema.py index 152da789..e2bf403f 100644 --- a/bert_e/git_host/github/schema.py +++ b/bert_e/git_host/github/schema.py @@ -117,6 +117,8 @@ class WorkflowRun(Schema): head_branch = fields.Str() status = fields.Str() check_suite_id = fields.Integer() + html_url = fields.Str() + event = fields.Str() class AggregateWorkflowRuns(Schema):