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
use crate::core::frontend::GenericResult;
use crate::Metadata;
use crate::Result;
use octocrab;
use std::collections::HashMap;
pub fn lookup_pat() -> Result<String> {
Ok(std::env::var("github_pat")?)
}
#[derive(Serialize)]
pub struct DispatchWorkflowRequest {
r#ref: String,
inputs: HashMap<String, String>,
}
impl DispatchWorkflowRequest {
pub fn new(git_ref: &str, inputs: Option<HashMap<String, String>>) -> Self {
Self {
r#ref: git_ref.to_string(),
inputs: {
if let Some(ins) = inputs {
ins
} else {
let h: HashMap<String, String> = HashMap::new();
h
}
},
}
}
}
pub fn dispatch_workflow(
owner: &str,
repo: &str,
workflow: &str,
git_ref: &str,
inputs: Option<HashMap<String, String>>,
) -> Result<GenericResult> {
let o = octocrab::OctocrabBuilder::new()
.personal_token(lookup_pat()?)
.add_header(
reqwest::header::ACCEPT,
"application/vnd.github.v3+json".to_string(),
)
.build()?;
let r = tokio::runtime::Runtime::new().unwrap();
let _guard = r.enter();
let response = futures::executor::block_on(o._post(
format!(
"https://api.github.com/repos/{}/{}/actions/workflows/{}/dispatches",
owner, repo, workflow
),
Some(&DispatchWorkflowRequest::new(git_ref, inputs)),
))?;
let headers = response.headers().clone();
let status = response.status().as_u16() as usize;
let body = futures::executor::block_on(response.text())?;
let mut res = GenericResult::new_with_empty_metadata(body.is_empty(), Some(body));
res.metadata.as_mut().unwrap().insert(
"header".to_string(),
Metadata::String(format!("{:?}", headers)),
);
res.metadata
.as_mut()
.unwrap()
.insert("status".to_string(), Metadata::Usize(status));
Ok(res)
}