Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Negative time duration for labels applied after issue close; Inflated metric numbers for unlabeled labels on open issues #287

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading