Last active
August 29, 2015 14:23
-
-
Save mwiegant/5f786a2bbaac21f8ea5f to your computer and use it in GitHub Desktop.
Use github api to remove old branches from github repositories
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
/* I used the npm module github-api, check it out. | |
npm -- https://www.npmjs.com/package/github-api | |
github -- https://github.com/michael/github | |
*/ | |
var Github = require('github-api'); | |
//------------------ API ------------------------- | |
function API() { | |
// I assume you'll pass your username and password in as environment variables, which is probably the | |
// safest way to go, as then you won't have your username and password just written somewhere in your code | |
if( !process.env.GH_USER ) return console.error("missing essential environment variable: GH_USER"); | |
if( !process.env.GH_PASS ) return console.error("missing essential environment variable: GH_PASS"); | |
// initialize the github connection | |
this.github = new Github({ | |
username: process.env.GH_USER, | |
password: process.env.GH_PASS, | |
auth: "basic" | |
}); | |
} | |
/* | |
I used this initially to make sure I was actually properly connecting to github's api. | |
*/ | |
API.prototype.testUserConnection = function(cb) { | |
var user = this.github.getUser(); | |
user.repos(function(err, repos) { | |
if(err) { | |
return cb(err); | |
} | |
return cb(null, repos); | |
}); | |
}; | |
/* | |
If you're trying to get information on a repository within your organization, 'username' will be the name of your | |
organization, not YOUR actual github username. | |
*/ | |
API.prototype.getRepoInformation = function(username, reponame, cb) { | |
var repo = this.github.getRepo(username, reponame); | |
repo.show(function(err, repo) { | |
if(err) { | |
return cb(err); | |
} | |
return cb(null, repo); | |
}); | |
}; | |
/* | |
If you're trying to get information on a repository within your organization, 'username' will be the name of your | |
organization, not YOUR actual github username. | |
*/ | |
API.prototype.getRepoBranches = function(username, reponame, cb){ | |
var repo = this.github.getRepo(username, reponame); | |
repo.listBranches(function(err, branches) { | |
if(err) { | |
return cb(err); | |
} | |
return cb(null, branches); | |
}); | |
}; | |
API.prototype.deleteRepoBranch = function(username, reponame, branch, cb) { | |
var repo = this.github.getRepo(username, reponame); | |
repo.deleteRef('heads/' + branch, function(err) { | |
if(err) { | |
return cb(err); | |
} | |
return cb(null); | |
}); | |
}; | |
module.exports.API = API; |
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
var async = require('async'); | |
var API = require('myGithubApi.js').API; | |
var api = new API(); | |
/* | |
Gets all branches from the specified repository. It then removes all branches from | |
that repository which contain the specified branchQuery. | |
**WARNING** This is a VERY dangerous process. Some safeguards | |
have been added for the safety of the user. | |
*/ | |
function removeOldBranches(username, repo, branchQuery) { | |
var repoBranches = []; | |
var filteredBranches = []; | |
// safeguard | |
if( !branchQuery || branchQuery == 'master' || branchQuery == 'production' || branchQuery == 'gh-pages' || branchQuery.length < 6) { | |
return console.error("did not clear stale branches, please change or lengthen your branchQuery."); | |
} | |
function getBranches(cb) { | |
api.getRepoBranches(username, repo, function(err, branches) { | |
if(err) { | |
return cb(err); | |
} | |
repoBranches = branches; | |
return cb(null); | |
}); | |
} | |
function filterBranches(cb) { | |
repoBranches.forEach(function(branch) { | |
if( branch.indexOf(branchQuery) > -1 ) { | |
filteredBranches.push(branch); | |
} | |
}); | |
console.log("Of %d branches found in the %s repo, %d branches matched the given branch-name query.", repoBranches.length, repo, filteredBranches.length ); | |
return cb(null); | |
} | |
function removeBranches(cb) { | |
filteredBranches.forEach(function(branch) { | |
api.deleteRepoBranch(username, repo, branch, function(err) { | |
if(err) { | |
return cb(err); | |
} | |
console.log("Successfully deleted the branch '%s' from the '%s' repository", branch, repo); | |
}); | |
}); | |
return cb(null); | |
} | |
async.waterfall( [getBranches, filterBranches, removeBranches], function(err) { | |
if(err) { | |
console.error(err); | |
} | |
else { | |
// console.log(filteredBranches); | |
console.log("Finished clearing out branches from the specified repository. No errors were thrown during this process."); | |
} | |
}); | |
} | |
/* | |
Remove the branches. | |
Be very careful with the third parameter, below. Any branches which contain that | |
string will be deleted. You will not be "asked" if it's ok to delete any matching | |
branches, so be sure your query is correct before you run the code. | |
*/ | |
//removeOldBranches('mwiegant', 'nodejs_boilerplate', 'release_0.0.'); | |
removeOldBranches('project-owner', 'project-name', 'string-matching-the-branches-you-want-deleted'); |
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
# I run my code out of my IDE of choice (Webstorm) but you could just as | |
# easily run this code out of the terminal, which I show how to do, below. | |
# run your code, filling in 'myusername' and 'mypassword' with your unique credentials | |
GH_USER=myusername GH_PASS=mypassword node removeGithubBranches.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment