Skip to content

Commit

Permalink
updated repositories table
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMimir committed Oct 13, 2023
1 parent 6e9d07b commit a96d3ed
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 28 deletions.
3 changes: 3 additions & 0 deletions backend/api/src/api/cryptocurrencies/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ impl DbRepositoryContract for PgRepository {
.columns([
github_repositories::Column::Id,
github_repositories::Column::RepositoryName,
github_repositories::Column::Language,
github_repositories::Column::StargazersCount,
github_repositories::Column::ForksCount,
])
.into_model::<Repository>()
.all(self.conn.as_ref())
Expand Down
3 changes: 3 additions & 0 deletions backend/api/src/api/repository/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ impl DbRepositoryContract for PgRepository {
.select_only()
.column(github_repositories::Column::Id)
.column_as(github_repositories::Column::RepositoryName, "name")
.column(github_repositories::Column::Language)
.column(github_repositories::Column::ForksCount)
.column_as(github_repositories::Column::StargazersCount, "stars_count")
.column_as(github_repositories::Column::Project, "project_id")
.column_as(github_projects::Column::Name, "project")
.into_model::<RepositoryView>()
Expand Down
3 changes: 2 additions & 1 deletion backend/api/src/jobs/github_repositories/contract.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use error::Result;
use sdks::github::data::GithubRepository;
use sea_orm::prelude::Uuid;
use store::github_projects::Model;

Expand All @@ -15,5 +16,5 @@ pub trait DbServiceContract {
///
/// Create entries in `github_repositories` table
///
async fn create_repository(&self, project_id: Uuid, repositories: Vec<String>) -> Result<()>;
async fn create_repository(&self, project_id: Uuid, repositories: Vec<GithubRepository>) -> Result<()>;
}
3 changes: 3 additions & 0 deletions backend/api/src/jobs/github_repositories/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl<
self.service
.create_repository(github.id, repositories)
.await?;

page += 1;
}

Expand All @@ -84,6 +85,8 @@ impl<
/// Spawns tokio task, that waits for a day, then runs once a week
///
pub fn spawn_cron(self) -> JoinHandle<()> {
info!("Spawning github repositories task");

tokio::spawn(async move {
let mut interval = interval_at(
(Instant::now() + Duration::from_secs(86_400)).into(),
Expand Down
30 changes: 21 additions & 9 deletions backend/api/src/jobs/github_repositories/infrastructure/service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use error::Result;
use sdks::github::data::GithubRepository;
use sea_orm::{
prelude::Uuid, sea_query::OnConflict, ActiveValue::Set, DatabaseConnection, EntityTrait,
};
Expand All @@ -19,22 +20,33 @@ impl PgService {

#[async_trait]
impl DbServiceContract for PgService {
async fn create_repository(&self, project: Uuid, repositories: Vec<String>) -> Result<()> {
async fn create_repository(
&self,
project: Uuid,
repositories: Vec<GithubRepository>,
) -> Result<()> {
let models = repositories
.into_iter()
.map(|repository_name| ActiveModel {
.map(|repository| ActiveModel {
id: Default::default(),
project: Set(project.to_owned()),
repository_name: Set(repository_name),
..Default::default()
repository_name: Set(repository.name),
language: Set(repository.language),
stargazers_count: Set(repository.stargazers_count),
forks_count: Set(repository.forks_count),
})
.collect::<Vec<_>>();

let mut on_conflict = OnConflict::columns([Column::Project, Column::RepositoryName]);

on_conflict.update_columns([
Column::ForksCount,
Column::StargazersCount,
Column::Language,
]);

Entity::insert_many(models)
.on_conflict(
OnConflict::columns([Column::Project, Column::RepositoryName])
.do_nothing()
.to_owned(),
)
.on_conflict(on_conflict)
.exec(self.conn.as_ref())
.await?;

Expand Down
1 change: 1 addition & 0 deletions backend/libs/sdks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ serde.workspace = true
serde_json.workspace = true

[dev-dependencies]
config.workspace = true
tokio.workspace = true
4 changes: 2 additions & 2 deletions backend/libs/sdks/src/github/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use error::Result;

use super::data::GithubIssue;
use super::data::{GithubIssue, GithubRepository};

#[async_trait]
pub trait GithubContract {
///
/// Returns repos for github username for page
///
async fn get_repos(&self, username: &str, page: u64) -> Result<Vec<String>>;
async fn get_repos(&self, username: &str, page: u64) -> Result<Vec<GithubRepository>>;

///
/// Get issues for repository
Expand Down
13 changes: 5 additions & 8 deletions backend/libs/sdks/src/github/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ use chrono::NaiveDateTime;
use error::Error;
use serde::{de::Error as DeError, Deserialize, Deserializer};

#[derive(Deserialize)]
pub(super) struct GithubRepository {
#[derive(Deserialize, Debug)]
pub struct GithubRepository {
pub name: String,
}

impl GithubRepository {
pub fn into(response: Vec<Self>) -> Vec<String> {
response.into_iter().map(|gr| gr.name).collect()
}
pub language: Option<String>,
pub stargazers_count: i64,
pub forks_count: i64
}

#[derive(Deserialize)]
Expand Down
6 changes: 2 additions & 4 deletions backend/libs/sdks/src/github/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ impl Github {

#[async_trait]
impl GithubContract for Github {
async fn get_repos(&self, username: &str, page: u64) -> Result<Vec<String>> {
async fn get_repos(&self, username: &str, page: u64) -> Result<Vec<GithubRepository>> {
let url = format!("https://api.github.com/users/{username}/repos?page={page}&per_page=100");
let response = self.get(url).await.map_err(|e| e.add_cause(username))?;

Ok(GithubRepository::into(response))
self.get(url).await.map_err(|e| e.add_cause(username))
}

async fn get_issues(
Expand Down
23 changes: 19 additions & 4 deletions backend/libs/sdks/tests/github.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use error::{Error, Result};
use sdks::github::{Github, GithubContract};

fn client() -> Github {
let key = config::get("GITHUB_KEY");

match key.ok() {
Some(api_key) => Github::new_with_auth(api_key),
None => Github::default(),
}
}

#[tokio::test]
async fn test_repos() -> Result<()> {
let github = Github::default();

let mut repos = github.get_repos("bitcoin", 1).await?;
let github = client();

let mut repos = github
.get_repos("bitcoin", 1)
.await?
.into_iter()
.map(|repo| repo.name)
.collect::<Vec<_>>();

repos.sort();
assert_eq!(repos, vec!["bips", "bitcoin", "libbase58", "libblkmaker"]);

Expand All @@ -14,7 +29,7 @@ async fn test_repos() -> Result<()> {

#[tokio::test]
async fn test_issues() -> Result<()> {
let github = Github::default();
let github = client();

let issues = github.get_issues("bitcoin", "bitcoin", 1).await?;

Expand Down
3 changes: 3 additions & 0 deletions backend/libs/store/src/migrations/github_repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub struct Model {
pub id: Uuid,
pub project: Uuid,
pub repository_name: String,
pub language: Option<String>,
pub stargazers_count: i64,
pub forks_count: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ pub struct CryptoCurrencyWithRepositories {
pub struct Repository {
pub id: Uuid,
pub repository_name: String,
pub language: Option<String>,
pub stargazers_count: i64,
pub forks_count: i64,
}
3 changes: 3 additions & 0 deletions backend/libs/store/src/objects/repository_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use sea_orm::{prelude::Uuid, FromQueryResult};
pub struct RepositoryView {
pub id: Uuid,
pub name: String,
pub language: Option<String>,
pub forks_count: i64,
pub stars_count: i64,
pub project_id: Uuid,
pub project: String,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ create table github_repositories(
id uuid primary key default uuid_generate_v4() not null,
project uuid not null references github_projects(id) on delete cascade,
repository_name varchar(255) not null,
language varchar(255),
stargazers_count bigint not null,
forks_count bigint not null,
unique(project, repository_name)
)

0 comments on commit a96d3ed

Please sign in to comment.