Add support for bitbucket
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Valentin Brandl 2019-07-26 14:21:43 +02:00
parent 27f7e8f35c
commit 12a3d66b6f
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 44 additions and 0 deletions

View File

@ -114,6 +114,10 @@ fn main() -> Result<()> {
"/github/{user}/{repo}/{commit}/{file:.*}", "/github/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(handle_request::<Github>), web::get().to_async(handle_request::<Github>),
) )
.route(
"/bitbucket/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(handle_request::<Bitbucket>),
)
// .default_service(web::resource("").route(web::get().to_async(p404))) // .default_service(web::resource("").route(web::get().to_async(p404)))
}) })
// .workers(OPT.workers) // .workers(OPT.workers)

View File

@ -15,6 +15,22 @@ impl ApiResponse for GitHubApiResponse {
} }
} }
#[derive(Deserialize)]
pub(crate) struct BitbucketApiResponse {
values: Vec<BitbucketEntry>,
}
#[derive(Deserialize)]
pub(crate) struct BitbucketEntry {
pub(crate) hash: String,
}
impl ApiResponse for BitbucketApiResponse {
fn commit_ref(&self) -> &str {
&self.values[0].hash
}
}
pub(crate) trait Service { pub(crate) trait Service {
type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static; type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static;
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String; fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String;
@ -45,3 +61,27 @@ impl Service for Github {
format!("/github/{}/{}/{}/{}", user, repo, commit, file) format!("/github/{}/{}/{}/{}", user, repo, commit, file)
} }
} }
pub(crate) struct Bitbucket;
impl Service for Bitbucket {
type Response = BitbucketApiResponse;
fn raw_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
format!(
"https://bitbucket.org/{}/{}/raw/{}/{}",
user, repo, commit, file
)
}
fn api_url(path: &FilePath) -> String {
format!(
"https://api.bitbucket.org/2.0/repositories/{}/{}/commits/{}?pagelen=1",
path.user, path.repo, path.commit
)
}
fn redirect_url(user: &str, repo: &str, commit: &str, file: &str) -> String {
format!("/bitbucket/{}/{}/{}/{}", user, repo, commit, file)
}
}