diff --git a/labels.py b/labels.py index 9b4e4df..adbb333 100644 --- a/labels.py +++ b/labels.py @@ -43,6 +43,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: """ label_metrics: dict = {} label_events = get_label_events(issue, labels) + label_last_event_type: dict = {} for label in labels: label_metrics[label] = None @@ -55,6 +56,12 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: # Calculate the time to add or subtract to the time spent in label based on the label events for event in label_events: + # Skip labeling events that have occured past issue close time + if issue.closed_at is not None and ( + event.created_at >= datetime.fromisoformat(issue.closed_at) + ): + continue + if event.event == "labeled": labeled[event.label["name"]] = True if event.label["name"] in labels: @@ -63,6 +70,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: label_metrics[ event.label["name"] ] -= event.created_at - datetime.fromisoformat(issue.created_at) + label_last_event_type[event.label["name"]] = "labeled" elif event.event == "unlabeled": unlabeled[event.label["name"]] = True if event.label["name"] in labels: @@ -71,9 +79,9 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: label_metrics[ event.label["name"] ] += event.created_at - datetime.fromisoformat(issue.created_at) + label_last_event_type[event.label["name"]] = "unlabeled" for label in labels: - # if the label is still on there, add the time from the last event to now if label in labeled: # if the issue is closed, add the time from the issue creation to the closed_at time if issue.state == "closed": @@ -81,6 +89,10 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict: issue.closed_at ) - datetime.fromisoformat(issue.created_at) else: + # skip label if last labeling event is 'unlabled' and issue is still open + if label_last_event_type[label] == "unlabeled": + continue + # if the issue is open, add the time from the issue creation to now label_metrics[label] += datetime.now(pytz.utc) - datetime.fromisoformat( issue.created_at diff --git a/test_labels.py b/test_labels.py index d5b6d43..e7e8b5f 100644 --- a/test_labels.py +++ b/test_labels.py @@ -40,6 +40,12 @@ def setUp(self): label={"name": "bug"}, created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC), ), + # Label labeled after issue close date + MagicMock( + event="labeled", + label={"name": "foo"}, + created_at=datetime(2021, 1, 20, tzinfo=pytz.UTC), + ), ] def test_get_label_events(self): @@ -80,6 +86,13 @@ def test_get_label_metrics_open_issue(self): datetime.now(pytz.utc) - datetime(2021, 1, 4, tzinfo=pytz.UTC), ) + def test_get_label_metrics_closed_issue_labeled_past_closed_at(self): + """Test get_label_metrics using a closed issue that was labeled past issue closed_at""" + self.issue.state = "closed" + labels = ["foo"] + metrics = get_label_metrics(self.issue, labels) + self.assertEqual(metrics["foo"], None) + class TestGetAverageTimeInLabels(unittest.TestCase): """Unit tests for get_stats_time_in_labels"""