From 12a3d66b6fa2402cb53166e3250def6d283666f8 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Fri, 26 Jul 2019 14:21:43 +0200 Subject: [PATCH] Add support for bitbucket --- src/main.rs | 4 ++++ src/service.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/main.rs b/src/main.rs index 93c39a9..fac4e19 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,6 +114,10 @@ fn main() -> Result<()> { "/github/{user}/{repo}/{commit}/{file:.*}", web::get().to_async(handle_request::), ) + .route( + "/bitbucket/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(handle_request::), + ) // .default_service(web::resource("").route(web::get().to_async(p404))) }) // .workers(OPT.workers) diff --git a/src/service.rs b/src/service.rs index 60d19f1..0bf53cb 100644 --- a/src/service.rs +++ b/src/service.rs @@ -15,6 +15,22 @@ impl ApiResponse for GitHubApiResponse { } } +#[derive(Deserialize)] +pub(crate) struct BitbucketApiResponse { + values: Vec, +} + +#[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 { type Response: for<'de> serde::Deserialize<'de> + ApiResponse + 'static; 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) } } + +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) + } +}