imbi-plugin-github 2.48.0__tar.gz → 2.49.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/PKG-INFO +2 -2
  2. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/pyproject.toml +2 -2
  3. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/commits.py +10 -0
  4. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/deployment.py +39 -3
  5. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/lifecycle.py +41 -0
  6. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_commits.py +40 -0
  7. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_deployment.py +62 -0
  8. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_lifecycle.py +48 -0
  9. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/.gitignore +0 -0
  10. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/README.md +0 -0
  11. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/moon.yml +0 -0
  12. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/README.md +0 -0
  13. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/__init__.py +0 -0
  14. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/_app_auth.py +0 -0
  15. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/_hosts.py +0 -0
  16. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/_repos.py +0 -0
  17. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/doctor.py +0 -0
  18. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/identity.py +0 -0
  19. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/plugin.py +0 -0
  20. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/public.py +0 -0
  21. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/pull_requests.py +0 -0
  22. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/py.typed +0 -0
  23. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/src/imbi/plugins/github/secret_provider.py +0 -0
  24. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/__init__.py +0 -0
  25. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_app_auth_scopes.py +0 -0
  26. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_doctor.py +0 -0
  27. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_hosts.py +0 -0
  28. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_identity.py +0 -0
  29. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_public.py +0 -0
  30. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_pull_requests.py +0 -0
  31. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_repos.py +0 -0
  32. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_secret_provider.py +0 -0
  33. {imbi_plugin_github-2.48.0 → imbi_plugin_github-2.49.0}/tests/test_secret_provider_live.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: imbi-plugin-github
3
- Version: 2.48.0
3
+ Version: 2.49.0
4
4
  Summary: GitHub identity plugin for Imbi (github.com / GHEC / GHES)
5
5
  Author-email: "Gavin M. Roy" <gavinr@aweber.com>
6
6
  License-Expression: BSD-3-Clause
@@ -12,7 +12,7 @@ Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Programming Language :: Python :: 3.14
13
13
  Requires-Python: >=3.14
14
14
  Requires-Dist: httpx>=0.27
15
- Requires-Dist: imbi-common[databases]==2.48.0
15
+ Requires-Dist: imbi-common[databases]==2.49.0
16
16
  Requires-Dist: pydantic>=2
17
17
  Requires-Dist: pyjwt[crypto]>=2.8
18
18
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "imbi-plugin-github"
3
- version = "2.48.0"
3
+ version = "2.49.0"
4
4
  description = "GitHub identity plugin for Imbi (github.com / GHEC / GHES)"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.14"
@@ -18,7 +18,7 @@ classifiers = [
18
18
  ]
19
19
  dependencies = [
20
20
  "httpx>=0.27",
21
- "imbi-common[databases]==2.48.0",
21
+ "imbi-common[databases]==2.49.0",
22
22
  "pydantic>=2",
23
23
  "pyjwt[crypto]>=2.8",
24
24
  ]
@@ -813,6 +813,16 @@ async def _fetch_all_commits(
813
813
  resp = await _request(
814
814
  client, 'GET', '/commits', params=params, max_wait=max_wait
815
815
  )
816
+ # GHES 404s a deep page it just linked to as ``next``. Keep the
817
+ # history read so far rather than drop the whole sync.
818
+ if resp.status_code == 404 and page > 1:
819
+ LOGGER.warning(
820
+ 'github-commit-sync got 404 for commits page %d (%d '
821
+ 'commits read); older commits will not be recorded',
822
+ page,
823
+ len(out),
824
+ )
825
+ return out
816
826
  resp.raise_for_status()
817
827
  out.extend(typing.cast('list[dict[str, typing.Any]]', resp.json()))
818
828
  next_url = _next_page_url(resp.headers.get('link'))
@@ -1067,12 +1067,48 @@ class GitHubDeployment(DeploymentCapability):
1067
1067
  async with self._client(
1068
1068
  ctx, credentials, SCOPE_CONTENTS_READ
1069
1069
  ) as client:
1070
- resp = await client.get(
1071
- f'/commits/{urllib.parse.quote(committish, safe="")}'
1070
+ # ``HEAD`` resolves to the default branch's head commit; the
1071
+ # ``sha`` media type answers with the bare sha.
1072
+ async def _head() -> httpx.Response | None:
1073
+ try:
1074
+ return await client.get(
1075
+ '/commits/HEAD',
1076
+ headers={'Accept': 'application/vnd.github.sha'},
1077
+ )
1078
+ except httpx.HTTPError:
1079
+ # Head detection is optional; keep the resolved commit.
1080
+ return None
1081
+
1082
+ resp, head = await asyncio.gather(
1083
+ client.get(
1084
+ f'/commits/{urllib.parse.quote(committish, safe="")}'
1085
+ ),
1086
+ _head(),
1072
1087
  )
1073
1088
  resp.raise_for_status()
1074
1089
  payload = typing.cast(dict[str, typing.Any], resp.json())
1075
- return _commit_from_payload(payload)
1090
+ commit = _commit_from_payload(payload)
1091
+ if (
1092
+ head is not None
1093
+ and head.status_code == 200
1094
+ and head.text.strip() == commit.sha
1095
+ ):
1096
+ commit = commit.model_copy(update={'is_head': True})
1097
+ host = self._resolve_host(ctx)
1098
+ owner, repo = self._owner_repo(ctx)
1099
+ if _checks_disabled(credentials, host, owner, repo):
1100
+ return commit
1101
+ # A separate checks-scoped token: an App without ``checks: read``
1102
+ # (a ``422`` on mint) still resolves, with ``ci_status`` unknown.
1103
+ try:
1104
+ async with self._client(
1105
+ ctx, credentials, SCOPE_CHECKS_READ
1106
+ ) as client:
1107
+ return await self._hydrate_check_status(
1108
+ client, credentials, host, owner, repo, commit
1109
+ )
1110
+ except httpx.HTTPError:
1111
+ return commit
1076
1112
 
1077
1113
  # -- Compare ------------------------------------------------------------
1078
1114
 
@@ -554,6 +554,47 @@ class GitHubLifecycle(LifecycleCapability):
554
554
  display=identifier,
555
555
  )
556
556
 
557
+ async def check_archive_access(
558
+ self,
559
+ ctx: PluginContext,
560
+ credentials: dict[str, str],
561
+ ) -> str | None:
562
+ """Refuse when the caller is not an admin of the repository.
563
+
564
+ GitHub answers an archive or unarchive ``PATCH`` from a non-admin
565
+ with ``403``. The repo payload carries the caller's
566
+ ``permissions`` for a user token; when it is absent (an App
567
+ installation token), allow the request. GitHub answers ``404``
568
+ for a private repository the caller cannot see, so refuse that
569
+ too, but only for a linked repo: a project-type fallback guess
570
+ carries no authority, so its ``404`` does not block.
571
+ """
572
+ host = self._resolve_host(ctx)
573
+ target = resolve_repository_target(
574
+ ctx, host, 'GitHub lifecycle plugin'
575
+ )
576
+ owner, repo = target.owner, target.repo
577
+ async with self._client(ctx, credentials) as client:
578
+ current = await self._get_repo_or_none(client, owner, repo)
579
+ if current is None:
580
+ if target.source != 'link':
581
+ return None
582
+ return (
583
+ f'The GitHub repository {owner}/{repo} is not accessible '
584
+ 'with your GitHub identity, so this project cannot be '
585
+ 'archived or restored'
586
+ )
587
+ perms = typing.cast(
588
+ dict[str, object] | None, current.get('permissions')
589
+ )
590
+ if perms is not None and not perms.get('admin'):
591
+ return (
592
+ 'You need admin access to the GitHub repository '
593
+ f'{current.get("full_name") or f"{owner}/{repo}"} '
594
+ 'to archive or restore this project'
595
+ )
596
+ return None
597
+
557
598
  async def on_project_archived(
558
599
  self,
559
600
  ctx: PluginContext,
@@ -1793,6 +1793,46 @@ class SyncAllHistoryTestCase(unittest.IsolatedAsyncioTestCase):
1793
1793
  # Only the commits insert ran; the empty tag list short-circuits.
1794
1794
  insert.assert_awaited_once()
1795
1795
 
1796
+ @respx.mock
1797
+ async def test_deep_page_404_ends_history(self) -> None:
1798
+ """IMBI-2K: GHES 404s a deep commits page; keep what was read."""
1799
+ self._mock_default_branch()
1800
+ url = f'{self._REPO}/commits'
1801
+ respx.get(url).mock(
1802
+ side_effect=[
1803
+ httpx.Response(
1804
+ 200,
1805
+ json=[_commit('1' * 40)],
1806
+ headers={'link': f'<{url}?page=2>; rel="next"'},
1807
+ ),
1808
+ httpx.Response(404, json={'message': 'Not Found'}),
1809
+ ]
1810
+ )
1811
+ respx.get(f'{self._REPO}/git/matching-refs/tags').mock(
1812
+ return_value=httpx.Response(200, json=[])
1813
+ )
1814
+ with mock.patch(_INSERT, new=mock.AsyncMock()):
1815
+ with self.assertLogs(commits.LOGGER, level='WARNING') as cm:
1816
+ (
1817
+ commits_recorded,
1818
+ _,
1819
+ ) = await commits.GitHubCommitSync().sync_all_history(
1820
+ ctx=self._ctx(), credentials=_CREDS
1821
+ )
1822
+ self.assertEqual(1, commits_recorded)
1823
+ self.assertTrue(any('page 2' in x for x in cm.output))
1824
+
1825
+ @respx.mock
1826
+ async def test_first_page_404_still_raises(self) -> None:
1827
+ self._mock_default_branch()
1828
+ respx.get(f'{self._REPO}/commits').mock(
1829
+ return_value=httpx.Response(404, json={'message': 'Not Found'})
1830
+ )
1831
+ with self.assertRaises(httpx.HTTPStatusError):
1832
+ await commits.GitHubCommitSync().sync_all_history(
1833
+ ctx=self._ctx(), credentials=_CREDS
1834
+ )
1835
+
1796
1836
  @respx.mock
1797
1837
  async def test_history_truncation_logs(self) -> None:
1798
1838
  self._mock_default_branch()
@@ -628,10 +628,72 @@ class CommitsTestCase(unittest.IsolatedAsyncioTestCase):
628
628
  },
629
629
  )
630
630
  )
631
+ head = respx.get(
632
+ 'https://api.github.com/repos/octo/demo/commits/HEAD'
633
+ ).mock(return_value=httpx.Response(200, text='abc'))
634
+ respx.get(
635
+ 'https://api.github.com/repos/octo/demo/commits/abc/check-runs'
636
+ ).mock(
637
+ return_value=httpx.Response(
638
+ 200,
639
+ json={
640
+ 'check_runs': [
641
+ {
642
+ 'id': 1,
643
+ 'name': 'unit',
644
+ 'status': 'completed',
645
+ 'conclusion': 'failure',
646
+ }
647
+ ]
648
+ },
649
+ )
650
+ )
631
651
  plugin = GitHubDeployment()
632
652
  commit = await plugin.resolve_committish(_ctx(), _CREDS, 'abc')
633
653
  self.assertEqual(commit.sha, 'abc')
634
654
  self.assertEqual(commit.message, 'fix')
655
+ self.assertTrue(commit.is_head)
656
+ self.assertEqual(commit.ci_status, 'fail')
657
+ self.assertEqual(
658
+ head.calls.last.request.headers['Accept'],
659
+ 'application/vnd.github.sha',
660
+ )
661
+
662
+ @respx.mock
663
+ async def test_resolve_committish_not_default_head(self) -> None:
664
+ respx.get('https://api.github.com/repos/octo/demo/commits/v1').mock(
665
+ return_value=httpx.Response(
666
+ 200, json={'sha': 'abc', 'commit': {'message': 'fix'}}
667
+ )
668
+ )
669
+ respx.get('https://api.github.com/repos/octo/demo/commits/HEAD').mock(
670
+ return_value=httpx.Response(200, text='def')
671
+ )
672
+ respx.get(
673
+ 'https://api.github.com/repos/octo/demo/commits/abc/check-runs'
674
+ ).mock(return_value=httpx.Response(404, json={}))
675
+ plugin = GitHubDeployment()
676
+ commit = await plugin.resolve_committish(_ctx(), _CREDS, 'v1')
677
+ self.assertFalse(commit.is_head)
678
+ self.assertEqual(commit.ci_status, 'unknown')
679
+
680
+ @respx.mock
681
+ async def test_resolve_committish_head_lookup_fails(self) -> None:
682
+ respx.get('https://api.github.com/repos/octo/demo/commits/v1').mock(
683
+ return_value=httpx.Response(
684
+ 200, json={'sha': 'abc', 'commit': {'message': 'fix'}}
685
+ )
686
+ )
687
+ respx.get('https://api.github.com/repos/octo/demo/commits/HEAD').mock(
688
+ side_effect=httpx.TimeoutException('timeout')
689
+ )
690
+ respx.get(
691
+ 'https://api.github.com/repos/octo/demo/commits/abc/check-runs'
692
+ ).mock(return_value=httpx.Response(404, json={}))
693
+ plugin = GitHubDeployment()
694
+ commit = await plugin.resolve_committish(_ctx(), _CREDS, 'v1')
695
+ self.assertEqual(commit.sha, 'abc')
696
+ self.assertFalse(commit.is_head)
635
697
 
636
698
 
637
699
  class CompareTestCase(unittest.IsolatedAsyncioTestCase):
@@ -551,6 +551,54 @@ class UnarchiveTestCase(unittest.IsolatedAsyncioTestCase):
551
551
  self.assertEqual(patch_route.calls.call_count, 0)
552
552
 
553
553
 
554
+ class CheckArchiveAccessTestCase(unittest.IsolatedAsyncioTestCase):
555
+ async def _check(self, payload: dict[str, object]) -> str | None:
556
+ with respx.mock:
557
+ respx.get('https://api.github.com/repos/octo/demo').mock(
558
+ return_value=httpx.Response(200, json=payload)
559
+ )
560
+ return await GitHubLifecycle().check_archive_access(_ctx(), _CREDS)
561
+
562
+ async def test_refuses_non_admin(self) -> None:
563
+ reason = await self._check(
564
+ {
565
+ 'full_name': 'octo/demo',
566
+ 'permissions': {'admin': False, 'push': True, 'pull': True},
567
+ }
568
+ )
569
+ self.assertIn('admin access', reason or '')
570
+ self.assertIn('octo/demo', reason or '')
571
+
572
+ async def test_allows_admin(self) -> None:
573
+ self.assertIsNone(await self._check({'permissions': {'admin': True}}))
574
+
575
+ async def test_allows_when_permissions_absent(self) -> None:
576
+ self.assertIsNone(await self._check({'full_name': 'octo/demo'}))
577
+
578
+ async def test_refuses_when_repo_not_visible(self) -> None:
579
+ with respx.mock:
580
+ respx.get('https://api.github.com/repos/octo/demo').mock(
581
+ return_value=httpx.Response(404, json={})
582
+ )
583
+ patch_route = respx.patch('https://api.github.com/repos/octo/demo')
584
+ reason = await GitHubLifecycle().check_archive_access(
585
+ _ctx(), _CREDS
586
+ )
587
+ self.assertIn('not accessible', reason or '')
588
+ self.assertNotIn('admin', reason or '')
589
+ self.assertEqual(patch_route.calls.call_count, 0)
590
+
591
+ async def test_allows_404_on_fallback_target(self) -> None:
592
+ with respx.mock:
593
+ respx.get('https://api.github.com/repos/octo/demo').mock(
594
+ return_value=httpx.Response(404, json={})
595
+ )
596
+ reason = await GitHubLifecycle().check_archive_access(
597
+ _ctx(project_links={}, project_type_slugs=['octo']), _CREDS
598
+ )
599
+ self.assertIsNone(reason)
600
+
601
+
554
602
  class HostResolutionTestCase(unittest.TestCase):
555
603
  def test_github_com_resolves_from_connection(self) -> None:
556
604
  plugin = GitHubLifecycle()