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 MigrationsHandler<'octo> {
crab: &'octo Octocrab,
}
impl<'octo> MigrationsHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab) -> Self {
Self { crab }
}
pub fn org(
&self,
org: impl Into<String>,
) -> crate::api::orgs::migrations::OrgMigrationsHandler<'octo> {
crate::api::orgs::migrations::OrgMigrationsHandler::new(self.crab, org)
}
pub fn list(&self) -> ListUserMigrationsBuilder<'octo, '_> {
ListUserMigrationsBuilder::new(self)
}
pub async fn start(&self, body: &StartMigration) -> Result<Migration> {
self.crab.post("/user/migrations", 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>,
) -> GetUserMigrationStatusBuilder<'octo, '_> {
GetUserMigrationStatusBuilder::new(self, migration_id.into())
}
pub async fn download_archive(
&self,
migration_id: impl Into<MigrationId>,
) -> Result<bytes::Bytes> {
let route = format!("/user/migrations/{}/archive", 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!("/user/migrations/{}/archive", 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!(
"/user/migrations/{}/repos/{}/lock",
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>,
) -> ListUserMigrationReposBuilder<'octo, '_> {
ListUserMigrationReposBuilder::new(self, migration_id.into())
}
pub fn list_repositories(
&self,
migration_id: impl Into<MigrationId>,
) -> ListUserMigrationReposBuilder<'octo, '_> {
self.list_repos(migration_id)
}
}
#[derive(serde::Serialize)]
pub struct ListUserMigrationsBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r MigrationsHandler<'octo>,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListUserMigrationsBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r MigrationsHandler<'octo>) -> Self {
Self {
handler,
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<Migration>> {
self.handler.crab.get("/user/migrations", Some(&self)).await
}
}
pub(crate) fn serialize_comma_separated<S>(
val: &Option<Vec<String>>,
serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match val {
Some(v) => serializer.serialize_str(&v.join(",")),
None => serializer.serialize_none(),
}
}
#[derive(serde::Serialize)]
pub struct GetUserMigrationStatusBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r MigrationsHandler<'octo>,
#[serde(skip)]
migration_id: MigrationId,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "serialize_comma_separated"
)]
exclude: Option<Vec<String>>,
}
impl<'octo, 'r> GetUserMigrationStatusBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r MigrationsHandler<'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!("/user/migrations/{}", self.migration_id);
self.handler.crab.get(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct ListUserMigrationReposBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r MigrationsHandler<'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> ListUserMigrationReposBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r MigrationsHandler<'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!("/user/migrations/{}/repositories", self.migration_id);
self.handler.crab.get(route, Some(&self)).await
}
}