Skip to content

Commit 6a1c0b6

Browse files
committed
Merge branch 'app-installation-repositories' into dev
2 parents 6f9ad3c + ae4e42a commit 6a1c0b6

4 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎README.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ GitHub Apps
209209

210210
GitHub Apps are ideal for organization backups in CI/CD. Tokens are scoped to specific repositories and expire after 1 hour.
211211

212+
For a user account (no ``--organization``), ``--as-app`` lists the repositories the installation can access, so private repositories are included.
213+
212214
**One-time setup:**
213215

214216
1. Create a GitHub App at *Settings -> Developer Settings -> GitHub Apps -> New GitHub App*

‎github_backup/github_backup.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,11 @@ def retrieve_repositories(args, authenticated_user):
19571957
if args.user == authenticated_user["login"]:
19581958
# we must use the /user/repos API to be able to access private repos
19591959
template = "https://{0}/user/repos".format(get_github_api_host(args))
1960+
elif args.as_app and not args.organization:
1961+
# /users/{user}/repos is public-only; the installation sees private repos too
1962+
template = "https://{0}/installation/repositories".format(
1963+
get_github_api_host(args)
1964+
)
19601965
else:
19611966
if args.private and not args.organization:
19621967
logger.warning(
@@ -1987,6 +1992,10 @@ def retrieve_repositories(args, authenticated_user):
19871992
logger.warning(f"Legal notice: {e.legal_url}")
19881993
return []
19891994

1995+
if template.endswith("/installation/repositories"):
1996+
# Each page is {"total_count": ..., "repositories": [...]}
1997+
repos = [repo for page in repos for repo in page["repositories"]]
1998+
19901999
return retrieve_additional_repositories(args, authenticated_user, repos)
19912000

19922001

‎release-requirements.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pytest==9.1.1
99
# Release & Publishing
1010
twine==7.0.0
1111
gitchangelog==3.0.4
12-
setuptools==84.2.0
12+
setuptools==84.2.1
1313

1414
# Documentation
1515
restructuredtext-lint==2.0.2

‎tests/test_app_repositories.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Tests for listing a user's repositories with a GitHub App installation token."""
2+
3+
from unittest.mock import patch
4+
5+
import pytest
6+
7+
from github_backup import github_backup
8+
9+
NO_USER = {"login": None}
10+
11+
12+
def page(*names):
13+
return {
14+
"total_count": 3,
15+
"repositories": [{"name": name, "private": True} for name in names],
16+
}
17+
18+
19+
@pytest.fixture
20+
def mock_retrieve_data():
21+
with patch("github_backup.github_backup.retrieve_data") as mock:
22+
yield mock
23+
24+
25+
def test_app_lists_user_repositories_from_installation(create_args, mock_retrieve_data):
26+
args = create_args(as_app=True, include_repository=True)
27+
mock_retrieve_data.return_value = [page("a", "b"), page("c")]
28+
29+
repos = github_backup.retrieve_repositories(args, NO_USER)
30+
31+
assert mock_retrieve_data.call_args.args[1] == (
32+
"https://api.github.com/installation/repositories"
33+
)
34+
assert [r["name"] for r in repos] == ["a", "b", "c"]
35+
36+
37+
def test_app_installation_listing_uses_ghes_host(create_args, mock_retrieve_data):
38+
args = create_args(
39+
as_app=True, include_repository=True, github_host="ghe.example.com"
40+
)
41+
mock_retrieve_data.return_value = [page("a")]
42+
43+
github_backup.retrieve_repositories(args, NO_USER)
44+
45+
assert mock_retrieve_data.call_args.args[1] == (
46+
"https://ghe.example.com/api/v3/installation/repositories"
47+
)
48+
49+
50+
def test_app_organization_still_lists_org_repositories(create_args, mock_retrieve_data):
51+
args = create_args(as_app=True, include_repository=True, organization=True)
52+
mock_retrieve_data.return_value = [{"name": "a"}]
53+
54+
repos = github_backup.retrieve_repositories(args, NO_USER)
55+
56+
assert mock_retrieve_data.call_args.args[1] == (
57+
"https://api.github.com/orgs/testuser/repos"
58+
)
59+
assert repos == [{"name": "a"}]
60+
61+
62+
def test_app_single_repository_is_fetched_directly(create_args, mock_retrieve_data):
63+
args = create_args(as_app=True, repository="dotfiles")
64+
mock_retrieve_data.return_value = [{"name": "dotfiles"}]
65+
66+
repos = github_backup.retrieve_repositories(args, NO_USER)
67+
68+
assert mock_retrieve_data.call_args.args[1] == (
69+
"https://api.github.com/repos/testuser/dotfiles"
70+
)
71+
assert repos == [{"name": "dotfiles"}]

0 commit comments

Comments
 (0)