Skip to content

Commit

Permalink
Git diff if the commit hasn't already been stored
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
niksudan committed Oct 24, 2016
1 parent e257290 commit 4cc0d95
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

```js
new DevRPG(config).run('/', 3000, (commits) => {
console.log(`Detected ${commits.length} new commit(s)`);
console.log(`Stored ${commits.length} new commit(s)`);
});
```
```
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const DevRPG = require('./src/devrpg');
const rpg = new DevRPG(config);

rpg.run('/', 3100, (commits) => {
console.log(`Detected ${commits.length} new commit(s)`);
console.log(`Stored ${commits.length} new commit(s)`);
});
87 changes: 54 additions & 33 deletions src/devrpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DevRPG {
process(data) {
return new Promise((resolve, reject) => {
const result = [];
const cache = [];
let checkedCommits = 0;

// Only accept push events
Expand All @@ -64,35 +65,64 @@ class DevRPG {

data.commits.forEach((commitData) => {

// Generate user
const user = new User({
name: commitData.author.name,
email: commitData.author.email,
});
if (!result[user.getName()]) {
result[user.getName()] = user;
}

// Fetch commit data
global.gitlab.fetchCommit(new Commit({
// Generate commit
const newCommit = new Commit({
id: commitData.id,
message: commitData.message,
date: new Date(commitData.timestamp),
url: commitData.url,
project,
})).then((commit) => {
});

// Update result
result[user.getName()].addCommit(commit);
checkedCommits += 1;
if (checkedCommits === data.commits.length) {
resolve(result);
// Get cached user
new Promise((resolve) => {
const user = new User({
name: commitData.author.name,
email: commitData.author.email,
});
if (!cache[user.getName()]) {
result[user.getName()] = user;
global.firebase.get(`users/${user.getName()}`).then((userResponse) => {
if (userResponse !== null) {
cache[user.getName()] = new User(userResponse);
} else {
cache[user.getName()] = user;
}
resolve(cache[user.getName()]);
});
} else {
resolve(cache[user.getName()]);
}
}).catch((err) => {
console.log(err);
checkedCommits += 1;
if (checkedCommits === data.commits.length) {
resolve(result);
}).then((user) => {

// Check that commit does not already exist
if (!user.hasCommit(newCommit)) {
console.log(`#${newCommit.getID()}: commit is new`);

// Fetch commit data and update result
global.gitlab.fetchCommit(newCommit).then((commit) => {
result[user.getName()].addCommit(commit);
checkedCommits += 1;
if (checkedCommits === data.commits.length) {
resolve(result);
}

// Problem fetching commit data
}).catch((err) => {
console.log(err);
checkedCommits += 1;
if (checkedCommits === data.commits.length) {
resolve(result);
}
});

// Commit already exists
} else {
console.log(`#${newCommit.getID()}: commit already checked`);
checkedCommits += 1;
if (checkedCommits === data.commits.length) {
resolve(result);
}
}
});
});
Expand Down Expand Up @@ -121,21 +151,12 @@ class DevRPG {

// Cycle through parsed commits
for (const i of Object.keys(newCommits)) {
let found = false;

// Determine if commit has already been stored
const newCommit = newCommits[i];
if (Object.keys(user.getCommits()).length !== 0) {
for (const j of Object.keys(user.getCommits())) {
const storedCommit = new Commit(user.getCommits()[j]);
if (newCommit.getID() === storedCommit.getID()) {
found = true;
}
}
}
if (!user.hasCommit(newCommit)) {

// Add if not found
if (!found) {
// Add if not found
user.addCommit(newCommit);
for (const k of Object.keys(newCommit.getSkills())) {
const skill = newCommit.getSkills()[k];
Expand Down
22 changes: 22 additions & 0 deletions src/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Commit = require('./commit');

const expBase = 100;
const expModifier = 1.5;

Expand Down Expand Up @@ -82,6 +84,26 @@ class User {
}
this.getSkills()[skillName].level = level;
}

/**
* @param Commit commit
* @return boolean
*/
hasCommit(commit) {
if (Object.keys(this.getCommits()).length !== 0) {
for (const j of Object.keys(this.getCommits())) {
const commitData = this.getCommits()[j];
let storedCommit = commitData;
if (storedCommit.constructor.name !== 'Commit') {
storedCommit = new Commit(commitData);
}
if (commit.getID() === storedCommit.getID()) {
return true;
}
}
}
return false;
}
}

module.exports = User;

0 comments on commit 4cc0d95

Please sign in to comment.