Skip to content

Commit 3326538

Browse files
Add gitlab support
1 parent 48635ff commit 3326538

13 files changed

+605
-33
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ docker-compose run -- database add-directory <DIRECTORY> [PREFIX]
191191
# You need to set CRATESFYI_GITHUB_USERNAME, CRATESFYI_GITHUB_ACCESSTOKEN
192192
# environment variables in order to run this command.
193193
# You can set this environment variables in ~/.cratesfyi.env file.
194-
docker-compose run -- database update-github-fields
194+
cargo run -- database update-github-fields
195+
# Updates github stats for crates.
196+
# You need to set CRATESFYI_GITLAB_ACCESSTOKEN environment variable.
197+
cargo run -- database update-gitlab-fields
195198
```
196199

197200
If you want to explore or edit database manually, you can connect to the database

src/bin/cratesfyi.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ enum DatabaseSubcommand {
377377
/// Backfill GitHub stats for crates.
378378
BackfillGithubStats,
379379

380+
/// Updates gitlab stats for crates.
381+
UpdateGitlabFields,
382+
383+
/// Backfill Gitlab stats for crates.
384+
BackfillGitlabStats,
385+
380386
/// Updates info for a crate from the registry's API
381387
UpdateCrateRegistryFields {
382388
#[structopt(name = "CRATE")]
@@ -436,6 +442,18 @@ impl DatabaseSubcommand {
436442
.backfill_repositories()?;
437443
}
438444

445+
Self::UpdateGitlabFields => {
446+
docs_rs::utils::GitlabUpdater::new(ctx.config()?, ctx.pool()?)?
447+
.ok_or_else(|| failure::format_err!("missing Gitlab token"))?
448+
.update_all_crates()?;
449+
}
450+
451+
Self::BackfillGitlabStats => {
452+
docs_rs::utils::GitlabUpdater::new(ctx.config()?, ctx.pool()?)?
453+
.ok_or_else(|| failure::format_err!("missing Gitlab token"))?
454+
.backfill_repositories()?;
455+
}
456+
439457
Self::UpdateCrateRegistryFields { name } => {
440458
let index = ctx.index()?;
441459

src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ pub struct Config {
3030
pub(crate) github_accesstoken: Option<String>,
3131
pub(crate) github_updater_min_rate_limit: u32,
3232

33+
// Gitlab authentication
34+
pub(crate) gitlab_accesstoken: Option<String>,
35+
pub(crate) gitlab_updater_min_rate_limit: u32,
36+
3337
// Max size of the files served by the docs.rs frontend
3438
pub(crate) max_file_size: usize,
3539
pub(crate) max_file_size_html: usize,
@@ -77,6 +81,9 @@ impl Config {
7781
github_accesstoken: maybe_env("CRATESFYI_GITHUB_ACCESSTOKEN")?,
7882
github_updater_min_rate_limit: env("DOCSRS_GITHUB_UPDATER_MIN_RATE_LIMIT", 2500)?,
7983

84+
gitlab_accesstoken: maybe_env("CRATESFYI_GITLAB_ACCESSTOKEN")?,
85+
gitlab_updater_min_rate_limit: env("DOCSRS_GITLAB_UPDATER_MIN_RATE_LIMIT", 2500)?,
86+
8087
max_file_size: env("DOCSRS_MAX_FILE_SIZE", 50 * 1024 * 1024)?,
8188
max_file_size_html: env("DOCSRS_MAX_FILE_SIZE_HTML", 50 * 1024 * 1024)?,
8289
// LOL HTML only uses as much memory as the size of the start tag!

src/db/add_package.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) fn add_package_into_database(
3838
has_examples: bool,
3939
compression_algorithms: std::collections::HashSet<CompressionAlgorithm>,
4040
github_repo: Option<String>,
41+
gitlab_repo: Option<String>,
4142
) -> Result<i32> {
4243
debug!("Adding package into database");
4344
let crate_id = initialize_package_in_database(conn, metadata_pkg)?;
@@ -55,12 +56,14 @@ pub(crate) fn add_package_into_database(
5556
homepage_url, description, description_long, readme,
5657
authors, keywords, have_examples, downloads, files,
5758
doc_targets, is_library, doc_rustc_version,
58-
documentation_url, default_target, features, github_repo
59+
documentation_url, default_target, features,
60+
github_repo, gitlab_repo
5961
)
6062
VALUES (
6163
$1, $2, $3, $4, $5, $6, $7, $8, $9,
6264
$10, $11, $12, $13, $14, $15, $16, $17, $18,
63-
$19, $20, $21, $22, $23, $24, $25, $26, $27
65+
$19, $20, $21, $22, $23, $24, $25, $26, $27,
66+
$28
6467
)
6568
ON CONFLICT (crate_id, version) DO UPDATE
6669
SET release_time = $3,
@@ -87,7 +90,8 @@ pub(crate) fn add_package_into_database(
8790
documentation_url = $24,
8891
default_target = $25,
8992
features = $26,
90-
github_repo = $27
93+
github_repo = $27,
94+
gitlab_repo = $28
9195
RETURNING id",
9296
&[
9397
&crate_id,
@@ -117,6 +121,7 @@ pub(crate) fn add_package_into_database(
117121
&default_target,
118122
&features,
119123
&github_repo,
124+
&gitlab_repo,
120125
],
121126
)?;
122127

src/db/migrate.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,43 @@ pub fn migrate(version: Option<Version>, conn: &mut Client) -> CratesfyiResult<(
618618
DROP INDEX github_repos_stars_idx;
619619
",
620620
),
621+
migration!(
622+
context,
623+
27,
624+
// description
625+
"Add the gitlab_repositories table to contain Gitlab information",
626+
// upgrade query
627+
"
628+
CREATE TABLE gitlab_repos (
629+
id VARCHAR PRIMARY KEY NOT NULL,
630+
name VARCHAR NOT NULL,
631+
description VARCHAR,
632+
last_commit TIMESTAMP,
633+
stars INT NOT NULL,
634+
forks INT NOT NULL,
635+
issues INT NOT NULL,
636+
updated_at TIMESTAMP NOT NULL
637+
);
638+
639+
ALTER TABLE releases ADD COLUMN gitlab_repo VARCHAR
640+
REFERENCES gitlab_repos(id) ON DELETE SET NULL;
641+
642+
CREATE INDEX releases_gitlab_repo_idx ON releases (gitlab_repo);
643+
CREATE INDEX gitlab_repos_stars_idx ON gitlab_repos(stars DESC);
644+
645+
ALTER TABLE gitlab_repos
646+
ALTER updated_at TYPE timestamptz USING updated_at AT TIME ZONE 'UTC',
647+
ALTER last_commit TYPE timestamptz USING last_commit AT TIME ZONE 'UTC';
648+
",
649+
// downgrade query
650+
"
651+
ALTER TABLE releases DROP COLUMN gitlab_repo;
652+
DROP TABLE gitlab_repos;
653+
654+
DROP INDEX releases_github_repo_idx;
655+
DROP INDEX github_repos_stars_idx;
656+
"
657+
),
621658
];
622659

623660
for migration in migrations {

src/docbuilder/rustwide_builder.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::docbuilder::{crates::crates_from_path, Limits};
77
use crate::error::Result;
88
use crate::index::api::ReleaseData;
99
use crate::storage::CompressionAlgorithms;
10-
use crate::utils::{copy_doc_dir, parse_rustc_version, CargoMetadata, GithubUpdater};
10+
use crate::utils::{
11+
copy_doc_dir, parse_rustc_version, CargoMetadata, GithubUpdater, GitlabUpdater,
12+
};
1113
use crate::{db::blacklist::is_blacklisted, utils::MetadataPackage};
1214
use crate::{Config, Context, Index, Metrics, Storage};
1315
use docsrs_metadata::{Metadata, DEFAULT_TARGETS, HOST_TARGET};
@@ -394,6 +396,7 @@ impl RustwideBuilder {
394396

395397
let cargo_metadata = res.cargo_metadata.root();
396398
let github_repo = self.get_github_repo(&mut conn, cargo_metadata)?;
399+
let gitlab_repo = self.get_gitlab_repo(&mut conn, cargo_metadata)?;
397400

398401
let release_id = add_package_into_database(
399402
&mut conn,
@@ -408,6 +411,7 @@ impl RustwideBuilder {
408411
has_examples,
409412
algs,
410413
github_repo,
414+
gitlab_repo,
411415
)?;
412416

413417
if let Some(doc_coverage) = res.doc_coverage {
@@ -701,6 +705,34 @@ impl RustwideBuilder {
701705
}
702706
}
703707
}
708+
709+
fn get_gitlab_repo(
710+
&self,
711+
conn: &mut Client,
712+
metadata: &MetadataPackage,
713+
) -> Result<Option<String>> {
714+
let updater = match GitlabUpdater::new(self.config.clone(), self.db.clone())? {
715+
Some(updater) => updater,
716+
None => {
717+
warn!("did not collect Gitlab stats as no token was provided");
718+
return Ok(None);
719+
}
720+
};
721+
let repo = match &metadata.repository {
722+
Some(url) => url,
723+
None => {
724+
debug!("did not collect Gitlab stats as no repository URL was present");
725+
return Ok(None);
726+
}
727+
};
728+
match updater.load_repository(conn, repo) {
729+
Ok(repo) => Ok(repo),
730+
Err(err) => {
731+
warn!("failed to collect Gitlab stats: {}", err);
732+
Ok(None)
733+
}
734+
}
735+
}
704736
}
705737

706738
struct FullBuildResult {

src/utils/daemon.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This daemon will start web server, track new packages and build them
44
55
use crate::{
6-
utils::{queue_builder, update_release_activity, GithubUpdater},
6+
utils::{queue_builder, update_release_activity, GithubUpdater, GitlabUpdater},
77
Context, DocBuilder, RustwideBuilder,
88
};
99
use chrono::{Timelike, Utc};
@@ -93,7 +93,7 @@ pub fn start_daemon(context: &dyn Context, enable_registry_watcher: bool) -> Res
9393
},
9494
)?;
9595

96-
if let Some(github_updater) = GithubUpdater::new(config, context.pool()?)? {
96+
if let Some(github_updater) = GithubUpdater::new(config.clone(), context.pool()?)? {
9797
cron(
9898
"github stats updater",
9999
Duration::from_secs(60 * 60),
@@ -106,6 +106,19 @@ pub fn start_daemon(context: &dyn Context, enable_registry_watcher: bool) -> Res
106106
log::warn!("GitHub stats updater not started as no token was provided");
107107
}
108108

109+
if let Some(gitlab_updater) = GitlabUpdater::new(config, context.pool()?)? {
110+
cron(
111+
"gitlab stats updater",
112+
Duration::from_secs(60 * 60),
113+
move || {
114+
gitlab_updater.update_all_crates()?;
115+
Ok(())
116+
},
117+
)?;
118+
} else {
119+
log::warn!("Gitlab stats updater not started as no token was provided");
120+
}
121+
109122
// Never returns; `server` blocks indefinitely when dropped
110123
// NOTE: if a failure occurred earlier in `start_daemon`, the server will _not_ be joined -
111124
// instead it will get killed when the process exits.

src/utils/github_updater.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const GRAPHQL_UPDATE: &str = "query($ids: [ID!]!) {
2828
description
2929
stargazerCount
3030
forkCount
31-
issues(states: [OPEN]) { totalCount }
31+
issues { totalCount }
3232
}
3333
}
3434
rateLimit {
@@ -44,7 +44,7 @@ const GRAPHQL_SINGLE: &str = "query($owner: String!, $repo: String!) {
4444
description
4545
stargazerCount
4646
forkCount
47-
issues(states: [OPEN]) { totalCount }
47+
issues { totalCount }
4848
}
4949
}";
5050

0 commit comments

Comments
 (0)