Skip to content

Instantly share code, notes, and snippets.

@Olegas
Created November 4, 2014 20:34
Show Gist options
  • Save Olegas/4e6711e4a0537eee516d to your computer and use it in GitHub Desktop.
Save Olegas/4e6711e4a0537eee516d to your computer and use it in GitHub Desktop.
Custom script to integrate GitLab with TeamCity 8+
// Configuration. GitLab API key, API url
var CIParams = {
gitlabKey: 'YOURSECREKAPIKEY',
gitlabApiUrl: 'https://gitlab/api/v3/',
tcApiUrl: '/guestAuth/app/rest/',
tcProtocol: 'https://'
},
cachedData = {
currentProject: {id: -1},
currentMergeRequest: {id: -1}
};
/**
* Checks if page is a merge request and renders build info
*/
function showCIInfo() {
if (isPageMergeRequest()) {
var projectParams = getCurrentProjectInfo();
if (projectParams) {
showBuildsInfo(projectParams);
}
}
}
function isPageMergeRequest() {
return /https?:\/\/git\.sbis\.ru\/.*\/merge_requests\/\d+\/?/.test(document.location.href);
}
/**
* Retrieve project meta-data from it's description.
*/
function getCurrentProjectInfo() {
var dataProjectID = $('body').attr('data-project-id'); // Project ID is stored here
if (cachedData.currentProject.id === dataProjectID) {
return cachedData.currentProject;
}
var URL = CIParams.gitlabApiUrl + 'projects/' + dataProjectID, // Project info URL
currentProject = null;
if (!dataProjectID) {
return null;
}
jQuery.ajax(URL, {
'async': false, // Yes, ASYNC = false
'headers': {
'Accept': 'application/json',
'PRIVATE-TOKEN': CIParams.gitlabKey
}
}).done(function (data) {
currentProject = data;
});
if (currentProject && currentProject.description) {
cachedData.currentProject = currentProject;
var metaInfo = currentProject.description.match(/\{.*\}/)[0];
return JSON.parse(metaInfo);
} else {
return null;
}
}
/**
* Generate build status
*/
function showBuildsInfo(projectParams) {
var mergeRequestInfo = getMergeRequestInfo();
if (!mergeRequestInfo) {
return;
}
var branchFrom = mergeRequestInfo.source_branch,
branchTo = mergeRequestInfo.target_branch,
buildsCount = projectParams.builds.length;
if (projectParams.buildsPostfix) {
var postfix = branchFrom.split('/')[0].replace(/\./g, '');
branchFrom = branchFrom.replace(/[^\/]+\//, '');
}
var $desc = $('.issue-box'),
$ci = $desc.find('.ci');
if (!$ci.length) {
$ci = $('<div class="ci" style="padding: 15px 25px;"></div>');
$desc.append($ci);
}
$ci.empty();
$ci.append('<h4>Build status</h4>');
for (var i = 0; i < buildsCount; i++) {
if (projectParams.buildsPostfix) {
projectParams.builds[i] += postfix;
}
$ci.append(generateBuildRow(projectParams, projectParams.builds[i], branchFrom, branchTo));
}
}
/**
* Get merge request info
*/
function getMergeRequestInfo() {
var mergeID = document.location.href.match(/\d+(?=$|\/$|\/diffs$)/)[0];
if (cachedData.currentMergeRequest.id === mergeID) {
return cachedData.currentMergeRequest;
}
var titleText = $('.merge-request > .page-title').text(),
projectID = $('body').attr('data-project-id'),
URL = CIParams.gitlabApiUrl + 'projects/' + projectID + '/merge_requests?per_page=1&page=' + mergeID,
mergeRequestInfo = null;
jQuery.ajax(URL, {
'async': false,
'headers': {
'Accept': 'application/json',
'PRIVATE-TOKEN': CIParams.gitlabKey
}
}).done(function (data) {
mergeRequestInfo = data[0];
});
cachedData.currentMergeRequest = mergeRequestInfo;
return mergeRequestInfo;
}
/**
* Generates HTML for each build status row
*/
function generateBuildRow(projectParams, buildType, branchFrom, branchTo) {
var encodedBranchFrom = (branchFrom === 'master') ? encodeURIComponent('<default>') : encodeURIComponent(branchFrom),
encodedBranchTo = (branchTo === 'master') ? encodeURIComponent('<default>') : encodeURIComponent(branchTo),
html = '<p><a href="' + CIParams.tcProtocol + projectParams.ciserver + '/viewType.html?buildTypeId=' + buildType + '&branch=' + encodedBranchFrom + '" class="' + buildType + '">' +
'<span class="buildName"></span> (' + branchFrom + ')</a>: ' +
'<img src="' + CIParams.tcProtocol + projectParams.ciserver + CIParams.tcApiUrl + 'builds/buildType:' + buildType + ',branch:' + encodedBranchFrom + '/statusIcon" /> &#8680 ' +
'<a href="' + CIParams.tcProtocol + projectParams.ciserver + '/viewType.html?buildTypeId=' + buildType + '&branch=' + encodedBranchTo + '" class="' + buildType + '">' +
'<span class="buildName"></span> (' + branchTo + ')</a>: ';
if (projectParams.buildsPostfix) {
html += '<img src="' + CIParams.tcProtocol + projectParams.ciserver + CIParams.tcApiUrl + 'builds/buildType:' + buildType + ',branch:' + encodeURIComponent('<default>') + '/statusIcon" /></p>';
} else {
html += '<img src="' + CIParams.tcProtocol + projectParams.ciserver + CIParams.tcApiUrl + 'builds/buildType:' + buildType + ',branch:' + encodedBranchTo + '/statusIcon" /></p>';
}
$.ajax({
url: CIParams.tcProtocol + projectParams.ciserver + CIParams.tcApiUrl + 'buildTypes/' + buildType,
headers: {
Accept: 'application/json'
}
}).done(function (res) {
$('.' + buildType + ' .buildName').text(res.name);
});
return html;
}
showCIInfo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment