@@ -43,6 +43,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
4343 """
4444 label_metrics : dict = {}
4545 label_events = get_label_events (issue , labels )
46+ label_last_event_type : dict = {}
4647
4748 for label in labels :
4849 label_metrics [label ] = None
@@ -55,6 +56,12 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
5556
5657 # Calculate the time to add or subtract to the time spent in label based on the label events
5758 for event in label_events :
59+ # Skip labeling events that have occured past issue close time
60+ if issue .closed_at is not None and (
61+ event .created_at >= datetime .fromisoformat (issue .closed_at )
62+ ):
63+ continue
64+
5865 if event .event == "labeled" :
5966 labeled [event .label ["name" ]] = True
6067 if event .label ["name" ] in labels :
@@ -63,6 +70,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
6370 label_metrics [
6471 event .label ["name" ]
6572 ] -= event .created_at - datetime .fromisoformat (issue .created_at )
73+ label_last_event_type [event .label ["name" ]] = "labeled"
6674 elif event .event == "unlabeled" :
6775 unlabeled [event .label ["name" ]] = True
6876 if event .label ["name" ] in labels :
@@ -71,16 +79,20 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:
7179 label_metrics [
7280 event .label ["name" ]
7381 ] += event .created_at - datetime .fromisoformat (issue .created_at )
82+ label_last_event_type [event .label ["name" ]] = "unlabeled"
7483
7584 for label in labels :
76- # if the label is still on there, add the time from the last event to now
7785 if label in labeled :
7886 # if the issue is closed, add the time from the issue creation to the closed_at time
7987 if issue .state == "closed" :
8088 label_metrics [label ] += datetime .fromisoformat (
8189 issue .closed_at
8290 ) - datetime .fromisoformat (issue .created_at )
8391 else :
92+ # skip label if last labeling event is 'unlabled' and issue is still open
93+ if label_last_event_type [label ] == "unlabeled" :
94+ continue
95+
8496 # if the issue is open, add the time from the issue creation to now
8597 label_metrics [label ] += datetime .now (pytz .utc ) - datetime .fromisoformat (
8698 issue .created_at
0 commit comments