-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from siketyan/feat/fork-before-clone
feat: Fork before cloning
- Loading branch information
Showing
9 changed files
with
1,013 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ mod config; | |
mod console; | ||
mod git; | ||
mod path; | ||
mod platform; | ||
mod profile; | ||
mod repository; | ||
mod root; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use anyhow::{anyhow, Result}; | ||
use async_trait::async_trait; | ||
use gh_config::{Hosts, GITHUB_COM}; | ||
use octocrab::Octocrab; | ||
use serde::Deserialize; | ||
|
||
use crate::platform::{Fork, Platform, PlatformInit}; | ||
use crate::url::Url; | ||
|
||
fn default_host() -> String { | ||
GITHUB_COM.to_string() | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Config { | ||
#[serde(default = "default_host")] | ||
pub(super) host: String, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
host: default_host(), | ||
} | ||
} | ||
} | ||
|
||
pub struct GitHub { | ||
client: Octocrab, | ||
} | ||
|
||
impl PlatformInit for GitHub { | ||
type Config = Config; | ||
|
||
fn init(config: &Config) -> Result<Self> { | ||
let token = Hosts::load()? | ||
.get(&config.host) | ||
.ok_or_else(|| { | ||
anyhow!( | ||
"gh CLI does not have any token for github.com. Run `gh auth login` and retry." | ||
) | ||
})? | ||
.oauth_token | ||
.clone(); | ||
|
||
let mut builder = Octocrab::builder().personal_token(token); | ||
if config.host != GITHUB_COM { | ||
builder = builder.base_url(format!("https://{}/api/v3", &config.host))?; | ||
} | ||
|
||
Ok(Self { | ||
client: builder.build()?, | ||
}) | ||
} | ||
} | ||
|
||
impl Platform for GitHub {} | ||
|
||
#[async_trait] | ||
impl Fork for GitHub { | ||
async fn fork(&self, url: &Url, owner: Option<String>) -> Result<String> { | ||
let request = self.client.repos(&url.owner, &url.repo); | ||
let request = match owner { | ||
Some(o) => request.create_fork().organization(o), | ||
_ => request.create_fork(), | ||
}; | ||
|
||
Ok(request | ||
.send() | ||
.await? | ||
.html_url | ||
.as_ref() | ||
.ok_or_else(|| anyhow!("GitHub API did not return HTML URL for the repository."))? | ||
.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
mod github; | ||
|
||
use std::result::Result as StdResult; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
use crate::url::Url; | ||
|
||
#[async_trait] | ||
pub trait Fork { | ||
async fn fork(&self, url: &Url, owner: Option<String>) -> Result<String>; | ||
} | ||
|
||
pub trait PlatformInit: Sized { | ||
type Config; | ||
|
||
fn init(config: &Self::Config) -> Result<Self>; | ||
} | ||
|
||
pub trait Platform: Fork {} | ||
|
||
#[derive(Debug, Deserialize)] | ||
#[serde(tag = "type")] | ||
pub enum PlatformConfig { | ||
#[cfg(feature = "github")] | ||
#[serde(rename = "github")] | ||
GitHub(github::Config), | ||
} | ||
|
||
impl PlatformConfig { | ||
pub fn try_into_platform(&self) -> Result<Box<dyn Platform>> { | ||
self.try_into() | ||
} | ||
|
||
fn host(&self) -> String { | ||
match self { | ||
#[cfg(feature = "github")] | ||
Self::GitHub(c) => c.host.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl TryInto<Box<dyn Platform>> for &PlatformConfig { | ||
type Error = anyhow::Error; | ||
|
||
fn try_into(self) -> StdResult<Box<dyn Platform>, Self::Error> { | ||
Ok(match self { | ||
#[cfg(feature = "github")] | ||
PlatformConfig::GitHub(c) => Box::new(github::GitHub::init(c)?), | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Config { | ||
#[serde(flatten)] | ||
map: HashMap<String, PlatformConfig>, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Self { | ||
map: HashMap::from([ | ||
#[cfg(feature = "github")] | ||
( | ||
"github".to_string(), | ||
PlatformConfig::GitHub(github::Config::default()), | ||
), | ||
]), | ||
} | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn find(&self, url: &Url) -> Option<&PlatformConfig> { | ||
self.map.values().find(|c| c.host() == url.host.to_string()) | ||
} | ||
} |