Skip to content

Commit

Permalink
Merge pull request #283 from smstone/negative-time-spent-in-issue
Browse files Browse the repository at this point in the history
fix: Negative time reported for "time label applied" measurement
  • Loading branch information
jmeridth authored May 19, 2024
2 parents e73a4bb + 6623f77 commit 7b1aa4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
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

0 comments on commit 7b1aa4c

Please sign in to comment.