Skip to content

Commit

Permalink
rust-lang-nursery#43: Make thanks work again
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatraszek committed Feb 1, 2017
1 parent d43761e commit bdfb1eb
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
29 changes: 29 additions & 0 deletions src/authors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use models::{Author, NewAuthor};

use diesel;
use diesel::result::Error;
use diesel::pg::PgConnection;
use diesel::prelude::*;

pub fn load_or_create(conn: &PgConnection, author_name: &str, author_email: &str) -> Author {
use schema::authors::dsl::*;
use diesel::associations::HasTable;

match authors
.filter(name.eq(author_name))
.filter(email.eq(author_email))
.first(conn) {
Ok(author) => author,
Err(Error::NotFound) => {
let new_author = NewAuthor {
name: author_name,
email: author_email,
};
diesel::insert(&new_author)
.into(authors::table())
.get_result(conn)
.expect("Error saving new author")
},
Err(_) => panic!("Error loading author from the datebase")
}
}
3 changes: 2 additions & 1 deletion src/bin/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ fn main() {

// We tag all commits initially to the first release. Each release will
// set this properly below.
thanks::commits::create(&connection, &sha, &author_name, &author_email, &first_release);
let author = thanks::authors::load_or_create(&connection, &author_name, &author_email);
thanks::commits::create(&connection, &sha, &author, &first_release);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/bin/update-commit-db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ fn update_commit_db(log: &slog::Logger, project: &Project, connection: &PgConnec
},
Err(_) => {
info!(log, "Creating commit {} for release {}", object.sha, master_release.version);
let author = thanks::authors::load_or_create(&connection, &object.commit.author.name, &object.commit.author.email);
// this commit will be part of master
thanks::commits::create(connection, &object.sha, &object.commit.author.name, &object.commit.author.email, &master_release);
thanks::commits::create(connection, &object.sha, &author, &master_release);
},
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/commits.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use models::{Commit, NewCommit};
use models::Author;
use models::Release;

use diesel;
use diesel::pg::PgConnection;
use diesel::prelude::*;

pub fn create<'a>(conn: &PgConnection, sha: &'a str, author_name: &'a str, author_email: &'a str, release: &Release) -> Commit {
pub fn create<'a>(conn: &PgConnection, sha: &'a str, author: &Author, release: &Release) -> Commit {
use schema::commits;

let new_commit = NewCommit {
sha: sha,
release_id: release.id,
author_name: author_name,
author_email: author_email,
author_id: author.id,
};

diesel::insert(&new_commit).into(commits::table)
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod models;
pub mod projects;
pub mod releases;
pub mod commits;
pub mod authors;

use serde_json::value::Value;

Expand All @@ -42,15 +43,18 @@ pub fn establish_connection() -> PgConnection {

pub fn scores() -> Vec<Value> {
use schema::commits::dsl::*;
use schema::authors::dsl::*;
use diesel::expression::dsl::sql;
use diesel::types::BigInt;
use diesel::associations::HasTable;

let connection = establish_connection();

let scores: Vec<_> =
commits
.select((author_name, sql::<BigInt>("COUNT(author_name) AS author_count")))
.group_by(author_name)
commits::table()
.inner_join(authors::table())
.select((name, sql::<BigInt>("COUNT(author_id) AS author_count")))
.group_by((author_id, name))
.order(sql::<BigInt>("author_count").desc())
.load(&connection)
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub struct NewProject<'a> {
pub url_path: &'a str,
pub github_name: &'a str,
}

use super::schema::commits;

#[derive(Insertable)]
Expand Down
10 changes: 6 additions & 4 deletions src/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ pub fn assign_commits(log: &Logger, release_name: &str, previous_release: &str,

info!(log, "Creating commit {} for release {}", the_sha, the_release.version);

::commits::create(&connection, &the_sha, &the_author_name, &the_author_email, &the_release);
let author = ::authors::load_or_create(&connection, &the_author_name, &the_author_email);
::commits::create(&connection, &the_sha, &author, &the_release);
},
};
}
Expand All @@ -114,8 +115,9 @@ pub fn create(conn: &PgConnection, version: &str, project_id: i32) -> Release {
pub fn contributors(project: &str, release_name: &str) -> Option<Vec<Value>> {
use schema::releases::dsl::*;
use schema::commits::dsl::*;
use schema::authors::dsl::*;
use models::Release;
use models::Commit;
use diesel::associations::HasTable;

let connection = ::establish_connection();

Expand Down Expand Up @@ -144,8 +146,8 @@ pub fn contributors(project: &str, release_name: &str) -> Option<Vec<Value>> {
// it'd be better to do this in the db
// but Postgres doesn't do Unicode collation correctly on OSX
// http://postgresql.nabble.com/Collate-order-on-Mac-OS-X-text-with-diacritics-in-UTF-8-td1912473.html
let mut names: Vec<String> = Commit::belonging_to(&release)
.select(author_name).distinct().load(&connection).unwrap();
let mut names: Vec<String> = authors::table().inner_join(commits::table()).filter(release_id.eq(release.id))
.select(name).distinct().load(&connection).unwrap();

inaccurate_sort(&mut names);

Expand Down

0 comments on commit bdfb1eb

Please sign in to comment.