Skip to content

Instantly share code, notes, and snippets.

@a-laughlin
Last active March 4, 2019 00:55
Show Gist options
  • Save a-laughlin/accf90fdb7c54163d65f4300d985e74e to your computer and use it in GitHub Desktop.
Save a-laughlin/accf90fdb7c54163d65f4300d985e74e to your computer and use it in GitHub Desktop.
Bookmarklet To Create New Github Projects Issues Using Template
javascript:((alert,$)=>{
const story_template = '### Story\nAs a ___, I should be able to ___, so that ___.\n\n### Acceptance Criteria\n - [ ] __\n - [ ] __\n - [ ] __\n - [ ] Tests Written (TDD)\n - [ ] Code Written\n - [ ] Documentation Written in Relevant Readme\n\n### Depends On\n- NA\n\n### Relates To\n- NA';
/*utility*/
const poll = (interval=100,maxwait=1000)=>(predicate,msg)=>(new Promise((res,rej)=>{
let waited = -1;
const t = setInterval(()=>{
const passes = predicate();
if(passes){clearInterval(t);res(passes);}
if((waited+=interval)>=maxwait) {clearInterval(t);rej(msg);}
},interval);
}));
const waitForElem = (sel,maxwait=4000)=>()=>poll(100,maxwait)(()=>$(sel),sel).then(obj=>{if (obj){return obj;};throw 'not found:'+sel;});
const click = obj=>{obj.click();return obj;};
const setValue = val=>obj=>{obj.value=val;return Promise.resolve(obj).then(o=>{if (o && o.value===val) {return o};throw 'value not set:'+val;});};
const enableElem = el=>{el.removeAttribute('disabled');return el;};
/*element getters*/
const getFirstNoteImmediate = waitForElem("#column-4563885 .js-comment-body > p",0);
const getFirstNote = waitForElem("#column-4563885 .js-comment-body > p");
const getFirstNoteConvertBtn = waitForElem(".issue-card button[data-dialog-id]");
const getNoteConversionBody = waitForElem("#convert-card-body");
const getNoteConversionSubmitBtn = waitForElem(".js-convert-note-to-issue-form [type=submit]");
const getNewNoteForm = waitForElem('#column-4563885 .project-note-form [aria-label="Enter a note"]');
const getNewNoteSubmitBtn = waitForElem('#column-4563885 .project-note-form button');
/*flow*/
getNewNoteForm()
.then(setValue('temp_title'))
.then(getNewNoteSubmitBtn)
.then(enableElem)
.then(click)
.then(getFirstNote)
.then(getFirstNoteConvertBtn)
.then(click)
.then(getNoteConversionBody)
.then(setValue(story_template))
.then(getNoteConversionSubmitBtn)
.then(click)
.catch(e=>alert(e+'\n\nSomething went wrong converting issue'));
})(window.alert,str=>document.querySelector(str));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment