Skip to content

Commit

Permalink
Fixes 305: option added to disable the repository in Yaydoc (fossasia…
Browse files Browse the repository at this point in the history
  • Loading branch information
poonai authored and imujjwal96 committed Aug 2, 2017
1 parent 4444ced commit 456f8d5
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 50 deletions.
7 changes: 7 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.isLoggedIn = function (req, res, next) {
if (req.user) {
next();
} else {
res.redirect('/');
}
}
24 changes: 23 additions & 1 deletion model/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const repositorySchema = new mongoose.Schema({
accessToken: String,
buildStatus: Boolean,
mailService: Boolean,
hook: String
hook: String,
enable: {
type: Boolean,
default: true
}
});

const Repository = module.exports = mongoose.model('Repository', repositorySchema);
Expand Down Expand Up @@ -53,6 +57,24 @@ module.exports.findOneRepository = function(query) {
})
};

/**
* Update repository
* @param query: condition for selecting collection
* @param update: data to be updated
* @returns {Promise}
*/
module.exports.updateRepository = function(query, update) {
return new Promise(function (resolve, reject) {
Repository.update(query, update, function(err, result) {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
};

/**
* Get repositories registered by a `registrant`
* @param registrant
Expand Down
14 changes: 13 additions & 1 deletion public/scripts/socket-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ $(function() {
'registration_unauthorized': "You do not have admin permission for this repository.",
'registration_mismatch': "A hook for Yaydoc is created but the repository is not registered!",
'delete_success': "Repository removed from Yaydoc successfully",
'delete_failure': "Failed to remove repository!"
'delete_failure': "Failed to remove repository!",
'enabled_successful': "Yaydoc enabled successfully for this repository",
'disabled_successful': "Yaydoc disabled successfully for this repository"
};

if ((predefinedMessages[styles.getParameterByName("status")] || '') !== '') {
Expand Down Expand Up @@ -101,4 +103,14 @@ $(function() {
}

});

$('.open-disable-modal').click(function () {
$("#disableInput").val($(this).data('name'));
$("#disableBtnYes").click(function () {
$("#disableForm").submit();
});
$("#disableBtnClose").click(function () {
$("#disableModal").modal('hide');
});
});
});
51 changes: 29 additions & 22 deletions routes/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,38 @@ router.post('/webhook', function(req, res, next) {
Repository.findOneRepository({
name: repositoryName
}).then(function(result) {
User.getUserById(result.registrant.id, function (error, user) {
var data = {
email: user.email,
gitUrl: req.body.repository.clone_url,
docTheme: "",
debug: true,
targetBranch: branch,
docPath: ''
};
generator.executeScript({}, data, function (err, generatedData) {
if (err) {
build.updateBuildStatus(repositoryName, false);
console.log(err);
return;
}
deploy.deployPages({}, {
if (result.enable === true) {
User.getUserById(result.registrant.id, function (error, user) {
var data = {
email: user.email,
gitURL: req.body.repository.clone_url,
username: result.registrant.login,
uniqueId: generatedData.uniqueId,
encryptedToken: result.accessToken
gitUrl: req.body.repository.clone_url,
docTheme: "",
debug: true,
targetBranch: branch,
docPath: ''
};
generator.executeScript({}, data, function (err, generatedData) {
if (err) {
build.updateBuildStatus(repositoryName, false);
console.log(err);
return;
}
deploy.deployPages({}, {
email: user.email,
gitURL: req.body.repository.clone_url,
username: result.registrant.login,
uniqueId: generatedData.uniqueId,
encryptedToken: result.accessToken
});
build.updateBuildStatus(repositoryName, true);
});
build.updateBuildStatus(repositoryName, true);
});
});
} else {
res.json({
status: false,
description: "Yaydoc is disabled for this repository"
})
}
}).catch((err) => {
console.log(err);
});
Expand Down
11 changes: 2 additions & 9 deletions routes/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ var router = express.Router();
var async = require("async");

var github = require('../backend/github');

var authMiddleware = require("../middleware/auth.js")
User = require('../model/user');
Repository = require('../model/repository');

router.get('/', isLoggedIn, function (req, res, next) {
router.get('/', authMiddleware.isLoggedIn, function (req, res, next) {
User.getUserById(req.session.passport.user, function (err, user) {
async.parallel({
organizations: function(callback) {
Expand All @@ -31,12 +31,5 @@ router.get('/', isLoggedIn, function (req, res, next) {
});
});

function isLoggedIn(req, res, next) {
if (req.user) {
next();
} else {
res.redirect('/');
}
}

module.exports = router;
39 changes: 38 additions & 1 deletion routes/repository.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var express = require("express");
var router = express.Router();
var async = require("async");

var github = require("../backend/github");
var authMiddleware = require("../middleware/auth.js");

Repository = require("../model/repository");

Expand Down Expand Up @@ -37,4 +37,41 @@ router.post('/delete', function (req, res, next) {
});
});


router.post("/disable", authMiddleware.isLoggedIn, function(req, res, next) {
var query = {
name: req.body.repository
};
Repository.updateRepository(query, {
enable: false
})
.then(function () {
res.redirect("/dashboard?status=disabled_successful")
})
.catch(function () {
next({
status: 500,
messages: 'Something went wrong'
});
});
});

router.post("/enable", authMiddleware.isLoggedIn, function(req, res, next) {
var query = {
name: req.body.repository
};
Repository.updateRepository(query, {
enable: true
})
.then(function () {
res.redirect("/dashboard?status=enabled_successful")
})
.catch(function () {
next({
status: 500,
messages: 'Something went wrong'
});
});
});

module.exports = router;
45 changes: 29 additions & 16 deletions views/dashboard.jade
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ block content
tr
td #{repository.name}
td
.btn-group(data-toggle="buttons")
label.btn.btn-default.active
input#disabled(type='radio' name='debug' value='false')
| Disabled
label.btn.btn-default
input#enabled(type='radio' name='debug' value='true')
| Enabled
if repository.enable === true
button.btn.btn-warning.open-disable-modal(data-toggle="modal" data-name="#{repository.name}" href="#disableModal") Disable
else
form(method="POST" action="/repository/enable")
input(type="hidden", name="repository", value="#{repository.name}")
button.btn.btn-primary(type="submit") Enable
td
button.btn.btn-danger.open-delete-modal(data-toggle="modal" data-name="#{repository.name}" href="#deleteModal" data-backdrop="static" data-keyboard="false") Delete
else
Expand All @@ -90,17 +89,16 @@ block content
tr
td #{repository.name}
td
.btn-group(data-toggle="buttons")
label.btn.btn-default.active
input#disabled(type='radio' name='debug' value='false')
| Disabled
label.btn.btn-default
input#enabled(type='radio' name='debug' value='true')
| Enabled
if repository.enable === true
button.btn.btn-warning.open-disable-modal(data-toggle="modal" data-name="#{repository.name}" href="#disableModal") Disable
else
form(method="POST" action="/repository/enable")
input(type="hidden", name="repository", value="#{repository.name}")
button.btn.btn-primary(type="submit") Enable
td
button.btn.btn-danger.open-delete-modal(data-toggle="modal" data-name="#{repository.name}" href="#deleteModal" data-backdrop="static" data-keyboard="false") Delete
else
p No repositories registered!
else
p No repositories registered!
.modal.fade#deleteModal(role='dialog')
.modal-dialog
.modal-content
Expand All @@ -117,3 +115,18 @@ block content
input.form-control#repository-name(name="name" autofocus)
.modal-footer
button.btn.btn-default#btnDelete(disabled="disabled") Delete
.modal.fade#disableModal(role='dialog')
.modal-dialog
.modal-content
form#disableForm(action="/repository/disable" method="post")
.modal-header
input#disableInput(type="hidden", name="repository")
button.close(type='button', data-dismiss='modal') ×
h4.modal-title Are you sure?
.modal-body
p
| Are you sure you want to disable this repo?
.modal-footer
button.btn.btn-default#disableBtnClose(type="button") Close
button.btn.btn-primary#disableBtnYes(type="button") Yes...Disable

0 comments on commit 456f8d5

Please sign in to comment.