Skip to content

Instantly share code, notes, and snippets.

@bpruitt-goddard
Created November 20, 2015 23:28
Show Gist options
  • Save bpruitt-goddard/9c32856851a210afe8e9 to your computer and use it in GitHub Desktop.
Save bpruitt-goddard/9c32856851a210afe8e9 to your computer and use it in GitHub Desktop.
VersionOne Backlog Defect Copy GreaseMonkey Script
// ==UserScript==
// @name V1 Copy Trello Template Board
// @namespace Anamespace
// @include https://www8.v1host.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @require https://api.trello.com/1/client.js?key=368dd97b50bf7c0f425deae164ff4384
// @require https://raw.githubusercontent.com/alexei/sprintf.js/master/dist/sprintf.min.js
// @version 1
// @grant GM_openInTab
// @grant GM_xmlhttpRequest
// ==/UserScript==
var trello_api_key = '368dd97b50bf7c0f425deae164ff4384';
var trello_api_baseUrl = 'https://api.trello.com/1';
var trello_backlog_template_board_id = '554ade28674ae6d966cbc19b';
var authenticationFailure = function() { unsafeWindow.console.log('Failed authentication'); };
function authSuccess() {
var newBoard = {
idBoardSource: trello_backlog_template_board_id,
name: boardTitle(),
idOrganization: 'trackabout'
};
var url = sprintf('%s/%s?key=%s&token=%s', trello_api_baseUrl, 'boards', trello_api_key, Trello.token);
//Have to form request by hand due to XSS from V1 to Trello API
GM_xmlhttpRequest ({
method: 'Post',
url: url,
data: JSON.stringify(newBoard),
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
onload: function (response) {
unsafeWindow.console.log (response.responseText);
var parsed = JSON.parse(response.responseText);
var responseUrl = parsed.shortUrl;
GM_openInTab(responseUrl);
},
onerror: function(response) {
unsafeWindow.console.log('failure');
GM_log(response);
unsafeWindow.console.log(response.responseText);
}
});
}
function addCopyButton(jNode) {
var currentUrl = $(jNode).next().text();
if (currentUrl != "") return;
var btn= $('<input type="button" value="Create"/>');
btn.click(function() { cloneBoard(); });
jNode.parent().append(btn);
}
function cloneBoard() {
Trello.authorize({
type: 'popup',
name: 'Backlog Template Automation',
scope: { read: true, write: true },
expiration: 'never',
persist: false,
interactive: true,
success: authSuccess,
error: authenticationFailure
});
//Todo update V1 value
}
function boardTitle() {
var backlogNumber = $('.asset-summary h2').text().match(/(B|D)\-\d{5}/)[0]
return backlogNumber + " " + $('.asset-summary .asset-heading h1').text();
}
//Add element once a backlog is clicked on
waitForKeyElements ('.inline-asset-detail .asset-summary .fields .custom-fields label:contains("Trello Board URL")', addCopyButton);
@bpruitt-goddard
Copy link
Author

The current status of the script is it will do the following:

  • Prompt in a pop-up window for Trello API permission granting. Does this every time because it gives an error when persisted.
  • Using the Trello API, copy the Backlog Template board into a new board. The new board will have the title of "{backlog/defect id} {backlog/defect title}".
  • Open the new board in a new tab.

There isn't yet support for automatically updating the VersionOne card with the Trello URL. Editing the form opens a new window. It will have to be done through the VersionOne API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment