-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
213 lines (190 loc) · 6.1 KB
/
Copy pathlib.rs
File metadata and controls
213 lines (190 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! GitHub API client: installation tokens, Check Runs, PRs, issue comments.
use serde::{Deserialize, Serialize};
pub mod check_run;
pub mod installation;
pub mod pr;
pub mod repo;
pub mod tasks;
pub const DEFAULT_API_BASE_URL: &str = "https://api.github.com";
/// Major version of the coven-code headless execution contract this adapter
/// speaks. See `docs/headless-contract.md`. Bump only on breaking changes.
pub const HEADLESS_CONTRACT_VERSION: &str = "1";
fn default_contract_version() -> String {
HEADLESS_CONTRACT_VERSION.to_string()
}
const GITHUB_API_VERSION: &str = "2026-03-10";
#[derive(Debug, Clone, PartialEq, Eq)]
struct GitHubRequest {
method: &'static str,
path: String,
body: serde_json::Value,
}
fn api_url(base_url: &str, path: &str) -> String {
format!("{}{}", base_url.trim_end_matches('/'), path)
}
fn client() -> anyhow::Result<reqwest::Client> {
reqwest::Client::builder()
.user_agent("coven-github/0.1")
.build()
.map_err(Into::into)
}
async fn send_json(
client: &reqwest::Client,
base_url: &str,
token: &str,
request: GitHubRequest,
) -> anyhow::Result<reqwest::Response> {
let method = reqwest::Method::from_bytes(request.method.as_bytes())?;
let mut builder = client
.request(method, api_url(base_url, &request.path))
.bearer_auth(token)
.header("Accept", "application/vnd.github+json")
.header("X-GitHub-Api-Version", GITHUB_API_VERSION);
// GET/metadata requests carry no body; only attach JSON for mutations.
if !request.body.is_null() {
builder = builder.json(&request.body);
}
let response = builder.send().await?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().await.unwrap_or_default();
anyhow::bail!("GitHub API request failed with {status}: {body}");
}
Ok(response)
}
/// Minimal GitHub event types parsed from webhooks.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "event_type", rename_all = "snake_case")]
pub enum GitHubEvent {
IssueAssigned(IssueAssignedEvent),
IssueLabeled(IssueLabeledEvent),
IssueComment(IssueCommentEvent),
PullRequestReview(PrReviewEvent),
PullRequestReviewComment(PrReviewCommentEvent),
/// `ping` delivery GitHub sends when a webhook is first configured.
Ping,
Unsupported { name: String },
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueAssignedEvent {
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub issue_number: u64,
pub issue_title: String,
pub issue_body: String,
pub assignee_login: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueLabeledEvent {
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub issue_number: u64,
pub issue_title: String,
pub issue_body: String,
pub label_name: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IssueCommentEvent {
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub issue_number: u64,
pub comment_body: String,
pub commenter_login: String,
/// `issue_comment` fires for pull-request conversation comments as well as
/// issue comments. GitHub flags the former with an `issue.pull_request`
/// object; this lets routing send PR comments through PR iteration rather
/// than issue-mention handling.
pub on_pull_request: bool,
}
/// Top-level pull request review submission (`pull_request_review` → `submitted`).
///
/// Distinct from [`PrReviewCommentEvent`], which is a single inline comment on a
/// diff hunk. This carries the review summary body and verdict (state).
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PrReviewEvent {
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub pr_number: u64,
pub review_body: String,
/// Review verdict: `approved`, `changes_requested`, or `commented`.
pub review_state: String,
pub reviewer_login: String,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct PrReviewCommentEvent {
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub pr_number: u64,
pub comment_body: String,
pub commenter_login: String,
}
/// A task dispatched to the worker queue.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
pub id: String,
pub installation_id: u64,
pub repo_owner: String,
pub repo_name: String,
pub kind: TaskKind,
pub familiar_id: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum TaskKind {
FixIssue {
issue_number: u64,
issue_title: String,
issue_body: String,
},
AddressReviewComment {
pr_number: u64,
comment_body: String,
diff_hunk: Option<String>,
},
RespondToMention {
issue_number: u64,
comment_body: String,
},
}
/// Structured result envelope written by coven-code --headless.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SessionResult {
/// Contract major version. Defaults to the current version when a runtime
/// omits it, but conformant producers MUST emit it. See
/// `docs/headless-contract.md`.
#[serde(default = "default_contract_version")]
pub contract_version: String,
pub status: SessionStatus,
pub branch: Option<String>,
pub commits: Vec<CommitInfo>,
pub files_changed: Vec<String>,
pub summary: String,
pub pr_body: String,
pub exit_reason: Option<ExitReason>,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SessionStatus {
Success,
Failure,
Partial,
NeedsInput,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CommitInfo {
pub sha: String,
pub message: String,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ExitReason {
TestFailure,
AmbiguousSpec,
GitConflict,
InfraError,
}