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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Spec: docs/specs/interfaces/additional-operations.md
// Workflow and workflow run operations for GitHub API
use std::fmt;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::client::InstallationClient;
use crate::error::ApiError;
/// State of a GitHub Actions workflow.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowState {
/// Workflow is active.
Active,
/// Workflow was disabled manually by a repository admin.
DisabledManually,
/// Workflow was disabled automatically due to inactivity.
DisabledInactivity,
/// Workflow is disabled because the repo is a fork.
DisabledFork,
/// Workflow has been deleted.
Deleted,
}
impl fmt::Display for WorkflowState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WorkflowState::Active => write!(f, "active"),
WorkflowState::DisabledManually => write!(f, "disabled_manually"),
WorkflowState::DisabledInactivity => write!(f, "disabled_inactivity"),
WorkflowState::DisabledFork => write!(f, "disabled_fork"),
WorkflowState::Deleted => write!(f, "deleted"),
}
}
}
/// GitHub Actions workflow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workflow {
/// Unique workflow identifier
pub id: u64,
/// Node ID for GraphQL API
pub node_id: String,
/// Workflow name
pub name: String,
/// Workflow file path
pub path: String,
/// Workflow state
pub state: WorkflowState,
/// Creation timestamp
pub created_at: DateTime<Utc>,
/// Last update timestamp
pub updated_at: DateTime<Utc>,
/// Workflow URL
pub url: String,
/// Workflow HTML URL
pub html_url: String,
/// Workflow badge URL
pub badge_url: String,
}
/// Status of a GitHub Actions workflow run.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowRunStatus {
/// Run is queued and waiting to start.
Queued,
/// Run is currently executing.
InProgress,
/// Run has finished.
#[default]
Completed,
/// Run is waiting on a required check or deployment protection rule.
Waiting,
/// Run has been requested.
Requested,
/// Run is pending.
Pending,
}
impl fmt::Display for WorkflowRunStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WorkflowRunStatus::Queued => write!(f, "queued"),
WorkflowRunStatus::InProgress => write!(f, "in_progress"),
WorkflowRunStatus::Completed => write!(f, "completed"),
WorkflowRunStatus::Waiting => write!(f, "waiting"),
WorkflowRunStatus::Requested => write!(f, "requested"),
WorkflowRunStatus::Pending => write!(f, "pending"),
}
}
}
/// Conclusion of a completed GitHub Actions workflow run.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowRunConclusion {
/// Run completed successfully.
#[default]
Success,
/// Run failed.
Failure,
/// Run was cancelled.
Cancelled,
/// Run was skipped.
Skipped,
/// Run timed out.
TimedOut,
/// Run requires manual action.
ActionRequired,
/// Run result is stale.
Stale,
/// Run completed with a neutral result.
Neutral,
}
impl fmt::Display for WorkflowRunConclusion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
WorkflowRunConclusion::Success => write!(f, "success"),
WorkflowRunConclusion::Failure => write!(f, "failure"),
WorkflowRunConclusion::Cancelled => write!(f, "cancelled"),
WorkflowRunConclusion::Skipped => write!(f, "skipped"),
WorkflowRunConclusion::TimedOut => write!(f, "timed_out"),
WorkflowRunConclusion::ActionRequired => write!(f, "action_required"),
WorkflowRunConclusion::Stale => write!(f, "stale"),
WorkflowRunConclusion::Neutral => write!(f, "neutral"),
}
}
}
/// GitHub Actions workflow run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowRun {
/// Unique workflow run identifier
pub id: u64,
/// Node ID for GraphQL API
pub node_id: String,
/// Workflow run name
pub name: String,
/// Workflow run number
pub run_number: u64,
/// Event that triggered the workflow
pub event: String,
/// Workflow run status
pub status: WorkflowRunStatus,
/// Workflow run conclusion (if completed)
pub conclusion: Option<WorkflowRunConclusion>,
/// Workflow ID
pub workflow_id: u64,
/// Head branch
pub head_branch: String,
/// Head commit SHA
pub head_sha: String,
/// Creation timestamp
pub created_at: DateTime<Utc>,
/// Last update timestamp
pub updated_at: DateTime<Utc>,
/// Workflow run URL
pub url: String,
/// Workflow run HTML URL
pub html_url: String,
}
/// Request to trigger a workflow.
#[derive(Debug, Clone, Serialize)]
pub struct TriggerWorkflowRequest {
/// Git reference (branch or tag)
#[serde(rename = "ref")]
pub git_ref: String,
/// Workflow inputs (key-value pairs)
#[serde(skip_serializing_if = "Option::is_none")]
pub inputs: Option<std::collections::HashMap<String, String>>,
}
/// Domain client for GitHub Actions workflow and run operations.
///
/// Obtained via [`InstallationClient::workflows()`]. Cheap to clone (Arc-backed).
///
/// See docs/specs/interfaces/additional-operations.md
#[derive(Debug, Clone)]
pub struct WorkflowsClient {
client: InstallationClient,
}
impl WorkflowsClient {
pub(crate) fn new(client: InstallationClient) -> Self {
Self { client }
}
/// List workflows in a repository.
///
/// Retrieves all GitHub Actions workflows for a repository.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
///
/// # Returns
///
/// Returns vector of workflows.
///
/// # Errors
///
/// * `ApiError::NotFound` - Repository does not exist
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// let workflows = client.list("owner", "repo").await?;
/// for workflow in workflows {
/// println!("Workflow: {} ({})", workflow.name, workflow.state);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list(&self, owner: &str, repo: &str) -> Result<Vec<Workflow>, ApiError> {
let path = format!("/repos/{}/{}/actions/workflows", owner, repo);
let response = self.client.get(&path).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
#[derive(Deserialize)]
struct WorkflowsResponse {
workflows: Vec<Workflow>,
}
let workflows_response: WorkflowsResponse =
response.json().await.map_err(ApiError::from)?;
Ok(workflows_response.workflows)
}
/// Get a specific workflow by ID.
///
/// Retrieves details about a single workflow.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `workflow_id` - Workflow ID
///
/// # Returns
///
/// Returns the `Workflow` with the specified ID.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow does not exist
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// let workflow = client.get("owner", "repo", 123456).await?;
/// println!("Workflow: {} at {}", workflow.name, workflow.path);
/// # Ok(())
/// # }
/// ```
pub async fn get(
&self,
owner: &str,
repo: &str,
workflow_id: u64,
) -> Result<Workflow, ApiError> {
let path = format!(
"/repos/{}/{}/actions/workflows/{}",
owner, repo, workflow_id
);
let response = self.client.get(&path).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
response.json().await.map_err(ApiError::from)
}
/// Trigger a workflow run.
///
/// Manually triggers a workflow run using workflow_dispatch event.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `workflow_id` - Workflow ID
/// * `request` - Trigger parameters (ref and optional inputs)
///
/// # Returns
///
/// Returns `Ok(())` on successful trigger.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow does not exist
/// * `ApiError::InvalidRequest` - Workflow not configured for manual dispatch
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::{WorkflowsClient, TriggerWorkflowRequest};
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// let request = TriggerWorkflowRequest {
/// git_ref: "main".to_string(),
/// inputs: None,
/// };
/// client.trigger("owner", "repo", 123456, request).await?;
/// println!("Workflow triggered");
/// # Ok(())
/// # }
/// ```
pub async fn trigger(
&self,
owner: &str,
repo: &str,
workflow_id: u64,
request: TriggerWorkflowRequest,
) -> Result<(), ApiError> {
let path = format!(
"/repos/{}/{}/actions/workflows/{}/dispatches",
owner, repo, workflow_id
);
let response = self.client.post(&path, &request).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
Ok(())
}
// ========================================================================
// Workflow Run Operations
// ========================================================================
/// List workflow runs for a workflow.
///
/// Retrieves workflow runs for a specific workflow.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `workflow_id` - Workflow ID
///
/// # Returns
///
/// Returns vector of workflow runs.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow does not exist
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// let runs = client.list_runs("owner", "repo", 123456).await?;
/// for run in runs {
/// println!("Run #{}: {} ({})", run.run_number, run.status, run.conclusion.unwrap_or_default());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list_runs(
&self,
owner: &str,
repo: &str,
workflow_id: u64,
) -> Result<Vec<WorkflowRun>, ApiError> {
let path = format!(
"/repos/{}/{}/actions/workflows/{}/runs",
owner, repo, workflow_id
);
let response = self.client.get(&path).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
#[derive(Deserialize)]
struct WorkflowRunsResponse {
workflow_runs: Vec<WorkflowRun>,
}
let runs_response: WorkflowRunsResponse = response.json().await.map_err(ApiError::from)?;
Ok(runs_response.workflow_runs)
}
/// Get a specific workflow run by ID.
///
/// Retrieves details about a single workflow run.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `run_id` - Workflow run ID
///
/// # Returns
///
/// Returns the `WorkflowRun` with the specified ID.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow run does not exist
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// let run = client.get_run("owner", "repo", 987654).await?;
/// println!("Run #{}: {}", run.run_number, run.status);
/// # Ok(())
/// # }
/// ```
pub async fn get_run(
&self,
owner: &str,
repo: &str,
run_id: u64,
) -> Result<WorkflowRun, ApiError> {
let path = format!("/repos/{}/{}/actions/runs/{}", owner, repo, run_id);
let response = self.client.get(&path).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
response.json().await.map_err(ApiError::from)
}
/// Cancel a workflow run.
///
/// Cancels a workflow run that is in progress.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `run_id` - Workflow run ID
///
/// # Returns
///
/// Returns `Ok(())` on successful cancellation.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow run does not exist
/// * `ApiError::InvalidRequest` - Workflow run cannot be cancelled
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// client.cancel_run("owner", "repo", 987654).await?;
/// println!("Workflow run cancelled");
/// # Ok(())
/// # }
/// ```
pub async fn cancel_run(&self, owner: &str, repo: &str, run_id: u64) -> Result<(), ApiError> {
let path = format!("/repos/{}/{}/actions/runs/{}/cancel", owner, repo, run_id);
let response = self.client.post(&path, &serde_json::json!({})).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
Ok(())
}
/// Re-run a workflow run.
///
/// Re-runs a completed workflow run.
///
/// # Arguments
///
/// * `owner` - Repository owner
/// * `repo` - Repository name
/// * `run_id` - Workflow run ID
///
/// # Returns
///
/// Returns `Ok(())` on successful re-run trigger.
///
/// # Errors
///
/// * `ApiError::NotFound` - Workflow run does not exist
/// * `ApiError::InvalidRequest` - Workflow run cannot be re-run
/// * `ApiError::AuthorizationFailed` - Insufficient permissions
///
/// # Example
///
/// ```no_run
/// # use github_bot_sdk::client::WorkflowsClient;
/// # async fn example(client: &WorkflowsClient) -> Result<(), Box<dyn std::error::Error>> {
/// client.rerun_run("owner", "repo", 987654).await?;
/// println!("Workflow run re-triggered");
/// # Ok(())
/// # }
/// ```
pub async fn rerun_run(&self, owner: &str, repo: &str, run_id: u64) -> Result<(), ApiError> {
let path = format!("/repos/{}/{}/actions/runs/{}/rerun", owner, repo, run_id);
let response = self.client.post(&path, &serde_json::json!({})).await?;
let status = response.status();
if !status.is_success() {
return Err(super::map_http_error(status, response).await);
}
Ok(())
}
}
#[cfg(test)]
#[path = "workflow_tests.rs"]
mod tests;