Skip to content

Commit

Permalink
fix(webserver): detect gitlab client errors more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 29, 2024
1 parent 4799a74 commit e54a1cb
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions ee/tabby-webserver/src/cron/db/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use anyhow::Result;
use chrono::Utc;
use gitlab::{
api::{projects::Projects, AsyncQuery, Pagination},
api::{projects::Projects, ApiError, AsyncQuery, Pagination},
GitlabBuilder,
};
use juniper::ID;
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn refresh_repositories_for_provider(
let provider = service.get_provider(provider_id).await?;
let repos = match fetch_all_repos(&provider).await {
Ok(repos) => repos,
Err(e) if e.to_string().contains("401 Unauthorized") => {
Err(e) if e.is_client_error() => {

Check warning on line 49 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L49

Added line #L49 was not covered by tests
service
.update_provider_status(provider.id.clone(), false)
.await?;
Expand All @@ -56,13 +56,13 @@ async fn refresh_repositories_for_provider(
provider.display_name
))
.await;
return Err(e);
return Err(e.into());

Check warning on line 59 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L59

Added line #L59 was not covered by tests
}
Err(e) => {
context
.stderr_writeline(format!("Failed to fetch repositories from gitlab: {e}"))
.await;
return Err(e);
return Err(e.into());

Check warning on line 65 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L65

Added line #L65 was not covered by tests
}
};
for repo in repos {
Expand Down Expand Up @@ -96,16 +96,44 @@ struct Repository {
http_url_to_repo: String,
}

#[derive(thiserror::Error, Debug)]

Check warning on line 99 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L99

Added line #L99 was not covered by tests
enum GitlabError {
#[error(transparent)]
Rest(#[from] gitlab::api::ApiError<gitlab::RestError>),
#[error(transparent)]
Gitlab(#[from] gitlab::GitlabError),
#[error(transparent)]
Projects(#[from] gitlab::api::projects::ProjectsBuilderError),
}

impl GitlabError {
fn is_client_error(&self) -> bool {
match self {
GitlabError::Rest(source)
| GitlabError::Gitlab(gitlab::GitlabError::Api { source }) => {
matches!(
source,

Check warning on line 115 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L110-L115

Added lines #L110 - L115 were not covered by tests
ApiError::Auth { .. }
| ApiError::Client {
source: gitlab::RestError::AuthError { .. }
}
| ApiError::Gitlab { .. }
)
}
_ => false,

Check warning on line 123 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L123

Added line #L123 was not covered by tests
}
}

Check warning on line 125 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L125

Added line #L125 was not covered by tests
}

async fn fetch_all_repos(
provider: &GitlabRepositoryProvider,
) -> Result<Vec<Repository>, anyhow::Error> {
) -> Result<Vec<Repository>, GitlabError> {

Check warning on line 130 in ee/tabby-webserver/src/cron/db/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/db/gitlab.rs#L130

Added line #L130 was not covered by tests
let Some(token) = &provider.access_token else {
return Ok(vec![]);
};
let gitlab = GitlabBuilder::new("gitlab.com", token)
.build_async()
.await?;

Ok(gitlab::api::paged(
Projects::builder().membership(true).build()?,
Pagination::All,
Expand Down

0 comments on commit e54a1cb

Please sign in to comment.