Skip to content
This repository was archived by the owner on Oct 29, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
13f9dd9
Started work on pulling commit data for project repositories
zmike808 Oct 18, 2016
8d87fc7
refactored function name to make more sense
zmike808 Oct 18, 2016
beb0333
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Oct 18, 2016
4203d62
Removed unused github.py worker
zmike808 Oct 18, 2016
cf05306
removed hardcoded github token for security purposes
zmike808 Oct 18, 2016
391b739
P.O.C/test example progress...still heavily debugging...
zmike808 Oct 19, 2016
3844aef
more debugging...just commiting to try to keep track of this mess...
zmike808 Oct 19, 2016
b90cb28
Revert "more debugging...just commiting to try to keep track of this …
zmike808 Oct 25, 2016
276f5cc
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Oct 25, 2016
33fba8d
removed api key from github worker (again...)
zmike808 Oct 25, 2016
c68570e
removed runThread from github.js worker; don't think it's currently n…
zmike808 Oct 25, 2016
459974e
Added GITHUB_WORKER_KEY env variable.\n Removed hard coded github key…
zmike808 Oct 25, 2016
155494b
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Oct 25, 2016
a3d0b6b
Github worker now populates commit model with commits! Still a W.I.P.
zmike808 Oct 25, 2016
cec9fed
Merge remote-tracking branch 'origin/master' into feature/454
zmike808 Nov 1, 2016
07b1ce2
pulling commits from github now works! Currently fixing up commit api…
zmike808 Nov 1, 2016
c312e90
fixed github token env variable, now imports correctly
zmike808 Nov 1, 2016
2e092c1
finished commit-project DB relationship link; cleaned up github worke…
zmike808 Nov 2, 2016
8d7c026
more env variable issues >.<
zmike808 Nov 2, 2016
c366a9c
removed un-needed debug statement
zmike808 Nov 2, 2016
d11b05e
Commits working for the most part. Just need to figure out async issu…
zmike808 Nov 2, 2016
5fc9797
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Nov 4, 2016
728d307
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Nov 15, 2016
e9649b9
removed un-needed console.log
zmike808 Nov 15, 2016
4df1f88
started on switching to a different github api wrapper and using prom…
zmike808 Nov 16, 2016
2342c04
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Nov 18, 2016
28dd8c5
Completely overhauled github commit retrival & storage; much more str…
zmike808 Nov 18, 2016
e2b3f99
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Dec 2, 2016
9764bd8
minor formatting cleanups
zmike808 Dec 2, 2016
a88fef9
cleanup & refactor. WIP: adding configuration options for fetching co…
zmike808 Dec 2, 2016
aae63ec
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Dec 8, 2016
0365fec
refactored out some un-needed & commented code
zmike808 Dec 9, 2016
1fd9457
Merge remote-tracking branch 'origin/master' into feature/455
zmike808 Dec 14, 2016
3faa629
fixed find project query
zmike808 Dec 14, 2016
aacba9a
fixed showProjectCommits query again
zmike808 Dec 14, 2016
fd3346f
implemented worker function to get commits for all projects; also add…
zmike808 Dec 14, 2016
7732b4b
cleaned up code & removed unused code
zmike808 Dec 14, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"mongoose": "^4.1.2",
"morgan": "^1.7.0",
"multiparty": "^4.1.1",
"octokat": "^0.4.18",
"passport": "~0.3.0",
"passport-facebook": "^2.0.0",
"passport-google-oauth": "^1.0.0",
Expand Down
9 changes: 1 addition & 8 deletions server/api/commit/commit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ exports.destroy = function(req, res) {
// Show a list of a projects Commits
// Get a single commit
exports.showProjectCommits = function(req, res) {
Commit.findById(req.params.projectId, function (err, commits) {
Commit.find({project: req.params.projectId}).populate('project').exec(function (err, commits) {
if(err) { return handleError(res, err); }
if(!commits) { return res.send(404); }
return res.json(commits);
Expand All @@ -68,16 +68,9 @@ exports.showProjectCommits = function(req, res) {
// Get a list of a user Commits
exports.showUserCommits = function(req, res) {
var prevDays = new Date();
if (req.params.timeperiod){
prevDays.setDate(prevDays.getDate()-Number(req.params.timeperiod));
}
else{
prevDays.setDate(prevDays.getDate()-14);
}

Commit.find()
.where('author.login').equals(String(req.params.githubProfile))
.where('date').gt(prevDays)
.exec(function(err, commits){
if(err) { return handleError(res, err); }
if(!commits) { return res.json([]); }
Expand Down
9 changes: 7 additions & 2 deletions server/api/commit/commit.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Project = require('../project/project.model');

var CommitSchema = new Schema({
url: String,
Expand All @@ -10,14 +11,18 @@ var CommitSchema = new Schema({
unique: true,
index: true,
},
userId: { type: String, index: true},
author: {
login: {type: String, lowercase: true},
id: {type: Number}
},
branch: String,
commentCount: Number,
message: String,
date: Date,
project: {
type : Schema.Types.ObjectId,
ref: 'Project',
index: true
},
},{ timestamps: true});

module.exports = mongoose.model('Commit', CommitSchema);
17 changes: 16 additions & 1 deletion server/api/project/project.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var Commit = require('../commit/commit.model');

var ProjectSchema = new Schema({
name: {
Expand Down Expand Up @@ -42,7 +43,21 @@ var ProjectSchema = new Schema({
default: false,
index: true
},
tech: [String]
tech: [String],
commits: [{
type : Schema.Types.ObjectId,
ref: 'Commit',
index: true
}],
},{ timestamps: true});

/*
@returns:
*/
ProjectSchema
.virtual('fullRepoPath')
.get(function(){
return (this.githubUsername + "/" + this.githubProjectName) ;
});

module.exports = mongoose.model('Project', ProjectSchema);
1 change: 1 addition & 0 deletions server/api/user/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var UserSchema = new Schema({
index: true
}], // project id


bio: String,

password: {
Expand Down
5 changes: 4 additions & 1 deletion server/config/environment/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ module.exports = {
// For testing verification
// attendanceVerificationRatio: 1,

seedDB: false
seedDB: false,
// Github token for worker, local.env.js kept returning undefined but it worked once I added it here...
GITHUB_WORKER_TOKEN: 'YOUR_KEY',
Copy link
Member

@kmcnellis kmcnellis Nov 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out server/config/environment/index.js for examples of how to read from the environment variables to set a config variable.


};
2 changes: 2 additions & 0 deletions server/config/local.env.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {

SERVER_ADDRESS: 'http://localhost:9000',
SENDGRID_API_KEY: 'YOUR_KEY',
GITHUB_WORKER_TOKEN: 'YOUR_KEY',


// Control debug level for modules using visionmedia/debug
DEBUG: ''
Expand Down
1 change: 1 addition & 0 deletions server/config/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var seed = function() {
if (!module.parent) {
if (args.length == 0) {
seed().then(function(){

db.disconnect()
});
}
Expand Down
191 changes: 191 additions & 0 deletions server/workers/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
'use strict';

if (!module.parent) {
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var mongoose = require('mongoose');
var config = require('../config/environment');
var args = process.argv.slice(2);

// Connect to database
var db = mongoose.connect(config.mongo.uri, config.mongo.options);
}
var Commit = require('../api/commit/commit.model');
var Project = require('../api/project/project.model');

var Octokat = require('octokat');

var Promise = require("bluebird");

mongoose.Promise = require('bluebird');

/*
The github token currently needs to be defined in development.js env.
Since, this is a worker that is currently run individually, it does not have app.js loaded beforehand.
Meaning, magic env stuff isn't automagically setup.
@TODO: bring back the magic
*/
var gtoken = config.GITHUB_WORKER_TOKEN;
if (gtoken == "YOUR_KEY") {
console.error('ERROR: PLEASE SET YOUR GITHUB_WORKER_TOKEN IN DEVELOPMENT.JS TOKEN BEFORE PROCEEDING');
throw new Error('ERROR: PLEASE SET YOUR GITHUB_WORKER_TOKEN IN DEVELOPMENT.JS TOKEN BEFORE PROCEEDING');
}

// setup & initilize our github API library
var octo = new Octokat({
token: gtoken
});

/*
@TODO: add support for filtering commits by date/author/etc..
ex: only get commits newer than date x, or only get commits older than date y
*/
function fetchCommitsFromProject(owner, repository, config) {
return fetchAll(octo.repos(owner, repository).commits.fetch, config);
}

function fetchAll(fn, args) {
let acc = [];
if (!args) {
args = {per_page: 100};
}
let p = new Promise((resolve, reject) => {
fn(args).then((val) => {
acc = acc.concat(val);
if (val.nextPage) {
fetchAll(val.nextPage).then((val2) => {
acc = acc.concat(val2);
resolve(acc);
}, reject);
} else {
resolve(acc);
}
}, reject);
});
return p;
}

var saveCommit = function (commitData, project) {
var newCommit = new Commit(commitData);
/*
establish the ownership relationship; i.e. what project does this commit belong to?
@TODO: setup the inverse relationship(?); i.e. what commits does the project own?
*/
newCommit.project = project;
/*
manually extract/configure the data to match our Commit schema.
*/
newCommit.message = commitData.commit.message;
newCommit.date = commitData.commit.author.date;
newCommit.commentCount = commitData.commit.comment_count;
// Save the new commit model to the db. This is an async operation, so it will be turned into a promise.
return newCommit.save();
};

/*
@promiseObject: JS object containing 2 fields.
commits: promise array of fetched commits from github
project: instance of project model that we are fetching the commits for.
*/
var saveCommits = saveCommitsFn;
function saveCommitsFn(promiseObject) {
console.dir(promiseObject);
console.log("entering...");
// extract the data from the promiseObject
var commits = promiseObject.commits;
var project = promiseObject.project;
// array of Commit.save() operations; to be turned into an array of Promises.
var commitPromises = [];
commits.forEach(function (commit) {
commitPromises.push(saveCommit(commit, project));
});
// return an array of promises, where each promise is a promise to save the commit model to the DB.
return Promise.all(commitPromises);
}

exports.getCommitsForProjectSinceDate = function (project, date) {
date = new Date(Date.parse(date)) || new Date();
var config = {
since: date.toISOString()
};
return processProject(project, config);
};

exports.getCommitsForProjectUntilDate = function (project, date) {
date = new Date(Date.parse(date)) || new Date();
var config = {
ungil: date.toISOString()
};
return processProject(project, config);
};

exports.getCommitsForProject = function (project) {
return processProject(project);
};

/**
*
* @param project: instance of Project model
* @param config:
*/
var fetchCommits = function (project, config) {
// extract the github username & github repository name to fetch the commits from.
var owner = project.githubUsername;
var repository = project.githubProjectName;

// Config is an optional arguement, so if it isn't pased init it ourselves
config = config || {};

// always set the results per page limit to the maximum, 100, to reduce github API calls
config["per_page"] = 100;

/*
Needed to pass through project model; to saveCommits; but javascript.
So instead I just tacked it onto an object.
Promise.props is essentially the same as using Promise.all([]),
Except it returns an object with arbitrary fields that can be a promise or arbitrary data.
with an added variable, @project.
Probably a better way to do this, but I currently can't think of any because javascript.
*/

return Promise.props({
commits: fetchCommitsFromProject(owner, repository, config),
project: project,
});
};

/*
For now just a hard coded example/P.O.C using the github repo rcos/observatory3
Planning to refactor this "soon(tm)"
*/

function processProject(project, config) {
/*
flow:
1. fetch commits from github for @project
2. save fetched commits to db
3. disconnect from DB
*/
return fetchCommits(project, config).then(saveCommits);
}

function processAllProjects() {
var start = Promise.resolve();
return Promise.map(Project.find({}).exec(), function (proj) {
start = start.then(function () {
return fetchCommits(proj);
});
return start;
}).map(saveCommitsFn).then(function (results) {
console.dir(results);
console.dir(results.length);
db.disconnect();
});
}

if (!module.parent) {
if (args.length == 0) {
processAllProjects();
}
}
Loading