claude-dev-env 4.2.0 → 4.3.0

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 (78) hide show
  1. package/.agents/skills/_shared/pr-loop/scripts/durable_post_lint.py +242 -0
  2. package/.agents/skills/_shared/pr-loop/scripts/skills_pr_loop_constants/durable_post_lint_constants.py +106 -0
  3. package/.agents/skills/_shared/pr-loop/scripts/test_durable_post_lint.py +332 -0
  4. package/.agents/skills/_shared/pr-loop/scripts/test_durable_post_lint_constants.py +25 -0
  5. package/.agents/skills/issue-tracker/SKILL.md +6 -0
  6. package/.agents/skills/pull-request/SKILL.md +218 -0
  7. package/.agents/skills/pull-request/reference/publication-tasks.md +18 -0
  8. package/.agents/skills/pull-request/scripts/github_pr_command_constants/__init__.py +1 -0
  9. package/.agents/skills/pull-request/scripts/github_pr_command_constants/config/__init__.py +1 -0
  10. package/.agents/skills/pull-request/scripts/github_pr_command_constants/config/constants.py +61 -0
  11. package/.agents/skills/pull-request/scripts/pull_request.py +276 -0
  12. package/.agents/skills/pull-request/scripts/recover_legacy_author.py +251 -0
  13. package/.agents/skills/pull-request/scripts/test_github_pr_command_constants.py +49 -0
  14. package/.agents/skills/pull-request/scripts/test_pull_request.py +398 -0
  15. package/.agents/skills/pull-request/scripts/test_recover_legacy_author.py +221 -0
  16. package/.agents/skills/test-runner/SKILL.md +69 -0
  17. package/.agents/skills/test-runner/scripts/preflight_checks.py +365 -0
  18. package/.agents/skills/test-runner/scripts/run_tests.py +316 -0
  19. package/.agents/skills/test-runner/scripts/test_preflight_checks.py +185 -0
  20. package/.agents/skills/test-runner/scripts/test_run_tests.py +281 -0
  21. package/.agents/skills/test-runner/scripts/test_runner_constants/__init__.py +1 -0
  22. package/.agents/skills/test-runner/scripts/test_runner_constants/config/__init__.py +1 -0
  23. package/.agents/skills/test-runner/scripts/test_runner_constants/config/constants.py +96 -0
  24. package/.agents/skills-archived/pr-loop-cloud-transport/reference/identity-and-hooks.md +1 -1
  25. package/bin/ever-shipped-skills.mjs +2 -0
  26. package/bin/ever-shipped-skills.test.mjs +8 -0
  27. package/bin/install.mjs +9 -1
  28. package/bin/install.prune.test.mjs +90 -0
  29. package/bin/install.test.mjs +24 -7
  30. package/hooks/blocking/pii_prevention_blocker.py +32 -51
  31. package/hooks/blocking/test_bash_post_call_dispatcher.py +5 -28
  32. package/hooks/blocking/test_bash_pre_tool_use_dispatcher.py +1 -4
  33. package/hooks/blocking/test_code_rules_command_dispatch.py +2 -2
  34. package/hooks/blocking/test_pii_prevention_blocker.py +37 -89
  35. package/hooks/gh_post_body_texts.py +195 -0
  36. package/hooks/hooks.json +0 -15
  37. package/hooks/hooks_constants/bash_post_call_dispatcher_constants.py +0 -1
  38. package/hooks/hooks_constants/bash_pre_tool_use_dispatcher_constants.py +0 -8
  39. package/hooks/hooks_constants/gh_post_body_texts_constants.py +12 -0
  40. package/hooks/hooks_constants/test_bash_post_call_dispatcher_constants.py +0 -7
  41. package/hooks/hooks_constants/test_bash_pre_tool_use_dispatcher_constants.py +0 -7
  42. package/hooks/hooks_constants/test_failure_recorder_constants.py +3 -3
  43. package/hooks/hooks_constants/test_gh_post_body_texts_constants.py +19 -0
  44. package/hooks/observability/test_failure_recorder.py +1 -1
  45. package/hooks/pyproject.toml +1 -1
  46. package/hooks/test_gh_post_body_texts.py +79 -0
  47. package/package.json +1 -1
  48. package/rules/durable-post-artifacts.md +31 -20
  49. package/rules/gh-cli-conventions.md +23 -13
  50. package/rules/git-workflow.md +21 -8
  51. package/scripts/policy_lint/adapter_configuration.py +53 -12
  52. package/scripts/repository_checks/config/constants.py +0 -5
  53. package/scripts/tests/test_adapter_configuration.py +54 -0
  54. package/hooks/_gh_pr_author_swap_utils.py +0 -1225
  55. package/hooks/blocking/conventional_pr_title_gate.py +0 -443
  56. package/hooks/blocking/gh_body_arg_blocker.py +0 -176
  57. package/hooks/blocking/gh_pr_author_enforcer.py +0 -487
  58. package/hooks/blocking/gh_pr_author_restore.py +0 -105
  59. package/hooks/blocking/pr_description_writer_gate.py +0 -256
  60. package/hooks/blocking/test_conventional_pr_title_gate.py +0 -640
  61. package/hooks/blocking/test_gh_body_arg_blocker.py +0 -441
  62. package/hooks/blocking/test_gh_pr_author_enforcer.py +0 -1166
  63. package/hooks/blocking/test_gh_pr_author_restore.py +0 -513
  64. package/hooks/blocking/test_gh_pr_author_swap_utils.py +0 -910
  65. package/hooks/blocking/test_pr_description_writer_gate.py +0 -234
  66. package/hooks/blocking/test_preflight_check.py +0 -343
  67. package/hooks/blocking/test_test_preflight_check.py +0 -80
  68. package/hooks/blocking/test_volatile_path_in_post_blocker.py +0 -322
  69. package/hooks/blocking/volatile_path_in_post_blocker.py +0 -461
  70. package/hooks/hooks_constants/conventional_pr_title_gate_constants.py +0 -58
  71. package/hooks/hooks_constants/gh_pr_author_swap_constants.py +0 -76
  72. package/hooks/hooks_constants/pr_description_writer_gate_constants.py +0 -73
  73. package/hooks/hooks_constants/volatile_path_in_post_blocker_constants.py +0 -55
  74. package/hooks/observability/pr_description_writer_spawn_tracker.py +0 -113
  75. package/hooks/observability/test_pr_description_writer_spawn_tracker.py +0 -150
  76. package/hooks/session/gh_pr_author_session_cleanup.py +0 -175
  77. package/hooks/session/test_gh_pr_author_session_cleanup.py +0 -576
  78. package/hooks/test__gh_pr_author_swap_utils.py +0 -333
@@ -60,6 +60,12 @@ Search open and closed issues on the target repo before create. Open twin -> upd
60
60
 
61
61
  Prefer GitHub MCP. Fall back to `gh` on the same REST endpoints. Full map and `.id` rule: [reference/operation-matrix.md](reference/operation-matrix.md).
62
62
 
63
+ Before issue creation, a full issue body edit, or an issue comment, write the
64
+ body to a UTF-8 file and run
65
+ `../_shared/pr-loop/scripts/durable_post_lint.py`. Use `issue-create`,
66
+ `issue-edit`, or `issue-comment` as the action. Send the server write only
67
+ after exit code `0`.
68
+
63
69
  ## Handoff input
64
70
 
65
71
  Optional issue-candidate JSON from orchestrator or closeout. Full schema and consume path: [reference/handoff-schema.md](reference/handoff-schema.md).
@@ -0,0 +1,218 @@
1
+ ---
2
+ name: pull-request
3
+ description: >-
4
+ Validate and publish GitHub pull request actions. Triggers: create PR, open
5
+ pull request, edit PR, update PR body, comment on PR, review PR, publish a
6
+ draft PR, scoped GitHub author.
7
+ ---
8
+
9
+ # Pull request
10
+
11
+ ## Contents
12
+
13
+ - [Principle](#principle)
14
+ - [When this applies](#when-this-applies)
15
+ - [Constraints](#constraints)
16
+ - [Dependencies](#dependencies)
17
+ - [Sub-skills](#sub-skills)
18
+ - [Task seeding](#task-seeding)
19
+ - [Process](#process)
20
+ - [Exit handling](#exit-handling)
21
+ - [Examples](#examples)
22
+ - [Gotchas](#gotchas)
23
+ - [File index](#file-index)
24
+ - [Folder map](#folder-map)
25
+
26
+ ## Principle
27
+
28
+ Publish one GitHub pull request action from validated local files and a
29
+ process-local author value. Keep the parent environment unchanged.
30
+
31
+ ## When this applies
32
+
33
+ Use this skill for one pull request action:
34
+
35
+ - Create a draft pull request.
36
+ - Edit a pull request title or body.
37
+ - Add a pull request comment.
38
+ - Submit a pull request review.
39
+ - Recover one selected legacy author record before the action.
40
+
41
+ Send issue create, edit, and comment work to `issue-tracker`. Send review
42
+ convergence to `pr-cleanup` or `autoconverge`. Send commits to
43
+ `source-command-commit`.
44
+
45
+ Require one repository and one action target. Stop before author lookup or
46
+ network work when the repository, pull request, action, author, or required
47
+ local file is missing or ambiguous.
48
+
49
+ Use these exact refusal responses when the request belongs to another skill:
50
+
51
+ - Issue action: `Use issue-tracker for this GitHub issue action.`
52
+ - Commit or push: `Use source-command-commit for this commit or push.`
53
+ - Missing target: `Provide one repository and one pull request action target.`
54
+
55
+ ## Constraints
56
+
57
+ - Run local validation and privacy checks before account lookup or network work.
58
+ - Use a body file. Do not send inline body text through the shell.
59
+ - Keep `GITHUB_DEFAULT_ACCOUNT`, the parent environment, and global GitHub CLI
60
+ state unchanged.
61
+ - Run one create, edit, comment, review, or recovery action per command.
62
+ - Use global account switching only for one explicitly selected legacy record.
63
+ - Read back the remote state before reporting success.
64
+
65
+ ## Dependencies
66
+
67
+ The commands use Python 3.11 or later and the Python standard library. GitHub
68
+ actions also require an authenticated `gh` executable. The linter and artifact
69
+ uploader ship in this package. Set `GITHUB_DEFAULT_ACCOUNT` to one exact GitHub
70
+ CLI login when the action needs a selected author. Leave it unset to use the
71
+ current process environment.
72
+
73
+ ## Sub-skills
74
+
75
+ | Skill | When | Produces | If missing |
76
+ |---|---|---|---|
77
+ | `pr-description-writer` | Before create or a full body rewrite | A reviewed title and body file | Stop the create or rewrite and report that authoring is required |
78
+ | `pr-title-description` | Optional title and body review | Review findings for the writer's output | Continue with the required writer output |
79
+ | `privacy-hygiene` | Before any durable GitHub post | A clean body and repository privacy sweep | Stop before publication and report the missing gate |
80
+ | `issue-tracker` | Issue create, edit, or comment requests | Issue state and issue URLs | Route the request there. |
81
+ | `pr-cleanup` | Placement, naming, sizing, or cleanup convergence | Cleanup findings or a focused PR boundary | Route the request there. |
82
+ | `autoconverge` | Autonomous PR review and fix loops | A converged draft or ready-state decision | Route the request there. |
83
+ | `source-command-commit` | Commit or push requests | A verified commit or pushed branch | Route the request there. |
84
+
85
+ ## Task seeding
86
+
87
+ At the start of the work, register every item in
88
+ `reference/publication-tasks.md` through `TaskCreate`, `TodoWrite`, or the host
89
+ task equivalent when one is available. If no task tool is available, use the
90
+ catalog and the running work ledger. Mark each task with `PASS`, `FAIL` plus
91
+ file and line evidence, or `N/A` plus a reason for a conditional task.
92
+
93
+ ## Process
94
+
95
+ ### 1. Scope the action
96
+
97
+ Resolve the repository, action, and selected GitHub author. For create, resolve
98
+ the source branch, base, and head. For other actions, resolve one existing pull
99
+ request target. Create actions publish drafts. Record the target before any write.
100
+
101
+ ### 2. Author the title and body
102
+
103
+ For create and full body rewrite, invoke the installed
104
+ `pr-description-writer`. Require its title and body file as the authoring
105
+ output. `pr-title-description` may review that output. A comment or review may
106
+ use a supplied body file when it does not rewrite the pull request description.
107
+
108
+ ### 3. Run the local linter
109
+
110
+ Run `_shared/pr-loop/scripts/durable_post_lint.py` with the matching action and
111
+ title or body file. Use `pr-create`, `pr-edit`, `pr-comment`, or `pr-review`.
112
+ The linter owns the action-specific body, title, and volatile-path rules.
113
+
114
+ Exit code `0` continues to the next gate. Any non-zero exit stops the action. Fix
115
+ the named local input and rerun the linter. Do not look up the author or make a
116
+ network call while validation fails.
117
+
118
+ ### 4. Run privacy and artifact checks
119
+
120
+ Invoke `privacy-hygiene` before the GitHub post. Upload binary evidence with
121
+ `scripts/gh_artifact_upload.py` and replace local artifact references with
122
+ permanent URLs. Rerun the linter after changing the body.
123
+
124
+ ### 5. Resolve the process-local author
125
+
126
+ Run `.agents/skills/pull-request/scripts/pull_request.py` with one structured
127
+ action and the selected author. The command runs the linter again, resolves
128
+ the author value, and passes it only to the child `gh` process. It never
129
+ changes the parent environment or calls `gh auth switch`.
130
+
131
+ If the selected author has no usable value, stop and report the account lookup
132
+ failure. Never print the value.
133
+
134
+ ### 6. Recover a selected legacy record
135
+
136
+ Run `.agents/skills/pull-request/scripts/recover_legacy_author.py
137
+ <exact-state-file> --confirm-inactive` only when the user selects one legacy
138
+ state file. The command checks the record's age, secure file metadata, and
139
+ contents. The confirmation flag records that the caller verified inactivity.
140
+ Leave every other record untouched. Delete the selected record only after a
141
+ successful restore. Mark this step `N/A` when no legacy state file is selected.
142
+
143
+ ### 7. Run the action
144
+
145
+ Run `pull_request.py` for exactly one of `create`, `edit`, `comment`, or
146
+ `review`. Supply the validated body file path. Treat a non-zero exit as a
147
+ failed publication and preserve the local inputs for retry.
148
+
149
+ ### 8. Read back the remote state
150
+
151
+ Use the same selected process-local author for a read-only remote readback.
152
+ Confirm the pull request URL and number, title, body, head SHA, and draft state.
153
+ For comments and reviews, confirm the new remote entry. Report artifact URLs
154
+ without exposing author values.
155
+
156
+ ## Exit handling
157
+
158
+ Stop before network work when authoring, local validation, privacy, or author
159
+ selection fails. A failed action preserves its body and any recovery record.
160
+ Report the generic error and the next required input. A successful action ends
161
+ only after remote readback proves the requested state.
162
+
163
+ ## Examples
164
+
165
+ Create example: the writer produces `pr-body.md`. The linter exits `0` for
166
+ `pr-create`. `pull_request.py create` publishes one draft pull request. The
167
+ readback matches the title, body, head SHA, and draft state.
168
+
169
+ Rejected comment example: the comment body names a worktree file. The linter
170
+ returns a non-zero exit. No account lookup or GitHub request runs. Replace the
171
+ path with inline text or a permanent artifact URL, then rerun.
172
+
173
+ Recovery example: one old record remains after an interrupted legacy account
174
+ swap. The user selects that exact file and confirms its session is inactive.
175
+ The recovery command restores the named account and deletes only that record.
176
+
177
+ ## Gotchas
178
+
179
+ - Relative body-file paths depend on the caller directory. Use an absolute path
180
+ when another process or worktree starts the command.
181
+ - A record older than 30 minutes can still belong to a live session. Require
182
+ `--confirm-inactive` before recovery.
183
+ - Normal pull request actions never call `gh auth switch`. If that command
184
+ appears outside explicit recovery, stop.
185
+ - GitHub CLI output can contain account data. The command captures account
186
+ lookup and recovery output and prints only generic errors.
187
+
188
+ ## File index
189
+
190
+ | Path | Purpose |
191
+ |---|---|
192
+ | `SKILL.md` | Hub for pull request publication, validation, author selection, recovery, and readback |
193
+ | `reference/publication-tasks.md` | Ordered tasks for every publication gate |
194
+ | `scripts/pull_request.py` | Command for author selection and create, edit, comment, and review actions |
195
+ | `scripts/github_pr_command_constants/` | Constants for pull request actions, recovery, exit codes, and generic messages |
196
+ | `scripts/test_pull_request.py` | Tests for `pull_request.py` |
197
+ | `scripts/recover_legacy_author.py` | Command for one selected legacy author record |
198
+ | `scripts/test_github_pr_command_constants.py` | Tests for the command and recovery constants |
199
+ | `scripts/test_recover_legacy_author.py` | Tests for `recover_legacy_author.py` |
200
+ | `_shared/pr-loop/scripts/durable_post_lint.py` | Shared action-aware title, body, and path validator |
201
+ | `_shared/pr-loop/scripts/test_durable_post_lint.py` | Tests for the shared validator |
202
+ | `scripts/gh_artifact_upload.py` | Helper for permanent GitHub binary evidence URLs |
203
+ | `scripts/tests/test_gh_artifact_upload.py` | Tests for binary evidence upload |
204
+
205
+ ## Folder map
206
+
207
+ ```text
208
+ pull-request/
209
+ ├── SKILL.md
210
+ ├── reference/
211
+ │ └── publication-tasks.md
212
+ └── scripts/
213
+ ├── pull_request.py
214
+ ├── github_pr_command_constants/
215
+ ├── recover_legacy_author.py
216
+ ├── test_pull_request.py
217
+ └── test_recover_legacy_author.py
218
+ ```
@@ -0,0 +1,18 @@
1
+ # Pull request publication tasks
2
+
3
+ Register each line as one session task before starting the process. Complete
4
+ each task with `PASS`, `FAIL` plus file and line evidence, or `N/A` plus a
5
+ reason when the task is conditional. This file is a task seed catalog, not a
6
+ markdown progress board.
7
+
8
+ 1. Resolve one repository, action, and selected author. For create, resolve the source branch, base, and head. For other actions, resolve one existing pull request target. Create actions publish drafts.
9
+ 2. Invoke the required `pr-description-writer` for create or full body rewrite, then record its title and body file. `pr-title-description` may assist. Mark `N/A` for comment or review bodies that do not rewrite the pull request description.
10
+ 3. Validate every title, body, artifact, and local path input. Reject inline body input.
11
+ 4. Run `_shared/pr-loop/scripts/durable_post_lint.py` for the matching action and record exit code `0` before credential lookup or network work.
12
+ 5. Run `privacy-hygiene` before the GitHub post and record its clean result.
13
+ 6. Upload binary evidence through `scripts/gh_artifact_upload.py`, replace local references with permanent URLs, and rerun the shared body linter. Mark `N/A` when no binary evidence exists.
14
+ 7. Resolve the selected author in the child process environment and prove that the parent environment and global `gh` account remain unchanged.
15
+ 8. Run `.agents/skills/pull-request/scripts/pull_request.py` for exactly one action: `create`, `edit`, `comment`, or `review`.
16
+ 9. When the user selects a legacy state file, run `recover_legacy_author.py <exact-state-file> --confirm-inactive` for exactly that record. Preserve every other record and delete the selected record only after successful restore. Mark `N/A` otherwise.
17
+ 10. Read back the remote pull request title, body, head SHA, draft state, and requested comment or review, then record the URL and number without credentials.
18
+ 11. Mark every session task complete with evidence and report the action, remote URL, readback result, and any remaining blocker.
@@ -0,0 +1 @@
1
+ """Constants for the explicit GitHub pull request command."""
@@ -0,0 +1 @@
1
+ """Configuration constants for the explicit GitHub pull request command."""
@@ -0,0 +1,61 @@
1
+ """Configuration constants for process-local GitHub pull request actions."""
2
+
3
+ import re
4
+
5
+ ACTION_COMMENT = "comment"
6
+ ACTION_CREATE = "create"
7
+ ACTION_EDIT = "edit"
8
+ ACTION_REVIEW = "review"
9
+
10
+ LINTER_ACTION_COMMENT = "pr-comment"
11
+ LINTER_ACTION_CREATE = "pr-create"
12
+ LINTER_ACTION_EDIT = "pr-edit"
13
+ LINTER_ACTION_REVIEW = "pr-review"
14
+
15
+ REVIEW_EVENT_APPROVE = "approve"
16
+ REVIEW_EVENT_COMMENT = "comment"
17
+ REVIEW_EVENT_REQUEST_CHANGES = "request-changes"
18
+ ALL_REVIEW_FLAGS_BY_EVENT: dict[str, str] = {
19
+ REVIEW_EVENT_APPROVE: "--approve",
20
+ REVIEW_EVENT_COMMENT: "--comment",
21
+ REVIEW_EVENT_REQUEST_CHANGES: "--request-changes",
22
+ }
23
+
24
+ ALL_LINTER_ACTIONS_BY_COMMAND: dict[str, str] = {
25
+ ACTION_COMMENT: LINTER_ACTION_COMMENT,
26
+ ACTION_CREATE: LINTER_ACTION_CREATE,
27
+ ACTION_EDIT: LINTER_ACTION_EDIT,
28
+ ACTION_REVIEW: LINTER_ACTION_REVIEW,
29
+ }
30
+
31
+ SELECTED_ACCOUNT_ENVIRONMENT_KEY = "GITHUB_DEFAULT_ACCOUNT"
32
+
33
+ ACCOUNT_LOOKUP_FAILED_MESSAGE = "error: GitHub account lookup failed\n"
34
+ ACCOUNT_LOOKUP_EMPTY_MESSAGE = "error: GitHub account lookup returned no value\n"
35
+ ACTION_FAILED_MESSAGE = "error: GitHub action failed\n"
36
+ ACCOUNT_LOOKUP_FAILURE_EXIT_CODE = 1
37
+ PACKAGE_ROOT_PARENT_INDEX = 4
38
+ SKILLS_DIRECTORY_PARENT_INDEX = 2
39
+
40
+ ALL_GH_AUTH_SWITCH_COMMAND_HEAD: tuple[str, ...] = (
41
+ "gh",
42
+ "auth",
43
+ "switch",
44
+ "--user",
45
+ )
46
+ LEGACY_RECORD_ACTIVE_MESSAGE = "error: selected legacy state record is active\n"
47
+ LEGACY_RECORD_CHANGED_MESSAGE = "error: selected legacy state record changed\n"
48
+ LEGACY_RECORD_CONFIRMATION_MESSAGE = "error: confirm inactive legacy state\n"
49
+ LEGACY_RECORD_REJECTED_MESSAGE = "error: selected legacy state record is invalid\n"
50
+ LEGACY_RESTORE_FAILED_MESSAGE = "error: GitHub account restore failed\n"
51
+ LEGACY_RECORD_INACTIVE_CONFIRMATION_FLAG = "--confirm-inactive"
52
+ LEGACY_RECORD_ORIGINAL_ACCOUNT_KEY = "original_account"
53
+ LEGACY_RECORD_PERMISSION_MODE = 0o600
54
+ LEGACY_RECORD_STALE_AGE_SECONDS = 1800
55
+ LEGACY_RECORD_NAME_PATTERN: re.Pattern[str] = re.compile(
56
+ r"^gh_pr_author_swap_[A-Za-z0-9_-]+\.json$"
57
+ )
58
+ RECOVERY_CLEAN_EXIT_CODE = 0
59
+ RECOVERY_FAILED_EXIT_CODE = 1
60
+ RECOVERY_REJECTED_EXIT_CODE = 2
61
+ RECOVERY_UNRESOLVED_EXIT_CODE = 3
@@ -0,0 +1,276 @@
1
+ """Run validated GitHub pull request actions with process-local account selection."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import importlib
7
+ import os
8
+ import subprocess
9
+ import sys
10
+ from collections.abc import Callable, Mapping, Sequence
11
+ from pathlib import Path
12
+
13
+ from github_pr_command_constants.config.constants import (
14
+ ACCOUNT_LOOKUP_EMPTY_MESSAGE,
15
+ ACCOUNT_LOOKUP_FAILED_MESSAGE,
16
+ ACCOUNT_LOOKUP_FAILURE_EXIT_CODE,
17
+ ACTION_COMMENT,
18
+ ACTION_CREATE,
19
+ ACTION_EDIT,
20
+ ACTION_FAILED_MESSAGE,
21
+ ACTION_REVIEW,
22
+ ALL_LINTER_ACTIONS_BY_COMMAND,
23
+ ALL_REVIEW_FLAGS_BY_EVENT,
24
+ PACKAGE_ROOT_PARENT_INDEX,
25
+ REVIEW_EVENT_COMMENT,
26
+ SELECTED_ACCOUNT_ENVIRONMENT_KEY,
27
+ SKILLS_DIRECTORY_PARENT_INDEX,
28
+ )
29
+
30
+ CommandRunner = Callable[..., subprocess.CompletedProcess[str]]
31
+
32
+
33
+ def _build_parser() -> argparse.ArgumentParser:
34
+ parser = argparse.ArgumentParser(description=__doc__, allow_abbrev=False)
35
+ all_actions = parser.add_subparsers(dest="action", required=True)
36
+ create = all_actions.add_parser(ACTION_CREATE, allow_abbrev=False)
37
+ create.add_argument("--repo", required=True)
38
+ create.add_argument("--base", required=True)
39
+ create.add_argument("--head", required=True)
40
+ create.add_argument("--title", required=True)
41
+ create.add_argument("--body-file", required=True, type=Path)
42
+ edit = all_actions.add_parser(ACTION_EDIT, allow_abbrev=False)
43
+ edit.add_argument("--repo", required=True)
44
+ edit.add_argument("--number", required=True)
45
+ edit.add_argument("--title")
46
+ edit.add_argument("--body-file", type=Path)
47
+ comment = all_actions.add_parser(ACTION_COMMENT, allow_abbrev=False)
48
+ comment.add_argument("--repo", required=True)
49
+ comment.add_argument("--number", required=True)
50
+ comment.add_argument("--body-file", required=True, type=Path)
51
+ review = all_actions.add_parser(ACTION_REVIEW, allow_abbrev=False)
52
+ review.add_argument("--repo", required=True)
53
+ review.add_argument("--number", required=True)
54
+ review.add_argument("--body-file", required=True, type=Path)
55
+ review.add_argument(
56
+ "--event",
57
+ choices=tuple(ALL_REVIEW_FLAGS_BY_EVENT),
58
+ default=REVIEW_EVENT_COMMENT,
59
+ )
60
+ return parser
61
+
62
+
63
+ def _linter_path() -> Path:
64
+ return (
65
+ Path(__file__).resolve().parents[SKILLS_DIRECTORY_PARENT_INDEX]
66
+ / "_shared/pr-loop/scripts/durable_post_lint.py"
67
+ )
68
+
69
+
70
+ def _linter_arguments(arguments: argparse.Namespace) -> list[str]:
71
+ all_arguments = [
72
+ sys.executable,
73
+ str(_linter_path()),
74
+ "--action",
75
+ ALL_LINTER_ACTIONS_BY_COMMAND[arguments.action],
76
+ ]
77
+ if getattr(arguments, "title", None) is not None:
78
+ all_arguments.extend(["--title", arguments.title])
79
+ if getattr(arguments, "body_file", None) is not None:
80
+ all_arguments.extend(["--body-file", str(arguments.body_file)])
81
+ return all_arguments
82
+
83
+
84
+ def _create_arguments(arguments: argparse.Namespace) -> list[str]:
85
+ return [
86
+ "gh",
87
+ "pr",
88
+ "create",
89
+ "--draft",
90
+ "--repo",
91
+ arguments.repo,
92
+ "--base",
93
+ arguments.base,
94
+ "--head",
95
+ arguments.head,
96
+ "--title",
97
+ arguments.title,
98
+ "--body-file",
99
+ str(arguments.body_file),
100
+ ]
101
+
102
+
103
+ def _edit_arguments(arguments: argparse.Namespace) -> list[str]:
104
+ all_arguments = ["gh", "pr", "edit", arguments.number, "--repo", arguments.repo]
105
+ if arguments.title is not None:
106
+ all_arguments.extend(["--title", arguments.title])
107
+ if arguments.body_file is not None:
108
+ all_arguments.extend(["--body-file", str(arguments.body_file)])
109
+ return all_arguments
110
+
111
+
112
+ def _post_arguments(arguments: argparse.Namespace) -> list[str]:
113
+ if arguments.action == ACTION_CREATE:
114
+ return _create_arguments(arguments)
115
+ if arguments.action == ACTION_EDIT:
116
+ return _edit_arguments(arguments)
117
+ if arguments.action == ACTION_COMMENT:
118
+ return [
119
+ "gh",
120
+ "pr",
121
+ "comment",
122
+ arguments.number,
123
+ "--repo",
124
+ arguments.repo,
125
+ "--body-file",
126
+ str(arguments.body_file),
127
+ ]
128
+ return [
129
+ "gh",
130
+ "pr",
131
+ "review",
132
+ arguments.number,
133
+ "--repo",
134
+ arguments.repo,
135
+ ALL_REVIEW_FLAGS_BY_EVENT[arguments.event],
136
+ "--body-file",
137
+ str(arguments.body_file),
138
+ ]
139
+
140
+
141
+ def _shared_environment_key_names() -> tuple[str, str, tuple[str, ...]]:
142
+ shared_directory = (
143
+ Path(__file__).resolve().parents[PACKAGE_ROOT_PARENT_INDEX]
144
+ / "_shared/pr-loop/scripts"
145
+ )
146
+ shared_directory_text = str(shared_directory)
147
+ if shared_directory_text not in sys.path:
148
+ sys.path.insert(0, shared_directory_text)
149
+ constants_module = importlib.import_module(
150
+ "pr_loop_shared_constants.post_audit_thread_constants"
151
+ )
152
+ return (
153
+ constants_module.GITHUB_TOKEN_ENV_VAR_NAME,
154
+ constants_module.GH_TOKEN_ENV_VAR_NAME,
155
+ constants_module.ALL_GH_TOKEN_ENV_VAR_NAMES,
156
+ )
157
+
158
+
159
+ def _lookup_environment(all_environment: Mapping[str, str]) -> dict[str, str]:
160
+ lookup_environment = dict(all_environment)
161
+ _, _, all_authorization_names = _shared_environment_key_names()
162
+ for each_name in all_authorization_names:
163
+ lookup_environment.pop(each_name, None)
164
+ return lookup_environment
165
+
166
+
167
+ def _lookup_account_authorization(
168
+ selected_account: str,
169
+ all_environment: Mapping[str, str],
170
+ command_runner: CommandRunner,
171
+ ) -> str | None:
172
+ try:
173
+ completed_lookup = command_runner(
174
+ ["gh", "auth", "token", "--user", selected_account],
175
+ capture_output=True,
176
+ check=False,
177
+ env=_lookup_environment(all_environment),
178
+ shell=False,
179
+ text=True,
180
+ )
181
+ except OSError:
182
+ sys.stderr.write(ACCOUNT_LOOKUP_FAILED_MESSAGE)
183
+ return None
184
+ if completed_lookup.returncode != 0:
185
+ sys.stderr.write(ACCOUNT_LOOKUP_FAILED_MESSAGE)
186
+ return None
187
+ account_authorization = completed_lookup.stdout.strip()
188
+ if not account_authorization:
189
+ sys.stderr.write(ACCOUNT_LOOKUP_EMPTY_MESSAGE)
190
+ return None
191
+ return account_authorization
192
+
193
+
194
+ def _action_environment(
195
+ all_environment: Mapping[str, str],
196
+ command_runner: CommandRunner,
197
+ ) -> dict[str, str] | None:
198
+ selected_account = all_environment.get(SELECTED_ACCOUNT_ENVIRONMENT_KEY)
199
+ if not selected_account:
200
+ return dict(all_environment)
201
+ account_authorization = _lookup_account_authorization(
202
+ selected_account, all_environment, command_runner
203
+ )
204
+ if account_authorization is None:
205
+ return None
206
+ parent_authorization_name, child_authorization_name, _ = (
207
+ _shared_environment_key_names()
208
+ )
209
+ action_environment = dict(all_environment)
210
+ action_environment.pop(parent_authorization_name, None)
211
+ action_environment[child_authorization_name] = account_authorization
212
+ return action_environment
213
+
214
+
215
+ def _run_post_action(
216
+ arguments: argparse.Namespace,
217
+ all_action_environment: Mapping[str, str],
218
+ command_runner: CommandRunner,
219
+ ) -> int:
220
+ try:
221
+ completed_action = command_runner(
222
+ _post_arguments(arguments),
223
+ check=False,
224
+ env=all_action_environment,
225
+ shell=False,
226
+ )
227
+ except OSError:
228
+ sys.stderr.write(ACTION_FAILED_MESSAGE)
229
+ return ACCOUNT_LOOKUP_FAILURE_EXIT_CODE
230
+ return completed_action.returncode
231
+
232
+
233
+ def _run_action(
234
+ arguments: argparse.Namespace,
235
+ all_environment: Mapping[str, str],
236
+ command_runner: CommandRunner,
237
+ ) -> int:
238
+ action_environment = _action_environment(all_environment, command_runner)
239
+ if action_environment is None:
240
+ return ACCOUNT_LOOKUP_FAILURE_EXIT_CODE
241
+ return _run_post_action(arguments, action_environment, command_runner)
242
+
243
+
244
+ def main(
245
+ all_arguments: Sequence[str],
246
+ *,
247
+ all_environment: Mapping[str, str],
248
+ command_runner: CommandRunner,
249
+ ) -> int:
250
+ """Run one validated pull request action.
251
+
252
+ Args:
253
+ all_arguments: Command arguments after the script name.
254
+ all_environment: Parent environment copied for child processes.
255
+ command_runner: Process runner used by tests and production.
256
+
257
+ Returns:
258
+ The linter, account lookup, or GitHub action exit status.
259
+ """
260
+ arguments = _build_parser().parse_args(list(all_arguments))
261
+ completed_lint = command_runner(
262
+ _linter_arguments(arguments), check=False, shell=False
263
+ )
264
+ if completed_lint.returncode != 0:
265
+ return completed_lint.returncode
266
+ return _run_action(arguments, all_environment, command_runner)
267
+
268
+
269
+ if __name__ == "__main__":
270
+ raise SystemExit(
271
+ main(
272
+ sys.argv[1:],
273
+ all_environment=os.environ,
274
+ command_runner=subprocess.run,
275
+ )
276
+ )