Feature: A class to interact with the github API¶
Okay, here's a prompt designed to be given to Gemini Code Assist, building on our previous discussion and leveraging its ability to generate multi-file projects and detailed code.
Gemini Code Assist Prompt:
"My project needs an object-oriented Python utility to interact with GitHub repositories, specifically for fetching issues and pull requests. I already have a shared low-level API fetching utility located at scripts/github_api.py which contains a fetch_from_github(owner, repo, endpoint, params) function that handles pagination and authentication via GITHUB_TOKEN environment variable.
I need you to create a new Python package and populate it with two classes: Repo and Item.
Here are the requirements:
-
Project Structure:
- Create a new directory
local_data_platform. - Inside
local_data_platform, create an empty__init__.pyfile to make it a package. - Inside
local_data_platform, create a new file namedgithub.py.
- Create a new directory
-
local_data_platform/github.pycontent:-
Import: It should import
fetch_from_githubfromscripts.github_api. -
RepoClass:- Represents a GitHub repository.
__init__(self, owner: str, name: str): Initializes with the repository owner (username or organization) and name.__repr__(self): Provides a helpful string representation.
-
ItemClass:- Represents a single GitHub issue or pull request.
__init__(self, data: dict, repo: 'Repo'):- Takes a
datadictionary (the raw JSON response for an issue/PR from GitHub API) and arepoobject (an instance of theRepoclass). - Determines
self.typeas either'issue'or'pull_request'based on the presence of the'pull_request'key indata. - Initializes common attributes directly from
data:number,title,html_url,state,created_at,updated_at,closed_at,user_login(fromuser.login), andlabels.
- Takes a
is_pr(self) -> bool: ReturnsTrueif the item is a pull request,Falseotherwise.is_issue(self) -> bool: ReturnsTrueif the item is a regular issue,Falseotherwise.__repr__(self): Provides a helpful string representation including type, number, title (truncated), state, and repo name.-
__getattr__(self, name): Implement this to allow accessing any key from the underlying_datadictionary directly as an attribute (e.g.,item.bodyoritem.assignee). If the attribute doesn't exist in_data, raise anAttributeError. -
@classmethod fetch_all_items(cls, repo: Repo, status: str = "open") -> list['Item']:- This is the core fetching method.
- It should take a
Repoobject and an optionalstatusstring (defaulting to "open"). - It must use the imported
fetch_from_githubfunction, passingrepo.owner,repo.name, the endpoint"issues", andparams={"state": status}. (Note: The GitHub/issuesendpoint returns both issues and PRs, which is suitable here). - It should then iterate through the raw data returned by
fetch_from_githuband wrap each dictionary in anItemobject, returning a list ofIteminstances. - Include basic type validation for
repoparameter.
-
-
Example Usage Script (
main.py):- Create a
main.pyfile in the project root (sibling tolocal_data_platformandscripts). - It should demonstrate how to:
- Import
RepoandItem. - Define
REPO_OWNERandREPO_NAMEconstants (e.g., "tusharchou", "local-data-platform"). - Instantiate a
Repoobject. - Call
Item.fetch_all_itemsto get open items. - Print the total count of open items.
- Separate the fetched items into
open_issuesandopen_prslists using theis_issue()andis_pr()methods. - Print the counts for open issues and open PRs.
- Loop through and print the
number,title,type,state,user_login,created_at(formatted), andhtml_urlfor a few (e.g., top 5) issues and PRs. - Demonstrate fetching closed items as well.
- Show an example of using the
__getattr__functionality (e.g., printingitem.bodyif available).
- Import
- Include a note about setting the
GITHUB_TOKENenvironment variable for authenticated requests.
- Create a
Please provide the complete code for local_data_platform/__init__.py, local_data_platform/github.py, and main.py."