gitcode-api 1.2.4__tar.gz → 1.2.6__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 (46) hide show
  1. gitcode_api-1.2.6/PKG-INFO +371 -0
  2. gitcode_api-1.2.6/README.md +337 -0
  3. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/cli.py +67 -9
  4. gitcode_api-1.2.6/gitcode_api/llm/__init__.py +49 -0
  5. gitcode_api-1.2.6/gitcode_api/llm/_tool.py +368 -0
  6. gitcode_api-1.2.6/gitcode_api/llm/mcp.py +96 -0
  7. gitcode_api-1.2.6/gitcode_api/llm/openai.py +56 -0
  8. gitcode_api-1.2.6/gitcode_api/py.typed +1 -0
  9. gitcode_api-1.2.6/gitcode_api/version.txt +1 -0
  10. gitcode_api-1.2.6/gitcode_api.egg-info/PKG-INFO +371 -0
  11. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api.egg-info/SOURCES.txt +6 -0
  12. gitcode_api-1.2.6/gitcode_api.egg-info/requires.txt +6 -0
  13. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/pyproject.toml +23 -4
  14. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_cli.py +76 -7
  15. gitcode_api-1.2.6/tests/test_llm_tools.py +105 -0
  16. gitcode_api-1.2.4/PKG-INFO +0 -237
  17. gitcode_api-1.2.4/README.md +0 -210
  18. gitcode_api-1.2.4/gitcode_api/version.txt +0 -1
  19. gitcode_api-1.2.4/gitcode_api.egg-info/PKG-INFO +0 -237
  20. gitcode_api-1.2.4/gitcode_api.egg-info/requires.txt +0 -1
  21. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/LICENSE +0 -0
  22. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/__init__.py +0 -0
  23. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/__main__.py +0 -0
  24. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_base_client.py +0 -0
  25. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_base_resource.py +0 -0
  26. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_cli_banner.py +1 -1
  27. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_client.py +0 -0
  28. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_exceptions.py +0 -0
  29. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/_models.py +0 -0
  30. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/__init__.py +0 -0
  31. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/_shared.py +0 -0
  32. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/account.py +0 -0
  33. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/collaboration.py +0 -0
  34. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/misc.py +0 -0
  35. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api/resources/repositories.py +0 -0
  36. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api.egg-info/dependency_links.txt +0 -0
  37. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api.egg-info/entry_points.txt +0 -0
  38. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/gitcode_api.egg-info/top_level.txt +0 -0
  39. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/setup.cfg +0 -0
  40. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_base_client.py +0 -0
  41. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_client.py +0 -0
  42. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_models.py +0 -0
  43. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_resources_account.py +0 -0
  44. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_resources_collaboration.py +0 -0
  45. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_resources_misc.py +0 -0
  46. {gitcode_api-1.2.4 → gitcode_api-1.2.6}/tests/test_resources_repositories.py +0 -0
@@ -0,0 +1,371 @@
1
+ Metadata-Version: 2.4
2
+ Name: gitcode-api
3
+ Version: 1.2.6
4
+ Summary: Easy to use Python SDK for the GitCode REST API. Providing builtin CLI tool, and optional LLM integration (MCP and OpenAI tool) for agents. Community-maintained.
5
+ Author-email: Hugo Huang <hugo@hugohuang.com>
6
+ License-Expression: MIT
7
+ Project-URL: changelog, https://gitcode-api.readthedocs.io/en/latest/changelog.html
8
+ Project-URL: documentation, https://gitcode-api.readthedocs.io
9
+ Project-URL: gitcode, https://gitcode.com/SushiNinja/GitCode-API
10
+ Project-URL: github, https://github.com/Trenza1ore/GitCode-API
11
+ Keywords: gitcode,git,devops,api,sdk,python,httpx,client,mcp,agent,fastmcp,llm,mcp client,mcp server,model context protocol
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Programming Language :: Python
14
+ Classifier: Programming Language :: Python :: Implementation :: CPython
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.9
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Software Development :: Version Control :: Git
24
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
25
+ Classifier: Intended Audience :: Developers
26
+ Classifier: Typing :: Typed
27
+ Requires-Python: <4,>=3.9
28
+ Description-Content-Type: text/markdown
29
+ License-File: LICENSE
30
+ Requires-Dist: httpx
31
+ Provides-Extra: mcp
32
+ Requires-Dist: fastmcp; python_version >= "3.10" and extra == "mcp"
33
+ Dynamic: license-file
34
+
35
+ # GitCode-API
36
+
37
+ [![PyPI - Version](https://img.shields.io/pypi/v/gitcode-api?link=https%3A%2F%2Fpypi.org%2Fproject%2Fgitcode-api%2F)](https://pypi.org/project/gitcode-api) [![PyPI Downloads](https://static.pepy.tech/personalized-badge/gitcode-api?period=total&units=INTERNATIONAL_SYSTEM&left_color=GRAY&right_color=RED&left_text=downloads)](https://pepy.tech/projects/gitcode-api) [![CodeFactor](https://www.codefactor.io/repository/github/trenza1ore/gitcode-api/badge)](https://www.codefactor.io/repository/github/trenza1ore/gitcode-api)
38
+ [![GitHub Badge](https://img.shields.io/badge/github-repo-blue?logo=github&link=https%3A%2F%2Fgithub.com%2FTrenza1ore%2FGitCode-API)](https://github.com/Trenza1ore/GitCode-API) [![GitCode Badge](https://img.shields.io/badge/gitcode-repo-brown?logo=gitcode&link=https%3A%2F%2Fgitcode.com%2FSushiNinja%2FGitCode-API)](https://gitcode.com/SushiNinja/GitCode-API)
39
+
40
+ [![Docs](https://img.shields.io/badge/%E6%96%87%E6%A1%A3-Docs-cyan?style=for-the-badge&logo=readthedocs&link=https%3A%2F%2Fgitcode-api.readthedocs.io%2Fen%2Flatest%2Findex.html)](https://gitcode-api.readthedocs.io) [![中文README](https://img.shields.io/badge/%E4%B8%AD%E6%96%87-README-brown?style=for-the-badge&logo=googledocs&link=README.zh.md)](README.zh.md)
41
+
42
+ `gitcode-api` is a community-maintained Python SDK for the GitCode REST API. It provides easy-to-use synchronous and asynchronous clients, repository-scoped helpers, and lightweight response models so you can work with GitCode from Python without hand-writing raw HTTP requests. The `gitcode_api.llm` module adds an OpenAI-style function tool and MCP service so agents can reuse the same resource-oriented API.
43
+
44
+ ## Why This Project
45
+
46
+ - Community project for developers who want a practical GitCode Python library.
47
+ - Sync and async clients with a consistent API surface.
48
+ - Resource groups such as `client.repos`, `client.pulls`, and `client.users`.
49
+ - Repository defaults via `owner=` and `repo=` on the client.
50
+ - Sphinx docs plus a mirrored GitCode REST API reference in `docs/`.
51
+
52
+ ## Installation
53
+
54
+ Install from PyPI:
55
+
56
+ ```bash
57
+ pip install -U gitcode-api
58
+ ```
59
+
60
+ ## Authentication
61
+
62
+ Pass `api_key=` directly, or set `GITCODE_ACCESS_TOKEN` in your environment:
63
+
64
+ ```bash
65
+ export GITCODE_ACCESS_TOKEN="your-token"
66
+ ```
67
+
68
+ If your token is stored in encrypted form, pass `decrypt=` to decode either an
69
+ encrypted `api_key=` value or an encrypted `GITCODE_ACCESS_TOKEN` value before
70
+ the client uses it.
71
+
72
+ ```python
73
+ from gitcode_api import GitCode
74
+ from trusted_library import decrypt_token
75
+
76
+ client = GitCode(
77
+ api_key="encrypted-token",
78
+ decrypt=decrypt_token,
79
+ )
80
+ ```
81
+
82
+ ## CLI
83
+
84
+ After installation, you can invoke the SDK directly from the command line:
85
+
86
+ ```bash
87
+ gitcode-api repos get --api-key "$GITCODE_ACCESS_TOKEN" --owner SushiNinja --repo GitCode-API
88
+ python -m gitcode_api pulls list --api-key "$GITCODE_ACCESS_TOKEN" --owner SushiNinja --repo GitCode-API --state open
89
+ ```
90
+
91
+ With `gitcode-api[mcp]` installed (Python 3.10+), you can start the bundled FastMCP server over stdio:
92
+
93
+ ```bash
94
+ gitcode-api serve --api-key "$GITCODE_ACCESS_TOKEN"
95
+ ```
96
+
97
+ Use `gitcode-api serve -h` for defaults such as `--owner`, `--repo`, and `--transport`.
98
+
99
+ Commands mirror the synchronous resource methods on `GitCode`, using the pattern
100
+ `gitcode-api <resource> <method> ...`. For methods that accept extra `**params`
101
+ or `**payload`, pass repeated `--set key=value` flags or `--set-json '{"key": "value"}'`.
102
+
103
+ ## Quick Start
104
+
105
+ ### Sync client
106
+
107
+ ```python
108
+ from gitcode_api import GitCode
109
+
110
+ client = GitCode(
111
+ owner="SushiNinja",
112
+ repo="GitCode-API",
113
+ )
114
+
115
+ repo = client.repos.get()
116
+ branches = client.branches.list(per_page=5)
117
+
118
+ print(repo.full_name)
119
+ for branch in branches:
120
+ print(branch.name)
121
+ ```
122
+
123
+ ### Async client
124
+
125
+ ```python
126
+ import asyncio
127
+ from gitcode_api import AsyncGitCode
128
+
129
+ async def main() -> None:
130
+ client = AsyncGitCode(owner="SushiNinja", repo="GitCode-API")
131
+ pulls = await client.pulls.list(state="open", per_page=20)
132
+ print(len(pulls))
133
+
134
+ asyncio.run(main())
135
+ ```
136
+
137
+ ### Context managers
138
+
139
+ `GitCode` and `AsyncGitCode` (and the lower-level `SyncAPIClient` / `AsyncAPIClient`) support `with` / `async with`. Leaving the block calls `close()` / `await close()` on the underlying client automatically, including a custom `http_client=` you passed in. `close()` also clears the LRU cache used by each resource group's `method_signature(...)` helper (see the [Available Resources](#available-resources) section).
140
+
141
+ ```python
142
+ from gitcode_api import GitCode
143
+
144
+ with GitCode(owner="SushiNinja", repo="GitCode-API") as client:
145
+ repo = client.repos.get()
146
+ print(repo.full_name)
147
+ ```
148
+
149
+ ```python
150
+ import asyncio
151
+ from gitcode_api import AsyncGitCode
152
+
153
+ async def main() -> None:
154
+ async with AsyncGitCode(owner="SushiNinja", repo="GitCode-API") as client:
155
+ pulls = await client.pulls.list(state="open", per_page=20)
156
+ print(len(pulls))
157
+
158
+ asyncio.run(main())
159
+ ```
160
+
161
+ ## Common Workflows
162
+
163
+ Create a pull request:
164
+
165
+ ```python
166
+ from gitcode_api import GitCode
167
+
168
+ client = GitCode(owner="SushiNinja", repo="GitCode-API")
169
+
170
+ pull = client.pulls.create(
171
+ title="Add feature",
172
+ head="feature-branch",
173
+ base="main",
174
+ body="Implements the new flow.",
175
+ )
176
+ print(pull.number)
177
+ ```
178
+
179
+ Get the authenticated user:
180
+
181
+ ```python
182
+ from gitcode_api import GitCode
183
+
184
+ client = GitCode()
185
+
186
+ user = client.users.me()
187
+ print(user.login)
188
+ ```
189
+
190
+ Search repositories:
191
+
192
+ ```python
193
+ from gitcode_api import GitCode
194
+
195
+ client = GitCode()
196
+
197
+ repos = client.search.repositories(q="sdk language:python", per_page=10)
198
+ for repo in repos:
199
+ print(repo.full_name)
200
+ ```
201
+
202
+ ## Available Resources
203
+
204
+ Both `GitCode` and `AsyncGitCode` expose:
205
+
206
+ - `repos` and `contents`
207
+ - `branches` and `commits`
208
+ - `issues` and `pulls`
209
+ - `labels`, `milestones`, and `members`
210
+ - `releases`, `tags`, and `webhooks`
211
+ - `users`, `orgs`, `search`, and `oauth`
212
+
213
+ Every resource group inherits a cached `methods` property from the shared resource base: a `tuple` of public callable names in stable SDK order (underscore-segment sort key, not plain A–Z on the full identifier). Private names and the introspection helpers `methods` and `method_signature` are omitted. For example, `client.pulls.methods` helps with discovery or tooling without reading the full manual list. For one method’s parameters and return type, call `client.pulls.method_signature("list_issues")` (a cached string from `inspect.signature`, with `gitcode_api._models.` stripped from annotations).
214
+
215
+ ## LLM tools and MCP
216
+
217
+ The `gitcode_api.llm` module exposes a single logical tool, **`gitcode_api_tool`**, that routes calls to sync or async SDK resources. Model-facing parameters match the JSON schema used by OpenAI-style function tools:
218
+
219
+ | Parameter | Role |
220
+ | --- | --- |
221
+ | `op_type` | Required. Resource group on the client (same names as `GitCode` attributes: `repos`, `pulls`, `issues`, and so on). |
222
+ | `action` | Method on that resource (for example `get`, `list`). Empty with `help` returns method discovery text. |
223
+ | `params` | Keyword arguments for the method as a JSON object; omitted or `null` is treated as `{}`. |
224
+ | `help` | When `true`, returns formatted help (available methods or a target signature) instead of performing a normal API call where applicable. |
225
+
226
+ Successful results are JSON-friendly (`APIObject.to_dict()`, base64-wrapped `bytes`, and similar). Failures are returned as objects with `"error": true` and a `"message"` string (HTTP and configuration errors include extra fields when available).
227
+
228
+ ### OpenAI tool (`GitCodeOpenAITool`)
229
+
230
+ No extra dependencies beyond the core package. Build a Chat Completions–style tool definition with `.tool` or `.to_dict()`, then invoke the same instance with the arguments above (sync) or configure async mode for `await`.
231
+
232
+ ```python
233
+ from gitcode_api.llm import GitCodeOpenAITool
234
+
235
+ tool = GitCodeOpenAITool(owner="SushiNinja", repo="GitCode-API")
236
+ tools_payload = [tool.tool] # or tool.to_dict() for a single entry
237
+
238
+ # Sync invocation (default)
239
+ result = tool("repos", "get", params={})
240
+
241
+ # Async client / awaitable wrapper
242
+ async_tool = GitCodeOpenAITool(owner="SushiNinja", repo="GitCode-API", async_mode=True)
243
+ # await async_tool("pulls", "list", params={"state": "open", "per_page": 5})
244
+ ```
245
+
246
+ You can pass the emitted tool definition into `chat.completions.create(...)`
247
+ and handle tool calls directly:
248
+
249
+ ```python
250
+ import json
251
+ from openai import OpenAI
252
+ from gitcode_api.llm import GitCodeOpenAITool
253
+
254
+ tools = {
255
+ "gitcode_api_tool": GitCodeOpenAITool(owner="SushiNinja", repo="GitCode-API"),
256
+ }
257
+ client = OpenAI(
258
+ api_key="your-openai-compatible-api-key",
259
+ base_url="https://your-openai-compatible-base-url/v1",
260
+ )
261
+
262
+ response = client.chat.completions.create(
263
+ model="gpt-4.1-mini",
264
+ messages=[{"role": "user", "content": "List the last 5 commits."}],
265
+ tools=[tools["gitcode_api_tool"].tool],
266
+ )
267
+
268
+ for tool_call in response.choices[0].message.tool_calls:
269
+ selected_tool = tools[tool_call.function.name]
270
+ print(f"Calling tool {tool_call.function.name}({tool_call.function.arguments}):")
271
+ print("---result---")
272
+ print(selected_tool(**json.loads(tool_call.function.arguments)))
273
+ print("---result---")
274
+ ```
275
+
276
+ Constructor options mirror `GitCode` / `AsyncGitCode`: `client=`, `async_client=`, `api_key=`, `owner=`, `repo=`, `base_url=`, `timeout=`, and `decrypt=`. For dict-driven setups that reserve the name `async`, you may pass `**{"async": True}` instead of `async_mode=True` (but not both).
277
+
278
+ ### MCP server and MCP tool (FastMCP)
279
+
280
+ [MCP](https://modelcontextprotocol.io) integration uses [FastMCP](https://github.com/jlowin/fastmcp). Install the optional extra (requires **Python 3.10+** because of the `fastmcp` dependency):
281
+
282
+ ```bash
283
+ pip install 'gitcode-api[mcp]'
284
+ ```
285
+
286
+ - **`create_mcp_server`** — builds a `FastMCP` instance with `gitcode_api_tool` already registered; optional `name=`, `tool=`, and extra keyword arguments are forwarded to `FastMCP(...)`.
287
+ - **`GitCodeMCP`** — thin wrapper that constructs that server and registers the tool; unknown attributes are delegated to the underlying `FastMCP` object (for example transport helpers exposed by your FastMCP version).
288
+ - **`create_mcp_gitcode_api_tool`** — returns the standalone async callable used as the tool body (for custom wiring).
289
+ - **`register_mcp_gitcode_api_tool`** — attaches that callable to an existing FastMCP-compatible object (`mcp.tool(...)` or `mcp.add_tool(...)`).
290
+
291
+ ```python
292
+ from gitcode_api.llm import create_mcp_server
293
+
294
+ mcp = create_mcp_server(name="GitCode API", owner="SushiNinja", repo="GitCode-API")
295
+ # Run or export the server using FastMCP’s API for your version (stdio, HTTP, etc.).
296
+ ```
297
+
298
+ The same server is available from the CLI as `gitcode-api serve` (see the [CLI](#cli) section).
299
+
300
+ To share auth or clients across tools, build `GitCodeLLMTool` once (`from gitcode_api.llm._tool import GitCodeLLMTool`) and pass it as `tool=` into `GitCodeMCP`, `create_mcp_server`, `register_mcp_gitcode_api_tool`, or `create_mcp_gitcode_api_tool`.
301
+
302
+ ## Examples
303
+
304
+ Runnable examples live in `examples/`:
305
+
306
+ - `get_current_user.py`
307
+ - `get_repository_overview.py`
308
+ - `list_pull_requests.py`
309
+ - `async_list_branches.py`
310
+
311
+ Example scripts load shared configuration from `examples/.env` using `python-dotenv`.
312
+
313
+ ```bash
314
+ uv run python examples/get_current_user.py
315
+ uv run python examples/get_repository_overview.py
316
+ uv run python examples/list_pull_requests.py
317
+ uv run python examples/async_list_branches.py
318
+ ```
319
+
320
+ See `examples/.env.example` for the expected variables.
321
+
322
+ ## Documentation
323
+
324
+ - Project docs entry: `docs/index.rst`
325
+ - SDK docs: `docs/sdk/index.rst`
326
+ - REST API mirror: `docs/rest_api/index.rst`
327
+
328
+ Build the docs locally from the repository root. The `docs` Makefile target removes stale `docs/_build` and `docs/sdk/generated` output, then runs Sphinx (via `uv`) with the `html`, `epub`, and `singlehtml` builders. Outputs land under `docs/_build/html/`, `docs/_build/epub/` (including `GitCodeAPI.epub`), and `docs/_build/singlehtml/`:
329
+
330
+ ```bash
331
+ make docs
332
+ ```
333
+
334
+ Other common targets from the repository root (after `uv sync --all-groups` so optional dependency groups are available):
335
+
336
+ - `make docs-clean` — remove `docs/_build` and `docs/sdk/generated` without rebuilding.
337
+ - `make format` — Ruff lint fixes, import sorting, and formatting.
338
+ - `make test` — install the package into the active environment and run pytest.
339
+ - `make docstring` — pydocstyle checks for `gitcode_api/`.
340
+ - `make binary` — PyInstaller one-file CLI under `dist/` (requires the `binary` group).
341
+
342
+ ## FAQ
343
+
344
+ ### SSL or corporate network errors ("self-signed certificate")
345
+
346
+ If GitCode HTTPS fails behind a corporate proxy or private PKI, point `httpx` at a CA bundle with `verify` (similar in spirit to `REQUESTS_CA_BUNDLE` for `requests`):
347
+
348
+ ```python
349
+ from gitcode_api import GitCode
350
+ from httpx import Client
351
+
352
+ with GitCode(
353
+ owner="SushiNinja",
354
+ repo="GitCode-API",
355
+ http_client=Client(verify="path/to/my/certificate.crt"),
356
+ ) as client:
357
+ repo = client.repos.get()
358
+ pulls = client.pulls.list(state="open", per_page=5)
359
+ ```
360
+
361
+ Use `httpx.AsyncClient(verify=...)` with `AsyncGitCode` for async code.
362
+
363
+ The OpenAI tool (`GitCodeOpenAITool`) and MCP helpers accept the same `client=` / `async_client=` arguments (or a shared `GitCodeLLMTool` via `tool=` with those clients set). Build `GitCode` / `AsyncGitCode` with your custom `http_client` once and pass it through so LLM tool calls reuse the same TLS settings.
364
+
365
+ ## Project Status
366
+
367
+ This is a community project and is still evolving. API coverage is already broad, but some endpoints and behaviors may continue to be refined as the SDK grows.
368
+
369
+ ## Contributing
370
+
371
+ Issues, bug reports, API coverage improvements, docs fixes, and pull requests are welcome. If you are using GitCode heavily and notice missing endpoints or awkward ergonomics, contributions are especially appreciated.