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

Fixes #279 : adding CI support for sub projects. #318

Merged
merged 1 commit into from Aug 11, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 73 additions & 0 deletions backend/github.js
Expand Up @@ -96,3 +96,76 @@ exports.deleteHook = function (name, hook, accessToken, callback) {
}
});
};

/**
* Check whether hook is registered to the repository or not
* @param name: `full_name` of a repository
* @param token: github access token
*/
exports.hookValidator = function (name, token) {
return new Promise(function(resolve, reject) {
request({
url: `https://api.github.com/repos/${name}/hooks`,
headers: {
'User-Agent': 'Yaydoc',
'Authorization': 'token ' + crypter.decrypt(token)
}
}, function (error, response, body) {
var isRegistered = false;
if (response.statusCode !== 200) {
console.log(response.statusCode + ': ' + response.statusMessage);
reject()
}
var hooks = JSON.parse(body);
var hookurl = 'http://' + process.env.HOSTNAME + '/ci/webhook';
Copy link
Member

Choose a reason for hiding this comment

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

Would using http be a problem? I'm not sure, jusk asking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do not strict it to https if a deployment does not have SSL encryption the webhooks will fail. Had this problem in my deployment

Copy link
Member

Choose a reason for hiding this comment

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

Can't we use the current protocol of the browser by using just //? Like if the site is opened using http, use http else use https

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hookurl is accessed by GitHub. Once we done with ssl in kuernetes we'll change this

for (var i = 0; i < hooks.length; i++) {
if (hooks[i].config.url === hookurl) {
isRegistered = true;
break;
}
}
resolve({repositoryName: name, isRegistered: isRegistered})
});
});
}

/**
* Register hook to the respository
* @param {Object} data: Data of the repository
* @param {String} data.name: full_name of the repository
* @param {Boolean} data.sub: Flag to check whether the repository is sub project or not
*/
exports.registerHook = function (data, token) {
return new Promise(function(resolve, reject) {
var hookurl = 'http://' + process.env.HOSTNAME + '/ci/webhook';
if (data.sub === true) {
hookurl += `?sub=true`;
}
request({
url: `https://api.github.com/repos/${data.name}/hooks`,
headers: {
'User-Agent': 'Yaydoc',
'Authorization': 'token ' + crypter.decrypt(token)
},
method: 'POST',
json: {
name: "web",
active: true,
events: [
"push"
],
config: {
url: hookurl,
content_type: "json"
}
}
}, function(error, response, body) {
if (response.statusCode !== 201) {
console.log(response.statusCode + ': ' + response.statusMessage);
resolve({status: false, body:body});
} else {
resolve({status: true, body: body});
}
});
});
}
10 changes: 7 additions & 3 deletions model/repository.js
Expand Up @@ -14,11 +14,11 @@ const repositorySchema = new mongoose.Schema({
},
accessToken: String,
buildStatus: Boolean,
mailService: {
mailService:{
status: Boolean,
email: String,
email: String
},
hook: String,
hook: [String],
enable: {
type: Boolean,
default: true
Expand All @@ -27,6 +27,10 @@ const repositorySchema = new mongoose.Schema({
type: Number,
default: 0,
},
subRepositories:{
type: [String],
default: []
}
});

const Repository = module.exports = mongoose.model('Repository', repositorySchema);
Expand Down
105 changes: 81 additions & 24 deletions public/scripts/socket-dashboard.js
@@ -1,46 +1,70 @@
/**
* Search for repositories
*/
var search = function () {
var username = $("#orgs").val().split(":")[1];
const searchBarInput = $("#search_bar");
const searchResultDiv = $("#search_result");
var subProjectId = 0; // sub projects dynamic id
var repositorySelectIds = []; // array for storing repositories select element

var registerDisableChecker = function () {
var flag = true; // flag to check whether any of the select is
repositorySelectIds.forEach(function(x) {
if ($(`#${x}`).val() === "") {
flag = false;
}
})
if (flag) {
styles.enableButton("btnRegister");
} else {
styles.disableButton("btnRegister");
}
}

var search = function (searchBarInput, searchResultDiv, tempSubProjectId) {
var username = $("#orgs").val().split(":")[1];
var repositorySelectId = "repositories";
if (tempSubProjectId !== 0) {
repositorySelectId = `repositories_${tempSubProjectId}`
}
searchResultDiv.empty();
if (searchBarInput.val() === "") {
searchResultDiv.append('<p class="text-center">Please enter the repository name<p>');
return;
}

searchResultDiv.append('<p class="text-center">Fetching data<p>');

$.get(`https://api.github.com/search/repositories?q=user:${username}+fork:true+${searchBarInput.val()}`, function (result) {
searchResultDiv.empty();
if (result.total_count === 0) {
searchResultDiv.append(`<p class="text-center">No results found<p>`);
styles.disableButton("btnRegister");
var index = repositorySelectIds.indexOf(repositorySelectId);
if (index > -1) {
repositorySelectIds.splice(index,0)
}
registerDisableChecker();
} else {
styles.disableButton("btnRegister");
repositorySelectIds.push(repositorySelectId);
var select = '<label class="control-label" for="repositories">Repositories:</label>';
select += '<select class="form-control" id="repositories" name="repository" required>';
if (tempSubProjectId === 0) {
select += `<select class="form-control" id="${repositorySelectId}" name="repository" required>`;
} else {
select += `<select class="form-control" id="${repositorySelectId}" name="subRepositories" required>`;
}

select += `<option value="">Please select</option>`;
result.items.forEach(function (x){
select += `<option value="${x.full_name}">${x.full_name}</option>`;
});
select += '</select>';
searchResultDiv.append(select);
$("#repositories").change(function () {
if ($("#repositories").val() !== "") {
styles.enableButton("btnRegister");
} else {
styles.disableButton("btnRegister");
}
$(`#${repositorySelectId}`).change(function () {
registerDisableChecker();
});
}
});
};

$(function() {

const predefinedMessages = {
'registration_successful': "Registration successful! Hereafter Documentation will be pushed to the GitHub pages on each commit.",
'registration_already': "This repository has already been registered.",
Expand All @@ -58,17 +82,6 @@ $(function() {
window.history.pushState("", "", location.pathname);
}

$("#search").click(function () {
search();
});

$('#search_bar').on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
search();
}
});

$("#register_btn").click(function () {
if (!$("#register_btn").hasClass("disabled")) {
$('#ci_register').submit();
Expand Down Expand Up @@ -113,4 +126,48 @@ $(function() {
$("#disableModal").modal('hide');
});
});

/**
* Register search events to the search bar
* @param id: Count of the search bar
*/
var registerSearchEvents = function (id) {
var searchBar = '#search_bar';
var searchResult = "#search_result";
var searchBtn = "#search";
if (id !== 0) {
searchBar += `_${id}`;
searchResult += `_${id}`;
searchBtn += `_${id}`;
}
$(searchBar).on('keyup keypress', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 13) {
search($(searchBar), $(searchResult), id);
}
});
$(searchBtn).click(function (){
search($(searchBar), $(searchResult), id);
});
}

registerSearchEvents(0);

$("#btnAddSub").click(function () {
subProjectId += 1;
styles.disableButton("btnRegister");
$("#SubProject").append(`<div class="form-group" class="subProject_${subProjectId}">
<label for="search_bar" class="control-label">Sub Project ${subProjectId}:</label>
<div class="input-group">
<input id="search_bar_${subProjectId}" placeholder="Enter project name" class="form-control">
<span class="input-group-btn">
<button id="search_${subProjectId}" type="button" class="btn btn-default">Search</button>
</span>
</div>
</div>
<div id="search_result_${subProjectId}"></div>`);
(function(tempSubProjectId){
registerSearchEvents(tempSubProjectId);
}(subProjectId));
})
});
7 changes: 7 additions & 0 deletions public/stylesheets/style.css
Expand Up @@ -53,6 +53,9 @@ a {
margin-left: 10px;
}

#btnAddSub {
margin-left: 10px;
}
#btnWebsiteLink, #btnGithubPages, #btnLogs {
display: none;
width: -moz-fit-content;
Expand All @@ -63,6 +66,10 @@ a {
margin-top: 20px;
}

#SubProject {
margin-bottom: 10px;
}

#notification-container {
position: absolute;
text-align: center;
Expand Down