Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jan 8, 2025
1 parent a7146f6 commit 13f9841
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 54 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-mover"
version = "1.0.3"
version = "1.0.4"
edition = "2021"
keywords = ["utility", "git", "cli"]
categories = ["command-line-utilities"]
Expand All @@ -25,3 +25,4 @@ serde_json = "1.0.135"
tokio = { version = "1", features = ["full"] }
toml = "0.8.19"
url = "2.5.4"
urlencoding = "2.1.3"
35 changes: 26 additions & 9 deletions src/codeberg/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use std::pin::Pin;
use urlencoding::encode;

use super::{repo::CodebergRepo, CODEBERG_URL};
use crate::{
errors::{GitMoverError, GitMoverErrorKind},
platform::Platform,
platform::{Platform, PlatformType},
utils::Repo,
};

Expand Down Expand Up @@ -62,7 +63,15 @@ impl Platform for CodebergPlatform {

let response = request.await?;
if !response.status().is_success() {
let get_repo = self.get_repo(repo_name.as_str()).await?;
let text = response.text().await?;
let get_repo = match self.get_repo(repo_name.as_str()).await {
Ok(repo) => repo,
Err(_e) => {
return Err(GitMoverError::new(GitMoverErrorKind::RepoCreation)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
}
};
let json_body_as_repo = json_body.clone().into();
if get_repo != json_body_as_repo {
eprintln!(
Expand Down Expand Up @@ -91,7 +100,7 @@ impl Platform for CodebergPlatform {
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
self.get_username(),
repo_name
encode(&repo_name)
);
let request = client
.get(&url)
Expand All @@ -103,7 +112,9 @@ impl Platform for CodebergPlatform {
let response = request.await?;
if !response.status().is_success() {
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetRepo).with_text(&text));
return Err(GitMoverError::new(GitMoverErrorKind::GetRepo)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
}
let repo: CodebergRepo = response.json().await?;
Ok(repo.into())
Expand All @@ -122,7 +133,7 @@ impl Platform for CodebergPlatform {
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
self.get_username(),
repo.name
encode(&repo.name)
);
let json_body = CodebergRepo {
name: repo.name.to_string(),
Expand All @@ -141,7 +152,9 @@ impl Platform for CodebergPlatform {
let response = request.await?;
if !response.status().is_success() {
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::RepoEdition).with_text(&text));
return Err(GitMoverError::new(GitMoverErrorKind::RepoEdition)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
}
Ok(())
})
Expand All @@ -168,7 +181,9 @@ impl Platform for CodebergPlatform {
let response = request.await?;
if !response.status().is_success() {
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos).with_text(&text));
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
}
let text = response.text().await?;
let repos: Vec<CodebergRepo> = serde_json::from_str(&text)?;
Expand Down Expand Up @@ -196,7 +211,7 @@ impl Platform for CodebergPlatform {
"https://{}/api/v1/repos/{}/{}",
CODEBERG_URL,
self.get_username(),
name
encode(&name)
);
let request = client
.delete(&url)
Expand All @@ -207,7 +222,9 @@ impl Platform for CodebergPlatform {
let response = request.await?;
if !response.status().is_success() {
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::RepoDeletion).with_text(&text));
return Err(GitMoverError::new(GitMoverErrorKind::RepoDeletion)
.with_platform(PlatformType::Codeberg)
.with_text(&text));
}
Ok(())
})
Expand Down
31 changes: 27 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Error handling for the git-mover crate.
use std::{error::Error as StdError, fmt};

use crate::platform::PlatformType;

/// Error type for the git-mover crate.
#[derive(Debug)]
pub struct GitMoverError {
Expand All @@ -12,7 +14,11 @@ impl GitMoverError {
/// Create a new error.
pub(crate) fn new(kind: GitMoverErrorKind) -> Self {
Self {
inner: Box::new(Inner { kind, source: None }),
inner: Box::new(Inner {
kind,
source: None,
platform: None,
}),
}
}

Expand All @@ -24,6 +30,12 @@ impl GitMoverError {
)));
self
}

/// Create a new error with a platform.
pub(crate) fn with_platform(mut self, platform: PlatformType) -> Self {
self.inner.platform = Some(platform);
self
}
}

/// Type alias for a boxed error.
Expand All @@ -34,8 +46,12 @@ pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;
struct Inner {
/// Error kind.
kind: GitMoverErrorKind,

/// Source error.
source: Option<BoxError>,

/// Platform error
platform: Option<PlatformType>,
}

#[derive(Debug)]
Expand All @@ -49,12 +65,15 @@ pub(crate) enum GitMoverErrorKind {
/// Error related to serde.
Serde,

/// Error related to the configuration.
Unimplemented,
/// Error related to Git2.
Git2,

/// Error related to the RepoEdition func.
RepoEdition,

/// Error related to the RepoCreation func.
RepoCreation,

/// Error related to the GetAllRepo func.
GetAllRepos,

Expand Down Expand Up @@ -86,6 +105,7 @@ impl From<reqwest::Error> for GitMoverError {
inner: Box::new(Inner {
kind: GitMoverErrorKind::Reqwest,
source: Some(Box::new(e)),
platform: None,
}),
}
}
Expand All @@ -97,6 +117,7 @@ impl From<serde_json::Error> for GitMoverError {
inner: Box::new(Inner {
kind: GitMoverErrorKind::Serde,
source: Some(Box::new(e)),
platform: None,
}),
}
}
Expand All @@ -108,6 +129,7 @@ impl From<std::io::Error> for GitMoverError {
inner: Box::new(Inner {
kind: GitMoverErrorKind::Platform,
source: Some(Box::new(e)),
platform: None,
}),
}
}
Expand All @@ -117,8 +139,9 @@ impl From<git2::Error> for GitMoverError {
fn from(e: git2::Error) -> Self {
Self {
inner: Box::new(Inner {
kind: GitMoverErrorKind::Platform,
kind: GitMoverErrorKind::Git2,
source: Some(Box::new(e)),
platform: None,
}),
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ pub(crate) mod repo;

/// GitHub URL
const GITHUB_URL: &str = "github.com";

/// GitHub API URL
const GITHUB_API_URL: &str = "api.github.com";

/// GitHub API Header
const GITHUB_API_HEADER: &str = "X-GitHub-Api-Version";

/// GitHub API Version
const GITHUB_API_VERSION: &str = "2022-11-28";
Loading

0 comments on commit 13f9841

Please sign in to comment.