I have a dozen repositories in the GitHub repository. The repository structure looks like below:
+ project1
+------- trunk
+------- tags
+------- branches
+ ------- releases
+ project2
....
Our policy requires any active branch to be deleted after 30 days of inactivity. However, there is no automatic way of detecting such an inactive branch. Occasionally, I have some inactive branch that survive past the 30-day mark.
Is there a script to list branches, as well as their last commit date in all GitHub repositories?
Edit1 -- Also is there a way of getting how many organizations and what projects they house through the API?
The GitHub Repository API should be able to help you with that.
GET /repos/:owner/:repo/branches
GET /repos/:owner/:repo/branches/:branch
This call method exposes the tip of the branch (ie. the latest commit), from which you can retrieve the commit date. Based on that, you may be able to evaluate the "activity" of each branch.
Below a sample output of a branch detail
{
"name": "coverity",
"commit": {
"sha": "f341f3a1276cbec3f6ee9d02264bd4453ca20835",
"commit": {
"author": {
"name": "nulltoken",
"email": "[email protected]",
"date": "2014-05-03T21:28:26Z"
},
"committer": {
"name": "nulltoken",
"email": "[email protected]",
"date": "2014-05-09T11:10:01Z"
},
"message": "Configure Coverity Scan hook for Travis",
"tree": {
"sha": "a5092e975145b96356df6b57cbf50e2d8c6140f8",
"url": "https://api.github.com/repos/libgit2/libgit2sharp/git/trees/a5092e975145b96356df6b57cbf50e2d8c6140f8"
},
"url": "https://api.github.com/repos/libgit2/libgit2sharp/git/commits/f341f3a1276cbec3f6ee9d02264bd4453ca20835",
"comment_count": 0
},
"url": "https://api.github.com/repos/libgit2/libgit2sharp/commits/f341f3a1276cbec3f6ee9d02264bd4453ca20835",
[...]
If you don't mind python below is a code snippet that lists inactive branches of a bare repository:
#!/bin/env python3
import pygit2, os, datetime
repo = pygit2.Repository(pygit2.discover_repository(os.getcwd()))
time_now = datetime.datetime.now()
for branch in (repo.lookup_branch(b) for b in repo.listall_branches()):
last_commit = branch.get_object()
commit_time = datetime.datetime.fromtimestamp(last_commit.commit_time)
age = time_now - commit_time
if age > datetime.timedelta(days=30):
print("{} {} {}".format(last_commit.author.email, branch.branch_name, commit_time))
Or a shell script version which deletes branches which are older than 100 days:
git for-each-ref --sort=committerdate refs/ --format='%(committerdate:raw) %(refname:short)' | awk "\$1 < $(date -d "-100 day" "+%s") {print(\$3)}" | xargs git branch -D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With