Skip to content

Commit

Permalink
add gitlab webhook creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Bayer authored and Anton Bayer committed May 25, 2016
1 parent fe00fba commit c4b6d36
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";

require('./jenkins-download');
require('./jenkins-upload');
require('./jenkins-upload');
require('./gitlab-webhook');
6 changes: 6 additions & 0 deletions gitlab-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

exports.url = 'https://gitlab.server.com';
exports.token = 'anytoken';
exports.pushEventUrl= 'http://url_to_jenkins/gitlab/build_now';
exports.mergeEventUrl= "http://url_to_jenkins/project/#PROJECT_NAME#-merge-request-verifier"
90 changes: 90 additions & 0 deletions gitlab-webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use strict";

var gulp = require('gulp');
var gitlabProperties = require('./gitlab-properties');

var gitlab = require('gitlab')({
url: gitlabProperties.url,
token: gitlabProperties.token
});

var fs = require('fs');

gulp.task('gitlabWebhook', function() {

var projectName = process.env.npm_config_project_name;
if (projectName === undefined) {
projectName = "";
}
var gitRepo = process.env.npm_config_git_repo;
if (gitRepo === undefined) {
gitRepo = "";
}
var gitGroup = process.env.npm_config_git_group;
if (gitGroup === undefined) {
gitGroup = "";
}

function getAllProjects() {
gitlab.projects.all(function(projects) {
for (var i = 0; i < projects.length; i++) {
var project = projects[i];
if (project.name == gitRepo && project.namespace.name == gitGroup) {
getHooks(project.id);
break;
}
}
});
}

function getHooks(projectId) {
gitlab.projects.hooks.list(projectId, function(hooks) {

if (hooks.length) {
var callback = function(i) {
i++;
if (i < hooks.length) {
deleteHook(projectId, hooks[i].id, i, callback);
}
else {
createNewHooks(projectId);}
};
deleteHook(projectId, hooks[0].id, 0, callback);
}
else {
createNewHooks(projectId);
}
});
}

function deleteHook(projectId, hookId, index, callback) {
gitlab.projects.hooks.remove(projectId, hookId, function(ret) {
callback(index);
});
}

function createNewHooks(projectId) {
gitlab.projects.hooks.add(projectId, {
url: replacePlaceholder(gitlabProperties.pushEventUrl),
push_events: true
},
function() {
gitlab.projects.hooks.add(projectId, {
url: replacePlaceholder(gitlabProperties.mergeEventUrl),
push_events: false,
merge_requests_events: true
},
function() {

});
});
}

function replacePlaceholder(str) {
return str.replace(new RegExp('#GIT_GROUP#', 'g'), gitGroup)
.replace(new RegExp('#GIT_REPO#', 'g'), gitRepo)
.replace(new RegExp('#PROJECT_NAME#', 'g'), projectName);
}

getAllProjects();
});
4 changes: 0 additions & 4 deletions jenkins-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ gulp.task('jenkinsUpload', function() {
if (gitRepo === undefined) {
gitRepo = "";
}
var gitRepo = process.env.npm_config_git_repo;
if (gitRepo === undefined) {
gitRepo = "";
}
var gitGroup = process.env.npm_config_git_group;
if (gitGroup === undefined) {
gitGroup = "";
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"homepage": "http://www.catalysts.cc/",
"dependencies": {
"gulp": "3.9.1",
"jenkins": "0.17.0"
"jenkins": "0.17.0",
"gitlab": "1.6.0"
},
"main": "Gulpfile.js",
"scripts": {
"jenkins-upload": "gulp jenkinsUpload",
"jenkins-download": "gulp jenkinsDownload",
"gitlab-webhook": "gulp gitlabWebhook",
"start": "gulp jenkinsDownload"
},
"devDependencies": {},
Expand Down

0 comments on commit c4b6d36

Please sign in to comment.