Skip to content

Instantly share code, notes, and snippets.

@mbektchiev
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save mbektchiev/b5a3acd926c5ff548509 to your computer and use it in GitHub Desktop.

Select an option

Save mbektchiev/b5a3acd926c5ff548509 to your computer and use it in GitHub Desktop.
Recover lost (force-pushed) commit from GitHub
#!/usr/bin/env node
var debug = false;
var authorizationString = null;
// uncomment and fill-in when authorization is needed
//authorizationString = 'Basic <base64encoded username:password>';
var querystring = require('querystring');
var https = require('https');
getEventsPerRepo("Icenium/icenium-cli");
//getEventsPerUser("mbektchiev");
//doesn't work for some reason ... recoverCommit("Icenium/Ice", "e0a311beb90e674953a8babe4d1f00fec037a635", "bektchiev/temp-branch");
function getEventsPerRepo(repoId) {
performRequest(
"api.github.com",
"/repos/" + repoId + "/events?page=3",
"get",
{},
function (data) {
if (!(data instanceof Array)) {
throw new Error("Invalid response for "+ repoId +" from GitHub: " + JSON.stringify(data, null, 2));
}
console.log(JSON.stringify(data, null, 2));
}
);
}
function getEventsPerUser(userName) {
performRequest(
"api.github.com",
"/users/" + userName + "/events",
"get",
{},
function (data) {
if (!(data instanceof Array)) {
throw new Error("Invalid response for "+ repoId +" from GitHub: " + JSON.stringify(data, null, 2));
}
console.log(JSON.stringify(data, null, 2));
}
);
}
// Adds a branch pointing to the specified commit (I couldn't make this work :( )
function recoverCommit(repoId, commitId, newBranchName) {
var data = { ref: "refs/heads/" + newBranchName, sha: commitId };
performRequest(
"api.github.com",
"repos/" + repoId + "/git/refs",
"POST",
data,
function (data) {
if (!(data instanceof Array)) {
throw new Error("Invalid response for "+ repoId +" from GitHub: " + JSON.stringify(data, null, 2));
}
console.log(JSON.stringify(data, null, 2));
});
}
function performRequest(host, endpoint, method, data, success) {
var dataString = JSON.stringify(data);
var headers = {};
if (method == 'GET') {
endpoint += '?' + querystring.stringify(data);
}
else {
headers = {
'Content-Type': 'application/json',
'Content-Length': dataString.length,
'User-Agent': 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36',
};
if (authorizationString ) {
headers['Authorization'] = authorizationString;
}
}
var options = {
host: host,
path: endpoint,
method: method,
headers: headers
};
if (debug) console.log(JSON.stringify(options));
var req = https.request(options, function(res) {
res.setEncoding('utf-8');
var responseString = '';
res.on('data', function(data) {
responseString += data;
});
res.on('end', function() {
var responseObject = JSON.parse(responseString);
success(responseObject);
});
});
req.write(dataString);
req.end();
}
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment