use super::OrgHandler;
use crate::models::rulesets::{RuleSuite, RuleSuiteId, RuleSuiteSummary, Ruleset, RulesetId};
pub struct OrgRulesetsHandler<'octo, 'r> {
handler: &'r OrgHandler<'octo>,
}
impl<'octo, 'r> OrgRulesetsHandler<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgHandler<'octo>) -> Self {
Self { handler }
}
pub fn list(&self) -> ListOrgRulesetsBuilder<'octo, 'r> {
ListOrgRulesetsBuilder::new(self.handler)
}
pub async fn get(&self, ruleset_id: impl Into<RulesetId>) -> crate::Result<Ruleset> {
let route = format!(
"/orgs/{org}/rulesets/{id}",
org = self.handler.owner,
id = ruleset_id.into(),
);
self.handler.crab.get(route, None::<&()>).await
}
pub async fn create(&self, ruleset: &impl serde::Serialize) -> crate::Result<Ruleset> {
let route = format!("/orgs/{org}/rulesets", org = self.handler.owner);
self.handler.crab.post(route, Some(ruleset)).await
}
pub async fn update(
&self,
ruleset_id: impl Into<RulesetId>,
ruleset: &impl serde::Serialize,
) -> crate::Result<Ruleset> {
let route = format!(
"/orgs/{org}/rulesets/{id}",
org = self.handler.owner,
id = ruleset_id.into(),
);
self.handler.crab.put(route, Some(ruleset)).await
}
pub async fn delete(&self, ruleset_id: impl Into<RulesetId>) -> crate::Result<()> {
let route = format!(
"/orgs/{org}/rulesets/{id}",
org = self.handler.owner,
id = ruleset_id.into(),
);
let response = self.handler.crab._delete(route, None::<&()>).await?;
if !response.status().is_success() {
return Err(crate::map_github_error(response).await.unwrap_err());
}
Ok(())
}
pub fn rule_suites(&self) -> OrgRuleSuitesHandler<'octo, 'r> {
OrgRuleSuitesHandler::new(self.handler)
}
}
#[derive(serde::Serialize)]
pub struct ListOrgRulesetsBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r OrgHandler<'octo>,
#[serde(skip_serializing_if = "Option::is_none")]
targets: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListOrgRulesetsBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgHandler<'octo>) -> Self {
Self {
handler,
targets: None,
per_page: None,
page: None,
}
}
pub fn targets(mut self, targets: impl Into<String>) -> Self {
self.targets = Some(targets.into());
self
}
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
pub async fn send(self) -> crate::Result<Vec<Ruleset>> {
let route = format!("/orgs/{org}/rulesets", org = self.handler.owner);
self.handler.crab.get(route, Some(&self)).await
}
}
pub struct OrgRuleSuitesHandler<'octo, 'r> {
handler: &'r OrgHandler<'octo>,
}
impl<'octo, 'r> OrgRuleSuitesHandler<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgHandler<'octo>) -> Self {
Self { handler }
}
pub fn list(&self) -> ListOrgRuleSuitesBuilder<'octo, 'r> {
ListOrgRuleSuitesBuilder::new(self.handler)
}
pub async fn get(&self, rule_suite_id: impl Into<RuleSuiteId>) -> crate::Result<RuleSuite> {
let route = format!(
"/orgs/{org}/rulesets/rule-suites/{id}",
org = self.handler.owner,
id = rule_suite_id.into(),
);
self.handler.crab.get(route, None::<&()>).await
}
}
#[derive(serde::Serialize)]
pub struct ListOrgRuleSuitesBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r OrgHandler<'octo>,
#[serde(rename = "ref", skip_serializing_if = "Option::is_none")]
ref_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
repository_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
time_period: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
actor_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
rule_suite_result: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
evaluate_status: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListOrgRuleSuitesBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgHandler<'octo>) -> Self {
Self {
handler,
ref_name: None,
repository_name: None,
time_period: None,
actor_name: None,
rule_suite_result: None,
evaluate_status: None,
per_page: None,
page: None,
}
}
pub fn ref_name(mut self, ref_name: impl Into<String>) -> Self {
self.ref_name = Some(ref_name.into());
self
}
pub fn repository_name(mut self, repository_name: impl Into<String>) -> Self {
self.repository_name = Some(repository_name.into());
self
}
pub fn time_period(mut self, time_period: impl Into<String>) -> Self {
self.time_period = Some(time_period.into());
self
}
pub fn actor_name(mut self, actor_name: impl Into<String>) -> Self {
self.actor_name = Some(actor_name.into());
self
}
pub fn rule_suite_result(mut self, rule_suite_result: impl Into<String>) -> Self {
self.rule_suite_result = Some(rule_suite_result.into());
self
}
pub fn evaluate_status(mut self, evaluate_status: impl Into<String>) -> Self {
self.evaluate_status = Some(evaluate_status.into());
self
}
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
pub async fn send(self) -> crate::Result<Vec<RuleSuiteSummary>> {
let route = format!("/orgs/{org}/rulesets/rule-suites", org = self.handler.owner);
self.handler.crab.get(route, Some(&self)).await
}
}