eddieland-canopy-github 0.14.0__py3-none-any.whl

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.
@@ -0,0 +1,91 @@
1
+ """Reading a repository's branches under canopy's prefix, and a search of a host's PRs.
2
+
3
+ What the `vcs` source reads beside the PRs it is named (DESIGN §30).
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from collections.abc import Mapping
9
+ from typing import Final
10
+
11
+ from canopy_github import Client, GitHubError, InputError, Repository, obj, pages, parse_pr, text
12
+
13
+ #: The most branches under the prefix a repository may have before it counts as not read: narrow the prefix.
14
+ MAX_BRANCHES: Final = 1000
15
+ #: The most results GitHub's search returns of one query; a query that matches more cannot be read to its end.
16
+ MAX_SEARCH: Final = 1000
17
+
18
+ _BRANCHES: Final = """
19
+ query Branches($owner: String!, $name: String!, $prefix: String!, $after: String) {
20
+ repository(owner: $owner, name: $name) {
21
+ refs(refPrefix: "refs/heads/", query: $prefix, first: 100, after: $after) {
22
+ pageInfo { hasNextPage endCursor }
23
+ nodes { name target { oid } }
24
+ }
25
+ }
26
+ }
27
+ """
28
+
29
+ _SEARCH: Final = """
30
+ query Search($query: String!, $after: String) {
31
+ search(query: $query, type: ISSUE, first: 100, after: $after) {
32
+ issueCount
33
+ pageInfo { hasNextPage endCursor }
34
+ nodes { ... on PullRequest { url } }
35
+ }
36
+ }
37
+ """
38
+
39
+
40
+ def branches(client: Client, repository: Repository, prefix: str) -> list[Mapping[str, object]]:
41
+ """A `branch` line for each branch of `repository` whose name starts with `prefix`, every page of them.
42
+
43
+ GitHub matches `query` against a ref's name more loosely than a prefix, so each name is held to it here.
44
+
45
+ Returns:
46
+ The lines, in GitHub's order.
47
+
48
+ Raises:
49
+ GitHubError: A page could not be read, GitHub has no such repository, or it has more than `MAX_BRANCHES`.
50
+ """
51
+ where = f"repository {repository.owner}/{repository.repo}"
52
+
53
+ def fetch(after: str | None) -> Mapping[str, object]:
54
+ variables = {"owner": repository.owner, "name": repository.repo, "prefix": prefix, "after": after}
55
+ return obj(obj(client.query(_BRANCHES, variables).get("repository"), where).get("refs"), "refs")
56
+
57
+ lines: list[Mapping[str, object]] = []
58
+ for node in pages(fetch):
59
+ name, sha = text(node.get("name")), text(obj(node.get("target"), "target").get("oid"))
60
+ if name is None or sha is None or not name.startswith(prefix):
61
+ continue
62
+ if len(lines) == MAX_BRANCHES:
63
+ raise GitHubError(f"it has more than {MAX_BRANCHES} branches under {prefix}", certain=True)
64
+ lines.append({"kind": "branch", "repository": repository.name, "name": name, "head_sha": sha})
65
+ return lines
66
+
67
+
68
+ def search(client: Client, query: str) -> list[str]:
69
+ """The PRs a search in GitHub's own syntax returns, to its last page; an issue it returns is passed over.
70
+
71
+ Returns:
72
+ Their URLs, each once, in the search's order.
73
+
74
+ Raises:
75
+ GitHubError: A page could not be read, or the search matches more than `MAX_SEARCH`, which GitHub cuts short.
76
+ """
77
+
78
+ def fetch(after: str | None) -> Mapping[str, object]:
79
+ found = obj(client.query(_SEARCH, {"query": query, "after": after}).get("search"), "search")
80
+ total = found.get("issueCount")
81
+ if isinstance(total, int) and total > MAX_SEARCH:
82
+ raise GitHubError(f"it matches {total}, more than the {MAX_SEARCH} GitHub returns", certain=True)
83
+ return found
84
+
85
+ urls: dict[str, None] = {}
86
+ for node in pages(fetch):
87
+ try:
88
+ urls.setdefault(parse_pr(node.get("url")).url)
89
+ except InputError:
90
+ continue
91
+ return list(urls)
@@ -0,0 +1,115 @@
1
+ Metadata-Version: 2.5
2
+ Name: eddieland-canopy-github
3
+ Version: 0.14.0
4
+ Summary: canopy's GitHub programs: read PRs, branches, and issues into canopy; post what the operator approved.
5
+ Project-URL: Homepage, https://github.com/eddieland/canopy
6
+ Project-URL: Repository, https://github.com/eddieland/canopy/tree/main/packages/canopy-github
7
+ Project-URL: Issues, https://github.com/eddieland/canopy/issues
8
+ Author: eddieland
9
+ License-Expression: MIT
10
+ Keywords: canopy,github,graphql,review
11
+ Classifier: Development Status :: 2 - Pre-Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: MacOS
15
+ Classifier: Operating System :: POSIX :: Linux
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Software Development :: Version Control :: Git
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: click>=8.5.1
22
+ Requires-Dist: eddieland-canopy-http==0.14.0
23
+ Description-Content-Type: text/markdown
24
+
25
+ # canopy-github
26
+
27
+ canopy's GitHub programs (DESIGN §29, §30): `canopy-connect-github` reads PRs and branches into canopy, or a
28
+ repository's issues as its tracker, and `canopy-post-github` posts the replies and reviews the operator approved, word
29
+ for word. canopy runs them as commands
30
+ and never imports them, so the harness holds no token and opens no connection (TRUST_MODEL T2, D003).
31
+
32
+ They are installed beside canopy: `uv tool install` puts both on `PATH`. Each reads the token `gh` holds for the host
33
+ the PR's URL, the repository's clone URL, or `--repo` names, when it runs, and talks to that host's GraphQL API, so GitHub
34
+ Enterprise works as github.com does. Run `gh auth login` for each host. They take no token from argv or canopy's
35
+ config, only the one `gh` answers for that host, once `gh auth status` names it; `gh` itself answers `GH_TOKEN` or
36
+ `GH_ENTERPRISE_TOKEN` when one is set in the environment canopy runs them in.
37
+
38
+ ## Config
39
+
40
+ Unset, canopy uses both for its `vcs` source. Naming them, in `~/.config/canopy/config.toml`, looks like this; with
41
+ GitHub Issues as the tracker, a `tracker` source given the repository reads its issues, and its `post_cmd` lets canopy
42
+ post a ticket's drafts to them too ([examples/config.toml](../../examples/config.toml)):
43
+
44
+ ```toml
45
+ [connect.sources.github]
46
+ kind = "vcs"
47
+ cmd = "canopy-connect-github"
48
+ post_cmd = "canopy-post-github"
49
+
50
+ [connect.sources.issues]
51
+ kind = "tracker"
52
+ cmd = "canopy-connect-github --repo acme/api"
53
+ post_cmd = "canopy-post-github"
54
+ ```
55
+
56
+ ## `canopy-connect-github`
57
+
58
+ Reads, and writes nothing to GitHub. On stdin, one JSON object naming what to observe, each key optional, but
59
+ `branch_prefix` is required once `repositories` names one:
60
+
61
+ ```json
62
+ {"prs": ["https://github.com/acme/api/pull/7"],
63
+ "repositories": [{"name": "api", "url": "git@github.com:acme/api.git"}], "branch_prefix": "canopy/",
64
+ "review_query": "repo:acme/api is:pr is:open author:@me"}
65
+ ```
66
+
67
+ On stdout, one JSON line per observation, in the shapes [DESIGN §30](../../docs/design/CONNECTORS.md) gives: for each
68
+ PR, a `pr` line, a `check` line per check at its head, and a `review` line per review, review comment, and comment on
69
+ its conversation; for each repository, a `branch` line per branch under the prefix; last, an `end` line naming what was
70
+ read whole. What could not be read whole prints only its error on stderr, so a cut-off read never looks like a PR with
71
+ no checks. A PR's or a review's author is its login, a bot's ending `[bot]` as in `trust.trusted_reviewers`.
72
+
73
+ Exit codes: 0 everything was read; 1 something was not, as the `end` line says; 2 stdin or the command line is not
74
+ usable, and nothing was read.
75
+
76
+ ## `canopy-connect-github --repo OWNER/NAME`
77
+
78
+ The `tracker` source for that repository's GitHub Issues (`HOST/OWNER/NAME` for GitHub Enterprise): a ticket's key is
79
+ `GH-` and its issue's number, so issue 12 is `GH-12`, as the `tracker-github` skill has it. It reads, and writes nothing
80
+ to GitHub. On stdin, what canopy asks any tracker source, each key optional; the query is GitHub's search syntax:
81
+
82
+ ```json
83
+ {"query": "repo:acme/api is:issue is:open label:agent", "tickets": [{"key": "GH-12", "comments_since": "2026-10-18T10:00:00Z"}]}
84
+ ```
85
+
86
+ On stdout, as `canopy-connect-jira` prints Jira's: for each ticket asked, a `ticket` line, each label with when its
87
+ timeline last added it, and a `comment` line per comment since `comments_since`; a `ticket` line for each open issue
88
+ the query returns, PRs passed over, and for each blocker one of them names; last, an `end` line naming what was read
89
+ whole. A blocker is an issue GitHub's issue dependencies say blocks it, or one a line of its body names,
90
+ `Blocked by #N`. A key names an issue of this repository alone, so a query that returns another repository's issue is
91
+ not read, and a ticket an open issue of another repository blocks is not read whole: it stays stale, and canopy
92
+ dispatches nothing on it. A comment's author is its login, a bot's ending `[bot]`.
93
+
94
+ The same exit codes, 2 also when `--repo` is not a repository's name or `gh` holds no token for its host.
95
+
96
+ ## `canopy-post-github`
97
+
98
+ Posts one PR's drafts as one review, or one ticket's as one issue comment. A reply's reaction is added on its own,
99
+ before the review, since GitHub has no pending reaction, so drafts that are only reactions open no review. On stdin,
100
+ one JSON object:
101
+
102
+ - a PR's: `{"pr": URL, "body": TEXT | null, "replies": [{"comment": ID, "text": TEXT | null, "reaction": REACTION |
103
+ null}, ...], "delete_pending": ID | null}`, with `commit` and `comments` for a review of a PR canopy reviewed (§34),
104
+ and `"submit": false` to leave the review pending for the operator to submit on GitHub;
105
+ - an issue's: `{"issue": URL, "ticket": KEY, "body": TEXT}`.
106
+
107
+ On stdout, one JSON line for each thing it posts, then `end`, which says whether it submitted. No `end` line means the
108
+ outcome is unknown: the submit may have reached GitHub. The module docstring in
109
+ [post.py](src/canopy_github/post.py) holds every field and line.
110
+
111
+ It never approves, requests changes, resolves a thread, or deletes a review but the pending one it is handed, or one it
112
+ opened and could not fill.
113
+
114
+ Exit codes: 0 posted; 1 GitHub refused, or the outcome is unknown, as the `end` line says; 2 stdin or the command line
115
+ is not usable, `gh` holds no token for the host, or a reply names a comment with no thread, and nothing was posted.
@@ -0,0 +1,10 @@
1
+ canopy_github/__init__.py,sha256=4t-k3qE2Oo7-UsZV8pB_vImV-EnedGEhEK7NSmCHwS8,19433
2
+ canopy_github/connect.py,sha256=W4FQDLCA9A34oyacLwOaQAX_iFIoLGZvSPj_ALlXq7g,11379
3
+ canopy_github/issues.py,sha256=Jg0OHfYA_S85-AqpwepN-6d2bnaF0lNPAUkOlL1GmEc,27977
4
+ canopy_github/post.py,sha256=NrXOIPm3hXpvNKnXWDMHj7AMN32RWxDc94uStQYFYWo,38013
5
+ canopy_github/read.py,sha256=uQqleFTdbEdGMjVqHCF1beTOO0HYjnAAEjFu08xdL5g,25075
6
+ canopy_github/repository.py,sha256=yGWBNv5JwA_yssOI69ryTKAOcH54VNxtVUzmKuqnXr0,3525
7
+ eddieland_canopy_github-0.14.0.dist-info/METADATA,sha256=dbpXUFvEEH7cF9xkbUDvCtR6h-i77NlZBZbm1WliksE,6439
8
+ eddieland_canopy_github-0.14.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
9
+ eddieland_canopy_github-0.14.0.dist-info/entry_points.txt,sha256=Q0agqla9XqSpQG7ryvB2GpoTA1zesavQBHxXecIYkTs,114
10
+ eddieland_canopy_github-0.14.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ canopy-connect-github = canopy_github.connect:main
3
+ canopy-post-github = canopy_github.post:main