From c4b6d3651d25beeb1fd1813f28f4ed4aaeaa5189 Mon Sep 17 00:00:00 2001 From: Anton Bayer Date: Wed, 25 May 2016 14:04:00 +0200 Subject: [PATCH] add gitlab webhook creation --- Gulpfile.js | 3 +- gitlab-properties.js | 6 +++ gitlab-webhook.js | 90 ++++++++++++++++++++++++++++++++++++++++++++ jenkins-upload.js | 4 -- package.json | 4 +- 5 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 gitlab-properties.js create mode 100644 gitlab-webhook.js diff --git a/Gulpfile.js b/Gulpfile.js index 350bf12..9fbb02b 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -1,4 +1,5 @@ "use strict"; require('./jenkins-download'); -require('./jenkins-upload'); \ No newline at end of file +require('./jenkins-upload'); +require('./gitlab-webhook'); \ No newline at end of file diff --git a/gitlab-properties.js b/gitlab-properties.js new file mode 100644 index 0000000..1609e4f --- /dev/null +++ b/gitlab-properties.js @@ -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" \ No newline at end of file diff --git a/gitlab-webhook.js b/gitlab-webhook.js new file mode 100644 index 0000000..1b17004 --- /dev/null +++ b/gitlab-webhook.js @@ -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(); +}); \ No newline at end of file diff --git a/jenkins-upload.js b/jenkins-upload.js index f16cf68..b7d3171 100644 --- a/jenkins-upload.js +++ b/jenkins-upload.js @@ -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 = ""; diff --git a/package.json b/package.json index fa4f761..ab72e2a 100644 --- a/package.json +++ b/package.json @@ -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": {},