From 670c6c738e5e89246df7f3046972c63ce7e3558b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sun, 6 Oct 2024 19:31:14 +0200 Subject: [PATCH 1/2] Add commits-since function --- nixprstatus/pr.py | 8 + ...30d177e57d912d614c88c622dcfdb2e6e6515.json | 185 ++++++++++++++++++ tests/test_pr.py | 11 +- 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/compare_nixos-unstable_27e30d177e57d912d614c88c622dcfdb2e6e6515.json diff --git a/nixprstatus/pr.py b/nixprstatus/pr.py index ebc8ad8..05c7013 100644 --- a/nixprstatus/pr.py +++ b/nixprstatus/pr.py @@ -23,6 +23,14 @@ def commit_in_branch(commit_sha: str, branch: str) -> bool: return False +def commits_since(first_ref: str, last_ref: str) -> int: + url = f"https://api.github.com/repos/NixOS/nixpkgs/compare/{first_ref}...{last_ref}" + commit_response = requests.get(url, headers=DEFAULT_HEADERS) + commit_response.raise_for_status() + + return commit_response.json()["behind_by"] + + def pr_merge_status(pr: int, branches: list[str] = DEFAULT_BRANCHES) -> PRStatus: url = f"https://api.github.com/repos/NixOS/nixpkgs/pulls/{pr}" pr_response = requests.get(url, headers=DEFAULT_HEADERS) diff --git a/tests/fixtures/compare_nixos-unstable_27e30d177e57d912d614c88c622dcfdb2e6e6515.json b/tests/fixtures/compare_nixos-unstable_27e30d177e57d912d614c88c622dcfdb2e6e6515.json new file mode 100644 index 0000000..0afd505 --- /dev/null +++ b/tests/fixtures/compare_nixos-unstable_27e30d177e57d912d614c88c622dcfdb2e6e6515.json @@ -0,0 +1,185 @@ +{ + "url": "https://api.github.com/repos/NixOS/nixpkgs/compare/nixos-unstable...27e30d177e57d912d614c88c622dcfdb2e6e6515", + "html_url": "https://github.com/NixOS/nixpkgs/compare/nixos-unstable...27e30d177e57d912d614c88c622dcfdb2e6e6515", + "permalink_url": "https://github.com/NixOS/nixpkgs/compare/NixOS:bc947f5...NixOS:27e30d1", + "diff_url": "https://github.com/NixOS/nixpkgs/compare/nixos-unstable...27e30d177e57d912d614c88c622dcfdb2e6e6515.diff", + "patch_url": "https://github.com/NixOS/nixpkgs/compare/nixos-unstable...27e30d177e57d912d614c88c622dcfdb2e6e6515.patch", + "base_commit": { + "sha": "bc947f541ae55e999ffdb4013441347d83b00feb", + "node_id": "C_kwDOAEVQ_NoAKGJjOTQ3ZjU0MWFlNTVlOTk5ZmZkYjQwMTM0NDEzNDdkODNiMDBmZWI", + "commit": { + "author": { + "name": "Masum Reza", + "email": "50095635+JohnRTitor@users.noreply.github.com", + "date": "2024-10-04T05:06:13Z" + }, + "committer": { + "name": "GitHub", + "email": "noreply@github.com", + "date": "2024-10-04T05:06:13Z" + }, + "message": "alpaca: 2.0.3 -> 2.0.5 (#345070)", + "tree": { + "sha": "3e7376cebbaf8793c39bb68447950f1045fd4ea5", + "url": "https://api.github.com/repos/NixOS/nixpkgs/git/trees/3e7376cebbaf8793c39bb68447950f1045fd4ea5" + }, + "url": "https://api.github.com/repos/NixOS/nixpkgs/git/commits/bc947f541ae55e999ffdb4013441347d83b00feb", + "comment_count": 0, + "verification": { + "verified": true, + "reason": "valid", + "signature": "-----BEGIN PGP SIGNATURE-----\n\nwsFcBAABCAAQBQJm/3fFCRC1aQ7uu5UhlAAA2JMQAIz5pfsohrsrdPgPxlZVDuaQ\nVPLlM82MOpkq958U90OnXyC4ZlIX2+YwvCUkuRjYjMv9VlHMHU6olHy/jvt5VF/g\nckoaAMztOsPqgyKUXiMew83j8L8EBqVQQZ3W2Br9EnzG2RNEDUQdP5c6Fwc4tccl\nmb3PPb15Nm0lFnitwUO78KxILhrekjdxxrKC/wUihaL/gH4CzdkReVZJ6FPbq9l6\n/HTTDW+amrpOr2XLQ2dC5Cy/nvZS+MPjduNSPsf5a5/ElKmNltbPFChTY0pnv/dq\nrH1zDsohfbtk5WgcmLhiIOpvO86yLfa82sPUJA65GuAW7gYGbMsN6Sh3CnfY7sxb\n7x5r7/sSodxG4t2lML571JfX2aFCeNKgeYomViRu1ZGyHg3LJAN8jKi1JIyVCpH1\nkiTOmQQWzPJRC2COvMGSdXVc7h4CuInev7MmDDiubJDkFYyT8DBzpqVxUTmni13p\ndwr6QAWZZpjnxZxVFnYLlTSifLUIazEJTqaNWnCCc99hyWAo+f5njUlT+9fPLoKE\nHAgMmC55sOzOXnLLgPIoZe2g3wtHNELY3Y6wq7hfS2K9GCxJnJ3mttiUypnyHEuR\n/dVGOlES5p7xWUZ+zEYOp+cLnMWudB5X5cf/ojooLQJc8CL8gIfOD7KOrKewf2Yi\nUUUYH6/erCgoQE2klAaS\n=Hu+V\n-----END PGP SIGNATURE-----\n", + "payload": "tree 3e7376cebbaf8793c39bb68447950f1045fd4ea5\nparent 674acd883193175de91d37a6b3ea8c814d12c8bb\nparent e229318f7d34f4988a815e6d10b8fb0f7e0e2974\nauthor Masum Reza <50095635+JohnRTitor@users.noreply.github.com> 1728018373 +0530\ncommitter GitHub 1728018373 +0530\n\nalpaca: 2.0.3 -> 2.0.5 (#345070)\n\n" + } + }, + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/bc947f541ae55e999ffdb4013441347d83b00feb", + "html_url": "https://github.com/NixOS/nixpkgs/commit/bc947f541ae55e999ffdb4013441347d83b00feb", + "comments_url": "https://api.github.com/repos/NixOS/nixpkgs/commits/bc947f541ae55e999ffdb4013441347d83b00feb/comments", + "author": { + "login": "JohnRTitor", + "id": 50095635, + "node_id": "MDQ6VXNlcjUwMDk1NjM1", + "avatar_url": "https://avatars.githubusercontent.com/u/50095635?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/JohnRTitor", + "html_url": "https://github.com/JohnRTitor", + "followers_url": "https://api.github.com/users/JohnRTitor/followers", + "following_url": "https://api.github.com/users/JohnRTitor/following{/other_user}", + "gists_url": "https://api.github.com/users/JohnRTitor/gists{/gist_id}", + "starred_url": "https://api.github.com/users/JohnRTitor/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/JohnRTitor/subscriptions", + "organizations_url": "https://api.github.com/users/JohnRTitor/orgs", + "repos_url": "https://api.github.com/users/JohnRTitor/repos", + "events_url": "https://api.github.com/users/JohnRTitor/events{/privacy}", + "received_events_url": "https://api.github.com/users/JohnRTitor/received_events", + "type": "User", + "site_admin": false + }, + "committer": { + "login": "web-flow", + "id": 19864447, + "node_id": "MDQ6VXNlcjE5ODY0NDQ3", + "avatar_url": "https://avatars.githubusercontent.com/u/19864447?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/web-flow", + "html_url": "https://github.com/web-flow", + "followers_url": "https://api.github.com/users/web-flow/followers", + "following_url": "https://api.github.com/users/web-flow/following{/other_user}", + "gists_url": "https://api.github.com/users/web-flow/gists{/gist_id}", + "starred_url": "https://api.github.com/users/web-flow/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/web-flow/subscriptions", + "organizations_url": "https://api.github.com/users/web-flow/orgs", + "repos_url": "https://api.github.com/users/web-flow/repos", + "events_url": "https://api.github.com/users/web-flow/events{/privacy}", + "received_events_url": "https://api.github.com/users/web-flow/received_events", + "type": "User", + "site_admin": false + }, + "parents": [ + { + "sha": "674acd883193175de91d37a6b3ea8c814d12c8bb", + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/674acd883193175de91d37a6b3ea8c814d12c8bb", + "html_url": "https://github.com/NixOS/nixpkgs/commit/674acd883193175de91d37a6b3ea8c814d12c8bb" + }, + { + "sha": "e229318f7d34f4988a815e6d10b8fb0f7e0e2974", + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/e229318f7d34f4988a815e6d10b8fb0f7e0e2974", + "html_url": "https://github.com/NixOS/nixpkgs/commit/e229318f7d34f4988a815e6d10b8fb0f7e0e2974" + } + ] + }, + "merge_base_commit": { + "sha": "27e30d177e57d912d614c88c622dcfdb2e6e6515", + "node_id": "C_kwDOAEVQ_NoAKDI3ZTMwZDE3N2U1N2Q5MTJkNjE0Yzg4YzYyMmRjZmRiMmU2ZTY1MTU", + "commit": { + "author": { + "name": "nixpkgs-merge-bot[bot]", + "email": "148217876+nixpkgs-merge-bot[bot]@users.noreply.github.com", + "date": "2024-10-01T17:15:20Z" + }, + "committer": { + "name": "GitHub", + "email": "noreply@github.com", + "date": "2024-10-01T17:15:20Z" + }, + "message": "fflogs: 8.13.5 -> 8.14.0 (#344409)", + "tree": { + "sha": "209d35171564896577260569f6da7d444d22435d", + "url": "https://api.github.com/repos/NixOS/nixpkgs/git/trees/209d35171564896577260569f6da7d444d22435d" + }, + "url": "https://api.github.com/repos/NixOS/nixpkgs/git/commits/27e30d177e57d912d614c88c622dcfdb2e6e6515", + "comment_count": 0, + "verification": { + "verified": true, + "reason": "valid", + "signature": "-----BEGIN PGP SIGNATURE-----\n\nwsFcBAABCAAQBQJm/C4oCRC1aQ7uu5UhlAAAqUMQAINLlEPM+qnZU0Tb+7sJW/Ww\nu7SViLn6rH2EzZSVxS4DBD/phRm2wmQyjmVRpYMkRFsUQQFLQBjA5oPfzlT+frej\nNCHADxJ8h27xfV3mi40hJkWaC++ssuElh2ZYoyC9wC1yuViO44tuDQKapt6ROQIG\n8Wrg10RN7FBjd5Mb87NBOlSamiIdwobnQUBFCN3MweZ7i7CiuSq2ShKKyEy1KZIn\nH1GVV6rSkRXS0uN9s62uiISSVsvS88RpIMES83Z+fh8v6C+gw6fhUOIP0fLSVogi\neYAB3btJMCryvam7r7gfDDn2yQ6gxlpQYRDgVoK1sLeXg5hy4ouzykkrxvBbvgFd\nGl1eOePkRX+Pz5dPBIYON6QkhgEq6Ysnk4Z7FKvCmXpzDD8Yu0DWWMh65gzMXl9n\nFtThPLEurDXtZoxGnTmqD5MgNpYdH+NgLGH7ml69TinkgkRfvNeDeScmECydBnwN\n/4WTVXCzr6O0sER2MOd3Lb+LjOcymegzLnkySzvCousB2O2viVet9xs2D8QhD1LW\n9eiWfPNsuoC4xjBk/4DPmw0aSjfFiueVudG8cHg0+QxYMnbCVb3kBbjaPTmDTj8U\n/c8Eni7wIp7KEquEJTIh9Uq0thloNk/wgqY9Hj9XErcuyWZCmcB3CqIm2ZuRk9Bh\neHggC9G9VXULWzRj2OvC\n=G9Lq\n-----END PGP SIGNATURE-----\n", + "payload": "tree 209d35171564896577260569f6da7d444d22435d\nparent 9133b9f5554437083a6618581eeae88c53593b0d\nparent c914c30b5b9f65577b83cf0b84a01e46fa1ddf96\nauthor nixpkgs-merge-bot[bot] <148217876+nixpkgs-merge-bot[bot]@users.noreply.github.com> 1727802920 +0000\ncommitter GitHub 1727802920 +0000\n\nfflogs: 8.13.5 -> 8.14.0 (#344409)\n\n" + } + }, + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/27e30d177e57d912d614c88c622dcfdb2e6e6515", + "html_url": "https://github.com/NixOS/nixpkgs/commit/27e30d177e57d912d614c88c622dcfdb2e6e6515", + "comments_url": "https://api.github.com/repos/NixOS/nixpkgs/commits/27e30d177e57d912d614c88c622dcfdb2e6e6515/comments", + "author": { + "login": "nixpkgs-merge-bot[bot]", + "id": 148217876, + "node_id": "BOT_kgDOCNWgFA", + "avatar_url": "https://avatars.githubusercontent.com/in/409421?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D", + "html_url": "https://github.com/apps/nixpkgs-merge-bot", + "followers_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/followers", + "following_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/following{/other_user}", + "gists_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/gists{/gist_id}", + "starred_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/subscriptions", + "organizations_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/orgs", + "repos_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/repos", + "events_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/events{/privacy}", + "received_events_url": "https://api.github.com/users/nixpkgs-merge-bot%5Bbot%5D/received_events", + "type": "Bot", + "site_admin": false + }, + "committer": { + "login": "web-flow", + "id": 19864447, + "node_id": "MDQ6VXNlcjE5ODY0NDQ3", + "avatar_url": "https://avatars.githubusercontent.com/u/19864447?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/web-flow", + "html_url": "https://github.com/web-flow", + "followers_url": "https://api.github.com/users/web-flow/followers", + "following_url": "https://api.github.com/users/web-flow/following{/other_user}", + "gists_url": "https://api.github.com/users/web-flow/gists{/gist_id}", + "starred_url": "https://api.github.com/users/web-flow/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/web-flow/subscriptions", + "organizations_url": "https://api.github.com/users/web-flow/orgs", + "repos_url": "https://api.github.com/users/web-flow/repos", + "events_url": "https://api.github.com/users/web-flow/events{/privacy}", + "received_events_url": "https://api.github.com/users/web-flow/received_events", + "type": "User", + "site_admin": false + }, + "parents": [ + { + "sha": "9133b9f5554437083a6618581eeae88c53593b0d", + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/9133b9f5554437083a6618581eeae88c53593b0d", + "html_url": "https://github.com/NixOS/nixpkgs/commit/9133b9f5554437083a6618581eeae88c53593b0d" + }, + { + "sha": "c914c30b5b9f65577b83cf0b84a01e46fa1ddf96", + "url": "https://api.github.com/repos/NixOS/nixpkgs/commits/c914c30b5b9f65577b83cf0b84a01e46fa1ddf96", + "html_url": "https://github.com/NixOS/nixpkgs/commit/c914c30b5b9f65577b83cf0b84a01e46fa1ddf96" + } + ] + }, + "status": "behind", + "ahead_by": 0, + "behind_by": 795, + "total_commits": 0, + "commits": [ + + ], + "files": [ + + ] +} diff --git a/tests/test_pr.py b/tests/test_pr.py index 72edf9b..927fc81 100644 --- a/tests/test_pr.py +++ b/tests/test_pr.py @@ -3,7 +3,7 @@ import unittest.mock import requests import json -from nixprstatus.pr import commit_in_branch, pr_merge_status +from nixprstatus.pr import commit_in_branch, pr_merge_status, commits_since def mocked_requests_get(*args, **kwargs): @@ -53,3 +53,12 @@ class TestCommitInBranch(unittest.TestCase): self.assertTrue(res) res = commit_in_branch(commit_sha, "nixos-24.05") self.assertFalse(res) + + +class TestCommitsSince(unittest.TestCase): + @unittest.mock.patch("requests.get", side_effect=mocked_requests_get) + def test_commits_since_27e30d17(self, mock_get): + commit_sha = "27e30d177e57d912d614c88c622dcfdb2e6e6515" + + res = commits_since("nixos-unstable", commit_sha) + self.assertEqual(res, 795) -- 2.45.2 From de64f73c464647a177ac26b3d30e4b2eba68fb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sun, 6 Oct 2024 19:39:42 +0200 Subject: [PATCH 2/2] Add since command --- nixprstatus/__main__.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/nixprstatus/__main__.py b/nixprstatus/__main__.py index 0580fde..4ded06d 100644 --- a/nixprstatus/__main__.py +++ b/nixprstatus/__main__.py @@ -1,7 +1,9 @@ import typer +import json from typing import Annotated from rich.console import Console from nixprstatus.pr import pr_merge_status +from nixprstatus.pr import commits_since app = typer.Typer() @@ -38,6 +40,26 @@ def pr( console.print(output, highlight=False) +@app.command() +def since( + commit_sha: str, + target: str = "nixos-unstable", + waybar_format: Annotated[ + bool, typer.Option(help="Output in format expected by waybar") + ] = False, +): + """ + Return the count of commits that has happened between the two refs. + """ + count = commits_since(target, commit_sha) + + if waybar_format: + output = {"text": str(count), "tooltip": f"{target}: {count}"} + typer.echo(json.dumps(output)) + return + typer.echo(count) + + def main(): app() -- 2.45.2