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
use crate::models::RunId;
use crate::{models, Octocrab, Page, Result};
pub struct WorkflowsHandler<'octo> {
crab: &'octo Octocrab,
owner: String,
repo: String,
}
/// Handler for GitHub's workflows API for actions.
///
/// Created with [`Octocrab::workflows`].
impl<'octo> WorkflowsHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self {
Self { crab, owner, repo }
}
/// List workflow definitions in the repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// # let octocrab = octocrab::Octocrab::default();
///
/// let issue = octocrab.workflows("owner", "repo")
/// .list()
/// // Optional Parameters
/// .per_page(100)
/// .page(1u8)
/// // Send the request
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list(&self) -> ListWorkflowsBuilder<'_, '_> {
ListWorkflowsBuilder::new(self)
}
pub async fn get(&self, run_id: RunId) -> Result<models::workflows::Run> {
let route = format!(
"/repos/{owner}/{repo}/actions/runs/{run_id}",
owner = self.owner,
repo = self.repo,
run_id = run_id,
);
self.crab.get(route, None::<&()>).await
}
/// List runs in the specified workflow.
/// workflow_file_or_id can be either file name or numeric expression.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// # let octocrab = octocrab::Octocrab::default();
///
/// let issue = octocrab.workflows("owner", "repo")
/// .list_runs("ci.yml")
/// // Optional Parameters
/// .actor("octocat")
/// .branch("master")
/// .event("push")
/// .status("success")
/// .per_page(100)
/// .page(1u8)
/// // Send the request
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list_runs(&self, workflow_file_or_id: impl Into<String>) -> ListRunsBuilder<'_, '_> {
ListRunsBuilder::new(
self,
ListRunsRequestType::ByWorkflow(workflow_file_or_id.into()),
)
}
/// List runs for the specified owner and repository.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let octocrab = octocrab::Octocrab::default();
///
/// let runs = octocrab.workflows("owner", "repo")
/// .list_all_runs()
/// // Optional Parameters
/// .actor("octocat")
/// .branch("master")
/// .event("pull_request")
/// .status("success")
/// .per_page(100)
/// .page(1u8)
/// // Send the request
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list_all_runs(&self) -> ListRunsBuilder<'_, '_> {
ListRunsBuilder::new(self, ListRunsRequestType::ByRepo)
}
/// List job results in the specified run.
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// # let octocrab = octocrab::Octocrab::default();
/// use octocrab::params::workflows::Filter;
///
/// let issue = octocrab.workflows("owner", "repo")
/// .list_jobs(1234u64.into())
/// // Optional Parameters
/// .per_page(100)
/// .page(1u8)
/// .filter(Filter::All)
/// // Send the request
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list_jobs(&self, run_id: RunId) -> ListJobsBuilder<'_, '_> {
ListJobsBuilder::new(self, run_id)
}
}
#[derive(serde::Serialize)]
pub struct ListWorkflowsBuilder<'octo, 'b> {
#[serde(skip)]
handler: &'b WorkflowsHandler<'octo>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'b> ListWorkflowsBuilder<'octo, 'b> {
pub(crate) fn new(handler: &'b WorkflowsHandler<'octo>) -> Self {
Self {
handler,
per_page: None,
page: None,
}
}
/// Results per page (max 100).
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
/// Page number of the results to fetch.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<Page<models::workflows::WorkFlow>> {
let route = format!(
"/repos/{owner}/{repo}/actions/workflows",
owner = self.handler.owner,
repo = self.handler.repo
);
self.handler.crab.get(route, Some(&self)).await
}
}
/// The type of list workflow runs request.
pub(crate) enum ListRunsRequestType {
ByRepo,
ByWorkflow(String),
}
#[derive(serde::Serialize)]
pub struct ListRunsBuilder<'octo, 'b> {
#[serde(skip)]
handler: &'b WorkflowsHandler<'octo>,
#[serde(skip)]
r#type: ListRunsRequestType,
#[serde(skip_serializing_if = "Option::is_none")]
actor: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
branch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
event: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
exclude_pull_requests: Option<bool>,
}
impl<'octo, 'b> ListRunsBuilder<'octo, 'b> {
pub(crate) fn new(handler: &'b WorkflowsHandler<'octo>, r#type: ListRunsRequestType) -> Self {
Self {
handler,
r#type,
actor: None,
branch: None,
event: None,
status: None,
per_page: None,
page: None,
exclude_pull_requests: None,
}
}
/// Someone who runs workflows. Use the login to specify a user.
pub fn actor(mut self, actor: impl Into<String>) -> Self {
self.actor = Some(actor.into());
self
}
/// A branch associated with workflows. Use the name of the branch of the push.
pub fn branch(mut self, branch: impl Into<String>) -> Self {
self.branch = Some(branch.into());
self
}
/// An event associated with workflows. Can be e.g. push, pull_request, issue,
/// ... and many variations. See official "Events that trigger workflows." doc.
pub fn event(mut self, event: impl Into<String>) -> Self {
self.event = Some(event.into());
self
}
/// A status associated with workflows.
/// status or conclusion can be specified. e.g. success, in_progress, waiting...
pub fn status(mut self, status: impl Into<String>) -> Self {
self.status = Some(status.into());
self
}
/// Results per page (max 100).
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
/// Page number of the results to fetch.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
/// Whether to exclude the pull requests or not.
pub fn exclude_pull_requests(mut self, exclude_pull_requests: impl Into<bool>) -> Self {
self.exclude_pull_requests = Some(exclude_pull_requests.into());
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<Page<models::workflows::Run>> {
let route = match self.r#type {
ListRunsRequestType::ByRepo => format!(
"/repos/{owner}/{repo}/actions/runs",
owner = self.handler.owner,
repo = self.handler.repo
),
ListRunsRequestType::ByWorkflow(ref workflow_id) => format!(
"/repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs",
owner = self.handler.owner,
repo = self.handler.repo,
workflow_id = workflow_id
),
};
self.handler.crab.get(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct ListJobsBuilder<'octo, 'b> {
#[serde(skip)]
handler: &'b WorkflowsHandler<'octo>,
#[serde(skip)]
run_id: RunId,
#[serde(skip_serializing_if = "Option::is_none")]
filter: Option<crate::params::workflows::Filter>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'b> ListJobsBuilder<'octo, 'b> {
pub(crate) fn new(handler: &'b WorkflowsHandler<'octo>, run_id: RunId) -> Self {
Self {
handler,
run_id,
per_page: None,
page: None,
filter: None,
}
}
/// Results per page (max 100).
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
/// Page number of the results to fetch.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
/// Filters jobs by their completed_at timestamp. Choose latest or all.
pub fn filter(mut self, filter: impl Into<crate::params::workflows::Filter>) -> Self {
self.filter = Some(filter.into());
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<Page<models::workflows::Job>> {
let route = format!(
"/repos/{owner}/{repo}/actions/runs/{run_id}/jobs",
owner = self.handler.owner,
repo = self.handler.repo,
run_id = self.run_id,
);
self.handler.crab.get(route, Some(&self)).await
}
}
#[cfg(test)]
mod tests {
#[tokio::test]
async fn serialize() {
use crate::params::workflows::Filter;
let octocrab = crate::Octocrab::default();
let handler = octocrab.workflows("rust-lang", "rust");
let list_jobs = handler
.list_jobs(1234u64.into())
.filter(Filter::All)
.per_page(100)
.page(1u8);
assert_eq!(
serde_json::to_value(list_jobs).unwrap(),
serde_json::json!({
"filter": "all",
"per_page": 100,
"page": 1,
})
)
}
}