Skip to content

Instantly share code, notes, and snippets.

@halorgium
Created November 1, 2011 16:10
Show Gist options
  • Save halorgium/1330956 to your computer and use it in GitHub Desktop.
Save halorgium/1330956 to your computer and use it in GitHub Desktop.
Load the ISSUES?.md file content as the default issue body
// A crazy dotjs hack
// https://twitter.com/johnbender/status/131123843812179968
// You need to generate an OAuth token: http://developer.github.com/v3/oauth/#oauth-authorizations-api
var functionToLocation = function (fn) {
$(function () {
var js = fn.toString().replace(/\n/g, " ");
console.log(js);
window.location = "javascript: (" + js + ")()";
});
};
if (window.location.pathname.match(/issues\/new/)) {
// Base64 decoder from http://decodebase64.com/
functionToLocation(function () {
var token = "PUT AN OAUTH TOKEN HERE";
var b64array = "ABCDEFGHIJKLM" +
"NOPQRSTUVWXYZ" +
"abcdefghijklm" +
"nopqrstuvwxyz" +
"0123456789+/=";
var decode64 = function (input) {
var output = "";
var hex = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
var base64test = /[^A-Za-z0-9\+\/\=\n]/g;
if (base64test.exec(input)) {
throw("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
"Expect errors in decoding.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do {
enc1 = b64array.indexOf(input.charAt(i++));
enc2 = b64array.indexOf(input.charAt(i++));
enc3 = b64array.indexOf(input.charAt(i++));
enc4 = b64array.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 !== 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return unescape(output);
};
var req = function (prefix) {
return $.ajax(prefix + "?access_token=" + token, {dataType: "jsonp"});
};
var log = function (message) {
console.log(message);
};
var currentRepo = $(".js-current-repository").attr("href");
req("https://api.github.com/repos" + currentRepo + "/git/refs/heads/master").done(function (ref) {
var commitUrl = ref.data.object.url;
log(commitUrl);
req(commitUrl).done(function (commit) {
var treeUrl = commit.data.tree.url;
log(treeUrl);
req(treeUrl).done(function (tree) {
var blobUrl = null;
$.each(tree.data.tree, function (i,obj) {
log(JSON.stringify(obj));
if (obj.type === "blob") {
if (["ISSUE.md", "ISSUES.md"].indexOf(obj.path) !== -1) {
blobUrl = obj.url;
}
}
});
if (blobUrl !== null) {
req(blobUrl).done(function (blob) {
var base64 = blob.data.content;
log(base64);
var body = decode64(base64);
log(body);
$("#js-new-issue-form textarea[name='issue[body]']").text(body);
});
}
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment