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 reported for "time label applied" measurement #283

Merged
merged 2 commits into from
May 19, 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
2 changes: 1 addition & 1 deletion labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_label_metrics(issue: github3.issues.Issue, labels: List[str]) -> dict:

for label in labels:
# if the label is still on there, add the time from the last event to now
if label in labeled and label not in unlabeled:
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(
Expand Down
21 changes: 17 additions & 4 deletions test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestLabels(unittest.TestCase):
def setUp(self):
self.issue = MagicMock() # type: ignore
self.issue.issue = MagicMock(spec=github3.issues.Issue) # type: ignore
self.issue.created_at = "2020-01-01T00:00:00Z"
self.issue.created_at = "2021-01-01T00:00:00Z"
self.issue.closed_at = "2021-01-05T00:00:00Z"
self.issue.state = "closed"
self.issue.issue.events.return_value = [
Expand All @@ -35,29 +35,42 @@ def setUp(self):
label={"name": "bug"},
created_at=datetime(2021, 1, 3, tzinfo=pytz.UTC),
),
MagicMock(
event="labeled",
label={"name": "bug"},
created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC),
),
]

def test_get_label_events(self):
"""Test get_label_events"""
labels = ["bug"]
events = get_label_events(self.issue, labels)
self.assertEqual(len(events), 2)
self.assertEqual(len(events), 3)
self.assertEqual(events[0].label["name"], "bug")
self.assertEqual(events[1].label["name"], "bug")
self.assertEqual(events[2].label["name"], "bug")

def test_get_label_metrics_closed_issue(self):
"""Test get_label_metrics using a closed issue"""
labels = ["bug", "feature"]
metrics = get_label_metrics(self.issue, labels)
self.assertEqual(metrics["bug"], timedelta(days=2))
self.assertEqual(metrics["bug"], timedelta(days=3))
self.assertEqual(metrics["feature"], timedelta(days=3))

def test_get_label_metrics_open_issue(self):
"""Test get_label_metrics using an open issue"""
self.issue.state = "open"
labels = ["bug", "feature"]
metrics = get_label_metrics(self.issue, labels)
self.assertEqual(metrics["bug"], timedelta(days=2))
self.assertLessEqual(
metrics["bug"],
datetime.now(pytz.utc) - datetime(2021, 1, 2, tzinfo=pytz.UTC),
)
self.assertGreater(
metrics["bug"],
datetime.now(pytz.utc) - datetime(2021, 1, 3, tzinfo=pytz.UTC),
)
self.assertLessEqual(
metrics["feature"],
datetime.now(pytz.utc) - datetime(2021, 1, 2, tzinfo=pytz.UTC),
Expand Down
Loading