-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gitlab): Introduce a self-hosted gitlab endpoint
This introduces the `/v1/gitlab/:host/:user/:repo` routes, where `:host` is any GitLab instance (including, possibly, gitlab.com). Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
- Loading branch information
Showing
9 changed files
with
175 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Path, | ||
http::{Request, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
}; | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
use super::super::utils::gitlab_api_get_latest_tag; | ||
|
||
pub async fn get_repo( | ||
Path((host, user, repo)): Path<(String, String, String)>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
if repo.ends_with(".tar.gz") { | ||
let tag = gitlab_api_get_latest_tag( | ||
host.clone(), | ||
user.clone(), | ||
repo.clone() | ||
.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix") | ||
.to_string(), | ||
) | ||
.await | ||
.expect("failed to await gitlab_api_get_latest_tag"); | ||
let result_uri = format!( | ||
"/v1/gitlab/{}/{}/{}/v/{}.tar.gz", | ||
host, | ||
user, | ||
repo.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix"), | ||
tag, | ||
); | ||
trace!("{result_uri:#?}"); | ||
Redirect::to(&result_uri).into_response() | ||
} else { | ||
let body = format!( | ||
"Hi friend, you probably meant to request {:#?}{}.tar.gz, that should work <3", | ||
request.headers()["host"], | ||
request.uri() | ||
); | ||
(StatusCode::BAD_REQUEST, body).into_response() | ||
} | ||
} |
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,37 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::Path, | ||
http::{Request, StatusCode}, | ||
response::{IntoResponse, Redirect}, | ||
}; | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
pub async fn get_repo_ref( | ||
Path((host, user, repo, git_ref)): Path<(String, String, String, String)>, | ||
request: Request<Body>, | ||
) -> impl IntoResponse { | ||
if git_ref.ends_with(".tar.gz") { | ||
let git_ref_name = git_ref | ||
.strip_suffix(".tar.gz") | ||
.expect("couldn't strip .tar.gz suffix"); | ||
let uri = format!( | ||
"https://{}/{}/{}/-/archive/{}/{}-{}.tar.gz", | ||
host, user, repo, git_ref_name, repo, git_ref_name, | ||
); | ||
Redirect::to(&uri).into_response() | ||
} else { | ||
let body = format!( | ||
"Hi friend, you probably meant to request {:#?}{}.tar.gz, that should work <3", | ||
request.headers()["host"], | ||
request.uri() | ||
); | ||
(StatusCode::BAD_REQUEST, body).into_response() | ||
} | ||
} |
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,9 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
mod get_repo; | ||
pub use self::get_repo::get_repo; | ||
mod get_repo_ref; | ||
pub use self::get_repo_ref::get_repo_ref; |
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,9 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pub mod routes; | ||
|
||
mod endpoints; | ||
mod utils; |
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,19 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
use super::endpoints::{get_repo, get_repo_ref}; | ||
|
||
use axum::{routing::get, Router}; | ||
|
||
pub fn get_routes() -> Router { | ||
Router::new() | ||
.route("/:host/:user/:repo/b/:branch", get(get_repo_ref)) | ||
.route("/:host/:user/:repo/branch/:branch", get(get_repo_ref)) | ||
.route("/:host/:user/:repo/v/:version", get(get_repo_ref)) | ||
.route("/:host/:user/:repo/version/:version", get(get_repo_ref)) | ||
.route("/:host/:user/:repo/t/:version", get(get_repo_ref)) | ||
.route("/:host/:user/:repo/tag/:version", get(get_repo_ref)) | ||
.route("/:host/:user/:repo", get(get_repo)) | ||
} |
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,40 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
#[allow(unused)] | ||
use log::{debug, error, info, trace, warn}; | ||
|
||
pub async fn gitlab_api_get_latest_tag( | ||
host: String, | ||
user: String, | ||
repo: String, | ||
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { | ||
use reqwest::{ | ||
header::{ACCEPT, USER_AGENT}, | ||
Url, | ||
}; | ||
// TODO: The middle part, `{}%2F{}` is really `user/repo` URL-encoded. We | ||
// should do proper URL encoding. | ||
let version_uri = Url::parse(&format!( | ||
"https://{}/api/v4/projects/{}%2F{}/repository/tags", | ||
host, user, repo | ||
))?; | ||
trace!("{:#?}", version_uri); | ||
let client = reqwest::Client::builder().user_agent(USER_AGENT).build()?; | ||
let res = client | ||
.get(version_uri) | ||
.header(ACCEPT, "application/json") | ||
.send() | ||
.await? | ||
.json::<serde_json::Value>() | ||
.await?; | ||
|
||
trace!("got:\n {:#?}", res[0]["name"]); | ||
|
||
Ok(res[0]["name"] | ||
.as_str() | ||
.expect("failed to get release name as_str()") | ||
.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,7 @@ | ||
// SPDX-FileCopyrightText: 2023 Gergely Nagy | ||
// SPDX-FileContributor: Gergely Nagy | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
mod gitlab_api_get_latest_tag; | ||
pub use self::gitlab_api_get_latest_tag::gitlab_api_get_latest_tag; |
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 |
---|---|---|
|
@@ -8,3 +8,4 @@ pub mod routes; | |
mod flakehub; | ||
mod forgejo; | ||
mod github; | ||
mod gitlab; |
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