Skip to content

Commit 6973f6b

Browse files
committed
extract repository-stats logic into subcrate
1 parent 54220bd commit 6973f6b

File tree

19 files changed

+141
-117
lines changed

19 files changed

+141
-117
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222

2323
- uses: taiki-e/install-action@v2
2424
with:
25-
tool: just,sqlx-cli
25+
tool: just,sqlx-cli,fd-find
2626

2727
- name: restore build & cargo cache
2828
uses: Swatinem/rust-cache@v2

.sqlx/query-0107ab57a47a423721cc6257cf1572348bf76ecf16632fe625ebafa17f45738a.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

.sqlx/query-f550ed904fdb5d3ee6581fe1ad036c9b5b8db8765d5665042deb9ade67394d3c.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

Cargo.lock

Lines changed: 24 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ docs_rs_headers = { path = "crates/lib/docs_rs_headers" }
5656
docs_rs_logging = { path = "crates/lib/docs_rs_logging" }
5757
docs_rs_opentelemetry = { path = "crates/lib/docs_rs_opentelemetry" }
5858
docs_rs_registry_api = { path = "crates/lib/docs_rs_registry_api" }
59+
docs_rs_repository_stats = { path = "crates/lib/docs_rs_repository_stats" }
5960
docs_rs_types = { path = "crates/lib/docs_rs_types" }
6061
docs_rs_uri = { path = "crates/lib/docs_rs_uri" }
6162
docs_rs_utils = { path = "crates/lib/docs_rs_utils" }
@@ -120,7 +121,6 @@ serde_json = { workspace = true }
120121
bincode = { workspace = true }
121122

122123
# axum dependencies
123-
async-trait = "0.1.83"
124124
axum = { version = "0.8.1", features = ["macros"] }
125125
axum-extra = { workspace = true }
126126
tower = "0.5.1"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "docs_rs_repository_stats"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
anyhow = { workspace = true }
8+
async-trait = "0.1.89"
9+
chrono = { workspace = true }
10+
docs_rs_cargo_metadata = { path = "../docs_rs_cargo_metadata" }
11+
docs_rs_database = { path = "../docs_rs_database" }
12+
docs_rs_env_vars = { path = "../docs_rs_env_vars" }
13+
docs_rs_utils = { path = "../docs_rs_utils" }
14+
futures-util = { workspace = true }
15+
regex = { workspace = true }
16+
reqwest = { workspace = true }
17+
serde = { workspace = true }
18+
serde_json = { workspace = true }
19+
sqlx = { workspace = true }
20+
thiserror = { workspace = true }
21+
tracing = { workspace = true }
22+
23+
[dev-dependencies]
24+
mockito = { workspace = true }
25+
tokio = { workspace = true }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use docs_rs_env_vars::{env, maybe_env};
2+
3+
#[derive(Debug)]
4+
pub struct Config {
5+
// Github authentication
6+
pub(crate) github_accesstoken: Option<String>,
7+
pub(crate) github_updater_min_rate_limit: u32,
8+
9+
// GitLab authentication
10+
pub(crate) gitlab_accesstoken: Option<String>,
11+
}
12+
13+
impl Config {
14+
pub fn from_environment() -> anyhow::Result<Self> {
15+
Ok(Self {
16+
github_accesstoken: maybe_env("DOCSRS_GITHUB_ACCESSTOKEN")?,
17+
github_updater_min_rate_limit: env("DOCSRS_GITHUB_UPDATER_MIN_RATE_LIMIT", 2500u32)?,
18+
gitlab_accesstoken: maybe_env("DOCSRS_GITLAB_ACCESSTOKEN")?,
19+
})
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[derive(Debug, thiserror::Error)]
2+
#[error("rate limit reached")]
3+
pub struct RateLimitReached;

src/repositories/github.rs renamed to crates/lib/docs_rs_repository_stats/src/github.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
use crate::Config;
2-
use crate::error::Result;
1+
use crate::{
2+
RateLimitReached,
3+
config::Config,
4+
updater::{FetchRepositoriesResult, Repository, RepositoryForge, RepositoryName},
5+
};
6+
use anyhow::Result;
37
use async_trait::async_trait;
48
use chrono::{DateTime, Utc};
9+
use docs_rs_utils::APP_USER_AGENT;
510
use reqwest::{
611
Client as HttpClient,
712
header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT},
813
};
914
use serde::Deserialize;
1015
use tracing::{trace, warn};
1116

12-
use crate::{
13-
APP_USER_AGENT,
14-
repositories::{
15-
FetchRepositoriesResult, RateLimitReached, Repository, RepositoryForge, RepositoryName,
16-
},
17-
};
18-
1917
const GRAPHQL_UPDATE: &str = "query($ids: [ID!]!) {
2018
nodes(ids: $ids) {
2119
... on Repository {
@@ -92,10 +90,6 @@ impl RepositoryForge for GitHub {
9290
"github.com"
9391
}
9492

95-
fn icon(&self) -> &'static str {
96-
"github"
97-
}
98-
9993
/// How many repositories to update in a single chunk. Values over 100 are probably going to be
10094
/// rejected by the GraphQL API.
10195
fn chunk_size(&self) -> usize {
@@ -268,19 +262,18 @@ struct GraphIssues {
268262

269263
#[cfg(test)]
270264
mod tests {
271-
use super::{Config, GitHub};
272-
use crate::repositories::RateLimitReached;
273-
use crate::repositories::updater::{RepositoryForge, repository_name};
274-
use crate::test::TestEnvironment;
265+
use crate::{
266+
Config, GitHub, RateLimitReached,
267+
updater::{RepositoryForge, repository_name},
268+
};
275269
use anyhow::Result;
276270

277271
const TEST_TOKEN: &str = "qsjdnfqdq";
278272

279273
fn github_config() -> anyhow::Result<Config> {
280-
TestEnvironment::base_config()
281-
.github_accesstoken(Some(TEST_TOKEN.to_owned()))
282-
.build()
283-
.map_err(Into::into)
274+
let mut cfg = Config::from_environment()?;
275+
cfg.github_accesstoken = Some(TEST_TOKEN.to_owned());
276+
Ok(cfg)
284277
}
285278

286279
async fn mock_server_and_github(config: &Config) -> (mockito::ServerGuard, GitHub) {

src/repositories/gitlab.rs renamed to crates/lib/docs_rs_repository_stats/src/gitlab.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::error::Result;
1+
use anyhow::Result;
22
use async_trait::async_trait;
33
use chrono::{DateTime, Utc};
4+
use docs_rs_utils::APP_USER_AGENT;
45
use reqwest::{
56
Client as HttpClient,
67
header::{ACCEPT, AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT},
@@ -11,10 +12,8 @@ use std::str::FromStr;
1112
use tracing::warn;
1213

1314
use crate::{
14-
APP_USER_AGENT,
15-
repositories::{
16-
FetchRepositoriesResult, RateLimitReached, Repository, RepositoryForge, RepositoryName,
17-
},
15+
RateLimitReached,
16+
updater::{FetchRepositoriesResult, Repository, RepositoryForge, RepositoryName},
1817
};
1918

2019
const GRAPHQL_UPDATE: &str = "query($ids: [ID!]!) {
@@ -90,10 +89,6 @@ impl RepositoryForge for GitLab {
9089
self.host
9190
}
9291

93-
fn icon(&self) -> &'static str {
94-
"gitlab"
95-
}
96-
9792
fn chunk_size(&self) -> usize {
9893
100
9994
}
@@ -266,9 +261,10 @@ struct GraphProject {
266261

267262
#[cfg(test)]
268263
mod tests {
269-
use super::GitLab;
270-
use crate::repositories::RateLimitReached;
271-
use crate::repositories::updater::{RepositoryForge, repository_name};
264+
use crate::{
265+
GitLab, RateLimitReached,
266+
updater::{RepositoryForge, repository_name},
267+
};
272268
use anyhow::Result;
273269

274270
async fn mock_server_and_gitlab() -> (mockito::ServerGuard, GitLab) {

0 commit comments

Comments
 (0)