Merge pull request #71 from revsys/libraries-open-issues

#65 Add count of open issues to library detail page
This commit is contained in:
Lacey Williams Henschel
2022-12-12 12:39:30 -08:00
committed by GitHub
4 changed files with 47 additions and 5 deletions

View File

@@ -28,9 +28,13 @@ def get_repo(api, owner, repo):
return api.repos.get(owner=owner, repo=repo)
def repo_issues(owner, repo, state="all"):
def repo_issues(owner, repo, state="all", issues_only=True):
"""
Get all issues for a repo
Get all issues for a repo.
Note: The GitHub API considers both PRs and Issues to be "Issues" and does not
support filtering in the request, so to exclude PRs from the list of issues, we
do some manual filtering of the results
"""
api = get_api()
pages = list(
@@ -43,9 +47,16 @@ def repo_issues(owner, repo, state="all"):
)
)
# Concatenate all pages into a single list
all_results = []
for page in pages:
all_results.extend(page)
# Filter results
results = []
for p in pages:
results.extend(p)
if issues_only:
results = [result for result in all_results if not result.get("pull_request")]
else:
results = all_results
return results

View File

@@ -0,0 +1,15 @@
from unittest.mock import patch
def test_library_list(library, tp):
res = tp.get("libraries")
tp.response_200(res)
def test_library_detail(library, tp):
url = tp.reverse("library-detail", library.slug)
with patch("libraries.views.LibraryDetail.get_open_issues_count") as count_mock:
count_mock.return_value = 21
res = tp.get(url)
tp.response_200(res)

View File

@@ -1,5 +1,6 @@
from django.views.generic import DetailView, ListView
from .github import repo_issues
from .models import Category, Library
@@ -55,3 +56,18 @@ class LibraryDetail(CategoryMixin, DetailView):
model = Library
template_name = "libraries/detail.html"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
context["open_issues_count"] = self.get_open_issues_count(self.object)
return self.render_to_response(context)
def get_open_issues_count(self, obj):
try:
issues = repo_issues(
obj.github_owner, obj.github_repo, state="open", issues_only=True
)
return len(issues)
except Exception:
return 0

View File

@@ -68,7 +68,7 @@
<div class="py-6">
<h3 class="text-2xl mb-1">Open Issues</h3>
Y
{{ open_issues_count }}
</div>
<div class="py-6">