Skip to content

Instantly share code, notes, and snippets.

@loominade
Last active July 13, 2020 09:18
Show Gist options
  • Save loominade/078521409ca84345b133b451f57af78d to your computer and use it in GitHub Desktop.
Save loominade/078521409ca84345b133b451f57af78d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name List to redmine ticket
// @namespace https://redmine.loom.de
// @version 0.1
// @updateURL https://gist.github.com/loominade/078521409ca84345b133b451f57af78d/raw/02ca789c973667147e393c9e132f185e8f6a053e/list-to-subticket.user.js
// @downloadURL https://gist.github.com/loominade/078521409ca84345b133b451f57af78d/raw/02ca789c973667147e393c9e132f185e8f6a053e/list-to-subticket.user.js
// @description create tickets from lists
// @author You
// @match https://redmine.loom.de/*
// @grant none
// ==/UserScript==
function createTicketPath(subject) {
let subtaskLink = document.querySelector('#issue_tree > .contextual > a');
return subtaskLink.href + '&issue[subject]=' + encodeURIComponent(subject);
}
let init = function() {
let lists = document.querySelectorAll('.wiki ul');
for (let list of lists) {
let button = document.createElement('button');
button.innerText = 'Diese Liste in Untertickets umwandeln';
list.parentNode.insertBefore(button, list.nextSibling);
button.onclick = () => {
for (let item of list.children) {
let del = item.querySelector('del');
if (del) {
continue;
}
fetch(createTicketPath(item.innerText)).then(function (response) {
// The API call was successful!
return response.text();
}).then(function (html) {
// Convert the HTML string into a document object
let parser = new DOMParser();
let doc = parser.parseFromString(html, 'text/html');
let issueForm = doc.body.querySelector('#issue-form');
let issueFormData = new FormData(issueForm);
fetch(issueForm.action, {
method: issueForm.method,
body: issueFormData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error.message))
.finally(() => console.log("Done"));
}).catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
}
};
}
};
document.addEventListener('DOMContentLoaded',init());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment