Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create team forks #101

Merged
merged 2 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 51 additions & 3 deletions server/app/routes/api/v1/assessments/assessments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const utils = require('../../../utils');
const {ensureAuthenticated, ensureIsAdmin, Credentials, respondWith404, _err, db} = utils;
const Assessment = db().models.assessment;
const Team = db().models.team
const StudentTest = db().models.studentTest
const User = db().models.user
const Resource = Assessment;
const GitHub = require('../../../../configure/github')

import omit from 'lodash/object/omit'

Expand All @@ -17,6 +20,7 @@ import omit from 'lodash/object/omit'
router.get( '/', sequelizeHandlers.query(Resource));
router.get( '/:id', ensureAuthenticated, sequelizeHandlers.get(Resource));
router.post( '/', ensureAuthenticated, (req, res, next) => {
let newAssessment
Promise.all([
Team.findOrCreate({
where: {
Expand All @@ -28,10 +32,54 @@ router.post( '/', ensureAuthenticated, (req, res, next) => {
])
.spread((teams, assessment) => {
const team = teams[0]
return assessment.setTeam(team)
let baseUrl = assessment.basePath.split('/')
return Promise.all([
assessment.setTeam(team),
GitHub.repos.getForksAsync({
user: baseUrl[0],
repo: baseUrl[1]
}),
GitHub.orgs.getTeamMembersAsync({ id: team.github_team_id }),
])
})
.then(assessment => {
return Assessment.findById(assessment.id)
.spread((assessment, forks, members) => {
newAssessment = assessment
const memberLogins = members.map(member => member.login)
const teamForks = forks.filter(fork => memberLogins.includes(fork.owner.login))
const creatingUsersAndTests = teamForks.map(teamFork => {
let { owner } = teamFork
return Promise.all([
GitHub.users.getByIdAsync({ id: owner.id })
.then(user => {
return User.findOrCreate({ where: {
github_id: user.id,
name: user.name,
username: user.login,
photo: user.avatar_url
}})
}),
StudentTest.findOrCreate({
where: {
repoUrl: `${owner.login}/${assessment.basePath.split('/')[1]}`,
assessmentId: assessment.id,
creatorId: req.user.id
}
})
])
})
return Promise.all(creatingUsersAndTests)
})
.then(usersAndTests => {
return Promise.all(
usersAndTests.map(userAndTest => {
let user = userAndTest[0][0]
let test = userAndTest[1][0]
return test.setUser(user)
})
)
})
.then(() => {
return Assessment.findById(newAssessment.id)
})
.then(assessment => res.json(assessment))
.catch(next)
Expand Down
6 changes: 4 additions & 2 deletions server/db/models/Assessments/StudentTest/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ module.exports = function(db){
type: Sequelize.TEXT
},
isStudent: {
type: Sequelize.BOOLEAN
type: Sequelize.BOOLEAN,
defaultValue: true
},
isGraded: {
type: Sequelize.BOOLEAN
type: Sequelize.BOOLEAN,
defaultValue: false
},
score: {
type: Sequelize.INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion server/db/models/Organizations/User/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(db){
type: Sequelize.STRING
},
github_id: {
type: Sequelize.STRING
type: Sequelize.BIGINT
},
github_token: {
type: Sequelize.STRING
Expand Down