Skip to content
Open
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
26 changes: 26 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Python unit tests

on: # yamllint disable-line rule:truthy
pull_request:
push:
branches:
- dev
- main

permissions:
contents: read

jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Install test dependencies
run: python -m pip install "pandas~=2.3.0"
- name: Run unit tests
run: python -m unittest discover -s tests -p '*_test.py' -v
env:
PYTHONPATH: 8Knot
51 changes: 1 addition & 50 deletions 8Knot/pages/contributions/visualizations/pr_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pages.utils.graph_utils import get_graph_time_values, baby_blue
from queries.pr_assignee_query import pr_assignee_query as praq
from pages.utils.job_utils import nodata_graph, POLL_INTERVAL_MS
from pages.utils.pr_assignment_utils import pr_assignment
import time
import app
import cache_manager.cache_facade as cf
Expand Down Expand Up @@ -206,53 +207,3 @@ def create_figure(df: pd.DataFrame, interval):
)

return fig


def pr_assignment(df, start_date, end_date):
"""
This function takes a start and a end date and determines how many
prs in that time interval are assigned and unassigned.

Args:
-----
df : Pandas Dataframe
Dataframe with pr assignment actions of the assignees

start_date : Datetime Timestamp
Timestamp of the start time of the time interval

end_date : Datetime Timestamp
Timestamp of the end time of the time interval

Returns:
--------
int, int: Number of assigned and unassigned prs in the time window
"""

# drop rows that are more recent than the end date
df_created = df[df["created_at"] <= end_date]

# Keep prs that were either still open after the 'start_date' or that have not been closed.
df_in_range = df_created[(df_created["closed_at"] > start_date) | (df_created["closed_at"].isnull())]

# number of prs open in time interval
num_prs_open = df_in_range["pull_request_id"].nunique()

# get all pr review unassignments and drop rows that have been unassigned more recent than the end date
num_unassigned_actions = df_in_range[
(df_in_range["assignment_action"] == "unassigned") & (df_in_range["assign_date"] <= end_date)
].shape[0]

# get all issue assignments and drop rows that have been assigned more recent than the end date
num_assigned_actions = df_in_range[
(df_in_range["assignment_action"] == "assigned") & (df_in_range["assign_date"] <= end_date)
].shape[0]

# number of assigned prs during the time interval
num_prs_assigned = num_assigned_actions - num_unassigned_actions

# number of unassigned prs during the time interval
num_prs_unassigned = num_prs_open - num_prs_assigned

# return the number of assigned and unassigned prs
return num_prs_assigned, num_prs_unassigned
43 changes: 43 additions & 0 deletions 8Knot/pages/utils/pr_assignment_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def pr_assignment(df, start_date, end_date):
"""
This function takes a start and a end date and determines how many
prs in that time interval are assigned and unassigned.

Args:
-----
df : Pandas Dataframe
Dataframe with pr assignment actions of the assignees

start_date : Datetime Timestamp
Timestamp of the start time of the time interval

end_date : Datetime Timestamp
Timestamp of the end time of the time interval

Returns:
--------
int, int: Number of assigned and unassigned prs in the time window
"""

# drop rows that are more recent than the end date
df_created = df[df["created_at"] <= end_date]

# Keep prs that were either still open after the 'start_date' or that have not been closed.
df_in_range = df_created[(df_created["closed_at"] > start_date) | (df_created["closed_at"].isnull())]

# number of prs open in time interval
num_prs_open = df_in_range["pull_request_id"].nunique()

events = df_in_range[df_in_range["assign_date"] <= end_date]
assignments = events[events["assignment_action"] == "assigned"].groupby("pull_request_id").size()
unassignments = events[events["assignment_action"] == "unassigned"].groupby("pull_request_id").size()

# A PR is assigned while any of its reviewer assignments remain active.
active_assignments = assignments.subtract(unassignments, fill_value=0)
num_prs_assigned = int((active_assignments > 0).sum())

# number of unassigned prs during the time interval
num_prs_unassigned = num_prs_open - num_prs_assigned

# return the number of assigned and unassigned prs
return num_prs_assigned, num_prs_unassigned
9 changes: 9 additions & 0 deletions docs/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ If you are interested in making a code contribution and would like to learn more
14. Make changes to the pull request if the reviewing maintainer recommends them.
15. Celebrate your success after your pull request is merged!

## Unit tests

The assignment-counting tests run without a database, Redis, or the Dash server:

```bash
python -m pip install "pandas~=2.3.0"
PYTHONPATH=8Knot python -m unittest discover -s tests -p '*_test.py' -v
```

## Steps to create a new visualization

1. Make a copy of the [visualization template](https://github.com/chaoss/8Knot/blob/dev/8Knot/pages/visualization_template/viz_template.py) for the visualization and put it in the folder of the [page](https://github.com/chaoss/8Knot/tree/dev/8Knot/pages) you want to add it to
Expand Down
123 changes: 123 additions & 0 deletions tests/pr_assignment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import unittest

import pandas as pd

from pages.utils.pr_assignment_utils import pr_assignment


class PullRequestAssignmentTests(unittest.TestCase):
start = pd.Timestamp("2026-01-01", tz="UTC")
end = pd.Timestamp("2026-01-08", tz="UTC")

def frame(self, events):
rows = []
for pr_id, assignee, action, date in events:
rows.append(
{
"pull_request_id": pr_id,
"assignee": assignee,
"assignment_action": action,
"assign_date": date,
"created_at": "2026-01-01",
"closed_at": None,
}
)
df = pd.DataFrame(
rows,
columns=["pull_request_id", "assignee", "assignment_action", "assign_date", "created_at", "closed_at"],
)
for column in ("created_at", "closed_at", "assign_date"):
df[column] = pd.to_datetime(df[column], utc=True)
return df

def test_multiple_reviewers_count_as_one_assigned_pull_request(self):
df = self.frame([(1, "alice", "assigned", "2026-01-02"), (1, "bob", "assigned", "2026-01-03")])
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 0))

def test_unassigning_one_reviewer_keeps_pull_request_assigned(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "bob", "assigned", "2026-01-03"),
(1, "alice", "unassigned", "2026-01-04"),
]
)
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 0))

def test_same_reviewer_on_different_pull_requests(self):
df = self.frame([(1, "alice", "assigned", "2026-01-02"), (2, "alice", "assigned", "2026-01-03")])
self.assertEqual(pr_assignment(df, self.start, self.end), (2, 0))

def test_unassigning_all_reviewers_makes_pull_request_unassigned(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "bob", "assigned", "2026-01-03"),
(1, "alice", "unassigned", "2026-01-04"),
(1, "bob", "unassigned", "2026-01-05"),
]
)
self.assertEqual(pr_assignment(df, self.start, self.end), (0, 1))

def test_mixed_pull_requests_are_counted_independently(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "bob", "assigned", "2026-01-03"),
(2, None, None, None),
(3, "carol", "assigned", "2026-01-02"),
(3, "carol", "unassigned", "2026-01-04"),
]
)
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 2))

def test_reassignment_does_not_duplicate_pull_request(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "alice", "unassigned", "2026-01-03"),
(1, "alice", "assigned", "2026-01-04"),
]
)
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 0))

def test_events_after_bucket_end_do_not_affect_counts(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "bob", "assigned", "2026-01-03"),
(1, "bob", "unassigned", "2026-01-09"),
(2, "carol", "assigned", "2026-01-09"),
]
)
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 1))

def test_only_pull_requests_open_in_bucket_are_counted(self):
df = self.frame(
[
(1, "alice", "assigned", "2026-01-02"),
(1, "bob", "assigned", "2026-01-03"),
(2, None, None, None),
(3, None, None, None),
]
)
df.loc[df.pull_request_id == 2, "closed_at"] = self.start
df.loc[df.pull_request_id == 3, "created_at"] = pd.Timestamp("2026-01-09", tz="UTC")
self.assertEqual(pr_assignment(df, self.start, self.end), (1, 0))

def test_no_assignments(self):
df = self.frame([(1, None, None, None)])
self.assertEqual(pr_assignment(df, self.start, self.end), (0, 1))

def test_empty_data(self):
self.assertEqual(pr_assignment(self.frame([]), self.start, self.end), (0, 0))

def test_input_is_not_modified(self):
df = self.frame([(1, "alice", "assigned", "2026-01-02")])
before = df.copy(deep=True)
pr_assignment(df, self.start, self.end)
pd.testing.assert_frame_equal(df, before)


if __name__ == "__main__":
unittest.main()