Forked from yu1ec/gist:ece7260c776208072afd665b86efc370
Created
August 9, 2019 12:55
-
-
Save c33s/794fe1fafa026835cf07b1fd658e46d4 to your computer and use it in GitHub Desktop.
Pipelines Cleaner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Pipelines Cleaner | |
// @namespace https://your-domain/ | |
// @version 0.1 | |
// @description Gitlab CI/CD Pipelines Cleaner | |
// @author ecareyu | |
// @match *://your-domain/*/*/pipelines | |
// @require https://code.jquery.com/jquery-2.2.4.min.js | |
// @run-at document-end | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// @see:https://docs.gitlab.com/ee/api/pipelines.html | |
$(function () { | |
let PRIVATE_TOKEN = 'your personal access token' | |
function destroyPipelines() { | |
let $this = $(this); | |
if (!confirm('Are your sure?')) { | |
return false; | |
} | |
$('input[name="pipelines[]"]:checked').each(function (index, elem) { | |
let $elem = $(elem) | |
let projectId = $elem.data('projectId'); | |
let pipelineId = $elem.val(); | |
$.ajax({ | |
'url': `/api/v4/projects/${projectId}/pipelines/${pipelineId}`, | |
'type': 'DELETE', | |
'success': function () { | |
$(`#pipeline-${pipelineId}`).remove(); | |
} | |
}); | |
}); | |
} | |
function toggleCheckbox() { | |
let $obj = $(this); | |
$('input[name="pipelines[]"]').prop('checked', $obj.prop('checked')); | |
} | |
let API_SEARCH_PROJECT = '/api/v4/projects'; | |
$.ajaxSetup({ | |
'dataType': 'json', | |
'headers': { | |
'PRIVATE-TOKEN': PRIVATE_TOKEN | |
} | |
}); | |
let projectName = $('.breadcrumbs-links ul li:eq(1)').find('a>span').text(); | |
setTimeout(function () { | |
let $btnEl = $('<a href="javascript:;" class="btn btn-danger" variant="success">Delete</a>').on('click', destroyPipelines) | |
$('.nav-controls').append($btnEl); | |
let $columnEl = $('<input type="checkbox" value="" /> ').on('click', toggleCheckbox); | |
$('.ci-table .table-section.section-10.js-pipeline-status').prepend($columnEl); | |
$.get(API_SEARCH_PROJECT, {'search':projectName}, function (projects) { | |
let project = projects[0] || null | |
if (!project) { | |
console.log(projectName + 'not found'); | |
} | |
let id = project.id; | |
let API_PIPELINES = `/api/v4/projects/${id}/pipelines`; | |
$.get(API_PIPELINES, function (pipelines) { | |
pipelines.forEach(function (pipeline, index) { | |
let $el = $('.commit.gl-responsive-table-row').eq(index) | |
$el.attr('id', `pipeline-${pipeline.id}`) | |
let checkBoxHtml = `<input name="pipelines[]" type="checkbox" data-project-id="${id}" value="${pipeline.id}" /> `; | |
$el.find('.table-section.section-10.commit-link .table-mobile-content').prepend(checkBoxHtml); | |
}); | |
}) | |
}); | |
}, 500) | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment