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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Pull request event handlers
//!
//! This module provides implementations for registering handlers for GitHub pull request
//! and related webhook events.
use std::sync::Arc;
use octocrab::models::webhook_events::WebhookEventType;
use crate::{Context, Octofer, SerdeToString};
impl Octofer {
/// Register a handler for pull request events
///
/// This method registers an event handler that will be called whenever a pull request
/// event is received. Pull request events are triggered when PRs are opened, closed,
/// merged, edited, and more.
///
/// # Arguments
///
/// * `handler` - An async function that takes a Context and extra data
/// * `extra` - Additional data to pass to the handler (wrapped in Arc for sharing)
///
/// # Event Types
///
/// Pull request events include:
/// - PR opened
/// - PR closed (merged or unmerged)
/// - PR edited
/// - PR assigned/unassigned
/// - PR review requested
/// - PR labeled/unlabeled
/// - PR synchronized (new commits pushed)
///
/// # Examples
///
/// ## Basic Pull Request Handler
///
/// ```rust,no_run
/// use octofer::{Octofer, Config, Context};
/// use std::sync::Arc;
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config::default();
/// let mut app = Octofer::new(config).await.unwrap_or_else(|_| Octofer::new_default());
///
/// app.on_pull_request(
/// |context: Context, _extra: Arc<()>| async move {
/// println!("Pull request event: {}", context.kind());
///
/// let payload = context.payload();
/// if let Some(action) = payload.get("action") {
/// println!("PR action: {}", action);
/// }
///
/// if let Some(pr) = payload.get("pull_request") {
/// if let Some(title) = pr.get("title") {
/// println!("PR title: {}", title);
/// }
/// if let Some(number) = pr.get("number") {
/// println!("PR number: {}", number);
/// }
/// }
///
/// Ok(())
/// },
/// Arc::new(()),
/// ).await;
/// # Ok(())
/// # }
/// ```
///
/// ## Auto-merge Handler
///
/// ```rust,no_run
/// use octofer::{Octofer, Config, Context};
/// use std::sync::Arc;
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config::from_env().unwrap_or_default();
/// let mut app = Octofer::new(config).await.unwrap_or_else(|_| Octofer::new_default());
///
/// app.on_pull_request(
/// |context: Context, _extra: Arc<()>| async move {
/// let payload = context.payload();
///
/// // Only process newly opened PRs
/// if payload.get("action").and_then(|v| v.as_str()) != Some("opened") {
/// return Ok(());
/// }
///
/// if let Some(pr) = payload.get("pull_request") {
/// // Check if it's a dependabot PR
/// if let Some(user) = pr.get("user").and_then(|u| u.get("login")) {
/// if user.as_str() == Some("dependabot[bot]") {
/// println!("Dependabot PR detected, would auto-approve");
/// // Use installation client to approve PR
/// }
/// }
/// }
///
/// Ok(())
/// },
/// Arc::new(()),
/// ).await;
/// # Ok(())
/// # }
/// ```
pub async fn on_pull_request<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
where
F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
E: Send + Sync + 'static,
{
self.server
.on(WebhookEventType::PullRequest.to_string(), handler, extra)
.await;
self
}
/// Register a handler for pull request review events
///
/// This method registers an event handler that will be called whenever a pull request
/// review is submitted. This includes approvals, change requests, and comment-only reviews.
///
/// # Arguments
///
/// * `handler` - An async function that takes a Context and extra data
/// * `extra` - Additional data to pass to the handler (wrapped in Arc for sharing)
///
/// # Event Types
///
/// Pull request review events include:
/// - Review submitted (approved, changes requested, or commented)
/// - Review edited
/// - Review dismissed
///
/// # Examples
///
/// ```rust,no_run
/// use octofer::{Octofer, Config, Context};
/// use std::sync::Arc;
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config::default();
/// let mut app = Octofer::new(config).await.unwrap_or_else(|_| Octofer::new_default());
///
/// app.on_pull_request_review(
/// |context: Context, _extra: Arc<()>| async move {
/// println!("Pull request review event: {}", context.kind());
///
/// let payload = context.payload();
/// if let Some(review) = payload.get("review") {
/// if let Some(state) = review.get("state") {
/// println!("Review state: {}", state);
/// }
/// if let Some(body) = review.get("body") {
/// println!("Review body: {}", body);
/// }
/// }
///
/// Ok(())
/// },
/// Arc::new(()),
/// ).await;
/// # Ok(())
/// # }
/// ```
pub async fn on_pull_request_review<F, Fut, E>(&mut self, handler: F, extra: Arc<E>) -> &Self
where
F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
E: Send + Sync + 'static,
{
self.server
.on(
WebhookEventType::PullRequestReview.to_string(),
handler,
extra,
)
.await;
self
}
/// Register a handler for pull request review comment events
///
/// This method registers an event handler that will be called whenever a comment
/// is made during a pull request review. These are line-specific comments on the
/// code changes in the PR.
///
/// # Arguments
///
/// * `handler` - An async function that takes a Context and extra data
/// * `extra` - Additional data to pass to the handler (wrapped in Arc for sharing)
///
/// # Event Types
///
/// Pull request review comment events include:
/// - Review comment created
/// - Review comment edited
/// - Review comment deleted
///
/// # Examples
///
/// ```rust,no_run
/// use octofer::{Octofer, Config, Context};
/// use std::sync::Arc;
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config::default();
/// let mut app = Octofer::new(config).await.unwrap_or_else(|_| Octofer::new_default());
///
/// app.on_pull_request_review_comment(
/// |context: Context, _extra: Arc<()>| async move {
/// println!("Pull request review comment event: {}", context.kind());
///
/// let payload = context.payload();
/// if let Some(comment) = payload.get("comment") {
/// if let Some(body) = comment.get("body") {
/// println!("Review comment: {}", body);
/// }
/// if let Some(path) = comment.get("path") {
/// println!("File: {}", path);
/// }
/// if let Some(line) = comment.get("line") {
/// println!("Line: {}", line);
/// }
/// }
///
/// Ok(())
/// },
/// Arc::new(()),
/// ).await;
/// # Ok(())
/// # }
/// ```
pub async fn on_pull_request_review_comment<F, Fut, E>(
&mut self,
handler: F,
extra: Arc<E>,
) -> &Self
where
F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
E: Send + Sync + 'static,
{
self.server
.on(
WebhookEventType::PullRequestReviewComment.to_string(),
handler,
extra,
)
.await;
self
}
/// Register a handler for pull request review thread events
///
/// This method registers an event handler that will be called whenever a pull request
/// review thread is resolved or unresolved. Review threads are created when someone
/// starts a conversation on a specific line of code.
///
/// # Arguments
///
/// * `handler` - An async function that takes a Context and extra data
/// * `extra` - Additional data to pass to the handler (wrapped in Arc for sharing)
///
/// # Event Types
///
/// Pull request review thread events include:
/// - Thread resolved
/// - Thread unresolved
///
/// # Examples
///
/// ```rust,no_run
/// use octofer::{Octofer, Config, Context};
/// use std::sync::Arc;
///
/// # async fn example() -> anyhow::Result<()> {
/// let config = Config::default();
/// let mut app = Octofer::new(config).await.unwrap_or_else(|_| Octofer::new_default());
///
/// app.on_pull_request_review_thread(
/// |context: Context, _extra: Arc<()>| async move {
/// println!("Pull request review thread event: {}", context.kind());
///
/// let payload = context.payload();
/// if let Some(action) = payload.get("action") {
/// println!("Thread action: {}", action);
/// }
///
/// if let Some(thread) = payload.get("thread") {
/// if let Some(node_id) = thread.get("node_id") {
/// println!("Thread ID: {}", node_id);
/// }
/// }
///
/// Ok(())
/// },
/// Arc::new(()),
/// ).await;
/// # Ok(())
/// # }
/// ```
pub async fn on_pull_request_review_thread<F, Fut, E>(
&mut self,
handler: F,
extra: Arc<E>,
) -> &Self
where
F: Fn(Context, Arc<E>) -> Fut + Send + Sync + 'static,
Fut: std::future::Future<Output = anyhow::Result<()>> + Send + 'static,
E: Send + Sync + 'static,
{
self.server
.on(
WebhookEventType::PullRequestReviewThread.to_string(),
handler,
extra,
)
.await;
self
}
}