use http::Uri;
use http_body_util::{BodyExt, Collected};
use snafu::ResultExt;
use crate::error::HttpSnafu;
use crate::models::migrations::{Migration, StartMigration};
use crate::models::{MigrationId, Repository};
use crate::{Octocrab, Page, Result};
pub struct OrgMigrationsHandler<'octo> {
crab: &'octo Octocrab,
owner: String,
}
impl<'octo> OrgMigrationsHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, owner: impl Into<String>) -> Self {
Self {
crab,
owner: owner.into(),
}
}
pub fn list(&self) -> ListOrgMigrationsBuilder<'octo, '_> {
ListOrgMigrationsBuilder::new(self)
}
pub async fn start(&self, body: &StartMigration) -> Result<Migration> {
let route = format!("/orgs/{}/migrations", self.owner);
self.crab.post(route, Some(body)).await
}
pub async fn get(&self, migration_id: impl Into<MigrationId>) -> Result<Migration> {
self.get_status(migration_id).send().await
}
pub fn get_status(
&self,
migration_id: impl Into<MigrationId>,
) -> GetOrgMigrationStatusBuilder<'octo, '_> {
GetOrgMigrationStatusBuilder::new(self, migration_id.into())
}
pub async fn download_archive(
&self,
migration_id: impl Into<MigrationId>,
) -> Result<bytes::Bytes> {
let route = format!(
"/orgs/{}/migrations/{}/archive",
self.owner,
migration_id.into()
);
let uri = Uri::builder()
.path_and_query(route)
.build()
.context(HttpSnafu)?;
let response = self.crab._get(uri).await?;
let data_response = self.crab.follow_location_to_data(response).await?;
data_response
.into_body()
.collect()
.await
.map(Collected::to_bytes)
}
pub async fn delete_archive(&self, migration_id: impl Into<MigrationId>) -> Result<()> {
let route = format!(
"/orgs/{}/migrations/{}/archive",
self.owner,
migration_id.into()
);
let resp = self.crab._delete(route, None::<&()>).await?;
crate::map_github_error(resp).await?;
Ok(())
}
pub async fn unlock_repo(
&self,
migration_id: impl Into<MigrationId>,
repo_name: impl AsRef<str>,
) -> Result<()> {
let route = format!(
"/orgs/{}/migrations/{}/repos/{}/lock",
self.owner,
migration_id.into(),
repo_name.as_ref()
);
let resp = self.crab._delete(route, None::<&()>).await?;
crate::map_github_error(resp).await?;
Ok(())
}
pub fn list_repos(
&self,
migration_id: impl Into<MigrationId>,
) -> ListOrgMigrationReposBuilder<'octo, '_> {
ListOrgMigrationReposBuilder::new(self, migration_id.into())
}
pub fn list_repositories(
&self,
migration_id: impl Into<MigrationId>,
) -> ListOrgMigrationReposBuilder<'octo, '_> {
self.list_repos(migration_id)
}
}
#[derive(serde::Serialize)]
pub struct ListOrgMigrationsBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r OrgMigrationsHandler<'octo>,
#[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",
serialize_with = "crate::api::migrations::serialize_comma_separated"
)]
exclude: Option<Vec<String>>,
}
impl<'octo, 'r> ListOrgMigrationsBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgMigrationsHandler<'octo>) -> Self {
Self {
handler,
per_page: None,
page: None,
exclude: None,
}
}
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 fn exclude(mut self, exclude: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.exclude = Some(exclude.into_iter().map(Into::into).collect());
self
}
pub async fn send(self) -> Result<Page<Migration>> {
let route = format!("/orgs/{}/migrations", self.handler.owner);
self.handler.crab.get(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct GetOrgMigrationStatusBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r OrgMigrationsHandler<'octo>,
#[serde(skip)]
migration_id: MigrationId,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "crate::api::migrations::serialize_comma_separated"
)]
exclude: Option<Vec<String>>,
}
impl<'octo, 'r> GetOrgMigrationStatusBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgMigrationsHandler<'octo>, migration_id: MigrationId) -> Self {
Self {
handler,
migration_id,
exclude: None,
}
}
pub fn exclude(mut self, exclude: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.exclude = Some(exclude.into_iter().map(Into::into).collect());
self
}
pub async fn send(self) -> Result<Migration> {
let route = format!(
"/orgs/{}/migrations/{}",
self.handler.owner, self.migration_id
);
self.handler.crab.get(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct ListOrgMigrationReposBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r OrgMigrationsHandler<'octo>,
#[serde(skip)]
migration_id: MigrationId,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListOrgMigrationReposBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r OrgMigrationsHandler<'octo>, migration_id: MigrationId) -> Self {
Self {
handler,
migration_id,
per_page: None,
page: None,
}
}
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) -> Result<Page<Repository>> {
let route = format!(
"/orgs/{}/migrations/{}/repositories",
self.handler.owner, self.migration_id
);
self.handler.crab.get(route, Some(&self)).await
}
}