Last active
December 17, 2019 20:32
-
-
Save mirubid/95f7822a04b1577b1aa9f0a4fab756c3 to your computer and use it in GitHub Desktop.
scrape info from swarm for release note
This file contains 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
/* global $ jQuery */ | |
// ==UserScript== | |
// @name swarm-scrape-rn-info | |
// @namespace http://gist.github.com/mirubid | |
// @version 0.2 | |
// @description Populate SD Release Note | |
// @author David Milby | |
// @website https://gist.github.com/mirubid/95f7822a04b1577b1aa9f0a4fab756c3 | |
// @match http://gdcswarm01.nextestate.com/reviews/* | |
// @match https://sd/secure/CreateIssue.jspa | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @require http://code.jquery.com/jquery-3.3.1.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function Swarm(){ | |
let _=this; | |
this.run=function(){ | |
_.result = scrape(); | |
inject(); | |
}; | |
function inject(){ | |
$('<div class="pull-right"><a class="btn bt-small" href="https://sd/secure/CreateIssue!default.jspa"><i class="icon-share"></i>create release note</a></div>') | |
.appendTo($('div.change-byline').parent()); | |
} | |
function scrape(){ | |
console.log('scraping...'); | |
GM_setValue('last-swarm-info',false); | |
let result={}; | |
var $byline= $('div.change-byline'); | |
var $a = $('div.change-description .first-line a'); | |
result.jiraHref=$a.attr('href'); | |
result.jiraId = $a.text(); | |
result.title= $('div.change-description .first-line').text(); | |
result.changelists=$byline.find('a') | |
.filter(function(){return this.href.indexOf('/changes/')>=0;}) | |
.map(function(){return {'changeid':$(this).text(),'change_href':this.href}; }) | |
.get(); | |
result.filelists=$('div.version-summary a') | |
.filter(function(){return this.href.indexOf('/files/')>=0;}) | |
.map(function(){return {'files':$(this).text(),'files_href':this.href}; }) | |
.get(); | |
var stringified=JSON.stringify(result); | |
console.log(stringified); | |
GM_setValue('last-swarm-info',stringified); | |
if(!result.changelists && result.changelists.length){ | |
console.log('changelists not available'); | |
} | |
return result; | |
} | |
} | |
function SD(){ | |
let self=this; | |
this.runCreatePage=function(){ | |
applicationhelper(); | |
populate(); | |
}; | |
this.redraw=function(){ | |
clear(); | |
applicationhelper(); | |
}; | |
function populate(){ | |
var lastswarm=GM_getValue('last-swarm-info'); | |
if(!lastswarm)return; | |
console.log(lastswarm); | |
var info = JSON.parse(lastswarm); | |
if(!confirm(`auto-fill with swarm info from ${info.title}?`))return; | |
// description | |
$('input#summary').val(info.title); | |
// update jira issue id list | |
$('#customfield_10254').val(info.jiraHref); | |
// check the Application checkbox | |
$('input:checkbox').filter(function(){return this.value=='Application';}).each(function(){$(this).attr('checked',true);}); | |
// file lists | |
var filelist = $.map(info.filelists,(f)=>f.files).join('\r\n'); | |
console.log('file lists',filelist); | |
$('#customfield_10251').val(filelist); | |
if(info.changelists&&info.changelists.length){ | |
$('#customfield_11520,#customfield_10280').val($.map(info.changelists,(c)=>c.changeid).join('\r\n')); | |
} | |
} | |
function commonapps(){ | |
var apps=GM_getValue('common-apps'); | |
apps=apps?JSON.parse(apps):[]; | |
if(arguments.length>0){ | |
var newones=Array.prototype.slice.apply(arguments); | |
console.log('adding',newones); | |
apps=apps.concat(newones); | |
// dedupe using Set | |
apps = Array.from(new Set(apps)); | |
GM_setValue('common-apps',JSON.stringify(apps)); | |
} | |
return apps; | |
} | |
function removeapp(name){ | |
var apps=GM_getValue('common-apps'); | |
apps=apps?JSON.parse(apps):[]; | |
apps=Array.from(new Set(apps));// dedupe | |
console.log('removing',name); | |
var index = apps.indexOf(name); | |
if (index !== -1) apps.splice(index, 1); | |
GM_setValue('common-apps',JSON.stringify(apps)); | |
} | |
function applicationhelper(){ | |
var appid='#customfield_11510'; | |
$(appid).parent().prepend('<div id="dnm-app-helper" style="float:right;width:33%;"><h2>fequently used</h2><button id="dnm-app-helper-add" type="button" title="add selected to list">save selected</button><hr /></div>'); | |
var $apphelper=$('#dnm-app-helper'); | |
let commonapplications=commonapps(); | |
for(var i =0;i<commonapplications.length;i++){ | |
$(`<a href="${commonapplications[i]}" title="add ${commonapplications[i]} to selection">[+]</a>`) | |
.click(function(){ | |
var ids=$.grep($(appid).val(),(x)=>x!='-1'); | |
ids.push($(this).attr('href')); | |
$(appid).val(ids); | |
return false; | |
}) | |
.appendTo($apphelper); | |
$(`<a href="${commonapplications[i]}" style="margin-left:1em;" title="select ${commonapplications[i]}">${commonapplications[i]}</a>`) | |
.click(function(){ | |
$(appid).val([$(this).text()]); | |
return false; | |
}) | |
.appendTo($apphelper); | |
$(`<a href="${commonapplications[i]}" style="margin-left:2em;" title="remove ${commonapplications[i]} from this list">[X]</a><br>`) | |
.click(function(){ | |
removeapp($(this).attr('href')); | |
self.redraw(); | |
return false; | |
}) | |
.appendTo($apphelper); | |
} | |
var $saveBtn=$('#dnm-app-helper-add',$apphelper); | |
console.log($saveBtn); | |
$saveBtn.click(function(){ | |
console.log("yup"); | |
var ids=$.grep($(appid).val(),(x)=>x!='-1'); | |
console.log(ids); | |
commonapps.apply(commonapps,ids); | |
self.redraw(); | |
}); | |
} | |
/*** clear the app helper **/ | |
function clear(){ | |
$('#dnm-app-helper').remove(); | |
} | |
} | |
if(document.title.indexOf('Swarm')>=0){ | |
new Swarm().run(); | |
}else if(/Step 2 of 2/i.test($('td.jiraformheader').text())){ | |
new SD().runCreatePage(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added frequently used component item