Skip to content

Commit

Permalink
Added issue labels
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMimir committed Dec 9, 2023
1 parent 9068478 commit 87c23a7
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 27 deletions.
1 change: 1 addition & 0 deletions backend/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ store.workspace = true
support.workspace = true
config.workspace = true
actix-cors.workspace = true
futures = "0.3.29"

[dev-dependencies]
adtest.workspace = true
Expand Down
21 changes: 19 additions & 2 deletions backend/api/src/api/repository/infrastructure/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sea_orm::{
};
use std::sync::Arc;
use store::{
github_projects, github_repositories, issues,
github_projects, github_repositories, issue_labels, issues,
objects::{GithubIssue, RepositoryView, SearchRepository},
};
use support::pagination::Pagination;
Expand All @@ -34,7 +34,24 @@ impl DbRepositoryContract for PgRepository {
repository_id: Uuid,
params: GetIssuesParams,
) -> Result<Pagination<GithubIssue>> {
let mut query = issues::Entity::find().filter(issues::Column::Repository.eq(repository_id));
let mut query = issues::Entity::find()
.filter(issues::Column::Repository.eq(repository_id))
.inner_join(issue_labels::Entity)
.select_only()
.columns([
issues::Column::Id,
issues::Column::Repository,
issues::Column::Issue,
issues::Column::Title,
issues::Column::Description,
issues::Column::CreatedAt,
issues::Column::Closed,
])
.column_as(
Expr::cust_with_expr("array_agg($1)", issue_labels::Column::Name.into_expr()),
"labels",
)
.group_by(issues::Column::Id);

if let Some(closed) = params.closed {
query = query.filter(issues::Column::Closed.eq(closed));
Expand Down
4 changes: 2 additions & 2 deletions backend/api/src/jobs/github_issues/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait DbRepositoryContract {
#[async_trait]
pub trait DbServiceContract {
///
/// Create entities in `issues` table
/// Create single issue in `issues` table, and it's labels
///
async fn create_issues(&self, repository_id: Uuid, issues: Vec<GithubIssue>) -> Result<()>;
async fn create_issues(&self, repository_id: Uuid, issue: GithubIssue) -> Result<()>;
}
11 changes: 10 additions & 1 deletion backend/api/src/jobs/github_issues/domain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cronus::{Job, Schedule};
use error::{Error, Result};
use futures::future::join_all;
use sdks::github::GithubContract;
use std::time::Duration;
use store::{
Expand Down Expand Up @@ -88,7 +89,15 @@ impl<
break;
}

self.service.create_issues(repository.id, issues).await?;
let futures = issues
.into_iter()
.map(|issue| self.service.create_issues(repository.id, issue));

let results = join_all(futures).await;
for result in results {
result?;
}

page += 1;
}

Expand Down
74 changes: 52 additions & 22 deletions backend/api/src/jobs/github_issues/infrastructure/service.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use error::Result;
use error::{Error, Result};
use sdks::github::data::{GithubIssue, State};
use sea_orm::{
prelude::Uuid, sea_query::OnConflict, ActiveValue::Set, DatabaseConnection, EntityTrait,
prelude::Uuid,
sea_query::OnConflict,
ActiveValue::{NotSet, Set},
DatabaseConnection, EntityTrait, TransactionTrait,
};
use std::sync::Arc;

use super::super::contract::DbServiceContract;
use store::issues::{ActiveModel, Column, Entity};
use store::{
issue_labels,
issues::{ActiveModel, Column, Entity},
};

pub struct PgService {
conn: Arc<DatabaseConnection>,
Expand All @@ -20,27 +26,51 @@ impl PgService {

#[async_trait]
impl DbServiceContract for PgService {
async fn create_issues(&self, repository_id: Uuid, issues: Vec<GithubIssue>) -> Result<()> {
let models = issues
.into_iter()
.map(|issue| ActiveModel {
repository: Set(repository_id),
issue: Set(issue.number),
title: Set(issue.title),
description: Set(issue.description),
created_at: Set(issue.created_at),
closed: Set(issue.state == State::Closed),
id: Default::default(),
})
.collect::<Vec<_>>();
async fn create_issues(&self, repository_id: Uuid, issue: GithubIssue) -> Result<()> {
self.conn
.as_ref()
.transaction::<_, (), sea_orm::error::DbErr>(|txn| {
Box::pin(async move {
let model = ActiveModel {
repository: Set(repository_id),
issue: Set(issue.number),
title: Set(issue.title),
description: Set(issue.description),
created_at: Set(issue.created_at),
closed: Set(issue.state == State::Closed),
id: Default::default(),
};

let mut on_conflict = OnConflict::columns([Column::Repository, Column::Issue]);
on_conflict.update_columns([
Column::Title,
Column::Description,
Column::Closed,
]);

let mut on_conflict = OnConflict::columns([Column::Repository, Column::Issue]);
on_conflict.update_columns([Column::Title, Column::Description, Column::Closed]);
let issue_id = Entity::insert(model)
.on_conflict(on_conflict)
.exec_with_returning(txn)
.await?
.id;

Entity::insert_many(models)
.on_conflict(on_conflict)
.exec(self.conn.as_ref())
.await?;
let labels = issue
.labels
.into_iter()
.map(|label| issue_labels::ActiveModel {
id: NotSet,
issue_id: Set(issue_id),
name: Set(label.name),
})
.collect::<Vec<_>>();

issue_labels::Entity::insert_many(labels).exec(txn).await?;

Ok(())
})
})
.await
.map_err(|e| Error::InternalServer(e.to_string()))?;

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions backend/libs/sdks/src/github/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ pub struct GithubIssue {
#[serde(deserialize_with = "deserialize_datetime")]
pub updated_at: NaiveDateTime,
pub state: State,
pub labels: Vec<Label>
}

#[derive(Deserialize)]
pub struct Label{
pub name: String
}

#[derive(Deserialize, PartialEq, Eq)]
Expand Down
34 changes: 34 additions & 0 deletions backend/libs/store/src/migrations/issue_labels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "issue_labels")]
#[serde(rename_all = "camelCase")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub issue_id: Uuid,
#[sea_orm(column_type = "Text")]
pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::issues::Entity",
from = "Column::IssueId",
to = "super::issues::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Issues,
}

impl Related<super::issues::Entity> for Entity {
fn to() -> RelationDef {
Relation::Issues.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
8 changes: 8 additions & 0 deletions backend/libs/store/src/migrations/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum Relation {
on_delete = "Cascade"
)]
GithubRepositories,
#[sea_orm(has_many = "super::issue_labels::Entity")]
IssueLabels,
}

impl Related<super::github_repositories::Entity> for Entity {
Expand All @@ -36,4 +38,10 @@ impl Related<super::github_repositories::Entity> for Entity {
}
}

impl Related<super::issue_labels::Entity> for Entity {
fn to() -> RelationDef {
Relation::IssueLabels.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
1 change: 1 addition & 0 deletions backend/libs/store/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod prelude;
pub mod cryptocurrencies;
pub mod github_projects;
pub mod github_repositories;
pub mod issue_labels;
pub mod issues;
pub mod topics_repositories;
1 change: 1 addition & 0 deletions backend/libs/store/src/migrations/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
pub use super::cryptocurrencies::Entity as Cryptocurrencies;
pub use super::github_projects::Entity as GithubProjects;
pub use super::github_repositories::Entity as GithubRepositories;
pub use super::issue_labels::Entity as IssueLabels;
pub use super::issues::Entity as Issues;
pub use super::topics_repositories::Entity as TopicsRepositories;
1 change: 1 addition & 0 deletions backend/libs/store/src/objects/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub struct GithubIssue {
pub description: Option<String>,
pub created_at: DateTime,
pub closed: bool,
pub labels: Vec<String>
}
1 change: 1 addition & 0 deletions backend/migrations/2023-12-09-122605_issue-labels/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table issue_labels;
5 changes: 5 additions & 0 deletions backend/migrations/2023-12-09-122605_issue-labels/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table issue_labels(
id uuid primary key default uuid_generate_v4() not null,
issue_id uuid not null references issues(id) on delete cascade,
name text not null
)

0 comments on commit 87c23a7

Please sign in to comment.