Skip to content

Commit

Permalink
Merge pull request #287 from smstone/label-metrics-measurements
Browse files Browse the repository at this point in the history
fix: Negative time duration for labels applied after issue close; Inflated metric numbers for unlabeled labels on open issues
  • Loading branch information
jmeridth authored May 21, 2024
2 parents 8136d81 + b8ab8a9 commit fbe4e7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -71,16 +79,20 @@ 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":
label_metrics[label] += datetime.fromisoformat(
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
Expand Down
13 changes: 13 additions & 0 deletions test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit fbe4e7c

Please sign in to comment.