Skip to content

Commit

Permalink
fix: error management with gitea API
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Sopena Ballesteros committed Oct 28, 2024
1 parent d5eff8b commit bd0e4ed
Showing 1 changed file with 11 additions and 39 deletions.
50 changes: 11 additions & 39 deletions src/common/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,23 @@ pub mod http_client {
// NOTE: repo_name value must not contain the group (eg in CSSC gitlab we have
// alps/csm-config/template-management and in gitea is vcs/api/v1/repos/template-management
pub async fn get_commit_details_from_external_url(
repo_url: &str,
// repo_url: &str,
repo_name: &str,
commitid: &str,
gitea_token: &str,
shasta_root_cert: &[u8],
) -> Result<Value, crate::error::Error> {
let gitea_external_base_url = "https://api.cmn.alps.cscs.ch/vcs/";

let repo_name = repo_url
.trim_end_matches(".git")
.trim_start_matches(gitea_external_base_url);
/* let repo_name = repo_url
.trim_end_matches(".git")
.trim_start_matches(gitea_external_base_url); */

if repo_name.ne(repo_url) {
/* if repo_name.ne(repo_url) {
crate::error::Error::Message(
"repo url provided does not match gitea internal URL".to_string(),
);
}
} */

get_commit_details(
gitea_external_base_url,
Expand All @@ -228,34 +229,6 @@ pub mod http_client {
.await
}

pub async fn get_commit_details_from_internal_url(
repo_url: &str,
commitid: &str,
gitea_token: &str,
shasta_root_cert: &[u8],
) -> Result<Value, crate::error::Error> {
let gitea_internal_base_url = "https://api-gw-service-nmn.local/vcs/";

let repo_name = repo_url
.trim_end_matches(".git")
.trim_start_matches(gitea_internal_base_url);

if repo_name.ne(repo_url) {
crate::error::Error::Message(
"repo url provided does not match gitea internal URL".to_string(),
);
}

get_commit_details(
gitea_internal_base_url,
repo_name,
commitid,
gitea_token,
shasta_root_cert,
)
.await
}

pub async fn get_commit_details(
gitea_base_url: &str,
repo_name: &str,
Expand Down Expand Up @@ -284,6 +257,8 @@ pub mod http_client {
gitea_base_url, repo_name, commitid
);

log::info!("url to get commit details: {}", api_url);

let response = client
.get(api_url)
.header("Authorization", format!("token {}", gitea_token))
Expand All @@ -297,11 +272,8 @@ pub mod http_client {
.await
.map_err(|error| Error::NetError(error))
} else {
let payload = response
.json::<Value>()
.await
.map_err(|error| Error::NetError(error))?;
Err(Error::CsmError(payload))
let payload = response.text().await.unwrap();
Err(Error::CsmError(serde_json::json!({ "message": payload })))
}
}

Expand Down

0 comments on commit bd0e4ed

Please sign in to comment.