github / issue-metrics

Gather metrics on issues/prs/discussions such as time to first response, count of issues opened, closed, etc.
https://github.blog/2023-07-19-metrics-for-issues-pull-requests-and-discussions/
MIT License
372 stars 46 forks source link

Negative time reported for "time label applied" measurement #282

Closed smstone closed 1 month ago

smstone commented 2 months ago

Describe the bug

Thank you for this repository.

When I ran issue-metrics v3 against a repository with several GitHub issues that utilize labels, I noticed a few of the "time label applied" measurements are reported as negative time, for example -1 day, 9:07:12. This should not be possible.

Negative time is reported for labels where they have moved from labeled, to unlabeled, to labeled on a given issue. This line skips labels that have been labeled but were at one point unlabeled causing this issue.

To Reproduce

Here is the Issue and respective Issue events for the given label where it displays negative time spent on label.

Issue data:

{
      "closed": True,
      "closedAt": "2024-01-26T09:26:04Z",
      "createdAt": "2024-01-25T16:41:33Z",
      "id": "1234",
      "labels": [
        {
          "id": "1234",
          "name": "waiting",
        }
      ],
      "number": 1234,
      "state": "CLOSED",
      "updatedAt": "2024-02-09T08:00:41Z"
    }

Issue label events data::

{
      "id": 1,
      "event": "labeled",
      "created_at": "2024-01-25T16:41:34Z",
      "label": {
        "name": "waiting",
      },
    },
    {
      "id": 2
      "event": "unlabeled",
      "created_at": "2024-01-25T18:06:47Z",
      "label": {
        "name": "waiting",
      },
    },
    {
      "id": 3
      "event": "labeled",
      "created_at": "2024-01-26T08:59:34Z",
      "label": {
        "name": "waiting",
      },
    }

This can also be reproduced in TestLabels if you add an additional labeled event for the label bug, it shows a negative number:

  MagicMock(
      event="labeled",
      label={"name": "bug"},
      created_at=datetime(2021, 1, 4, tzinfo=pytz.UTC),
  ),

Result:

_______________________________________________________________________________________________________________________________________ TestLabels.test_get_label_metrics_closed_issue _______________________________________________________________________________________________________________________________________

self = <test_labels.TestLabels testMethod=test_get_label_metrics_closed_issue>

    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))
E       AssertionError: datetime.timedelta(days=-367) != datetime.timedelta(days=2)

test_labels.py:57: AssertionError
________________________________________________________________________________________________________________________________________ TestLabels.test_get_label_metrics_open_issue ________________________________________________________________________________________________________________________________________

self = <test_labels.TestLabels testMethod=test_get_label_metrics_open_issue>

    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))
E       AssertionError: datetime.timedelta(days=-367) != datetime.timedelta(days=2)

Expected behavior

A "time label applied" measurement should not be reported as a negative time duration.

Screenshots

No response

Additional context

No response