-
-
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 #39 from siketyan/feat/resolve-default-org
feat: Resolve <repo> pattern using default owner
- Loading branch information
Showing
7 changed files
with
62 additions
and
16 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -92,6 +92,13 @@ pub struct Url { | |
} | ||
|
||
impl Url { | ||
pub fn from_str(s: &str, default_owner: Option<&str>) -> Result<Self> { | ||
match s.contains("://") { | ||
true => Self::from_url(&url::Url::from_str(s)?), | ||
_ => Self::from_pattern(s, default_owner), | ||
} | ||
} | ||
|
||
fn from_url(url: &url::Url) -> Result<Self> { | ||
let mut segments = url | ||
.path_segments() | ||
|
@@ -120,8 +127,11 @@ impl Url { | |
}) | ||
} | ||
|
||
fn from_pattern(s: &str) -> Result<Self> { | ||
fn from_pattern(s: &str, default_owner: Option<&str>) -> Result<Self> { | ||
lazy_static! { | ||
static ref REPO: Regex = | ||
Regex::new(r"^(?P<repo>[0-9A-Za-z_\.\-]+)$").unwrap(); | ||
|
||
static ref ORG_REPO: Regex = | ||
Regex::new(r"^(?P<org>[0-9A-Za-z_\.\-]+)/(?P<repo>[0-9A-Za-z_\.\-]+)$").unwrap(); | ||
|
||
|
@@ -152,6 +162,14 @@ impl Url { | |
}; | ||
} | ||
|
||
if let Some(owner) = default_owner { | ||
pattern!(REPO, |c: Captures| Ok(Self { | ||
owner: owner.to_string(), | ||
repo: Self::remove_extensions(&group!(c, "repo")), | ||
..Default::default() | ||
})); | ||
} | ||
|
||
pattern!(ORG_REPO, |c: Captures| Ok(Self { | ||
owner: group!(c, "org"), | ||
repo: Self::remove_extensions(&group!(c, "repo")), | ||
|
@@ -195,10 +213,7 @@ impl FromStr for Url { | |
type Err = Error; | ||
|
||
fn from_str(s: &str) -> Result<Self> { | ||
match s.contains("://") { | ||
true => Self::from_url(&url::Url::from_str(s)?), | ||
_ => Self::from_pattern(s), | ||
} | ||
Url::from_str(s, None) | ||
} | ||
} | ||
|
||
|
@@ -270,6 +285,21 @@ mod tests { | |
) | ||
} | ||
|
||
#[test] | ||
fn parse_from_pattern_repo() { | ||
assert_eq!( | ||
Url { | ||
vcs: Vcs::Git, | ||
scheme: Scheme::Https, | ||
user: None, | ||
host: Host::GitHub, | ||
owner: "siketyan".to_string(), | ||
repo: "siketyan.github.io".to_string() | ||
}, | ||
Url::from_pattern("siketyan.github.io", Some("siketyan")).unwrap(), | ||
) | ||
} | ||
|
||
#[test] | ||
fn parse_from_pattern_org_repo() { | ||
assert_eq!( | ||
|
@@ -281,7 +311,7 @@ mod tests { | |
owner: "siketyan".to_string(), | ||
repo: "siketyan.github.io".to_string() | ||
}, | ||
Url::from_pattern("siketyan/siketyan.github.io").unwrap(), | ||
Url::from_pattern("siketyan/siketyan.github.io", None).unwrap(), | ||
) | ||
} | ||
|
||
|
@@ -296,7 +326,7 @@ mod tests { | |
owner: "siketyan".to_string(), | ||
repo: "siketyan.github.io".to_string() | ||
}, | ||
Url::from_pattern("gitlab.com:siketyan/siketyan.github.io").unwrap(), | ||
Url::from_pattern("gitlab.com:siketyan/siketyan.github.io", None).unwrap(), | ||
) | ||
} | ||
|
||
|
@@ -311,7 +341,7 @@ mod tests { | |
owner: "siketyan".to_string(), | ||
repo: "siketyan.github.io".to_string() | ||
}, | ||
Url::from_pattern("[email protected]:siketyan/siketyan.github.io.git").unwrap(), | ||
Url::from_pattern("[email protected]:siketyan/siketyan.github.io.git", None).unwrap(), | ||
) | ||
} | ||
|
||
|